Index: trunk/extensions/MediaFunctions/MediaFunctions.class.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | * |
7 | 7 | * @addtogroup Extensions |
8 | 8 | * @author Rob Church <robchur@gmail.com> |
9 | | - * @version 1.0 |
| 9 | + * @version 1.1 |
10 | 10 | */ |
11 | 11 | class MediaFunctions { |
12 | 12 | |
— | — | @@ -23,16 +23,11 @@ |
24 | 24 | * @return string |
25 | 25 | */ |
26 | 26 | 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(); |
36 | 30 | } |
| 31 | + return self::error( $file, $name ); |
37 | 32 | } |
38 | 33 | |
39 | 34 | /** |
— | — | @@ -43,16 +38,11 @@ |
44 | 39 | * @return string |
45 | 40 | */ |
46 | 41 | 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() ); |
56 | 45 | } |
| 46 | + return self::error( $file, $name ); |
57 | 47 | } |
58 | 48 | |
59 | 49 | /** |
— | — | @@ -63,16 +53,11 @@ |
64 | 54 | * @return string |
65 | 55 | */ |
66 | 56 | 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(); |
76 | 60 | } |
| 61 | + return self::error( $file, $name ); |
77 | 62 | } |
78 | 63 | |
79 | 64 | /** |
— | — | @@ -83,42 +68,73 @@ |
84 | 69 | * @return string |
85 | 70 | */ |
86 | 71 | 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(); |
96 | 75 | } |
| 76 | + return self::error( $file, $name ); |
97 | 77 | } |
| 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 | + } |
98 | 115 | |
99 | 116 | /** |
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 |
101 | 119 | * |
102 | 120 | * The string can be with or without namespace, and might |
103 | 121 | * include an interwiki prefix, etc. |
104 | 122 | * |
105 | 123 | * @param string $text Title string |
106 | | - * @return mixed Title or null |
| 124 | + * @return mixed File or string |
107 | 125 | */ |
108 | 126 | private static function resolve( $text ) { |
109 | 127 | if( $text ) { |
110 | 128 | $title = Title::newFromText( $text ); |
111 | 129 | 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; |
119 | 136 | } |
120 | | - } else { |
121 | | - return null; |
122 | 137 | } |
| 138 | + return self::ERR_INVALID_TITLE; |
123 | 139 | } |
124 | 140 | |
125 | 141 | /** |
— | — | @@ -132,5 +148,4 @@ |
133 | 149 | return htmlspecialchars( wfMsgForContent( $error, $name ) ); |
134 | 150 | } |
135 | 151 | |
136 | | -} |
137 | | - |
| 152 | +} |
\ No newline at end of file |
Index: trunk/extensions/MediaFunctions/MediaFunctions.i18n.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | * |
7 | 7 | * @addtogroup Extensions |
8 | 8 | * @author Rob Church <robchur@gmail.com> |
9 | | - * @version 1.0 |
| 9 | + * @version 1.1 |
10 | 10 | */ |
11 | 11 | |
12 | 12 | /** |
— | — | @@ -21,10 +21,12 @@ |
22 | 22 | * English |
23 | 23 | */ |
24 | 24 | $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' ), |
29 | 31 | ); |
30 | 32 | |
31 | 33 | # English is used as a fallback, and the English synonyms are |
— | — | @@ -78,6 +80,4 @@ |
79 | 81 | $messages['zh-yue'] = $messages['yue']; |
80 | 82 | |
81 | 83 | return $messages; |
82 | | -} |
83 | | - |
84 | | - |
| 84 | +} |
\ No newline at end of file |
Index: trunk/extensions/MediaFunctions/MediaFunctions.php |
— | — | @@ -6,7 +6,7 @@ |
7 | 7 | * |
8 | 8 | * @addtogroup Extensions |
9 | 9 | * @author Rob Church <robchur@gmail.com> |
10 | | - * @version 1.0 |
| 10 | + * @version 1.1 |
11 | 11 | */ |
12 | 12 | |
13 | 13 | if( defined( 'MEDIAWIKI' ) ) { |
— | — | @@ -31,6 +31,8 @@ |
32 | 32 | $wgParser->setFunctionHook( 'mediasize', array( 'MediaFunctions', 'mediasize' ) ); |
33 | 33 | $wgParser->setFunctionHook( 'mediaheight', array( 'MediaFunctions', 'mediaheight' ) ); |
34 | 34 | $wgParser->setFunctionHook( 'mediawidth', array( 'MediaFunctions', 'mediawidth' ) ); |
| 35 | + $wgParser->setFunctionHook( 'mediadimensions', array( 'MediaFunctions', 'mediadimensions' ) ); |
| 36 | + $wgParser->setFunctionHook( 'mediaexif', array( 'MediaFunctions', 'mediaexif' ) ); |
35 | 37 | require_once( dirname( __FILE__ ) . '/MediaFunctions.i18n.php' ); |
36 | 38 | foreach( efMediaFunctionsMessages() as $lang => $messages ) |
37 | 39 | $wgMessageCache->addMessages( $messages, $lang ); |
— | — | @@ -53,5 +55,4 @@ |
54 | 56 | } else { |
55 | 57 | echo( "This file is an extension to the MediaWiki software, and cannot be used standalone.\n" ); |
56 | 58 | exit( 1 ); |
57 | | -} |
58 | | - |
| 59 | +} |
\ No newline at end of file |