Extension:MediaFunctions (old)

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
MediaFunctions

Release status: stable

Implementation Parser functions
Description Access to the media (file or image) meta data.
Author(s) Stéphane Brunner
Version 2007.01 (2007.01)
MediaWiki 1.9.0
Download no link

MediaFunctions is an extension to access to the media (file or image) meta data :

  • {{#mediasize:image}} returns the image size (human friendly) (ie: 23 ko).
  • {{#mediabytes:image}} returns the image size of the image in bytes (ie: 23552).
  • {{#mediafulltype:image}} returns the mime type image (ie: image/jpeg).
  • {{#mediatype:image}} returns the image main type (ie: image).
  • {{#mediasubtype:image}} returns the image sub type (ie: jpeg).
  • {{#mediapath:image}} returns the image path (ie: /wikipedia/commons/i/im/image.jpeg).
  • {{#mediawidth:image}} returns the image width in pixel (ie: 300).
  • {{#mediaheight:image}} returns the image height in pixel (ie: 200).

Contents

[edit] Install

  • copy the source on a file named MediaFunctions.php.
  • copy this file in the extensions directory.
  • add include_once('extensions/MediaFunctions.php'); in the LocalSettings.php file.

[edit] License

GNU General Public License (GPL)

Author: Stéphane Brunner

[edit] See also

[edit] Code

<?php
if (!defined('MEDIAWIKI')) die();
/*
 *  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  USA
 *
 * {{#mediasize:image}} returns the image size (human friendly) (ie: 23 ko).
 * {{#mediabytes:image}} returns the image size of the image in bytes (ie: 23552).
 * {{#mediafulltype:image}} returns the mime type image (ie: image/jpeg).
 * {{#mediatype:image}} returns the image main type (ie: image).
 * {{#mediasubtype:image}} returns the image sub type (ie: jpeg).
 * {{#mediapath:image}} returns the image path (ie: /wikipedia/commons/i/im/image.jpeg).
 * {{#mediawidth:image}} returns the image width in pixel (ie: 300).
 * {{#mediaheight:image}} returns the image height in pixel (ie: 200).
 *
 * @author sbrunner
 *
 */
 
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'MediaFunctions v1.0',
    'url' => 'http://www.mediawiki.org/wiki/Extension:MediaFunctions_(old)',
    'author' => 'Stéphane Brunner'
);
 
global $IP, $wgHooks;
$wgExtensionFunctions[] = "wfDirectMediaExtension";
$wgHooks['LanguageGetMagic'][] = 'wfDynamicMediaFunctionsLanguageGetMagic';
 
require_once( "$IP/includes/Image.php" );
 
function wfDirectMediaExtension() {
    global $wgParser, $wgMessageCache;
    $wgMediaDynamicFunctions = new MediaDynamicFunctions();
 
    $wgParser->setFunctionHook( 'mediasize', array( &$wgMediaDynamicFunctions, 'mediasize' ) );
    $wgParser->setFunctionHook( 'mediatype', array( &$wgMediaDynamicFunctions, 'mediatype' ) );
    $wgParser->setFunctionHook( 'mediabytes', array( &$wgMediaDynamicFunctions, 'mediabytes' ) );
    $wgParser->setFunctionHook( 'mediafulltype', array( &$wgMediaDynamicFunctions, 'mediafulltype' ) );
    $wgParser->setFunctionHook( 'mediasubtype', array( &$wgMediaDynamicFunctions, 'mediasubtype' ) );
    $wgParser->setFunctionHook( 'mediapath', array( &$wgMediaDynamicFunctions, 'mediapath' ) );
    $wgParser->setFunctionHook( 'mediawidth', array( &$wgMediaDynamicFunctions, 'mediawidth' ) );
    $wgParser->setFunctionHook( 'mediaheight', array( &$wgMediaDynamicFunctions, 'mediaheight' ) );
 
        $wgMessageCache->addMessages(array('media:o' => 'o'));
        $wgMessageCache->addMessages(array('media:ko' => 'ko'));
        $wgMessageCache->addMessages(array('media:Mo' => 'Mo'));
        $wgMessageCache->addMessages(array('media:Go' => 'Go'));
        $wgMessageCache->addMessages(array('media:To' => 'To'));
        $wgMessageCache->addMessages(array('media:Po' => 'Po'));
        $wgMessageCache->addMessages(array('media:Eo' => 'Eo'));
        $wgMessageCache->addMessages(array('media:Zo' => 'Zo'));
        $wgMessageCache->addMessages(array('media:Yo' => 'Yo'));
        $wgMessageCache->addMessages(array('media:notfound' => 'Media "$1" not found !'));
 
}
 
function wfDynamicMediaFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
    switch ( $langCode ) {
    default:
        $magicWords['mediasize'] = array( 0, 'mediasize' );
        $magicWords['mediatype'] = array( 0, 'mediatype' );
        $magicWords['mediabytes'] = array( 0, 'mediabytes' );
        $magicWords['mediafulltype'] = array( 0, 'mediafulltype' );
        $magicWords['mediasubtype'] = array( 0, 'mediasubtype' );
        $magicWords['mediapath'] = array( 0, 'mediapath' );
        $magicWords['mediawidth'] = array( 0, 'mediawidth' );
        $magicWords['mediaheight'] = array( 0, 'mediaheight' );
    }
    return true;
}
 
class MediaDynamicFunctions
{
        static function mediasize( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return MediaDynamicFunctions::size_hum_read($img->getSize());
        }
 
        static function mediabytes( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return ''.$img->getSize();
        }
 
        static function mediatype( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                $mimetype = $img->getMimeType();
                $index = strpos($mimetype, '/');
                return substr($mimetype, 0, $index);
        }
 
        static function mediafulltype( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return $img->getMimeType();
        }
 
        static function mediasubtype( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                $mimetype = $img->getMimeType();
                $index = strpos($mimetype, '/');
                return substr($mimetype, $index + 1);
        }
 
        static function mediapath( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return $img->getURL();
        }
 
        static function mediawidth( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return $img->getWidth();
        }
 
        static function mediaheight( $parser, $s = '', $arg = null ) {
                $img = Image::newFromName($s);
                if (!$img || !$img->exists()) return wfMsg('media:notfound', $s);
                return $img->getHeight();
        }
 
 
        /* Returns a human readable size */
        static function size_hum_read($size){
                $i=0;
                $iec = array(wfMsg('media:o'), wfMsg('media:ko'), wfMsg('media:Mo'),
                        wfMsg('media:Go'), wfMsg('media:To'), wfMsg('media:Po'),
                        wfMsg('media:Eo'), wfMsg('media:Zo'), wfMsg('media: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];
        }
}
Personal tools