Move a few braces noticed while doing CR
[lhc/web/wiklou.git] / includes / api / ApiQueryAllimages.php
1 <?php
2
3 /**
4 * Created on Mar 16, 2008
5 *
6 * API for MediaWiki 1.12+
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all available pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllimages extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'ai' );
41 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
42 }
43
44 /**
45 * Overide parent method to make sure to make sure the repo's DB is used
46 * which may not necesarilly be the same as the local DB.
47 *
48 * TODO: allow querying non-local repos.
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 public function executeGenerator( $resultPageSet ) {
63 if ( $resultPageSet->isResolvingRedirects() ) {
64 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
65 }
66
67 $this->run( $resultPageSet );
68 }
69
70 private function run( $resultPageSet = null ) {
71 $repo = $this->mRepo;
72 if ( !$repo instanceof LocalRepo ) {
73 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
74 }
75
76 $db = $this->getDB();
77
78 $params = $this->extractRequestParams();
79
80 // Image filters
81 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
82 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
83 $this->addWhereRange( 'img_name', $dir, $from, null );
84 if ( isset( $params['prefix'] ) )
85 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
86
87 if ( isset( $params['minsize'] ) ) {
88 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
89 }
90
91 if ( isset( $params['maxsize'] ) ) {
92 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
93 }
94
95 $sha1 = false;
96 if ( isset( $params['sha1'] ) ) {
97 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
98 } elseif ( isset( $params['sha1base36'] ) ) {
99 $sha1 = $params['sha1base36'];
100 }
101 if ( $sha1 ) {
102 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
103 }
104
105 $this->addTables( 'image' );
106
107 $prop = array_flip( $params['prop'] );
108 $this->addFields( LocalFile::selectFields() );
109
110 $limit = $params['limit'];
111 $this->addOption( 'LIMIT', $limit + 1 );
112 $this->addOption( 'ORDER BY', 'img_name' .
113 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
114
115 $res = $this->select( __METHOD__ );
116
117 $titles = array();
118 $count = 0;
119 $result = $this->getResult();
120 foreach ( $res as $row ) {
121 if ( ++ $count > $limit ) {
122 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
123 // TODO: Security issue - if the user has no right to view next title, it will still be shown
124 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
125 break;
126 }
127
128 if ( is_null( $resultPageSet ) ) {
129 $file = $repo->newFileFromRow( $row );
130 $info = array_merge( array( 'name' => $row->img_name ),
131 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
132 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
133 if ( !$fit ) {
134 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
135 break;
136 }
137 } else {
138 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name );
139 }
140 }
141
142 if ( is_null( $resultPageSet ) ) {
143 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
144 } else {
145 $resultPageSet->populateFromTitles( $titles );
146 }
147 }
148
149 public function getAllowedParams() {
150 return array (
151 'from' => null,
152 'prefix' => null,
153 'minsize' => array(
154 ApiBase::PARAM_TYPE => 'integer',
155 ),
156 'maxsize' => array(
157 ApiBase::PARAM_TYPE => 'integer',
158 ),
159 'limit' => array(
160 ApiBase::PARAM_DFLT => 10,
161 ApiBase::PARAM_TYPE => 'limit',
162 ApiBase::PARAM_MIN => 1,
163 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
164 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
165 ),
166 'dir' => array(
167 ApiBase::PARAM_DFLT => 'ascending',
168 ApiBase::PARAM_TYPE => array(
169 'ascending',
170 'descending'
171 )
172 ),
173 'sha1' => null,
174 'sha1base36' => null,
175 'prop' => array(
176 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames(),
177 ApiBase::PARAM_DFLT => 'timestamp|url',
178 ApiBase::PARAM_ISMULTI => true
179 )
180 );
181 }
182
183 public function getParamDescription() {
184 return array(
185 'from' => 'The image title to start enumerating from',
186 'prefix' => 'Search for all image titles that begin with this value',
187 'dir' => 'The direction in which to list',
188 'minsize' => 'Limit to images with at least this many bytes',
189 'maxsize' => 'Limit to images with at most this many bytes',
190 'limit' => 'How many images in total to return',
191 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
192 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
193 'prop' => array(
194 'Which properties to get',
195 ' timestamp - Adds the timestamp when the image was upload',
196 ' user - Adds the username of the last uploader',
197 ' comment - Adds the comment of the last upload',
198 ' url - Adds the URL of the image and its description page',
199 ' size - Adds the size of the image in bytes and its height and width',
200 ' dimensions - Alias of size',
201 ' sha1 - Adds the sha1 of the image',
202 ' mime - Adds the MIME of the image',
203 ' thumbmime - Adds the MIME of the tumbnail for the image',
204 ' archivename - Adds the file name of the archive version for non-latest versions',
205 ' bitdepth - Adds the bit depth of the version',
206 ),
207 );
208 }
209
210 public function getDescription() {
211 return 'Enumerate all images sequentially';
212 }
213
214 public function getPossibleErrors() {
215 return array_merge( parent::getPossibleErrors(), array(
216 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
217 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
218 ) );
219 }
220
221 protected function getExamples() {
222 return array(
223 'Simple Use',
224 ' Show a list of images starting at the letter "B"',
225 ' api.php?action=query&list=allimages&aifrom=B',
226 'Using as Generator',
227 ' Show info about 4 images starting at the letter "T"',
228 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
229 );
230 }
231
232 public function getVersion() {
233 return __CLASS__ . ': $Id$';
234 }
235 }