Page MenuHomePhabricator

Parser functions for media file size and type
Closed, ResolvedPublic

Description

Author: stephane.brunner

Description:
The variables :

  • {{mediasize:medianame}}
  • {{mediatype:medianame}}

is needed to know the file size and the mime type.

In page http://en.wikibooks.org/wiki/Chinese_%28Mandarin%29 the template {{PDF
version}} is call si this :
{{PDF version|Chinese (Mandarin) v0.2|File size: 272KB}}
with the file size but it should be generate automatically !

The type can be use for example to automatically select the icon, ...


Version: unspecified
Severity: enhancement
URL: http://en.wikibooks.org/wiki/Chinese_%28Mandarin%29

Details

Reference
bz8587

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 9:32 PM
bzimport set Reference to bz8587.
bzimport added a subscriber: Unknown Object (MLST).

stephane.brunner wrote:

This extension should do it !

<?php
/*

  • This program is free software; you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation; either version 2 of the License, or
  • (at your option) any later version. *
  • This program is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  • GNU General Public License for more details. *
  • You should have received a copy of the GNU General Public License
  • along with this program; if not, write to the Free Software
  • Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USAw *
  • Parts of the program are from the file 'Image.php' from the MediaWiki

project. The respective source can be acquired from
http://wikipedia.sourceforge.net/.

  • @author sbrunner */

if (!defined('MEDIAWIKI')) die();
$wgExtensionFunctions[] = "wfDirectLinkExtension";
$wgHooks['LanguageGetMagic'][] = 'wfDynamicFunctionsLanguageGetMagic';

require_once( "$IP/includes/Image.php" );

function wfDirectLinkExtension() {

global $wgParser, $wgHooks;

$wgExtDynamicFunctions = new ExtDynamicFunctions();

$wgParser->setFunctionHook( 'mediasize', array( &$wgExtDynamicFunctions,

'mediasize' ) );

$wgParser->setFunctionHook( 'mediatype', array( &$wgExtDynamicFunctions,

'mediatype' ) );
}

function wfDynamicFunctionsLanguageGetMagic( &$magicWords, $langCode ) {

switch ( $langCode ) {
default:

$magicWords['mediasize'] = array( 0, 'mediasize' );
$magicWords['mediatype'] = array( 0, 'mediatype' );

}
return true;

}

class ExtDynamicFunctions
{

static function mediasize( $parser, $s = '', $arg = null ) {

		$img = Image::newFromName($s);
		if (!$img) return 'media "'.$s.'" not found !';
		return size_hum_read($img->getSize());

}

static function mediatype( $parser, $s = '', $arg = null ) {

		$img = Image::newFromName($s);
		if (!$img) return 'media "'.$s.'" not found !';
		return $img->getMimeType();

}

/* Returns a human readable size */
static function size_hum_read($size){

		$i=0;
		$iec = array("o", "ko", "Mo", "Go", "To", "Po", "Eo", "Zo", "Yo");
		while (($size/1024)>1) {
			$size=$size/1024;
			$i++;
		}
		$deltapos = 2;
		if (strpos($size,'.') >= 3) $deltapos = 0;
		return substr($size,0,strpos($size,'.')+$deltapos).'&nbsp;'.$iec[$i];

}
}
?>

ayg wrote:

Please post as an attachment so the spacing and whatnot doesn't get mucked up.

robchur wrote:

Introduced the MediaFunctions extension in r22849 which provide both these as parser functions and will make it straightforward to add more.