In light of religious terrorist attacks in Paris, please see Category:blasphemy!

Freedom Porn:free technology/OmniBytes

From Freedom Porn
Jump to: navigation, search

OmniBytes is a MediaWiki extension that provides {{#omnibytes:}} function that takes in the size in bytes and generates something that can easily be understood by a human.

Licence

Available under the terms of GNU General Public License 3.0

Produced in the year 2013.
GPLv3
For more information please see Freedom Porn:copyrights.


Installation

Put the files from #Code into $IP/extensions/OmniBytes and then add the following to LocalSettings.php.

require_once("{$IP}/extensions/OmniBytes/OmniBytes.php");

Configuration

Default configuration is inside the #DefaultSettings.php. You can override these settings in your LocalSettings.php

Known issues

  • It would be a good idea to allow it to switch to the next power-of-three (or power-of-ten in binary) before it reaches a single unit of that size.

Code

OmniBytes.php

<?php
/**
 * @file OmniBytes.php
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Converts between different representations of sizes.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
**/
 
if ( !defined( 'MEDIAWIKI' ) )
{
  echo <<<EOT
To install OmniBytes, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/OmniBytes/OmniBytes.php" );
EOT;
  exit( 1 );
}
 
$wgExtensionCredits['parserhook'][] = array
(
  'path' => __FILE__,
  'name' => 'OmniBytes',
  'descriptionmsg' => 'omnibytes-description',
  'version' => '0.9.0',
  'author' => '[http://www.freedomporn.org/smut/User:VolodyA!_V_Anarhist VolodyA! V Anarhist]',
  'url' => 'http://www.freedomporn.org/smut/Freedom_Porn:free_technology/OmniBytes',
);
 
// The Class that will do the work
$wgAutoloadClasses['OmniBytes'] = __DIR__.'/OmniBytes.class.php'; // file
 
// Setting up the language
$wgExtensionMessagesFiles['OmniBytes'] = __DIR__.'/OmniBytes.i18n.php';
$wgExtensionMessagesFiles['OmniBytesAlias'] = __DIR__.'/OmniBytes.i18n.alias.php'; // alias
 
// Setting up the startup hook
$wgHooks['ParserFirstCallInit'][] = 'OmniBytes::setup';
 
// Default settings (can be overridden in LocalSettings.php)
require_once(__DIR__.'/DefaultSettings.php');

OmniBytes.class.php

<?php
/**
 * @file OmniBytes.class.php
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Converts between different representations of sizes.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
**/
 
if ( !defined( 'MEDIAWIKI' ) )
{
  echo <<<EOT
To install OmniBytes, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/OmniBytes/OmniBytes.php" );
EOT;
  exit( 1 );
}
 
class OmniBytes
{
  public static function run($parser, $bytes=0)
  {
    global $wgOmniBytesSystem, $wgOmniBytesBytesPerKilo, $wgOmniBytesDecimal;
 
    $output = '<span title=\''.wfMessage('omnibytes-bytes')->numParams($bytes)->text().'\'>';
 
    $value = (float)$bytes;
    $valueStr = '';
    for($i=0; $i<=5; $i++)
    {
      if($value < 1.0) break;
      $valueStr = wfMessage("omnibytes-{$wgOmniBytesSystem['display']}-{$i}")->numParams($value)->text();
      $value /= $wgOmniBytesBytesPerKilo[$wgOmniBytesSystem['sizes']];
      $value = round($value, $wgOmniBytesDecimal['places']);
    }
 
    $output .= $valueStr;
 
    $output .= '</span>';
 
    return $parser->insertStripItem( $output, $parser->mStripState );
  }
 
  public static function setup( &$parser )
  {
    $parser->setFunctionHook( 'omnibytes', 'OmniBytes::run' );
    return true;
  }
}

OmniBytes.i18n.php

<?php
/**
 * @file OmniBytes.i18n.php
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Converts between different representations of sizes.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
**/
 
if ( !defined( 'MEDIAWIKI' ) )
{
  echo <<<EOT
To install OmniBytes, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/OmniBytes/OmniBytes.php" );
EOT;
  exit( 1 );
}
 
$messages=array();
 
$messages['en'] = array (
	'omnibytes-description' => 'Converts between different representations of sizes.',
  'omnibytes-bytes' => '{{PLURAL:$1|$1 byte|$1 bytes}}',
  'omnibytes-si-0' => '$1 B',
  'omnibytes-si-1' => '$1 ㎅',
  'omnibytes-si-2' => '$1 ㎆',
  'omnibytes-si-3' => '$1 ㎇',
  'omnibytes-si-4' => '$1 TB',
  'omnibytes-si-5' => '$1 PB',
  'omnibytes-bi-0' => '$1 B',
  'omnibytes-bi-1' => '$1 KiB',
  'omnibytes-bi-2' => '$1 MiB',
  'omnibytes-bi-3' => '$1 GiB',
  'omnibytes-bi-4' => '$1 TiB',
  'omnibytes-bi-5' => '$1 PiB',
);

OmniBytes.i18n.alias.php

<?php
/**
 * @file OmniBytes.i18n.alias.php
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Converts between different representations of sizes.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
**/
 
if ( !defined( 'MEDIAWIKI' ) )
{
  echo <<<EOT
To install OmniBytes, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/OmniBytes/OmniBytes.php" );
EOT;
  exit( 1 );
}
 
$magicWords = array();
 
$magicWords['en'] = array (
	'omnibytes' => array(0, 'omnibytes'),
);

DefaultSettings.php

<?php
/**
 * @file DefaultSettings.php
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Converts between different representations of sizes.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
**/
 
if ( !defined( 'MEDIAWIKI' ) )
{
  echo <<<EOT
To install OmniBytes, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/OmniBytes/OmniBytes.php" );
EOT;
  exit( 1 );
}
 
$wgOmniBytesDecimal['places'] = 2;
 
// si for SI; bi for binary
$wgOmniBytesSystem['sizes'] = 'bi';
// for those who may want to use binary, but display it as regular kB, MB, etc
// you can add the following to LocalSettings.php
// $wgOmniBytesSystem['display']='si';
$wgOmniBytesSystem['display'] = $wgOmniBytesSystem['sizes'];
 
$wgOmniBytesBytesPerKilo['si'] = 1000;
$wgOmniBytesBytesPerKilo['bi'] = 1024;