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