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