4 * Created on May 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiQueryBase.php" );
32 * A query module to list all wiki links on a given set of pages.
36 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
38 const LINKS
= 'links';
39 const TEMPLATES
= 'templates';
41 private $table, $prefix, $description;
43 public function __construct( $query, $moduleName ) {
44 switch ( $moduleName ) {
46 $this->table
= 'pagelinks';
48 $this->description
= 'link';
49 $this->titlesParam
= 'titles';
52 $this->table
= 'templatelinks';
54 $this->description
= 'template';
55 $this->titlesParam
= 'templates';
58 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
61 parent
::__construct( $query, $moduleName, $this->prefix
);
64 public function execute() {
68 public function getCacheMode( $params ) {
72 public function executeGenerator( $resultPageSet ) {
73 $this->run( $resultPageSet );
76 private function run( $resultPageSet = null ) {
77 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
78 return; // nothing to do
81 $params = $this->extractRequestParams();
83 $this->addFields( array(
84 $this->prefix
. '_from AS pl_from',
85 $this->prefix
. '_namespace AS pl_namespace',
86 $this->prefix
. '_title AS pl_title'
89 $this->addTables( $this->table
);
90 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
91 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
93 if ( !is_null( $params[$this->titlesParam
] ) ) {
95 foreach ( $params[$this->titlesParam
] as $t ) {
96 $title = Title
::newFromText( $t );
98 $this->setWarning( "``$t'' is not a valid title" );
100 $lb->addObj( $title );
103 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
105 $this->addWhere( $cond );
109 if ( !is_null( $params['continue'] ) ) {
110 $cont = explode( '|', $params['continue'] );
111 if ( count( $cont ) != 3 ) {
112 $this->dieUsage( 'Invalid continue param. You should pass the ' .
113 'original value returned by the previous query', '_badcontinue' );
115 $plfrom = intval( $cont[0] );
116 $plns = intval( $cont[1] );
117 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
119 "{$this->prefix}_from > $plfrom OR " .
120 "({$this->prefix}_from = $plfrom AND " .
121 "({$this->prefix}_namespace > $plns OR " .
122 "({$this->prefix}_namespace = $plns AND " .
123 "{$this->prefix}_title >= '$pltitle')))"
127 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
128 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
129 // but instead goes and filesorts, because the index for foo was used
130 // already. To work around this, we drop constant fields in the WHERE
131 // clause from the ORDER BY clause
133 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
134 $order[] = "{$this->prefix}_from";
136 if ( count( $params['namespace'] ) != 1 ) {
137 $order[] = "{$this->prefix}_namespace";
140 $order[] = "{$this->prefix}_title";
141 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
142 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
143 $this->addOption( 'LIMIT', $params['limit'] +
1 );
145 $res = $this->select( __METHOD__
);
147 if ( is_null( $resultPageSet ) ) {
149 foreach ( $res as $row ) {
150 if ( ++
$count > $params['limit'] ) {
151 // We've reached the one extra which shows that
152 // there are additional pages to be had. Stop here...
153 $this->setContinueEnumParameter( 'continue',
154 "{$row->pl_from}|{$row->pl_namespace}|" .
155 $this->keyToTitle( $row->pl_title
) );
159 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
160 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
162 $this->setContinueEnumParameter( 'continue',
163 "{$row->pl_from}|{$row->pl_namespace}|" .
164 $this->keyToTitle( $row->pl_title
) );
171 foreach ( $res as $row ) {
172 if ( ++
$count > $params['limit'] ) {
173 // We've reached the one extra which shows that
174 // there are additional pages to be had. Stop here...
175 $this->setContinueEnumParameter( 'continue',
176 "{$row->pl_from}|{$row->pl_namespace}|" .
177 $this->keyToTitle( $row->pl_title
) );
180 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
182 $resultPageSet->populateFromTitles( $titles );
186 public function getAllowedParams() {
188 'namespace' => array(
189 ApiBase
::PARAM_TYPE
=> 'namespace',
190 ApiBase
::PARAM_ISMULTI
=> true
193 ApiBase
::PARAM_DFLT
=> 10,
194 ApiBase
::PARAM_TYPE
=> 'limit',
195 ApiBase
::PARAM_MIN
=> 1,
196 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
197 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
200 $this->titlesParam
=> array(
201 ApiBase
::PARAM_ISMULTI
=> true,
206 public function getParamDescription() {
207 $desc = $this->description
;
209 'namespace' => "Show {$desc}s in this namespace(s) only",
210 'limit' => "How many {$desc}s to return",
211 'continue' => 'When more results are available, use this to continue',
213 if ( $this->getModuleName() == self
::LINKS
) {
214 $arr[$this->titlesParam
] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
215 } else if ( $this->getModuleName() == self
::TEMPLATES
) {
216 $arr[$this->titlesParam
] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
221 public function getDescription() {
222 return "Returns all {$this->description}s from the given page(s)";
225 protected function getExamples() {
227 "Get {$this->description}s from the [[Main Page]]:",
228 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
229 "Get information about the {$this->description} pages in the [[Main Page]]:",
230 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
231 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
232 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
236 public function getVersion() {
237 return __CLASS__
. ': $Id$';