bdc561b500f4881c9485da2bc363810ba535bf43
[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 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
32 }
33
34 /**
35 * Query module to enumerate all available pages.
36 *
37 * @ingroup API
38 */
39 class ApiQueryAllimages extends ApiQueryGeneratorBase {
40
41 protected $mRepo;
42
43 public function __construct( $query, $moduleName ) {
44 parent::__construct( $query, $moduleName, 'ai' );
45 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
46 }
47
48 /**
49 * Override parent method to make sure to make sure the repo's DB is used
50 * which may not necesarilly be the same as the local DB.
51 *
52 * TODO: allow querying non-local repos.
53 * @return DatabaseBase
54 */
55 protected function getDB() {
56 return $this->mRepo->getSlaveDB();
57 }
58
59 public function execute() {
60 $this->run();
61 }
62
63 public function getCacheMode( $params ) {
64 return 'public';
65 }
66
67 /**
68 * @param $resultPageSet ApiPageSet
69 * @return void
70 */
71 public function executeGenerator( $resultPageSet ) {
72 if ( $resultPageSet->isResolvingRedirects() ) {
73 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
74 }
75
76 $this->run( $resultPageSet );
77 }
78
79 /**
80 * @param $resultPageSet ApiPageSet
81 * @return void
82 */
83 private function run( $resultPageSet = null ) {
84 $repo = $this->mRepo;
85 if ( !$repo instanceof LocalRepo ) {
86 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
87 }
88
89 $db = $this->getDB();
90
91 $params = $this->extractRequestParams();
92
93 // Image filters
94 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
95 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
96 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
97 $this->addWhereRange( 'img_name', $dir, $from, $to );
98
99 if ( isset( $params['prefix'] ) )
100 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
101
102 if ( isset( $params['minsize'] ) ) {
103 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
104 }
105
106 if ( isset( $params['maxsize'] ) ) {
107 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
108 }
109
110 $sha1 = false;
111 if ( isset( $params['sha1'] ) ) {
112 if ( !self::validateSha1Hash( $params['sha1'] ) ) {
113 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
114 }
115 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
116 } elseif ( isset( $params['sha1base36'] ) ) {
117 $sha1 = $params['sha1base36'];
118 if ( !self::validateSha1Base36Hash( $sha1 ) ) {
119 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
120 }
121 }
122 if ( $sha1 ) {
123 $this->addWhereFld( 'img_sha1', $sha1 );
124 }
125
126 if ( !is_null( $params['mime'] ) ) {
127 global $wgMiserMode;
128 if ( $wgMiserMode ) {
129 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimeearchdisabled' );
130 }
131
132 list( $major, $minor ) = File::splitMime( $params['mime'] );
133
134 $this->addWhereFld( 'img_major_mime', $major );
135 $this->addWhereFld( 'img_minor_mime', $minor );
136 }
137
138 $this->addTables( 'image' );
139
140 $prop = array_flip( $params['prop'] );
141 $this->addFields( LocalFile::selectFields() );
142
143 $limit = $params['limit'];
144 $this->addOption( 'LIMIT', $limit + 1 );
145 $this->addOption( 'ORDER BY', 'img_name' .
146 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
147
148 $res = $this->select( __METHOD__ );
149
150 $titles = array();
151 $count = 0;
152 $result = $this->getResult();
153 foreach ( $res as $row ) {
154 if ( ++ $count > $limit ) {
155 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
156 // TODO: Security issue - if the user has no right to view next title, it will still be shown
157 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
158 break;
159 }
160
161 if ( is_null( $resultPageSet ) ) {
162 $file = $repo->newFileFromRow( $row );
163 $info = array_merge( array( 'name' => $row->img_name ),
164 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
165 self::addTitleInfo( $info, $file->getTitle() );
166
167 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
168 if ( !$fit ) {
169 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
170 break;
171 }
172 } else {
173 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name );
174 }
175 }
176
177 if ( is_null( $resultPageSet ) ) {
178 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
179 } else {
180 $resultPageSet->populateFromTitles( $titles );
181 }
182 }
183
184 /**
185 * @param $hash string
186 * @return bool
187 */
188 public static function validateSha1Hash( $hash ) {
189 return preg_match( '/[a-f0-9]{40}/', $hash );
190 }
191
192 /**
193 * @param $hash string
194 * @return bool
195 */
196 public static function validateSha1Base36Hash( $hash ) {
197 return preg_match( '/[a-z0-9]{31}/', $hash );
198 }
199
200 public function getAllowedParams() {
201 return array (
202 'from' => null,
203 'to' => null,
204 'prefix' => null,
205 'minsize' => array(
206 ApiBase::PARAM_TYPE => 'integer',
207 ),
208 'maxsize' => array(
209 ApiBase::PARAM_TYPE => 'integer',
210 ),
211 'limit' => array(
212 ApiBase::PARAM_DFLT => 10,
213 ApiBase::PARAM_TYPE => 'limit',
214 ApiBase::PARAM_MIN => 1,
215 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
216 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
217 ),
218 'dir' => array(
219 ApiBase::PARAM_DFLT => 'ascending',
220 ApiBase::PARAM_TYPE => array(
221 'ascending',
222 'descending'
223 )
224 ),
225 'sha1' => null,
226 'sha1base36' => null,
227 'prop' => array(
228 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
229 ApiBase::PARAM_DFLT => 'timestamp|url',
230 ApiBase::PARAM_ISMULTI => true
231 ),
232 'mime' => null,
233 );
234 }
235
236 public function getParamDescription() {
237 return array(
238 'from' => 'The image title to start enumerating from',
239 'to' => 'The image title to stop enumerating at',
240 'prefix' => 'Search for all image titles that begin with this value',
241 'dir' => 'The direction in which to list',
242 'minsize' => 'Limit to images with at least this many bytes',
243 'maxsize' => 'Limit to images with at most this many bytes',
244 'limit' => 'How many images in total to return',
245 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
246 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
247 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
248 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
249 );
250 }
251
252 private $propertyFilter = array( 'archivename' );
253
254 public function getDescription() {
255 return 'Enumerate all images sequentially';
256 }
257
258 public function getPossibleErrors() {
259 return array_merge( parent::getPossibleErrors(), array(
260 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
261 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
262 array( 'code' => 'mimeearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
263 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
264 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
265 ) );
266 }
267
268 protected function getExamples() {
269 return array(
270 'Simple Use',
271 ' Show a list of images starting at the letter "B"',
272 ' api.php?action=query&list=allimages&aifrom=B',
273 'Using as Generator',
274 ' Show info about 4 images starting at the letter "T"',
275 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
276 );
277 }
278
279 public function getVersion() {
280 return __CLASS__ . ': $Id$';
281 }
282 }