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