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 );
78 * @param $resultPageSet ApiPageSet
81 private function run( $resultPageSet = null ) {
82 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
83 return; // nothing to do
86 $params = $this->extractRequestParams();
88 $this->addFields( array(
89 $this->prefix
. '_from AS pl_from',
90 $this->prefix
. '_namespace AS pl_namespace',
91 $this->prefix
. '_title AS pl_title'
94 $this->addTables( $this->table
);
95 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
96 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
98 if ( !is_null( $params[$this->titlesParam
] ) ) {
100 foreach ( $params[$this->titlesParam
] as $t ) {
101 $title = Title
::newFromText( $t );
103 $this->setWarning( "``$t'' is not a valid title" );
105 $lb->addObj( $title );
108 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
110 $this->addWhere( $cond );
114 if ( !is_null( $params['continue'] ) ) {
115 $cont = explode( '|', $params['continue'] );
116 if ( count( $cont ) != 3 ) {
117 $this->dieUsage( 'Invalid continue param. You should pass the ' .
118 'original value returned by the previous query', '_badcontinue' );
120 $plfrom = intval( $cont[0] );
121 $plns = intval( $cont[1] );
122 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
124 "{$this->prefix}_from > $plfrom OR " .
125 "({$this->prefix}_from = $plfrom AND " .
126 "({$this->prefix}_namespace > $plns OR " .
127 "({$this->prefix}_namespace = $plns AND " .
128 "{$this->prefix}_title >= '$pltitle')))"
132 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
133 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
134 // but instead goes and filesorts, because the index for foo was used
135 // already. To work around this, we drop constant fields in the WHERE
136 // clause from the ORDER BY clause
138 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
139 $order[] = "{$this->prefix}_from";
141 if ( count( $params['namespace'] ) != 1 ) {
142 $order[] = "{$this->prefix}_namespace";
145 $order[] = "{$this->prefix}_title";
146 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
147 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
148 $this->addOption( 'LIMIT', $params['limit'] +
1 );
150 $res = $this->select( __METHOD__
);
152 if ( is_null( $resultPageSet ) ) {
154 foreach ( $res as $row ) {
155 if ( ++
$count > $params['limit'] ) {
156 // We've reached the one extra which shows that
157 // there are additional pages to be had. Stop here...
158 $this->setContinueEnumParameter( 'continue',
159 "{$row->pl_from}|{$row->pl_namespace}|" .
160 $this->keyToTitle( $row->pl_title
) );
164 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
165 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
167 $this->setContinueEnumParameter( 'continue',
168 "{$row->pl_from}|{$row->pl_namespace}|" .
169 $this->keyToTitle( $row->pl_title
) );
176 foreach ( $res as $row ) {
177 if ( ++
$count > $params['limit'] ) {
178 // We've reached the one extra which shows that
179 // there are additional pages to be had. Stop here...
180 $this->setContinueEnumParameter( 'continue',
181 "{$row->pl_from}|{$row->pl_namespace}|" .
182 $this->keyToTitle( $row->pl_title
) );
185 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
187 $resultPageSet->populateFromTitles( $titles );
191 public function getAllowedParams() {
193 'namespace' => array(
194 ApiBase
::PARAM_TYPE
=> 'namespace',
195 ApiBase
::PARAM_ISMULTI
=> true
198 ApiBase
::PARAM_DFLT
=> 10,
199 ApiBase
::PARAM_TYPE
=> 'limit',
200 ApiBase
::PARAM_MIN
=> 1,
201 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
202 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
205 $this->titlesParam
=> array(
206 ApiBase
::PARAM_ISMULTI
=> true,
211 public function getParamDescription() {
212 $desc = $this->description
;
214 'namespace' => "Show {$desc}s in this namespace(s) only",
215 'limit' => "How many {$desc}s to return",
216 'continue' => 'When more results are available, use this to continue',
218 if ( $this->getModuleName() == self
::LINKS
) {
219 $arr[$this->titlesParam
] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
220 } else if ( $this->getModuleName() == self
::TEMPLATES
) {
221 $arr[$this->titlesParam
] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
226 public function getDescription() {
227 return "Returns all {$this->description}s from the given page(s)";
230 protected function getExamples() {
232 "Get {$this->description}s from the [[Main Page]]:",
233 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
234 "Get information about the {$this->description} pages in the [[Main Page]]:",
235 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
236 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
237 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
241 public function getVersion() {
242 return __CLASS__
. ': $Id$';