Call Linker methods statically
[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 ( !$this->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 ( !$this->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', 'mimesearchdisabled' );
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 public function getAllowedParams() {
185 return array (
186 'from' => null,
187 'to' => null,
188 'prefix' => null,
189 'minsize' => array(
190 ApiBase::PARAM_TYPE => 'integer',
191 ),
192 'maxsize' => array(
193 ApiBase::PARAM_TYPE => 'integer',
194 ),
195 'limit' => array(
196 ApiBase::PARAM_DFLT => 10,
197 ApiBase::PARAM_TYPE => 'limit',
198 ApiBase::PARAM_MIN => 1,
199 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
200 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
201 ),
202 'dir' => array(
203 ApiBase::PARAM_DFLT => 'ascending',
204 ApiBase::PARAM_TYPE => array(
205 'ascending',
206 'descending'
207 )
208 ),
209 'sha1' => null,
210 'sha1base36' => null,
211 'prop' => array(
212 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
213 ApiBase::PARAM_DFLT => 'timestamp|url',
214 ApiBase::PARAM_ISMULTI => true
215 ),
216 'mime' => null,
217 );
218 }
219
220 public function getParamDescription() {
221 return array(
222 'from' => 'The image title to start enumerating from',
223 'to' => 'The image title to stop enumerating at',
224 'prefix' => 'Search for all image titles that begin with this value',
225 'dir' => 'The direction in which to list',
226 'minsize' => 'Limit to images with at least this many bytes',
227 'maxsize' => 'Limit to images with at most this many bytes',
228 'limit' => 'How many images in total to return',
229 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
230 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
231 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
232 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
233 );
234 }
235
236 private $propertyFilter = array( 'archivename' );
237
238 public function getDescription() {
239 return 'Enumerate all images sequentially';
240 }
241
242 public function getPossibleErrors() {
243 return array_merge( parent::getPossibleErrors(), array(
244 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
245 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
246 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
247 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
248 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
249 ) );
250 }
251
252 public function getExamples() {
253 return array(
254 'Simple Use',
255 ' Show a list of images starting at the letter "B"',
256 ' api.php?action=query&list=allimages&aifrom=B',
257 'Using as Generator',
258 ' Show info about 4 images starting at the letter "T"',
259 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
260 );
261 }
262
263 public function getHelpUrls() {
264 return 'http://www.mediawiki.org/wiki/API:Allimages';
265 }
266
267 public function getVersion() {
268 return __CLASS__ . ': $Id$';
269 }
270 }