4 * Created on July 6, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * A query action to get image information and upload history.
36 class ApiQueryImageInfo
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
:: __construct( $query, $moduleName, 'ii' );
42 public function execute() {
43 $params = $this->extractRequestParams();
45 $prop = array_flip( $params['prop'] );
47 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 )
48 $this->dieUsage( "iiurlheight cannot be used without iiurlwidth", 'iiurlwidth' );
50 if ( $params['urlwidth'] != - 1 ) {
52 $scale['width'] = $params['urlwidth'];
53 $scale['height'] = $params['urlheight'];
58 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
59 if ( !empty( $pageIds[NS_FILE
] ) ) {
60 $titles = array_keys( $pageIds[NS_FILE
] );
61 asort( $titles ); // Ensure the order is always the same
64 if ( !is_null( $params['continue'] ) )
67 $cont = explode( '|', $params['continue'] );
68 if ( count( $cont ) != 2 )
69 $this->dieUsage( "Invalid continue param. You should pass the original " .
70 "value returned by the previous query", "_badcontinue" );
71 $fromTitle = strval( $cont[0] );
72 $fromTimestamp = $cont[1];
73 // Filter out any titles before $fromTitle
74 foreach ( $titles as $key => $title )
75 if ( $title < $fromTitle )
76 unset( $titles[$key] );
81 $result = $this->getResult();
82 $images = RepoGroup
::singleton()->findFiles( $titles );
83 foreach ( $images as $img ) {
85 if ( $img->getOriginalTitle()->isRedirect() )
88 $start = $skip ?
$fromTimestamp : $params['start'];
89 $pageId = $pageIds[NS_IMAGE
][ $img->getOriginalTitle()->getDBkey() ];
91 $fit = $result->addValue(
92 array( 'query', 'pages', intval( $pageId ) ),
93 'imagerepository', $img->getRepoName()
97 if ( count( $pageIds[NS_IMAGE
] ) == 1 )
98 # The user is screwed. imageinfo can't be solely
99 # responsible for exceeding the limit in this case,
100 # so set a query-continue that just returns the same
101 # thing again. When the violating queries have been
102 # out-continued, the result will get through
103 $this->setContinueEnumParameter( 'start',
104 wfTimestamp( TS_ISO_8601
, $img->getTimestamp() ) );
106 $this->setContinueEnumParameter( 'continue',
107 $this->getContinueStr( $img ) );
111 // Get information about the current version first
112 // Check that the current version is within the start-end boundaries
114 if ( ( is_null( $start ) ||
$img->getTimestamp() <= $start ) &&
115 ( is_null( $params['end'] ) ||
$img->getTimestamp() >= $params['end'] ) ) {
117 $fit = $this->addPageSubItem( $pageId,
118 self
::getInfo( $img, $prop, $result, $scale ) );
121 if ( count( $pageIds[NS_IMAGE
] ) == 1 )
122 # See the 'the user is screwed' comment above
123 $this->setContinueEnumParameter( 'start',
124 wfTimestamp( TS_ISO_8601
, $img->getTimestamp() ) );
126 $this->setContinueEnumParameter( 'continue',
127 $this->getContinueStr( $img ) );
132 // Now get the old revisions
133 // Get one more to facilitate query-continue functionality
134 $count = ( $gotOne ?
1 : 0 );
135 $oldies = $img->getHistory( $params['limit'] - $count +
1, $start, $params['end'] );
136 foreach ( $oldies as $oldie ) {
137 if ( ++
$count > $params['limit'] ) {
138 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
139 // Only set a query-continue if there was only one title
140 if ( count( $pageIds[NS_FILE
] ) == 1 )
142 $this->setContinueEnumParameter( 'start',
143 wfTimestamp( TS_ISO_8601
, $oldie->getTimestamp() ) );
147 $fit = $this->addPageSubItem( $pageId,
148 self
::getInfo( $oldie, $prop, $result ) );
151 if ( count( $pageIds[NS_IMAGE
] ) == 1 )
152 $this->setContinueEnumParameter( 'start',
153 wfTimestamp( TS_ISO_8601
, $oldie->getTimestamp() ) );
155 $this->setContinueEnumParameter( 'continue',
156 $this->getContinueStr( $oldie ) );
165 $data = $this->getResultData();
166 foreach ( $data['query']['pages'] as $pageid => $arr ) {
167 if ( !isset( $arr['imagerepository'] ) )
169 array( 'query', 'pages', $pageid ),
170 'imagerepository', ''
172 // The above can't fail because it doesn't increase the result size
178 * Get result information for an image revision
179 * @param File f The image
180 * @return array Result array
182 static function getInfo( $file, $prop, $result, $scale = null ) {
184 if ( isset( $prop['timestamp'] ) )
185 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $file->getTimestamp() );
186 if ( isset( $prop['user'] ) ) {
187 $vals['user'] = $file->getUser();
188 if ( !$file->getUser( 'id' ) )
191 if ( isset( $prop['size'] ) ||
isset( $prop['dimensions'] ) ) {
192 $vals['size'] = intval( $file->getSize() );
193 $vals['width'] = intval( $file->getWidth() );
194 $vals['height'] = intval( $file->getHeight() );
196 if ( isset( $prop['url'] ) ) {
197 if ( !is_null( $scale ) && !$file->isOld() ) {
198 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
199 if ( $mto && !$mto->isError() )
201 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
202 $vals['thumbwidth'] = intval( $mto->getWidth() );
203 $vals['thumbheight'] = intval( $mto->getHeight() );
206 $vals['url'] = $file->getFullURL();
207 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
209 if ( isset( $prop['comment'] ) )
210 $vals['comment'] = $file->getDescription();
211 if ( isset( $prop['sha1'] ) )
212 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
213 if ( isset( $prop['metadata'] ) ) {
214 $metadata = $file->getMetadata();
215 $vals['metadata'] = $metadata ? self
::processMetaData( unserialize( $metadata ), $result ) : null;
217 if ( isset( $prop['mime'] ) )
218 $vals['mime'] = $file->getMimeType();
220 if ( isset( $prop['archivename'] ) && $file->isOld() )
221 $vals['archivename'] = $file->getArchiveName();
223 if ( isset( $prop['bitdepth'] ) )
224 $vals['bitdepth'] = $file->getBitDepth();
229 public static function processMetaData( $metadata, $result )
232 if ( is_array( $metadata ) ) {
233 foreach ( $metadata as $key => $value )
235 $r = array( 'name' => $key );
236 if ( is_array( $value ) )
237 $r['value'] = self
::processMetaData( $value, $result );
239 $r['value'] = $value;
243 $result->setIndexedTagName( $retval, 'metadata' );
247 private function getContinueStr( $img )
249 return $img->getOriginalTitle()->getText() .
250 '|' . $img->getTimestamp();
253 public function getAllowedParams() {
256 ApiBase
:: PARAM_ISMULTI
=> true,
257 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
258 ApiBase
:: PARAM_TYPE
=> self
::getPropertyNames()
261 ApiBase
:: PARAM_TYPE
=> 'limit',
262 ApiBase
:: PARAM_DFLT
=> 1,
263 ApiBase
:: PARAM_MIN
=> 1,
264 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
265 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
268 ApiBase
:: PARAM_TYPE
=> 'timestamp'
271 ApiBase
:: PARAM_TYPE
=> 'timestamp'
274 ApiBase
:: PARAM_TYPE
=> 'integer',
275 ApiBase
:: PARAM_DFLT
=> - 1
277 'urlheight' => array(
278 ApiBase
:: PARAM_TYPE
=> 'integer',
279 ApiBase
:: PARAM_DFLT
=> - 1
286 * Returns all possible parameters to iiprop
288 public static function getPropertyNames() {
295 'dimensions', // For backwards compatibility with Allimages
304 public function getParamDescription() {
306 'prop' => 'What image information to get.',
307 'limit' => 'How many image revisions to return',
308 'start' => 'Timestamp to start listing from',
309 'end' => 'Timestamp to stop listing at',
310 'urlwidth' => array( 'If iiprop=url is set, a URL to an image scaled to this width will be returned.',
311 'Only the current version of the image can be scaled.' ),
312 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
313 'continue' => 'When more results are available, use this to continue',
317 public function getDescription() {
319 'Returns image information and upload history'
323 protected function getExamples() {
325 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
326 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
330 public function getVersion() {
331 return __CLASS__
. ': $Id$';