Last modified: 2007-08-29 01:22:38 UTC

Wikimedia Bugzilla is closed!

Wikimedia has migrated from Bugzilla to Phabricator. Bug reports should be created and updated in Wikimedia Phabricator instead. Please create an account in Phabricator and add your Bugzilla email address to it.
Wikimedia Bugzilla is read-only. If you try to edit or create any bug report in Bugzilla you will be shown an intentional error message.
In order to access the Phabricator task corresponding to a Bugzilla report, just remove "static-" from its URL.
You could still run searches in Bugzilla or access your list of votes but bug reports will obviously not be up-to-date in Bugzilla.
Bug 8587 - Parser functions for media file size and type
Parser functions for media file size and type
Product: MediaWiki
Classification: Unclassified
Page editing (Other open bugs)
All All
: Normal enhancement with 1 vote (vote)
: ---
Assigned To: Nobody - You can work on this!
Depends on:
  Show dependency treegraph
Reported: 2007-01-11 19:49 UTC by Stéphane Brunner
Modified: 2007-08-29 01:22 UTC (History)
2 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Description Stéphane Brunner 2007-01-11 19:49:54 UTC
The variables :
* {{mediasize:medianame}}
* {{mediatype:medianame}}
is needed to know the file size and the mime type.

In page 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, ...
Comment 1 Stéphane Brunner 2007-01-11 21:14:34 UTC
This extension should do it !

 *  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
 *  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
 * @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 ) {
	$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){
		$iec = array("o", "ko", "Mo", "Go", "To", "Po", "Eo", "Zo", "Yo");
		while (($size/1024)>1) {
		$deltapos = 2;
		if (strpos($size,'.') >= 3) $deltapos = 0;
		return substr($size,0,strpos($size,'.')+$deltapos).'&nbsp;'.$iec[$i];
Comment 2 Aryeh Gregor (not reading bugmail, please e-mail directly) 2007-01-11 21:25:22 UTC
Please post as an attachment so the spacing and whatnot doesn't get mucked up.
Comment 3 Rob Church 2007-06-08 19:49:32 UTC
Introduced the MediaFunctions extension in r22849 which provide both these as parser functions and will make it straightforward to add more.

Note You need to log in before you can comment on or make changes to this bug.