3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * A query module to list all wiki links on a given set of pages.
28 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
30 const LINKS
= 'links';
31 const TEMPLATES
= 'templates';
33 private $table, $prefix, $titlesParam, $helpUrl;
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 switch ( $moduleName ) {
38 $this->table
= 'pagelinks';
40 $this->titlesParam
= 'titles';
41 $this->helpUrl
= 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
44 $this->table
= 'templatelinks';
46 $this->titlesParam
= 'templates';
47 $this->helpUrl
= 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
50 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
53 parent
::__construct( $query, $moduleName, $this->prefix
);
56 public function execute() {
60 public function getCacheMode( $params ) {
64 public function executeGenerator( $resultPageSet ) {
65 $this->run( $resultPageSet );
69 * @param ApiPageSet $resultPageSet
71 private function run( $resultPageSet = null ) {
72 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
73 return; // nothing to do
76 $params = $this->extractRequestParams();
79 'pl_from' => $this->prefix
. '_from',
80 'pl_namespace' => $this->prefix
. '_namespace',
81 'pl_title' => $this->prefix
. '_title'
84 $this->addTables( $this->table
);
85 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
89 if ( $params[$this->titlesParam
] ) {
90 // Filter the titles in PHP so our ORDER BY bug avoidance below works right.
91 $filterNS = $params['namespace'] ?
array_flip( $params['namespace'] ) : false;
94 foreach ( $params[$this->titlesParam
] as $t ) {
95 $title = Title
::newFromText( $t );
97 $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
98 } elseif ( !$filterNS ||
isset( $filterNS[$title->getNamespace()] ) ) {
99 $lb->addObj( $title );
102 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
104 $this->addWhere( $cond );
105 $multiNS = count( $lb->data
) !== 1;
106 $multiTitle = count( call_user_func_array( 'array_merge', $lb->data
) ) !== 1;
108 // No titles so no results
111 } elseif ( $params['namespace'] ) {
112 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
113 $multiNS = $params['namespace'] === null ||
count( $params['namespace'] ) !== 1;
116 if ( !is_null( $params['continue'] ) ) {
117 $cont = explode( '|', $params['continue'] );
118 $this->dieContinueUsageIf( count( $cont ) != 3 );
119 $op = $params['dir'] == 'descending' ?
'<' : '>';
120 $plfrom = intval( $cont[0] );
121 $plns = intval( $cont[1] );
122 $pltitle = $this->getDB()->addQuotes( $cont[2] );
124 "{$this->prefix}_from $op $plfrom OR " .
125 "({$this->prefix}_from = $plfrom AND " .
126 "({$this->prefix}_namespace $op $plns OR " .
127 "({$this->prefix}_namespace = $plns AND " .
128 "{$this->prefix}_title $op= $pltitle)))"
132 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
133 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
134 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
135 // but instead goes and filesorts, because the index for foo was used
136 // already. To work around this, we drop constant fields in the WHERE
137 // clause from the ORDER BY clause
139 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
140 $order[] = $this->prefix
. '_from' . $sort;
143 $order[] = $this->prefix
. '_namespace' . $sort;
146 $order[] = $this->prefix
. '_title' . $sort;
149 $this->addOption( 'ORDER BY', $order );
151 $this->addOption( 'LIMIT', $params['limit'] +
1 );
153 $res = $this->select( __METHOD__
);
155 if ( is_null( $resultPageSet ) ) {
157 foreach ( $res as $row ) {
158 if ( ++
$count > $params['limit'] ) {
159 // We've reached the one extra which shows that
160 // there are additional pages to be had. Stop here...
161 $this->setContinueEnumParameter( 'continue',
162 "{$row->pl_from}|{$row->pl_namespace}|{$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}|{$row->pl_title}" );
177 foreach ( $res as $row ) {
178 if ( ++
$count > $params['limit'] ) {
179 // We've reached the one extra which shows that
180 // there are additional pages to be had. Stop here...
181 $this->setContinueEnumParameter( 'continue',
182 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
185 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
187 $resultPageSet->populateFromTitles( $titles );
191 public function getAllowedParams() {
194 ApiBase
::PARAM_TYPE
=> 'namespace',
195 ApiBase
::PARAM_ISMULTI
=> true,
196 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
199 ApiBase
::PARAM_DFLT
=> 10,
200 ApiBase
::PARAM_TYPE
=> 'limit',
201 ApiBase
::PARAM_MIN
=> 1,
202 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
203 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
206 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
208 $this->titlesParam
=> [
209 ApiBase
::PARAM_ISMULTI
=> true,
212 ApiBase
::PARAM_DFLT
=> 'ascending',
213 ApiBase
::PARAM_TYPE
=> [
221 protected function getExamplesMessages() {
222 $name = $this->getModuleName();
223 $path = $this->getModulePath();
226 "action=query&prop={$name}&titles=Main%20Page"
227 => "apihelp-{$path}-example-simple",
228 "action=query&generator={$name}&titles=Main%20Page&prop=info"
229 => "apihelp-{$path}-example-generator",
230 "action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
231 => "apihelp-{$path}-example-namespaces",
235 public function getHelpUrls() {
236 return $this->helpUrl
;