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