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
Status: RESOLVED FIXED
Product: MediaWiki
Classification: Unclassified
Page editing (Other open bugs)
unspecified
All All
: Normal enhancement with 1 vote (vote)
: ---
Assigned To: Nobody - You can work on this!
http://en.wikibooks.org/wiki/Chinese_...
:
Depends on:
Blocks:
  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: ---


Attachments

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 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, ...
Comment 1 Stéphane Brunner 2007-01-11 21:14:34 UTC
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];
	}
}
?>
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.


Navigation
Links