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';
58 'List all transclusions (pages embedded using {{x}}), including non-existing';
59 $this->descriptionWhat
= 'transclusion';
60 $this->descriptionTargets
= 'transcluded titles';
61 $this->descriptionLinking
= 'transcluding';
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';
77 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
80 parent
::__construct( $query, $moduleName, $prefix );
83 public function execute() {
87 public function getCacheMode( $params ) {
91 public function executeGenerator( $resultPageSet ) {
92 $this->run( $resultPageSet );
96 * @param $resultPageSet ApiPageSet
99 private function run( $resultPageSet = null ) {
100 $db = $this->getDB();
101 $params = $this->extractRequestParams();
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'];
111 $namespace = $this->dfltNamespace
;
114 if ( $params['unique'] ) {
117 "{$this->getModuleName()} cannot return corresponding page " .
118 "ids in unique {$this->descriptionWhat}s mode",
122 $this->addOption( 'DISTINCT' );
125 $this->addTables( $this->table
);
126 if ( $this->hasNamespace
) {
127 $this->addWhereFld( $pfx . 'namespace', $namespace );
130 $continue = !is_null( $params['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" );
139 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
140 $continueTitle = $db->addQuotes( $continueArr[0] );
141 $continueFrom = intval( $continueArr[1] );
143 "{$pfx}{$fieldTitle} $op $continueTitle OR " .
144 "({$pfx}{$fieldTitle} = $continueTitle AND " .
145 "{$pfx}from $op= $continueFrom)"
150 // 'continue' always overrides 'from'
151 $from = $continue ||
is_null( $params['from'] )
153 : $this->titlePartToKey( $params['from'] );
154 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
155 $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
157 if ( isset( $params['prefix'] ) ) {
158 $this->addWhere( $pfx . $fieldTitle .
159 $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
162 $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
163 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
165 if ( $this->hasNamespace
) {
166 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
168 $limit = $params['limit'];
169 $this->addOption( 'LIMIT', $limit +
1 );
171 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
173 $orderBy[] = $pfx . $fieldTitle . $sort;
174 if ( !$params['unique'] ) {
175 $orderBy[] = $pfx . 'from' . $sort;
177 $this->addOption( 'ORDER BY', $orderBy );
179 $res = $this->select( __METHOD__
);
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
);
192 $this->setContinueEnumParameter( 'continue', $row->pl_title
. '|' . $row->pl_from
);
197 if ( is_null( $resultPageSet ) ) {
200 $vals['fromid'] = intval( $row->pl_from
);
203 $title = Title
::makeTitle( $namespace, $row->pl_title
);
204 ApiQueryBase
::addTitleInfo( $vals, $title );
206 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
208 if ( $params['unique'] ) {
209 $this->setContinueEnumParameter( 'continue', $row->pl_title
);
211 $this->setContinueEnumParameter( 'continue', $row->pl_title
. '|' . $row->pl_from
);
215 } elseif ( $params['unique'] ) {
216 $titles[] = Title
::makeTitle( $namespace, $row->pl_title
);
218 $pageids[] = $row->pl_from
;
222 if ( is_null( $resultPageSet ) ) {
223 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag
);
224 } elseif ( $params['unique'] ) {
225 $resultPageSet->populateFromTitles( $titles );
227 $resultPageSet->populateFromPageIDs( $pageids );
231 public function getAllowedParams() {
232 $allowedParams = array(
239 ApiBase
::PARAM_ISMULTI
=> true,
240 ApiBase
::PARAM_DFLT
=> 'title',
241 ApiBase
::PARAM_TYPE
=> array(
246 'namespace' => array(
247 ApiBase
::PARAM_DFLT
=> $this->dfltNamespace
,
248 ApiBase
::PARAM_TYPE
=> 'namespace'
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
258 ApiBase
::PARAM_DFLT
=> 'ascending',
259 ApiBase
::PARAM_TYPE
=> array(
265 if ( !$this->hasNamespace
) {
266 unset( $allowedParams['namespace'] );
269 return $allowedParams;
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",
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.',
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",
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',
295 if ( !$this->hasNamespace
) {
296 unset( $paramDescription['namespace'] );
299 return $paramDescription;
302 public function getResultProperties() {
305 'fromid' => 'integer'
314 public function getDescription() {
315 return $this->description
;
318 public function getPossibleErrors() {
319 $m = $this->getModuleName();
320 $what = $this->descriptionWhat
;
322 return array_merge( parent
::getPossibleErrors(), array(
325 'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode"
330 public function getExamples() {
331 $p = $this->getModulePrefix();
332 $name = $this->getModuleName();
333 $what = $this->descriptionWhat
;
334 $targets = $this->descriptionTargets
;
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",
348 public function getHelpUrls() {
349 $name = ucfirst( $this->getModuleName() );
351 return "https://www.mediawiki.org/wiki/API:{$name}";