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_IMAGE
])) {
61 $result = $this->getResult();
62 $images = RepoGroup
::singleton()->findFiles( array_keys( $pageIds[NS_IMAGE
] ) );
63 foreach ( $images as $img ) {
66 // Get information about the current version first
67 // Check that the current version is within the start-end boundaries
68 if((is_null($params['start']) ||
$img->getTimestamp() <= $params['start']) &&
69 (is_null($params['end']) ||
$img->getTimestamp() >= $params['end'])) {
70 $data[] = self
::getInfo( $img, $prop, $result, $scale );
73 // Now get the old revisions
74 // Get one more to facilitate query-continue functionality
75 $count = count($data);
76 $oldies = $img->getHistory($params['limit'] - $count +
1, $params['start'], $params['end']);
77 foreach($oldies as $oldie) {
78 if(++
$count > $params['limit']) {
79 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
80 // Only set a query-continue if there was only one title
81 if(count($pageIds[NS_IMAGE
]) == 1)
82 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
85 $data[] = self
::getInfo( $oldie, $prop, $result );
88 $pageId = $pageIds[NS_IMAGE
][ $img->getOriginalTitle()->getDBkey() ];
90 array( 'query', 'pages', intval( $pageId ) ),
91 'imagerepository', $img->getRepoName()
93 $this->addPageSubItems($pageId, $data);
96 $missing = array_diff( array_keys( $pageIds[NS_IMAGE
] ), array_keys( $images ) );
97 foreach ( $missing as $title )
99 array( 'query', 'pages', intval( $pageIds[NS_IMAGE
][$title] ) ),
100 'imagerepository', ''
106 * Get result information for an image revision
107 * @param File f The image
108 * @return array Result array
110 static function getInfo($file, $prop, $result, $scale = null) {
112 if( isset( $prop['timestamp'] ) )
113 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $file->getTimestamp());
114 if( isset( $prop['user'] ) ) {
115 $vals['user'] = $file->getUser();
116 if( !$file->getUser( 'id' ) )
119 if( isset( $prop['size'] ) ||
isset( $prop['dimensions'] ) ) {
120 $vals['size'] = intval( $file->getSize() );
121 $vals['width'] = intval( $file->getWidth() );
122 $vals['height'] = intval( $file->getHeight() );
124 if( isset( $prop['url'] ) ) {
125 if( !is_null( $scale ) && !$file->isOld() ) {
126 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
127 if( $mto && !$mto->isError() )
129 $vals['thumburl'] = $mto->getUrl();
130 $vals['thumbwidth'] = $mto->getWidth();
131 $vals['thumbheight'] = $mto->getHeight();
134 $vals['url'] = $file->getFullURL();
135 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
137 if( isset( $prop['comment'] ) )
138 $vals['comment'] = $file->getDescription();
139 if( isset( $prop['sha1'] ) )
140 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
141 if( isset( $prop['metadata'] ) ) {
142 $metadata = $file->getMetadata();
143 $vals['metadata'] = $metadata ?
unserialize( $metadata ) : null;
144 $result->setIndexedTagName_recursive( $vals['metadata'], 'meta' );
146 if( isset( $prop['mime'] ) )
147 $vals['mime'] = $file->getMimeType();
149 if( isset( $prop['archivename'] ) && $file->isOld() )
150 $vals['archivename'] = $file->getArchiveName();
152 if( isset( $prop['bitdepth'] ) )
153 $vals['bitdepth'] = $file->getBitDepth();
158 public function getAllowedParams() {
161 ApiBase
:: PARAM_ISMULTI
=> true,
162 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
163 ApiBase
:: PARAM_TYPE
=> array (
177 ApiBase
:: PARAM_TYPE
=> 'limit',
178 ApiBase
:: PARAM_DFLT
=> 1,
179 ApiBase
:: PARAM_MIN
=> 1,
180 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
181 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
184 ApiBase
:: PARAM_TYPE
=> 'timestamp'
187 ApiBase
:: PARAM_TYPE
=> 'timestamp'
190 ApiBase
:: PARAM_TYPE
=> 'integer',
191 ApiBase
:: PARAM_DFLT
=> -1
193 'urlheight' => array(
194 ApiBase
:: PARAM_TYPE
=> 'integer',
195 ApiBase
:: PARAM_DFLT
=> -1
200 public function getParamDescription() {
202 'prop' => 'What image information to get.',
203 'limit' => 'How many image revisions to return',
204 'start' => 'Timestamp to start listing from',
205 'end' => 'Timestamp to stop listing at',
206 'urlwidth' => array('If iiprop=url is set, a URL to an image scaled to this width will be returned.',
207 'Only the current version of the image can be scaled.'),
208 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
212 public function getDescription() {
214 'Returns image information and upload history'
218 protected function getExamples() {
220 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
221 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
225 public function getVersion() {
226 return __CLASS__
. ': $Id$';