Merge "Don't match HTML entities in language conversion syntax"
[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 = 'List all transclusions (pages embedded using {{x}}), including non-existing';
58 $this->descriptionWhat = 'transclusion';
59 $this->descriptionTargets = 'transcluded titles';
60 $this->descriptionLinking = 'transcluding';
61 break;
62 case 'allfileusages':
63 $prefix = 'af';
64 $this->table = 'imagelinks';
65 $this->tablePrefix = 'il_';
66 $this->fieldTitle = 'to';
67 $this->dfltNamespace = NS_FILE;
68 $this->hasNamespace = false;
69 $this->indexTag = 'f';
70 $this->description = 'List all file usages, including non-existing';
71 $this->descriptionWhat = 'file';
72 $this->descriptionTargets = 'file titles';
73 $this->descriptionLinking = 'using';
74 break;
75 default:
76 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
77 }
78
79 parent::__construct( $query, $moduleName, $prefix );
80 }
81
82 public function execute() {
83 $this->run();
84 }
85
86 public function getCacheMode( $params ) {
87 return 'public';
88 }
89
90 public function executeGenerator( $resultPageSet ) {
91 $this->run( $resultPageSet );
92 }
93
94 /**
95 * @param $resultPageSet ApiPageSet
96 * @return void
97 */
98 private function run( $resultPageSet = null ) {
99 $db = $this->getDB();
100 $params = $this->extractRequestParams();
101
102 $pfx = $this->tablePrefix;
103 $fieldTitle = $this->fieldTitle;
104 $prop = array_flip( $params['prop'] );
105 $fld_ids = isset( $prop['ids'] );
106 $fld_title = isset( $prop['title'] );
107 if ( $this->hasNamespace ) {
108 $namespace = $params['namespace'];
109 } else {
110 $namespace = $this->dfltNamespace;
111 }
112
113 if ( $params['unique'] ) {
114 if ( $fld_ids ) {
115 $this->dieUsage(
116 "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionWhat}s mode",
117 'params' );
118 }
119 $this->addOption( 'DISTINCT' );
120 }
121
122 $this->addTables( $this->table );
123 if ( $this->hasNamespace ) {
124 $this->addWhereFld( $pfx . 'namespace', $namespace );
125 }
126
127 $continue = !is_null( $params['continue'] );
128 if ( $continue ) {
129 $continueArr = explode( '|', $params['continue'] );
130 $op = $params['dir'] == 'descending' ? '<' : '>';
131 if ( $params['unique'] ) {
132 $this->dieContinueUsageIf( count( $continueArr ) != 1 );
133 $continueTitle = $db->addQuotes( $continueArr[0] );
134 $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" );
135 } else {
136 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
137 $continueTitle = $db->addQuotes( $continueArr[0] );
138 $continueFrom = intval( $continueArr[1] );
139 $this->addWhere(
140 "{$pfx}{$fieldTitle} $op $continueTitle OR " .
141 "({$pfx}{$fieldTitle} = $continueTitle AND " .
142 "{$pfx}from $op= $continueFrom)"
143 );
144 }
145 }
146
147 // 'continue' always overrides 'from'
148 $from = ( $continue || is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
149 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
150 $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
151
152 if ( isset( $params['prefix'] ) ) {
153 $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
154 }
155
156 $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
157 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
158
159 if ( $this->hasNamespace ) {
160 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
161 }
162 $limit = $params['limit'];
163 $this->addOption( 'LIMIT', $limit + 1 );
164
165 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
166 $orderBy = array();
167 $orderBy[] = $pfx . $fieldTitle . $sort;
168 if ( !$params['unique'] ) {
169 $orderBy[] = $pfx . 'from' . $sort;
170 }
171 $this->addOption( 'ORDER BY', $orderBy );
172
173 $res = $this->select( __METHOD__ );
174
175 $pageids = array();
176 $titles = array();
177 $count = 0;
178 $result = $this->getResult();
179 foreach ( $res as $row ) {
180 if ( ++ $count > $limit ) {
181 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
182 if ( $params['unique'] ) {
183 $this->setContinueEnumParameter( 'continue', $row->pl_title );
184 } else {
185 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
186 }
187 break;
188 }
189
190 if ( is_null( $resultPageSet ) ) {
191 $vals = array();
192 if ( $fld_ids ) {
193 $vals['fromid'] = intval( $row->pl_from );
194 }
195 if ( $fld_title ) {
196 $title = Title::makeTitle( $namespace, $row->pl_title );
197 ApiQueryBase::addTitleInfo( $vals, $title );
198 }
199 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
200 if ( !$fit ) {
201 if ( $params['unique'] ) {
202 $this->setContinueEnumParameter( 'continue', $row->pl_title );
203 } else {
204 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
205 }
206 break;
207 }
208 } elseif ( $params['unique'] ) {
209 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
210 } else {
211 $pageids[] = $row->pl_from;
212 }
213 }
214
215 if ( is_null( $resultPageSet ) ) {
216 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag );
217 } elseif ( $params['unique'] ) {
218 $resultPageSet->populateFromTitles( $titles );
219 } else {
220 $resultPageSet->populateFromPageIDs( $pageids );
221 }
222 }
223
224 public function getAllowedParams() {
225 $allowedParams = array(
226 'continue' => null,
227 'from' => null,
228 'to' => null,
229 'prefix' => null,
230 'unique' => false,
231 'prop' => array(
232 ApiBase::PARAM_ISMULTI => true,
233 ApiBase::PARAM_DFLT => 'title',
234 ApiBase::PARAM_TYPE => array(
235 'ids',
236 'title'
237 )
238 ),
239 'namespace' => array(
240 ApiBase::PARAM_DFLT => $this->dfltNamespace,
241 ApiBase::PARAM_TYPE => 'namespace'
242 ),
243 'limit' => array(
244 ApiBase::PARAM_DFLT => 10,
245 ApiBase::PARAM_TYPE => 'limit',
246 ApiBase::PARAM_MIN => 1,
247 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
248 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
249 ),
250 'dir' => array(
251 ApiBase::PARAM_DFLT => 'ascending',
252 ApiBase::PARAM_TYPE => array(
253 'ascending',
254 'descending'
255 )
256 ),
257 );
258 if ( !$this->hasNamespace ) {
259 unset( $allowedParams['namespace'] );
260 }
261 return $allowedParams;
262 }
263
264 public function getParamDescription() {
265 $p = $this->getModulePrefix();
266 $what = $this->descriptionWhat;
267 $targets = $this->descriptionTargets;
268 $linking = $this->descriptionLinking;
269 $paramDescription = array(
270 'from' => "The title of the $what to start enumerating from",
271 'to' => "The title of the $what to stop enumerating at",
272 'prefix' => "Search for all $targets that begin with this value",
273 'unique' => array(
274 "Only show distinct $targets. Cannot be used with {$p}prop=ids.",
275 'When used as a generator, yields target pages instead of source pages.',
276 ),
277 'prop' => array(
278 'What pieces of information to include',
279 " ids - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
280 " title - Adds the title of the $what",
281 ),
282 'namespace' => 'The namespace to enumerate',
283 'limit' => 'How many total items to return',
284 'continue' => 'When more results are available, use this to continue',
285 'dir' => 'The direction in which to list',
286 );
287 if ( !$this->hasNamespace ) {
288 unset( $paramDescription['namespace'] );
289 }
290 return $paramDescription;
291 }
292
293 public function getResultProperties() {
294 return array(
295 'ids' => array(
296 'fromid' => 'integer'
297 ),
298 'title' => array(
299 'ns' => 'namespace',
300 'title' => 'string'
301 )
302 );
303 }
304
305 public function getDescription() {
306 return $this->description;
307 }
308
309 public function getPossibleErrors() {
310 $m = $this->getModuleName();
311 $what = $this->descriptionWhat;
312 return array_merge( parent::getPossibleErrors(), array(
313 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode" ),
314 ) );
315 }
316
317 public function getExamples() {
318 $p = $this->getModulePrefix();
319 $name = $this->getModuleName();
320 $what = $this->descriptionWhat;
321 $targets = $this->descriptionTargets;
322 return array(
323 "api.php?action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
324 => "List $targets with page ids they are from, including missing ones. Start at B",
325 "api.php?action=query&list={$name}&{$p}unique=&{$p}from=B"
326 => "List unique $targets",
327 "api.php?action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
328 => "Gets all $targets, marking the missing ones",
329 "api.php?action=query&generator={$name}&g{$p}from=B"
330 => "Gets pages containing the {$what}s",
331 );
332 }
333
334 public function getHelpUrls() {
335 $name = ucfirst( $this->getModuleName() );
336 return "https://www.mediawiki.org/wiki/API:{$name}";
337 }
338 }