Merge "Add some more detailed info about the xslt param of format=xml"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllImages.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.12+
5 *
6 * Created on Mar 16, 2008
7 *
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all available pages.
31 *
32 * @ingroup API
33 */
34 class ApiQueryAllImages extends ApiQueryGeneratorBase {
35
36 protected $mRepo;
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ai' );
40 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
41 }
42
43 /**
44 * Override parent method to make sure to make sure the repo's DB is used
45 * which may not necesarilly be the same as the local DB.
46 *
47 * TODO: allow querying non-local repos.
48 * @return DatabaseBase
49 */
50 protected function getDB() {
51 return $this->mRepo->getSlaveDB();
52 }
53
54 public function execute() {
55 $this->run();
56 }
57
58 public function getCacheMode( $params ) {
59 return 'public';
60 }
61
62 /**
63 * @param $resultPageSet ApiPageSet
64 * @return void
65 */
66 public function executeGenerator( $resultPageSet ) {
67 if ( $resultPageSet->isResolvingRedirects() ) {
68 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
69 }
70
71 $this->run( $resultPageSet );
72 }
73
74 /**
75 * @param $resultPageSet ApiPageSet
76 * @return void
77 */
78 private function run( $resultPageSet = null ) {
79 $repo = $this->mRepo;
80 if ( !$repo instanceof LocalRepo ) {
81 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
82 }
83
84 $db = $this->getDB();
85
86 $params = $this->extractRequestParams();
87
88 // Image filters
89 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
90 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
91 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
92 $this->addWhereRange( 'img_name', $dir, $from, $to );
93
94 if ( isset( $params['prefix'] ) )
95 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
96
97 if ( isset( $params['minsize'] ) ) {
98 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
99 }
100
101 if ( isset( $params['maxsize'] ) ) {
102 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
103 }
104
105 $sha1 = false;
106 if ( isset( $params['sha1'] ) ) {
107 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
108 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
109 }
110 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
111 } elseif ( isset( $params['sha1base36'] ) ) {
112 $sha1 = $params['sha1base36'];
113 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
114 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
115 }
116 }
117 if ( $sha1 ) {
118 $this->addWhereFld( 'img_sha1', $sha1 );
119 }
120
121 if ( !is_null( $params['mime'] ) ) {
122 global $wgMiserMode;
123 if ( $wgMiserMode ) {
124 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
125 }
126
127 list( $major, $minor ) = File::splitMime( $params['mime'] );
128
129 $this->addWhereFld( 'img_major_mime', $major );
130 $this->addWhereFld( 'img_minor_mime', $minor );
131 }
132
133 $this->addTables( 'image' );
134
135 $prop = array_flip( $params['prop'] );
136 $this->addFields( LocalFile::selectFields() );
137
138 $limit = $params['limit'];
139 $this->addOption( 'LIMIT', $limit + 1 );
140 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
141 $this->addOption( 'ORDER BY', 'img_name' . $sort );
142
143 $res = $this->select( __METHOD__ );
144
145 $titles = array();
146 $count = 0;
147 $result = $this->getResult();
148 foreach ( $res as $row ) {
149 if ( ++ $count > $limit ) {
150 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
151 // TODO: Security issue - if the user has no right to view next title, it will still be shown
152 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
153 break;
154 }
155
156 if ( is_null( $resultPageSet ) ) {
157 $file = $repo->newFileFromRow( $row );
158 $info = array_merge( array( 'name' => $row->img_name ),
159 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
160 self::addTitleInfo( $info, $file->getTitle() );
161
162 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
163 if ( !$fit ) {
164 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
165 break;
166 }
167 } else {
168 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name );
169 }
170 }
171
172 if ( is_null( $resultPageSet ) ) {
173 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
174 } else {
175 $resultPageSet->populateFromTitles( $titles );
176 }
177 }
178
179 public function getAllowedParams() {
180 return array (
181 'from' => null,
182 'to' => null,
183 'prefix' => null,
184 'minsize' => array(
185 ApiBase::PARAM_TYPE => 'integer',
186 ),
187 'maxsize' => array(
188 ApiBase::PARAM_TYPE => 'integer',
189 ),
190 'limit' => array(
191 ApiBase::PARAM_DFLT => 10,
192 ApiBase::PARAM_TYPE => 'limit',
193 ApiBase::PARAM_MIN => 1,
194 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
195 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
196 ),
197 'dir' => array(
198 ApiBase::PARAM_DFLT => 'ascending',
199 ApiBase::PARAM_TYPE => array(
200 'ascending',
201 'descending'
202 )
203 ),
204 'sha1' => null,
205 'sha1base36' => null,
206 'prop' => array(
207 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
208 ApiBase::PARAM_DFLT => 'timestamp|url',
209 ApiBase::PARAM_ISMULTI => true
210 ),
211 'mime' => null,
212 );
213 }
214
215 public function getParamDescription() {
216 return array(
217 'from' => 'The image title to start enumerating from',
218 'to' => 'The image title to stop enumerating at',
219 'prefix' => 'Search for all image titles that begin with this value',
220 'dir' => 'The direction in which to list',
221 'minsize' => 'Limit to images with at least this many bytes',
222 'maxsize' => 'Limit to images with at most this many bytes',
223 'limit' => 'How many images in total to return',
224 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
225 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
226 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
227 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
228 );
229 }
230
231 private $propertyFilter = array( 'archivename' );
232
233 public function getDescription() {
234 return 'Enumerate all images sequentially';
235 }
236
237 public function getPossibleErrors() {
238 return array_merge( parent::getPossibleErrors(), array(
239 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
240 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
241 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
242 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
243 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
244 ) );
245 }
246
247 public function getExamples() {
248 return array(
249 'api.php?action=query&list=allimages&aifrom=B' => array(
250 'Simple Use',
251 'Show a list of images starting at the letter "B"',
252 ),
253 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
254 'Using as Generator',
255 'Show info about 4 images starting at the letter "T"',
256 ),
257 );
258 }
259
260 public function getHelpUrls() {
261 return 'https://www.mediawiki.org/wiki/API:Allimages';
262 }
263
264 public function getVersion() {
265 return __CLASS__ . ': $Id$';
266 }
267 }