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