]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllimages.php
MediaWiki 1.15.5
[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         }
42
43         public function execute() {
44                 $this->run();
45         }
46
47         public function getCacheMode( $params ) {
48                 return 'public';
49         }
50
51         public function executeGenerator($resultPageSet) {
52                 if ($resultPageSet->isResolvingRedirects())
53                         $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
54
55                 $this->run($resultPageSet);
56         }
57
58         private function run($resultPageSet = null) {
59                 $repo = RepoGroup::singleton()->getLocalRepo();
60                 if ( !$repo instanceof LocalRepo )
61                         $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
62
63                 $db = $this->getDB();
64
65                 $params = $this->extractRequestParams();
66
67                 // Image filters
68                 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
69                 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
70                 $this->addWhereRange('img_name', $dir, $from, null);
71                 if (isset ($params['prefix']))
72                         $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
73
74                 if (isset ($params['minsize'])) {
75                         $this->addWhere('img_size>=' . intval($params['minsize']));
76                 }
77
78                 if (isset ($params['maxsize'])) {
79                         $this->addWhere('img_size<=' . intval($params['maxsize']));
80                 }
81
82                 $sha1 = false;
83                 if( isset( $params['sha1'] ) ) {
84                         $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
85                 } elseif( isset( $params['sha1base36'] ) ) {
86                         $sha1 = $params['sha1base36'];
87                 }
88                 if( $sha1 ) {
89                         $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
90                 }
91
92                 $this->addTables('image');
93
94                 $prop = array_flip($params['prop']);
95                 $this->addFields( LocalFile::selectFields() );
96
97                 $limit = $params['limit'];
98                 $this->addOption('LIMIT', $limit+1);
99                 $this->addOption('ORDER BY', 'img_name' .
100                                                 ($params['dir'] == 'descending' ? ' DESC' : ''));
101
102                 $res = $this->select(__METHOD__);
103
104                 $titles = array();
105                 $count = 0;
106                 $result = $this->getResult();
107                 while ($row = $db->fetchObject($res)) {
108                         if (++ $count > $limit) {
109                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
111                                 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
112                                 break;
113                         }
114
115                         if (is_null($resultPageSet)) {
116                                 $file = $repo->newFileFromRow( $row );
117                                 $info = array_merge(array('name' => $row->img_name),
118                                         ApiQueryImageInfo::getInfo($file, $prop, $result));
119                                 $fit = $result->addValue(array('query', $this->getModuleName()), null, $info);
120                                 if( !$fit ) {
121                                         $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
122                                         break;
123                                 }
124                         } else {
125                                 $titles[] = Title::makeTitle(NS_IMAGE, $row->img_name);
126                         }
127                 }
128                 $db->freeResult($res);
129
130                 if (is_null($resultPageSet)) {
131                         $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img');
132                 } else {
133                         $resultPageSet->populateFromTitles($titles);
134                 }
135         }
136
137         public function getAllowedParams() {
138                 return array (
139                         'from' => null,
140                         'prefix' => null,
141                         'minsize' => array (
142                                 ApiBase :: PARAM_TYPE => 'integer',
143                         ),
144                         'maxsize' => array (
145                                 ApiBase :: PARAM_TYPE => 'integer',
146                         ),
147                         'limit' => array (
148                                 ApiBase :: PARAM_DFLT => 10,
149                                 ApiBase :: PARAM_TYPE => 'limit',
150                                 ApiBase :: PARAM_MIN => 1,
151                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
152                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
153                         ),
154                         'dir' => array (
155                                 ApiBase :: PARAM_DFLT => 'ascending',
156                                 ApiBase :: PARAM_TYPE => array (
157                                         'ascending',
158                                         'descending'
159                                 )
160                         ),
161                         'sha1' => null,
162                         'sha1base36' => null,
163                         'prop' => array (
164                                 ApiBase :: PARAM_TYPE => array(
165                                         'timestamp',
166                                         'user',
167                                         'comment',
168                                         'url',
169                                         'size',
170                                         'dimensions', // Obsolete
171                                         'mime',
172                                         'sha1',
173                                         'metadata',
174                                         'bitdepth',
175                                 ),
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         protected function getExamples() {
201                 return array (
202                         'Simple Use',
203                         ' Show a list of images starting at the letter "B"',
204                         '  api.php?action=query&list=allimages&aifrom=B',
205                         'Using as Generator',
206                         ' Show info about 4 images starting at the letter "T"',
207                         '  api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
208                 );
209         }
210
211         public function getVersion() {
212                 return __CLASS__ . ': $Id: ApiQueryAllimages.php 69986 2010-07-27 03:57:39Z tstarling $';
213         }
214 }