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