3 * API for MediaWiki 1.8+
5 * Created on May 12, 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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
33 * A query module to list all wiki links on a given set of pages.
37 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
39 const LINKS
= 'links';
40 const TEMPLATES
= 'templates';
42 private $table, $prefix, $description;
44 public function __construct( $query, $moduleName ) {
45 switch ( $moduleName ) {
47 $this->table
= 'pagelinks';
49 $this->description
= 'link';
50 $this->titlesParam
= 'titles';
53 $this->table
= 'templatelinks';
55 $this->description
= 'template';
56 $this->titlesParam
= 'templates';
59 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
62 parent
::__construct( $query, $moduleName, $this->prefix
);
65 public function execute() {
69 public function getCacheMode( $params ) {
73 public function executeGenerator( $resultPageSet ) {
74 $this->run( $resultPageSet );
77 private function run( $resultPageSet = null ) {
78 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
79 return; // nothing to do
82 $params = $this->extractRequestParams();
84 $this->addFields( array(
85 $this->prefix
. '_from AS pl_from',
86 $this->prefix
. '_namespace AS pl_namespace',
87 $this->prefix
. '_title AS pl_title'
90 $this->addTables( $this->table
);
91 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
92 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
94 if ( !is_null( $params[$this->titlesParam
] ) ) {
96 foreach ( $params[$this->titlesParam
] as $t ) {
97 $title = Title
::newFromText( $t );
99 $this->setWarning( "``$t'' is not a valid title" );
101 $lb->addObj( $title );
104 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
106 $this->addWhere( $cond );
110 if ( !is_null( $params['continue'] ) ) {
111 $cont = explode( '|', $params['continue'] );
112 if ( count( $cont ) != 3 ) {
113 $this->dieUsage( 'Invalid continue param. You should pass the ' .
114 'original value returned by the previous query', '_badcontinue' );
116 $plfrom = intval( $cont[0] );
117 $plns = intval( $cont[1] );
118 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
120 "{$this->prefix}_from > $plfrom OR " .
121 "({$this->prefix}_from = $plfrom AND " .
122 "({$this->prefix}_namespace > $plns OR " .
123 "({$this->prefix}_namespace = $plns AND " .
124 "{$this->prefix}_title >= '$pltitle')))"
128 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
129 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
130 // but instead goes and filesorts, because the index for foo was used
131 // already. To work around this, we drop constant fields in the WHERE
132 // clause from the ORDER BY clause
134 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
135 $order[] = "{$this->prefix}_from";
137 if ( count( $params['namespace'] ) != 1 ) {
138 $order[] = "{$this->prefix}_namespace";
141 $order[] = "{$this->prefix}_title";
142 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
143 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
144 $this->addOption( 'LIMIT', $params['limit'] +
1 );
146 $res = $this->select( __METHOD__
);
148 if ( is_null( $resultPageSet ) ) {
150 foreach ( $res as $row ) {
151 if ( ++
$count > $params['limit'] ) {
152 // We've reached the one extra which shows that
153 // there are additional pages to be had. Stop here...
154 $this->setContinueEnumParameter( 'continue',
155 "{$row->pl_from}|{$row->pl_namespace}|" .
156 $this->keyToTitle( $row->pl_title
) );
160 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
161 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
163 $this->setContinueEnumParameter( 'continue',
164 "{$row->pl_from}|{$row->pl_namespace}|" .
165 $this->keyToTitle( $row->pl_title
) );
172 foreach ( $res as $row ) {
173 if ( ++
$count > $params['limit'] ) {
174 // We've reached the one extra which shows that
175 // there are additional pages to be had. Stop here...
176 $this->setContinueEnumParameter( 'continue',
177 "{$row->pl_from}|{$row->pl_namespace}|" .
178 $this->keyToTitle( $row->pl_title
) );
181 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
183 $resultPageSet->populateFromTitles( $titles );
187 public function getAllowedParams() {
189 'namespace' => array(
190 ApiBase
::PARAM_TYPE
=> 'namespace',
191 ApiBase
::PARAM_ISMULTI
=> true
194 ApiBase
::PARAM_DFLT
=> 10,
195 ApiBase
::PARAM_TYPE
=> 'limit',
196 ApiBase
::PARAM_MIN
=> 1,
197 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
198 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
201 $this->titlesParam
=> array(
202 ApiBase
::PARAM_ISMULTI
=> true,
207 public function getParamDescription() {
208 $desc = $this->description
;
210 'namespace' => "Show {$desc}s in this namespace(s) only",
211 'limit' => "How many {$desc}s to return",
212 'continue' => 'When more results are available, use this to continue',
214 if ( $this->getModuleName() == self
::LINKS
) {
215 $arr[$this->titlesParam
] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
216 } else if ( $this->getModuleName() == self
::TEMPLATES
) {
217 $arr[$this->titlesParam
] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
222 public function getDescription() {
223 return "Returns all {$this->description}s from the given page(s)";
226 protected function getExamples() {
228 "Get {$this->description}s from the [[Main Page]]:",
229 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
230 "Get information about the {$this->description} pages in the [[Main Page]]:",
231 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
232 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
233 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
237 public function getVersion() {
238 return __CLASS__
. ': $Id$';