bb8aed8a091d5f300288c057ab6c48ad355b996e
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 foreach ($pageIds[NS_IMAGE
] as $dbKey => $pageId) {
64 $title = Title
:: makeTitle(NS_IMAGE
, $dbKey);
65 $img = wfFindFile($title);
72 $repository = $img->getRepoName();
74 // Get information about the current version first
75 // Check that the current version is within the start-end boundaries
76 if((is_null($params['start']) ||
$img->getTimestamp() <= $params['start']) &&
77 (is_null($params['end']) ||
$img->getTimestamp() >= $params['end'])) {
78 $data[] = self
::getInfo( $img, $prop, $result, $scale );
81 // Now get the old revisions
82 // Get one more to facilitate query-continue functionality
83 $count = count($data);
84 $oldies = $img->getHistory($params['limit'] - $count +
1, $params['start'], $params['end']);
85 foreach($oldies as $oldie) {
86 if(++
$count > $params['limit']) {
87 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
88 // Only set a query-continue if there was only one title
89 if(count($pageIds[NS_IMAGE
]) == 1)
90 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
93 $data[] = self
::getInfo( $oldie, $prop, $result );
97 $result->addValue(array(
98 'query', 'pages', intval($pageId)),
99 'imagerepository', $repository
102 $this->addPageSubItems($pageId, $data);
108 * Get result information for an image revision
109 * @param File f The image
110 * @return array Result array
112 static function getInfo($file, $prop, $result, $scale = null) {
114 if( isset( $prop['timestamp'] ) )
115 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $file->getTimestamp());
116 if( isset( $prop['user'] ) ) {
117 $vals['user'] = $file->getUser();
118 if( !$file->getUser( 'id' ) )
121 if( isset( $prop['size'] ) ||
isset( $prop['dimensions'] ) ) {
122 $vals['size'] = intval( $file->getSize() );
123 $vals['width'] = intval( $file->getWidth() );
124 $vals['height'] = intval( $file->getHeight() );
126 if( isset( $prop['url'] ) ) {
127 if( !is_null( $scale ) && !$file->isOld() ) {
128 $thumb = $file->getThumbnail( $scale['width'], $scale['height'] );
131 $vals['thumburl'] = wfExpandUrl( $thumb->getURL() );
132 $vals['thumbwidth'] = $thumb->getWidth();
133 $vals['thumbheight'] = $thumb->getHeight();
136 $vals['url'] = $file->getFullURL();
138 if( isset( $prop['comment'] ) )
139 $vals['comment'] = $file->getDescription();
140 if( isset( $prop['sha1'] ) )
141 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
142 if( isset( $prop['metadata'] ) ) {
143 $metadata = $file->getMetadata();
144 $vals['metadata'] = $metadata ?
unserialize( $metadata ) : null;
145 $result->setIndexedTagName_recursive( $vals['metadata'], 'meta' );
147 if( isset( $prop['mimetype'] ) )
148 $vals['mime'] = $file->getMimeType();
150 if( isset( $prop['archivename'] ) && $file->isOld() )
151 $vals['archivename'] = $file->getArchiveName();
156 public function getAllowedParams() {
159 ApiBase
:: PARAM_ISMULTI
=> true,
160 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
161 ApiBase
:: PARAM_TYPE
=> array (
174 ApiBase
:: PARAM_TYPE
=> 'limit',
175 ApiBase
:: PARAM_DFLT
=> 1,
176 ApiBase
:: PARAM_MIN
=> 1,
177 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
178 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
181 ApiBase
:: PARAM_TYPE
=> 'timestamp'
184 ApiBase
:: PARAM_TYPE
=> 'timestamp'
187 ApiBase
:: PARAM_TYPE
=> 'integer',
188 ApiBase
:: PARAM_DFLT
=> -1
190 'urlheight' => array(
191 ApiBase
:: PARAM_TYPE
=> 'integer',
192 ApiBase
:: PARAM_DFLT
=> -1
197 public function getParamDescription() {
199 'prop' => 'What image information to get.',
200 'limit' => 'How many image revisions to return',
201 'start' => 'Timestamp to start listing from',
202 'end' => 'Timestamp to stop listing at',
203 'urlwidth' => 'If iiprop=url is set, a URL to an image scaled to this width will be returned. Only the current version of the image can be scaled.',
204 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
208 public function getDescription() {
210 'Returns image information and upload history'
214 protected function getExamples() {
216 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
217 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
221 public function getVersion() {
222 return __CLASS__
. ': $Id$';