61adc32bab92503affd285e5df39528928b16845
[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 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
113 } elseif ( isset( $params['sha1base36'] ) ) {
114 $sha1 = $params['sha1base36'];
115 }
116 if ( $sha1 ) {
117 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
118 }
119
120 if ( !is_null( $params['mime'] ) ) {
121 global $wgMiserMode;
122 if ( $wgMiserMode ) {
123 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimeearchdisabled' );
124 }
125
126 list( $major, $minor ) = File::splitMime( $params['mime'] );
127
128 $this->addWhereFld( 'img_major_mime', $major );
129 $this->addWhereFld( 'img_minor_mime', $minor );
130 }
131
132 $this->addTables( 'image' );
133
134 $prop = array_flip( $params['prop'] );
135 $this->addFields( LocalFile::selectFields() );
136
137 $limit = $params['limit'];
138 $this->addOption( 'LIMIT', $limit + 1 );
139 $this->addOption( 'ORDER BY', 'img_name' .
140 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
141
142 $res = $this->select( __METHOD__ );
143
144 $titles = array();
145 $count = 0;
146 $result = $this->getResult();
147 foreach ( $res as $row ) {
148 if ( ++ $count > $limit ) {
149 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
150 // TODO: Security issue - if the user has no right to view next title, it will still be shown
151 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
152 break;
153 }
154
155 if ( is_null( $resultPageSet ) ) {
156 $file = $repo->newFileFromRow( $row );
157 $info = array_merge( array( 'name' => $row->img_name ),
158 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
159 self::addTitleInfo( $info, $file->getTitle() );
160
161 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
162 if ( !$fit ) {
163 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
164 break;
165 }
166 } else {
167 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name );
168 }
169 }
170
171 if ( is_null( $resultPageSet ) ) {
172 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
173 } else {
174 $resultPageSet->populateFromTitles( $titles );
175 }
176 }
177
178 public function getAllowedParams() {
179 return array (
180 'from' => null,
181 'to' => null,
182 'prefix' => null,
183 'minsize' => array(
184 ApiBase::PARAM_TYPE => 'integer',
185 ),
186 'maxsize' => array(
187 ApiBase::PARAM_TYPE => 'integer',
188 ),
189 'limit' => array(
190 ApiBase::PARAM_DFLT => 10,
191 ApiBase::PARAM_TYPE => 'limit',
192 ApiBase::PARAM_MIN => 1,
193 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
194 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
195 ),
196 'dir' => array(
197 ApiBase::PARAM_DFLT => 'ascending',
198 ApiBase::PARAM_TYPE => array(
199 'ascending',
200 'descending'
201 )
202 ),
203 'sha1' => null,
204 'sha1base36' => null,
205 'prop' => array(
206 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames(),
207 ApiBase::PARAM_DFLT => 'timestamp|url',
208 ApiBase::PARAM_ISMULTI => true
209 ),
210 'mime' => null,
211 );
212 }
213
214 public function getParamDescription() {
215 return array(
216 'from' => 'The image title to start enumerating from',
217 'to' => 'The image title to stop enumerating at',
218 'prefix' => 'Search for all image titles that begin with this value',
219 'dir' => 'The direction in which to list',
220 'minsize' => 'Limit to images with at least this many bytes',
221 'maxsize' => 'Limit to images with at most this many bytes',
222 'limit' => 'How many images in total to return',
223 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
224 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
225 'prop' => ApiQueryImageInfo::getPropertyDescriptions(),
226 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
227 );
228 }
229
230 public function getDescription() {
231 return 'Enumerate all images sequentially';
232 }
233
234 public function getPossibleErrors() {
235 return array_merge( parent::getPossibleErrors(), array(
236 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
237 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
238 array( 'code' => 'mimeearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
239 ) );
240 }
241
242 protected function getExamples() {
243 return array(
244 'Simple Use',
245 ' Show a list of images starting at the letter "B"',
246 ' api.php?action=query&list=allimages&aifrom=B',
247 'Using as Generator',
248 ' Show info about 4 images starting at the letter "T"',
249 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
250 );
251 }
252
253 public function getVersion() {
254 return __CLASS__ . ': $Id$';
255 }
256 }