Don't require commandLine.inc when not using the command line; instead, move wfWaitFo...
[lhc/web/wiklou.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 * @addtogroup 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 executeGenerator($resultPageSet) {
48 if ($resultPageSet->isResolvingRedirects())
49 $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
50
51 $this->run($resultPageSet);
52 }
53
54 private function run($resultPageSet = null) {
55
56 $db = $this->getDB();
57
58 $params = $this->extractRequestParams();
59
60 // Image filters
61 if (!is_null($params['from']))
62 $this->addWhere('img_name>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
63 if (isset ($params['prefix']))
64 $this->addWhere("img_name LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
65
66 if (isset ($params['minsize'])) {
67 $this->addWhere('img_size>=' . intval($params['minsize']));
68 }
69
70 if (isset ($params['maxsize'])) {
71 $this->addWhere('img_size<=' . intval($params['maxsize']));
72 }
73
74 $sha1 = false;
75 if( isset( $params['sha1'] ) ) {
76 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
77 } elseif( isset( $params['sha1base36'] ) ) {
78 $sha1 = $params['sha1base36'];
79 }
80 if( $sha1 ) {
81 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
82 }
83
84 $this->addTables('image');
85
86 $prop = array_flip($params['prop']);
87 $this->addFields('img_name');
88 $this->addFieldsIf('img_size', isset($prop['size']));
89 $this->addFieldsIf(array('img_width', 'img_height'), isset($prop['dimensions']));
90 $this->addFieldsIf(array('img_major_mime', 'img_minor_mime'), isset($prop['mime']));
91 $this->addFieldsIf('img_timestamp', isset($prop['timestamp']));
92
93 $limit = $params['limit'];
94 $this->addOption('LIMIT', $limit+1);
95 $this->addOption('ORDER BY', 'img_name' .
96 ($params['dir'] == 'descending' ? ' DESC' : ''));
97
98 $res = $this->select(__METHOD__);
99
100 $data = array ();
101 $count = 0;
102 while ($row = $db->fetchObject($res)) {
103 if (++ $count > $limit) {
104 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
105 // TODO: Security issue - if the user has no right to view next title, it will still be shown
106 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->img_name));
107 break;
108 }
109
110 if (is_null($resultPageSet)) {
111 $file = wfLocalFile( $row->img_name );
112 $item['name'] = $row->img_name;
113 if(isset($prop['size']))
114 $item['size'] = $file->getSize();
115 if(isset($prop['dimensions']))
116 {
117 $item['width'] = $file->getWidth();
118 $item['height'] = $file->getHeight();
119 }
120 if(isset($prop['mime']))
121 $item['mime'] = $row->img_major_mime . '/' . $row->img_minor_mime;
122 if(isset($prop['sha1']))
123 $item['sha1'] = wfBaseConvert($file->getSha1(), 36, 16, 31);
124 if(isset($prop['timestamp']))
125 $item['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp());
126 if(isset($prop['url']))
127 $item['url'] = $file->getUrl();
128 $data[] = $item;
129 } else {
130 $data[] = Title::makeTitle( NS_IMAGE, $row->img_name );
131 }
132 }
133 $db->freeResult($res);
134
135 if (is_null($resultPageSet)) {
136 $result = $this->getResult();
137 $result->setIndexedTagName($data, 'img');
138 $result->addValue('query', $this->getModuleName(), $data);
139 } else {
140 $resultPageSet->populateFromTitles( $data );
141 }
142 }
143
144 public function getAllowedParams() {
145 return array (
146 'from' => null,
147 'prefix' => null,
148 'minsize' => array (
149 ApiBase :: PARAM_TYPE => 'integer',
150 ),
151 'maxsize' => array (
152 ApiBase :: PARAM_TYPE => 'integer',
153 ),
154 'limit' => array (
155 ApiBase :: PARAM_DFLT => 10,
156 ApiBase :: PARAM_TYPE => 'limit',
157 ApiBase :: PARAM_MIN => 1,
158 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
159 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
160 ),
161 'dir' => array (
162 ApiBase :: PARAM_DFLT => 'ascending',
163 ApiBase :: PARAM_TYPE => array (
164 'ascending',
165 'descending'
166 )
167 ),
168 'sha1' => null,
169 'sha1base36' => null,
170 'prop' => array (
171 ApiBase :: PARAM_TYPE => array(
172 'timestamp',
173 'url',
174 'size',
175 'dimensions',
176 'mime',
177 'sha1'
178 ),
179 ApiBase :: PARAM_DFLT => 'timestamp|url',
180 ApiBase :: PARAM_ISMULTI => true
181 )
182 );
183 }
184
185 public function getParamDescription() {
186 return array (
187 'from' => 'The image title to start enumerating from.',
188 'prefix' => 'Search for all image titles that begin with this value.',
189 'dir' => 'The direction in which to list',
190 'minsize' => 'Limit to images with at least this many bytes',
191 'maxsize' => 'Limit to images with at most this many bytes',
192 'limit' => 'How many total pages to return.',
193 'sha1' => 'SHA1 hash of image',
194 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
195 'prop' => 'Which properties to get',
196 );
197 }
198
199 public function getDescription() {
200 return 'Enumerate all images sequentially';
201 }
202
203 protected function getExamples() {
204 return array (
205 'Simple Use',
206 ' Show a list of images starting at the letter "B"',
207 ' api.php?action=query&list=allimages&aifrom=B',
208 'Using as Generator',
209 ' Show info about 4 images starting at the letter "T"',
210 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
211 );
212 }
213
214 public function getVersion() {
215 return __CLASS__ . ': $Id$';
216 }
217 }
218