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