r25019 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25018‎ | r25019 | r25020 >
Date:23:31, 21 August 2007
Author:robchurch
Status:old
Tags:
Comment:
* Make MediaFunctions::resolve() smarter; returns error string - reduces duplicated code in each callback
* Introduce {{#mediadimensions}} and [experimental] {{#mediaexif}} parser functions
Modified paths:
  • /trunk/extensions/MediaFunctions/MediaFunctions.class.php (modified) (history)
  • /trunk/extensions/MediaFunctions/MediaFunctions.i18n.php (modified) (history)
  • /trunk/extensions/MediaFunctions/MediaFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MediaFunctions/MediaFunctions.class.php
@@ -5,7 +5,7 @@
66 *
77 * @addtogroup Extensions
88 * @author Rob Church <robchur@gmail.com>
9 - * @version 1.0
 9+ * @version 1.1
1010 */
1111 class MediaFunctions {
1212
@@ -23,16 +23,11 @@
2424 * @return string
2525 */
2626 public static function mediamime( $parser, $name = '' ) {
27 - $title = self::resolve( $name );
28 - if( $title instanceof Title ) {
29 - $parser->mOutput->addImage( $title->getDBkey() );
30 - $file = wfFindFile( $title );
31 - return $file instanceof File
32 - ? $file->getMimeType()
33 - : self::error( self::ERR_NOT_EXIST, $name );
34 - } else {
35 - return self::error( self::ERR_INVALID_TITLE, $name );
 27+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 28+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 29+ return $file->getMimeType();
3630 }
 31+ return self::error( $file, $name );
3732 }
3833
3934 /**
@@ -43,16 +38,11 @@
4439 * @return string
4540 */
4641 public static function mediasize( $parser, $name = '' ) {
47 - $title = self::resolve( $name );
48 - if( $title instanceof Title ) {
49 - $parser->mOutput->addImage( $title->getDBkey() );
50 - $file = wfFindFile( $title );
51 - return $file instanceof File
52 - ? $parser->mOptions->getSkin()->formatSize( $file->getSize() )
53 - : self::error( self::ERR_NOT_EXIST, $name );
54 - } else {
55 - return self::error( self::ERR_INVALID_TITLE, $name );
 42+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 43+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 44+ return $parser->mOptions->getSkin()->formatSize( $file->getSize() );
5645 }
 46+ return self::error( $file, $name );
5747 }
5848
5949 /**
@@ -63,16 +53,11 @@
6454 * @return string
6555 */
6656 public static function mediaheight( $parser, $name = '' ) {
67 - $title = self::resolve( $name );
68 - if( $title instanceof Title ) {
69 - $parser->mOutput->addImage( $title->getDBkey() );
70 - $file = wfFindFile( $title );
71 - return $file instanceof File
72 - ? $file->getHeight()
73 - : self::error( self::ERR_NOT_EXIST, $name );
74 - } else {
75 - return self::error( self::ERR_INVALID_TITLE, $name );
 57+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 58+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 59+ return $file->getHeight();
7660 }
 61+ return self::error( $file, $name );
7762 }
7863
7964 /**
@@ -83,42 +68,73 @@
8469 * @return string
8570 */
8671 public static function mediawidth( $parser, $name = '' ) {
87 - $title = self::resolve( $name );
88 - if( $title instanceof Title ) {
89 - $parser->mOutput->addImage( $title->getDBkey() );
90 - $file = wfFindFile( $title );
91 - return $file instanceof File
92 - ? $file->getWidth()
93 - : self::error( self::ERR_NOT_EXIST, $name );
94 - } else {
95 - return self::error( self::ERR_INVALID_TITLE, $name );
 72+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 73+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 74+ return $file->getWidth();
9675 }
 76+ return self::error( $file, $name );
9777 }
 78+
 79+ /**
 80+ * Get the dimensions of a file
 81+ *
 82+ *
 83+ * @param Parser $parser Calling parser
 84+ * @param string $name File name
 85+ * @return string
 86+ */
 87+ public static function mediadimensions( $parser, $name = '' ) {
 88+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 89+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 90+ return $file->getDimensionsString();
 91+ }
 92+ return self::error( $file, $name );
 93+ }
 94+
 95+ /**
 96+ * Get EXIF metadata associated with a file
 97+ *
 98+ * @param Parser $parser Calling parser
 99+ * @param string $name File name
 100+ * @param string $meta Metadata name
 101+ * @return string
 102+ */
 103+ public static function mediaexif( $parser, $name = '', $meta = '' ) {
 104+ if( ( $file = self::resolve( $name ) ) instanceof File ) {
 105+ $parser->mOutput->addImage( $file->getTitle()->getDBkey() );
 106+ if( $meta && $file->getHandler()->getMetadataType( $file ) == 'exif' ) {
 107+ $data = unserialize( $file->getMetadata() );
 108+ if( $data && isset( $data[$meta] ) )
 109+ return htmlspecialchars( $data[$meta] );
 110+ }
 111+ return '';
 112+ }
 113+ return self::error( $file, $name );
 114+ }
98115
99116 /**
100 - * Convert a string title into a Title
 117+ * Convert a string title into a File, returning an appropriate
 118+ * error message string if this is not possible
101119 *
102120 * The string can be with or without namespace, and might
103121 * include an interwiki prefix, etc.
104122 *
105123 * @param string $text Title string
106 - * @return mixed Title or null
 124+ * @return mixed File or string
107125 */
108126 private static function resolve( $text ) {
109127 if( $text ) {
110128 $title = Title::newFromText( $text );
111129 if( $title instanceof Title ) {
112 - if( $title->getNamespace() == NS_IMAGE ) {
113 - return $title;
114 - } else {
115 - return Title::makeTitleSafe( NS_IMAGE, $title->getText() );
116 - }
117 - } else {
118 - return null;
 130+ if( $title->getNamespace() != NS_IMAGE )
 131+ $title = Title::makeTitle( NS_IMAGE, $title->getText() );
 132+ $file = wfFindFile( $title );
 133+ return $file instanceof File
 134+ ? $file
 135+ : self::ERR_NOT_EXIST;
119136 }
120 - } else {
121 - return null;
122137 }
 138+ return self::ERR_INVALID_TITLE;
123139 }
124140
125141 /**
@@ -132,5 +148,4 @@
133149 return htmlspecialchars( wfMsgForContent( $error, $name ) );
134150 }
135151
136 -}
137 -
 152+}
\ No newline at end of file
Index: trunk/extensions/MediaFunctions/MediaFunctions.i18n.php
@@ -5,7 +5,7 @@
66 *
77 * @addtogroup Extensions
88 * @author Rob Church <robchur@gmail.com>
9 - * @version 1.0
 9+ * @version 1.1
1010 */
1111
1212 /**
@@ -21,10 +21,12 @@
2222 * English
2323 */
2424 $words['en'] = array(
25 - 'mediamime' => array( 0, 'mediamime' ),
26 - 'mediasize' => array( 0, 'mediasize' ),
27 - 'mediaheight' => array( 0, 'mediaheight' ),
28 - 'mediawidth' => array( 0, 'mediawidth' ),
 25+ 'mediamime' => array( 0, 'mediamime' ),
 26+ 'mediasize' => array( 0, 'mediasize' ),
 27+ 'mediaheight' => array( 0, 'mediaheight' ),
 28+ 'mediawidth' => array( 0, 'mediawidth' ),
 29+ 'mediadimensions' => array( 0, 'mediadimensions' ),
 30+ 'mediaexif' => array( 0, 'mediaexif' ),
2931 );
3032
3133 # English is used as a fallback, and the English synonyms are
@@ -78,6 +80,4 @@
7981 $messages['zh-yue'] = $messages['yue'];
8082
8183 return $messages;
82 -}
83 -
84 -
 84+}
\ No newline at end of file
Index: trunk/extensions/MediaFunctions/MediaFunctions.php
@@ -6,7 +6,7 @@
77 *
88 * @addtogroup Extensions
99 * @author Rob Church <robchur@gmail.com>
10 - * @version 1.0
 10+ * @version 1.1
1111 */
1212
1313 if( defined( 'MEDIAWIKI' ) ) {
@@ -31,6 +31,8 @@
3232 $wgParser->setFunctionHook( 'mediasize', array( 'MediaFunctions', 'mediasize' ) );
3333 $wgParser->setFunctionHook( 'mediaheight', array( 'MediaFunctions', 'mediaheight' ) );
3434 $wgParser->setFunctionHook( 'mediawidth', array( 'MediaFunctions', 'mediawidth' ) );
 35+ $wgParser->setFunctionHook( 'mediadimensions', array( 'MediaFunctions', 'mediadimensions' ) );
 36+ $wgParser->setFunctionHook( 'mediaexif', array( 'MediaFunctions', 'mediaexif' ) );
3537 require_once( dirname( __FILE__ ) . '/MediaFunctions.i18n.php' );
3638 foreach( efMediaFunctionsMessages() as $lang => $messages )
3739 $wgMessageCache->addMessages( $messages, $lang );
@@ -53,5 +55,4 @@
5456 } else {
5557 echo( "This file is an extension to the MediaWiki software, and cannot be used standalone.\n" );
5658 exit( 1 );
57 -}
58 -
 59+}
\ No newline at end of file

Status & tagging log