r22849 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22848‎ | r22849 | r22850 >
Date:19:47, 8 June 2007
Author:robchurch
Status:old
Tags:
Comment:
Introduce MediaFunctions: Parser functions which provide information about media files, starting with MIME type and file size
Modified paths:
  • /trunk/extensions/MediaFunctions (added) (history)
  • /trunk/extensions/MediaFunctions/LICENCE (added) (history)
  • /trunk/extensions/MediaFunctions/MediaFunctions.class.php (added) (history)
  • /trunk/extensions/MediaFunctions/MediaFunctions.i18n.php (added) (history)
  • /trunk/extensions/MediaFunctions/MediaFunctions.php (added) (history)

Diff [purge]

Index: trunk/extensions/MediaFunctions/LICENCE
@@ -0,0 +1,27 @@
 2+Copyright © 2007 Rob Church
 3+All rights reserved
 4+
 5+Redistribution and use in source and binary forms, with or without
 6+modification, are permitted provided that the following conditions
 7+are met:
 8+
 9+ 1. Redistributions of source code must retain the above copyright
 10+ notice, this list of conditions and the following disclaimer.
 11+
 12+ 2. Redistributions in binary form must reproduce the above
 13+ copyright notice, this list of conditions and the following
 14+ disclaimer in the documentation and/or other materials provided
 15+ with the distribution.
 16+
 17+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 18+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20+DISCLAIMED.
 21+
 22+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 23+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 24+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 25+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 26+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 27+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 28+USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
Index: trunk/extensions/MediaFunctions/MediaFunctions.class.php
@@ -0,0 +1,96 @@
 2+<?php
 3+
 4+/**
 5+ * Parser function callbacks for the MediaFunctions extension
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Rob Church <robchur@gmail.com>
 9+ */
 10+class MediaFunctions {
 11+
 12+ /**
 13+ * Error message constants
 14+ */
 15+ const ERR_INVALID_TITLE = 'mediafunctions-invalid-title';
 16+ const ERR_NOT_EXIST = 'mediafunctions-not-exist';
 17+
 18+ /**
 19+ * Get the MIME type of a file
 20+ *
 21+ * @param Parser $parser Calling parser
 22+ * @param string $name File name
 23+ * @return string
 24+ */
 25+ public static function mediamime( $parser, $name = '' ) {
 26+ $title = self::resolve( $name );
 27+ if( $title instanceof Title ) {
 28+ $parser->mOutput->addImage( $title->getDBkey() );
 29+ $file = wfFindFile( $title );
 30+ return $file instanceof File
 31+ ? $file->getMimeType()
 32+ : self::error( self::ERR_NOT_EXIST, $name );
 33+ } else {
 34+ return self::error( self::ERR_INVALID_TITLE, $name );
 35+ }
 36+ }
 37+
 38+ /**
 39+ * Get the size of a file
 40+ *
 41+ * @param Parser $parser Calling parser
 42+ * @param string $name File name
 43+ * @return string
 44+ */
 45+ public static function mediasize( $parser, $name = '' ) {
 46+ $title = self::resolve( $name );
 47+ if( $title instanceof Title ) {
 48+ $parser->mOutput->addImage( $title->getDBkey() );
 49+ $file = wfFindFile( $title );
 50+ return $file instanceof File
 51+ ? $parser->mOptions->getSkin()->formatSize( $file->getSize() )
 52+ : self::error( self::ERR_NOT_EXIST, $name );
 53+ } else {
 54+ return self::error( self::ERR_INVALID_TITLE, $name );
 55+ }
 56+ }
 57+
 58+ /**
 59+ * Convert a string title into a Title
 60+ *
 61+ * The string can be with or without namespace, and might
 62+ * include an interwiki prefix, etc.
 63+ *
 64+ * @param string $text Title string
 65+ * @return mixed Title or null
 66+ */
 67+ private static function resolve( $text ) {
 68+ if( $text ) {
 69+ $title = Title::newFromText( $text );
 70+ if( $title instanceof Title ) {
 71+ if( $title->getNamespace() == NS_IMAGE ) {
 72+ return $title;
 73+ } else {
 74+ return Title::makeTitleSafe( NS_IMAGE, $title->getText() );
 75+ }
 76+ } else {
 77+ return null;
 78+ }
 79+ } else {
 80+ return null;
 81+ }
 82+ }
 83+
 84+ /**
 85+ * Generate an error
 86+ *
 87+ * @param string $error Error code
 88+ * @param string $name File name
 89+ * @return string
 90+ */
 91+ private static function error( $error, $name ) {
 92+ return htmlspecialchars( wfMsgForContent( $error, $name ) );
 93+ }
 94+
 95+}
 96+
 97+?>
\ No newline at end of file
Property changes on: trunk/extensions/MediaFunctions/MediaFunctions.class.php
___________________________________________________________________
Added: svn:eol-style
198 + native
Index: trunk/extensions/MediaFunctions/MediaFunctions.i18n.php
@@ -0,0 +1,54 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the MediaFunctions extension
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Rob Church <robchur@gmail.com>
 9+ */
 10+
 11+/**
 12+ * Get translated magic words, if available
 13+ *
 14+ * @param string $lang Language code
 15+ * @return array
 16+ */
 17+function efMediaFunctionsWords( $lang ) {
 18+ $words = array();
 19+
 20+ /**
 21+ * English
 22+ */
 23+ $words['en'] = array(
 24+ 'mediamime' => array( 0, 'mediamime' ),
 25+ 'mediasize' => array( 0, 'mediasize' ),
 26+ );
 27+
 28+ # English is used as a fallback, and the English synonyms are
 29+ # used if a translation has not been provided for a given word
 30+ return ( $lang == 'en' || !isset( $words[$lang] ) )
 31+ ? $words['en']
 32+ : array_merge( $words['en'], $words[$lang] );
 33+}
 34+
 35+/**
 36+ * Get error message translations
 37+ *
 38+ * @return array
 39+ */
 40+function efMediaFunctionsMessages() {
 41+ $messages = array(
 42+
 43+/**
 44+ * English
 45+ */
 46+'en' => array(
 47+'mediafunctions-invalid-title' => '"$1" is not a valid title',
 48+'mediafunctions-not-exist' => '"$1" does not exist',
 49+),
 50+
 51+ );
 52+ return $messages;
 53+}
 54+
 55+?>
\ No newline at end of file
Property changes on: trunk/extensions/MediaFunctions/MediaFunctions.i18n.php
___________________________________________________________________
Added: svn:eol-style
156 + native
Index: trunk/extensions/MediaFunctions/MediaFunctions.php
@@ -0,0 +1,55 @@
 2+<?php
 3+
 4+/**
 5+ * Parser functions for MediaWiki providing information
 6+ * about various media files
 7+ *
 8+ * @addtogroup Extensions
 9+ * @author Rob Church <robchur@gmail.com>
 10+ */
 11+
 12+if( defined( 'MEDIAWIKI' ) ) {
 13+
 14+ $wgHooks['LanguageGetMagic'][] = 'efMediaFunctionsGetMagic';
 15+ $wgExtensionFunctions[] = 'efMediaFunctionsSetup';
 16+ $wgAutoloadClasses['MediaFunctions'] = dirname( __FILE__ ) . '/MediaFunctions.class.php';
 17+ $wgExtensionCredits['parserhook'][] = array(
 18+ 'name' => 'MediaFunctions',
 19+ 'author' => 'Rob Church',
 20+ 'url' => 'http://www.mediawiki.org/wiki/Extension:MediaFunctions',
 21+ 'description' => 'Parser functions for obtaining information about media files',
 22+ );
 23+
 24+ /**
 25+ * Register function callbacks and add error messages to
 26+ * the message cache
 27+ */
 28+ function efMediaFunctionsSetup() {
 29+ global $wgParser, $wgMessageCache;
 30+ $wgParser->setFunctionHook( 'mediamime', array( 'MediaFunctions', 'mediamime' ) );
 31+ $wgParser->setFunctionHook( 'mediasize', array( 'MediaFunctions', 'mediasize' ) );
 32+ require_once( dirname( __FILE__ ) . '/MediaFunctions.i18n.php' );
 33+ foreach( efMediaFunctionsMessages() as $lang => $messages )
 34+ $wgMessageCache->addMessages( $messages, $lang );
 35+ }
 36+
 37+ /**
 38+ * Associate magic words with synonyms
 39+ *
 40+ * @param array $words Magic words
 41+ * @param string $lang Language code
 42+ * @return bool
 43+ */
 44+ function efMediaFunctionsGetMagic( &$words, $lang ) {
 45+ require_once( dirname( __FILE__ ) . '/MediaFunctions.i18n.php' );
 46+ foreach( efMediaFunctionsWords( $lang ) as $word => $trans )
 47+ $words[$word] = $trans;
 48+ return true;
 49+ }
 50+
 51+} else {
 52+ echo( "This file is an extension to the MediaWiki software, and cannot be used standalone.\n" );
 53+ exit( 1 );
 54+}
 55+
 56+?>
\ No newline at end of file
Property changes on: trunk/extensions/MediaFunctions/MediaFunctions.php
___________________________________________________________________
Added: svn:eol-style
157 + native

Status & tagging log