8090dafa7483ebe832ca9432cf2e40f6e7443b93
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, $helpUrl;
44 public function __construct( $query, $moduleName ) {
45 switch ( $moduleName ) {
47 $this->table
= 'pagelinks';
49 $this->description
= 'link';
50 $this->titlesParam
= 'titles';
51 $this->helpUrl
= 'http://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
54 $this->table
= 'templatelinks';
56 $this->description
= 'template';
57 $this->titlesParam
= 'templates';
58 $this->helpUrl
= 'http://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
61 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
64 parent
::__construct( $query, $moduleName, $this->prefix
);
67 public function execute() {
71 public function getCacheMode( $params ) {
75 public function executeGenerator( $resultPageSet ) {
76 $this->run( $resultPageSet );
80 * @param $resultPageSet ApiPageSet
83 private function run( $resultPageSet = null ) {
84 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
85 return; // nothing to do
88 $params = $this->extractRequestParams();
90 $this->addFields( array(
91 $this->prefix
. '_from AS pl_from',
92 $this->prefix
. '_namespace AS pl_namespace',
93 $this->prefix
. '_title AS pl_title'
96 $this->addTables( $this->table
);
97 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
98 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
100 if ( !is_null( $params[$this->titlesParam
] ) ) {
102 foreach ( $params[$this->titlesParam
] as $t ) {
103 $title = Title
::newFromText( $t );
105 $this->setWarning( "``$t'' is not a valid title" );
107 $lb->addObj( $title );
110 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
112 $this->addWhere( $cond );
116 if ( !is_null( $params['continue'] ) ) {
117 $cont = explode( '|', $params['continue'] );
118 if ( count( $cont ) != 3 ) {
119 $this->dieUsage( 'Invalid continue param. You should pass the ' .
120 'original value returned by the previous query', '_badcontinue' );
122 $plfrom = intval( $cont[0] );
123 $plns = intval( $cont[1] );
124 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
126 "{$this->prefix}_from > $plfrom OR " .
127 "({$this->prefix}_from = $plfrom AND " .
128 "({$this->prefix}_namespace > $plns OR " .
129 "({$this->prefix}_namespace = $plns AND " .
130 "{$this->prefix}_title >= '$pltitle')))"
134 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
135 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
136 // but instead goes and filesorts, because the index for foo was used
137 // already. To work around this, we drop constant fields in the WHERE
138 // clause from the ORDER BY clause
140 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
141 $order[] = "{$this->prefix}_from";
143 if ( count( $params['namespace'] ) != 1 ) {
144 $order[] = "{$this->prefix}_namespace";
147 $order[] = "{$this->prefix}_title";
148 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
149 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
150 $this->addOption( 'LIMIT', $params['limit'] +
1 );
152 $res = $this->select( __METHOD__
);
154 if ( is_null( $resultPageSet ) ) {
156 foreach ( $res as $row ) {
157 if ( ++
$count > $params['limit'] ) {
158 // We've reached the one extra which shows that
159 // there are additional pages to be had. Stop here...
160 $this->setContinueEnumParameter( 'continue',
161 "{$row->pl_from}|{$row->pl_namespace}|" .
162 $this->keyToTitle( $row->pl_title
) );
166 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
167 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
169 $this->setContinueEnumParameter( 'continue',
170 "{$row->pl_from}|{$row->pl_namespace}|" .
171 $this->keyToTitle( $row->pl_title
) );
178 foreach ( $res as $row ) {
179 if ( ++
$count > $params['limit'] ) {
180 // We've reached the one extra which shows that
181 // there are additional pages to be had. Stop here...
182 $this->setContinueEnumParameter( 'continue',
183 "{$row->pl_from}|{$row->pl_namespace}|" .
184 $this->keyToTitle( $row->pl_title
) );
187 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
189 $resultPageSet->populateFromTitles( $titles );
193 public function getAllowedParams() {
195 'namespace' => array(
196 ApiBase
::PARAM_TYPE
=> 'namespace',
197 ApiBase
::PARAM_ISMULTI
=> true
200 ApiBase
::PARAM_DFLT
=> 10,
201 ApiBase
::PARAM_TYPE
=> 'limit',
202 ApiBase
::PARAM_MIN
=> 1,
203 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
204 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
207 $this->titlesParam
=> array(
208 ApiBase
::PARAM_ISMULTI
=> true,
213 public function getParamDescription() {
214 $desc = $this->description
;
216 'namespace' => "Show {$desc}s in this namespace(s) only",
217 'limit' => "How many {$desc}s to return",
218 'continue' => 'When more results are available, use this to continue',
220 if ( $this->getModuleName() == self
::LINKS
) {
221 $arr[$this->titlesParam
] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
222 } elseif ( $this->getModuleName() == self
::TEMPLATES
) {
223 $arr[$this->titlesParam
] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
228 public function getDescription() {
229 return "Returns all {$this->description}s from the given page(s)";
232 public function getExamples() {
234 "Get {$this->description}s from the [[Main Page]]:",
235 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
236 "Get information about the {$this->description} pages in the [[Main Page]]:",
237 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
238 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
239 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
243 public function getHelpUrls() {
244 return $this->helpUrl
;
247 public function getVersion() {
248 return __CLASS__
. ': $Id$';