API: Fix a bug to make list=allfileusages functional again
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate links from all pages together.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 switch ( $moduleName ) {
36 case 'alllinks':
37 $prefix = 'al';
38 $this->table = 'pagelinks';
39 $this->tablePrefix = 'pl_';
40 $this->fieldTitle = 'title';
41 $this->dfltNamespace = NS_MAIN;
42 $this->hasNamespace = true;
43 $this->indexTag = 'l';
44 $this->description = 'Enumerate all links that point to a given namespace';
45 $this->descriptionWhat = 'link';
46 $this->descriptionTargets = 'linked titles';
47 $this->descriptionLinking = 'linking';
48 break;
49 case 'alltransclusions':
50 $prefix = 'at';
51 $this->table = 'templatelinks';
52 $this->tablePrefix = 'tl_';
53 $this->fieldTitle = 'title';
54 $this->dfltNamespace = NS_TEMPLATE;
55 $this->hasNamespace = true;
56 $this->indexTag = 't';
57 $this->description =
58 'List all transclusions (pages embedded using {{x}}), including non-existing';
59 $this->descriptionWhat = 'transclusion';
60 $this->descriptionTargets = 'transcluded titles';
61 $this->descriptionLinking = 'transcluding';
62 break;
63 case 'allfileusages':
64 $prefix = 'af';
65 $this->table = 'imagelinks';
66 $this->tablePrefix = 'il_';
67 $this->fieldTitle = 'to';
68 $this->dfltNamespace = NS_FILE;
69 $this->hasNamespace = false;
70 $this->indexTag = 'f';
71 $this->description = 'List all file usages, including non-existing';
72 $this->descriptionWhat = 'file';
73 $this->descriptionTargets = 'file titles';
74 $this->descriptionLinking = 'using';
75 break;
76 default:
77 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
78 }
79
80 parent::__construct( $query, $moduleName, $prefix );
81 }
82
83 public function execute() {
84 $this->run();
85 }
86
87 public function getCacheMode( $params ) {
88 return 'public';
89 }
90
91 public function executeGenerator( $resultPageSet ) {
92 $this->run( $resultPageSet );
93 }
94
95 /**
96 * @param $resultPageSet ApiPageSet
97 * @return void
98 */
99 private function run( $resultPageSet = null ) {
100 $db = $this->getDB();
101 $params = $this->extractRequestParams();
102
103 $pfx = $this->tablePrefix;
104 $fieldTitle = $this->fieldTitle;
105 $prop = array_flip( $params['prop'] );
106 $fld_ids = isset( $prop['ids'] );
107 $fld_title = isset( $prop['title'] );
108 if ( $this->hasNamespace ) {
109 $namespace = $params['namespace'];
110 } else {
111 $namespace = $this->dfltNamespace;
112 }
113
114 if ( $params['unique'] ) {
115 if ( $fld_ids ) {
116 $this->dieUsage(
117 "{$this->getModuleName()} cannot return corresponding page " .
118 "ids in unique {$this->descriptionWhat}s mode",
119 'params'
120 );
121 }
122 $this->addOption( 'DISTINCT' );
123 }
124
125 $this->addTables( $this->table );
126 if ( $this->hasNamespace ) {
127 $this->addWhereFld( $pfx . 'namespace', $namespace );
128 }
129
130 $continue = !is_null( $params['continue'] );
131 if ( $continue ) {
132 $continueArr = explode( '|', $params['continue'] );
133 $op = $params['dir'] == 'descending' ? '<' : '>';
134 if ( $params['unique'] ) {
135 $this->dieContinueUsageIf( count( $continueArr ) != 1 );
136 $continueTitle = $db->addQuotes( $continueArr[0] );
137 $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" );
138 } else {
139 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
140 $continueTitle = $db->addQuotes( $continueArr[0] );
141 $continueFrom = intval( $continueArr[1] );
142 $this->addWhere(
143 "{$pfx}{$fieldTitle} $op $continueTitle OR " .
144 "({$pfx}{$fieldTitle} = $continueTitle AND " .
145 "{$pfx}from $op= $continueFrom)"
146 );
147 }
148 }
149
150 // 'continue' always overrides 'from'
151 $from = ( $continue || $params['from'] === null ? null :
152 $this->titlePartToKey( $params['from'], $namespace ) );
153 $to = ( $params['to'] === null ? null :
154 $this->titlePartToKey( $params['to'], $namespace ) );
155 $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
156
157 if ( isset( $params['prefix'] ) ) {
158 $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey(
159 $params['prefix'], $namespace ), $db->anyString() ) );
160 }
161
162 $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
163 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
164
165 if ( $this->hasNamespace ) {
166 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
167 }
168 $limit = $params['limit'];
169 $this->addOption( 'LIMIT', $limit + 1 );
170
171 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
172 $orderBy = array();
173 $orderBy[] = $pfx . $fieldTitle . $sort;
174 if ( !$params['unique'] ) {
175 $orderBy[] = $pfx . 'from' . $sort;
176 }
177 $this->addOption( 'ORDER BY', $orderBy );
178
179 $res = $this->select( __METHOD__ );
180
181 $pageids = array();
182 $titles = array();
183 $count = 0;
184 $result = $this->getResult();
185 foreach ( $res as $row ) {
186 if ( ++$count > $limit ) {
187 // We've reached the one extra which shows that there are
188 // additional pages to be had. Stop here...
189 if ( $params['unique'] ) {
190 $this->setContinueEnumParameter( 'continue', $row->pl_title );
191 } else {
192 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
193 }
194 break;
195 }
196
197 if ( is_null( $resultPageSet ) ) {
198 $vals = array();
199 if ( $fld_ids ) {
200 $vals['fromid'] = intval( $row->pl_from );
201 }
202 if ( $fld_title ) {
203 $title = Title::makeTitle( $namespace, $row->pl_title );
204 ApiQueryBase::addTitleInfo( $vals, $title );
205 }
206 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
207 if ( !$fit ) {
208 if ( $params['unique'] ) {
209 $this->setContinueEnumParameter( 'continue', $row->pl_title );
210 } else {
211 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
212 }
213 break;
214 }
215 } elseif ( $params['unique'] ) {
216 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
217 } else {
218 $pageids[] = $row->pl_from;
219 }
220 }
221
222 if ( is_null( $resultPageSet ) ) {
223 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag );
224 } elseif ( $params['unique'] ) {
225 $resultPageSet->populateFromTitles( $titles );
226 } else {
227 $resultPageSet->populateFromPageIDs( $pageids );
228 }
229 }
230
231 public function getAllowedParams() {
232 $allowedParams = array(
233 'continue' => null,
234 'from' => null,
235 'to' => null,
236 'prefix' => null,
237 'unique' => false,
238 'prop' => array(
239 ApiBase::PARAM_ISMULTI => true,
240 ApiBase::PARAM_DFLT => 'title',
241 ApiBase::PARAM_TYPE => array(
242 'ids',
243 'title'
244 )
245 ),
246 'namespace' => array(
247 ApiBase::PARAM_DFLT => $this->dfltNamespace,
248 ApiBase::PARAM_TYPE => 'namespace'
249 ),
250 'limit' => array(
251 ApiBase::PARAM_DFLT => 10,
252 ApiBase::PARAM_TYPE => 'limit',
253 ApiBase::PARAM_MIN => 1,
254 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
255 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
256 ),
257 'dir' => array(
258 ApiBase::PARAM_DFLT => 'ascending',
259 ApiBase::PARAM_TYPE => array(
260 'ascending',
261 'descending'
262 )
263 ),
264 );
265 if ( !$this->hasNamespace ) {
266 unset( $allowedParams['namespace'] );
267 }
268
269 return $allowedParams;
270 }
271
272 public function getParamDescription() {
273 $p = $this->getModulePrefix();
274 $what = $this->descriptionWhat;
275 $targets = $this->descriptionTargets;
276 $linking = $this->descriptionLinking;
277 $paramDescription = array(
278 'from' => "The title of the $what to start enumerating from",
279 'to' => "The title of the $what to stop enumerating at",
280 'prefix' => "Search for all $targets that begin with this value",
281 'unique' => array(
282 "Only show distinct $targets. Cannot be used with {$p}prop=ids.",
283 'When used as a generator, yields target pages instead of source pages.',
284 ),
285 'prop' => array(
286 'What pieces of information to include',
287 " ids - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
288 " title - Adds the title of the $what",
289 ),
290 'namespace' => 'The namespace to enumerate',
291 'limit' => 'How many total items to return',
292 'continue' => 'When more results are available, use this to continue',
293 'dir' => 'The direction in which to list',
294 );
295 if ( !$this->hasNamespace ) {
296 unset( $paramDescription['namespace'] );
297 }
298
299 return $paramDescription;
300 }
301
302 public function getResultProperties() {
303 return array(
304 'ids' => array(
305 'fromid' => 'integer'
306 ),
307 'title' => array(
308 'ns' => 'namespace',
309 'title' => 'string'
310 )
311 );
312 }
313
314 public function getDescription() {
315 return $this->description;
316 }
317
318 public function getPossibleErrors() {
319 $m = $this->getModuleName();
320 $what = $this->descriptionWhat;
321
322 return array_merge( parent::getPossibleErrors(), array(
323 array(
324 'code' => 'params',
325 'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode"
326 ),
327 ) );
328 }
329
330 public function getExamples() {
331 $p = $this->getModulePrefix();
332 $name = $this->getModuleName();
333 $what = $this->descriptionWhat;
334 $targets = $this->descriptionTargets;
335
336 return array(
337 "api.php?action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
338 => "List $targets with page ids they are from, including missing ones. Start at B",
339 "api.php?action=query&list={$name}&{$p}unique=&{$p}from=B"
340 => "List unique $targets",
341 "api.php?action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
342 => "Gets all $targets, marking the missing ones",
343 "api.php?action=query&generator={$name}&g{$p}from=B"
344 => "Gets pages containing the {$what}s",
345 );
346 }
347
348 public function getHelpUrls() {
349 $name = ucfirst( $this->getModuleName() );
350
351 return "https://www.mediawiki.org/wiki/API:{$name}";
352 }
353 }