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
28 * A query module to list all wiki links on a given set of pages.
32 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
34 const LINKS
= 'links';
35 const TEMPLATES
= 'templates';
37 private $table, $prefix, $description, $helpUrl;
39 public function __construct( $query, $moduleName ) {
40 switch ( $moduleName ) {
42 $this->table
= 'pagelinks';
44 $this->description
= 'link';
45 $this->titlesParam
= 'titles';
46 $this->titlesParamDescription
= 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
47 $this->helpUrl
= 'https://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
50 $this->table
= 'templatelinks';
52 $this->description
= 'template';
53 $this->titlesParam
= 'templates';
54 $this->titlesParamDescription
= 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
55 $this->helpUrl
= 'https://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
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 );
77 * @param $resultPageSet ApiPageSet
80 private function run( $resultPageSet = null ) {
81 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
82 return; // nothing to do
85 $params = $this->extractRequestParams();
87 $this->addFields( array(
88 'pl_from' => $this->prefix
. '_from',
89 'pl_namespace' => $this->prefix
. '_namespace',
90 'pl_title' => $this->prefix
. '_title'
93 $this->addTables( $this->table
);
94 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
95 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
97 if ( !is_null( $params[$this->titlesParam
] ) ) {
99 foreach ( $params[$this->titlesParam
] as $t ) {
100 $title = Title
::newFromText( $t );
102 $this->setWarning( "\"$t\" is not a valid title" );
104 $lb->addObj( $title );
107 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
109 $this->addWhere( $cond );
113 if ( !is_null( $params['continue'] ) ) {
114 $cont = explode( '|', $params['continue'] );
115 $this->dieContinueUsageIf( count( $cont ) != 3 );
116 $op = $params['dir'] == 'descending' ?
'<' : '>';
117 $plfrom = intval( $cont[0] );
118 $plns = intval( $cont[1] );
119 $pltitle = $this->getDB()->addQuotes( $cont[2] );
121 "{$this->prefix}_from $op $plfrom OR " .
122 "({$this->prefix}_from = $plfrom AND " .
123 "({$this->prefix}_namespace $op $plns OR " .
124 "({$this->prefix}_namespace = $plns AND " .
125 "{$this->prefix}_title $op= $pltitle)))"
129 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
130 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
131 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
132 // but instead goes and filesorts, because the index for foo was used
133 // already. To work around this, we drop constant fields in the WHERE
134 // clause from the ORDER BY clause
136 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
137 $order[] = $this->prefix
. '_from' . $sort;
139 if ( count( $params['namespace'] ) != 1 ) {
140 $order[] = $this->prefix
. '_namespace' . $sort;
143 $order[] = $this->prefix
. '_title' . $sort;
144 $this->addOption( 'ORDER BY', $order );
145 $this->addOption( 'USE INDEX', $this->prefix
. '_from' );
146 $this->addOption( 'LIMIT', $params['limit'] +
1 );
148 $res = $this->select( __METHOD__
);
150 if ( is_null( $resultPageSet ) ) {
152 foreach ( $res as $row ) {
153 if ( ++
$count > $params['limit'] ) {
154 // We've reached the one extra which shows that
155 // there are additional pages to be had. Stop here...
156 $this->setContinueEnumParameter( 'continue',
157 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
161 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
162 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
164 $this->setContinueEnumParameter( 'continue',
165 "{$row->pl_from}|{$row->pl_namespace}|{$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}|{$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,
204 ApiBase
::PARAM_DFLT
=> 'ascending',
205 ApiBase
::PARAM_TYPE
=> array(
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',
219 $this->titlesParam
=> $this->titlesParamDescription
,
220 'dir' => 'The direction in which to list',
224 public function getResultProperties() {
233 public function getDescription() {
234 return "Returns all {$this->description}s from the given page(s)";
237 public function getExamples() {
238 $desc = $this->description
;
239 $name = $this->getModuleName();
241 "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]",
242 "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info" => "Get information about the {$desc} pages in the [[Main Page]]",
243 "api.php?action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10" => "Get {$desc}s from the Main Page in the User and Template namespaces",
247 public function getHelpUrls() {
248 return $this->helpUrl
;