5 * Created on July 7, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * Query module to enumerate links from all pages together.
32 class ApiQueryAllLinks
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 switch ( $moduleName ) {
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';
49 case 'alltransclusions':
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';
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';
76 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
79 parent
::__construct( $query, $moduleName, $prefix );
82 public function execute() {
86 public function getCacheMode( $params ) {
90 public function executeGenerator( $resultPageSet ) {
91 $this->run( $resultPageSet );
95 * @param $resultPageSet ApiPageSet
98 private function run( $resultPageSet = null ) {
100 $params = $this->extractRequestParams();
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'];
110 $namespace = $this->dfltNamespace
;
113 if ( $params['unique'] ) {
116 "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionWhat}s mode",
119 $this->addOption( 'DISTINCT' );
122 $this->addTables( $this->table
);
123 if ( $this->hasNamespace
) {
124 $this->addWhereFld( $pfx . 'namespace', $namespace );
127 $continue = !is_null( $params['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" );
136 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
137 $continueTitle = $db->addQuotes( $continueArr[0] );
138 $continueFrom = intval( $continueArr[1] );
140 "{$pfx}{$fieldTitle} $op $continueTitle OR " .
141 "({$pfx}{$fieldTitle} = $continueTitle AND " .
142 "{$pfx}from $op= $continueFrom)"
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 );
152 if ( isset( $params['prefix'] ) ) {
153 $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
156 $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
157 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
159 if ( $this->hasNamespace
) {
160 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
162 $limit = $params['limit'];
163 $this->addOption( 'LIMIT', $limit +
1 );
165 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
167 $orderBy[] = $pfx . $fieldTitle . $sort;
168 if ( !$params['unique'] ) {
169 $orderBy[] = $pfx . 'from' . $sort;
171 $this->addOption( 'ORDER BY', $orderBy );
173 $res = $this->select( __METHOD__
);
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
);
185 $this->setContinueEnumParameter( 'continue', $row->pl_title
. '|' . $row->pl_from
);
190 if ( is_null( $resultPageSet ) ) {
193 $vals['fromid'] = intval( $row->pl_from
);
196 $title = Title
::makeTitle( $namespace, $row->pl_title
);
197 ApiQueryBase
::addTitleInfo( $vals, $title );
199 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
201 if ( $params['unique'] ) {
202 $this->setContinueEnumParameter( 'continue', $row->pl_title
);
204 $this->setContinueEnumParameter( 'continue', $row->pl_title
. '|' . $row->pl_from
);
208 } elseif ( $params['unique'] ) {
209 $titles[] = Title
::makeTitle( $namespace, $row->pl_title
);
211 $pageids[] = $row->pl_from
;
215 if ( is_null( $resultPageSet ) ) {
216 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag
);
217 } elseif ( $params['unique'] ) {
218 $resultPageSet->populateFromTitles( $titles );
220 $resultPageSet->populateFromPageIDs( $pageids );
224 public function getAllowedParams() {
225 $allowedParams = array(
232 ApiBase
::PARAM_ISMULTI
=> true,
233 ApiBase
::PARAM_DFLT
=> 'title',
234 ApiBase
::PARAM_TYPE
=> array(
239 'namespace' => array(
240 ApiBase
::PARAM_DFLT
=> $this->dfltNamespace
,
241 ApiBase
::PARAM_TYPE
=> 'namespace'
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
251 ApiBase
::PARAM_DFLT
=> 'ascending',
252 ApiBase
::PARAM_TYPE
=> array(
258 if ( !$this->hasNamespace
) {
259 unset( $allowedParams['namespace'] );
261 return $allowedParams;
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",
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.',
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",
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',
287 if ( !$this->hasNamespace
) {
288 unset( $paramDescription['namespace'] );
290 return $paramDescription;
293 public function getResultProperties() {
296 'fromid' => 'integer'
305 public function getDescription() {
306 return $this->description
;
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" ),
317 public function getExamples() {
318 $p = $this->getModulePrefix();
319 $name = $this->getModuleName();
320 $what = $this->descriptionWhat
;
321 $targets = $this->descriptionTargets
;
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",
334 public function getHelpUrls() {
335 $name = ucfirst( $this->getModuleName() );
336 return "https://www.mediawiki.org/wiki/API:{$name}";