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, $titlesParam, $helpUrl;
39 public function __construct( ApiQuery
$query, $moduleName ) {
40 switch ( $moduleName ) {
42 $this->table
= 'pagelinks';
44 $this->titlesParam
= 'titles';
45 $this->helpUrl
= 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
48 $this->table
= 'templatelinks';
50 $this->titlesParam
= 'templates';
51 $this->helpUrl
= 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
54 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
57 parent
::__construct( $query, $moduleName, $this->prefix
);
60 public function execute() {
64 public function getCacheMode( $params ) {
68 public function executeGenerator( $resultPageSet ) {
69 $this->run( $resultPageSet );
73 * @param ApiPageSet $resultPageSet
75 private function run( $resultPageSet = null ) {
76 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
77 return; // nothing to do
80 $params = $this->extractRequestParams();
83 'pl_from' => $this->prefix
. '_from',
84 'pl_namespace' => $this->prefix
. '_namespace',
85 'pl_title' => $this->prefix
. '_title'
88 $this->addTables( $this->table
);
89 $this->addWhereFld( $this->prefix
. '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
93 if ( $params[$this->titlesParam
] ) {
94 // Filter the titles in PHP so our ORDER BY bug avoidance below works right.
95 $filterNS = $params['namespace'] ?
array_flip( $params['namespace'] ) : false;
98 foreach ( $params[$this->titlesParam
] as $t ) {
99 $title = Title
::newFromText( $t );
101 $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
102 } elseif ( !$filterNS ||
isset( $filterNS[$title->getNamespace()] ) ) {
103 $lb->addObj( $title );
106 $cond = $lb->constructSet( $this->prefix
, $this->getDB() );
108 $this->addWhere( $cond );
109 $multiNS = count( $lb->data
) !== 1;
110 $multiTitle = count( call_user_func_array( 'array_merge', $lb->data
) ) !== 1;
112 // No titles so no results
115 } elseif ( $params['namespace'] ) {
116 $this->addWhereFld( $this->prefix
. '_namespace', $params['namespace'] );
117 $multiNS = count( $params['namespace'] ) !== 1;
120 if ( !is_null( $params['continue'] ) ) {
121 $cont = explode( '|', $params['continue'] );
122 $this->dieContinueUsageIf( count( $cont ) != 3 );
123 $op = $params['dir'] == 'descending' ?
'<' : '>';
124 $plfrom = intval( $cont[0] );
125 $plns = intval( $cont[1] );
126 $pltitle = $this->getDB()->addQuotes( $cont[2] );
128 "{$this->prefix}_from $op $plfrom OR " .
129 "({$this->prefix}_from = $plfrom AND " .
130 "({$this->prefix}_namespace $op $plns OR " .
131 "({$this->prefix}_namespace = $plns AND " .
132 "{$this->prefix}_title $op= $pltitle)))"
136 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
137 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
138 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
139 // but instead goes and filesorts, because the index for foo was used
140 // already. To work around this, we drop constant fields in the WHERE
141 // clause from the ORDER BY clause
143 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
144 $order[] = $this->prefix
. '_from' . $sort;
147 $order[] = $this->prefix
. '_namespace' . $sort;
150 $order[] = $this->prefix
. '_title' . $sort;
153 $this->addOption( 'ORDER BY', $order );
155 $this->addOption( 'LIMIT', $params['limit'] +
1 );
157 $res = $this->select( __METHOD__
);
159 if ( is_null( $resultPageSet ) ) {
161 foreach ( $res as $row ) {
162 if ( ++
$count > $params['limit'] ) {
163 // We've reached the one extra which shows that
164 // there are additional pages to be had. Stop here...
165 $this->setContinueEnumParameter( 'continue',
166 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
170 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( $row->pl_namespace
, $row->pl_title
) );
171 $fit = $this->addPageSubItem( $row->pl_from
, $vals );
173 $this->setContinueEnumParameter( 'continue',
174 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
181 foreach ( $res as $row ) {
182 if ( ++
$count > $params['limit'] ) {
183 // We've reached the one extra which shows that
184 // there are additional pages to be had. Stop here...
185 $this->setContinueEnumParameter( 'continue',
186 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
189 $titles[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
191 $resultPageSet->populateFromTitles( $titles );
195 public function getAllowedParams() {
198 ApiBase
::PARAM_TYPE
=> 'namespace',
199 ApiBase
::PARAM_ISMULTI
=> true,
200 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
203 ApiBase
::PARAM_DFLT
=> 10,
204 ApiBase
::PARAM_TYPE
=> 'limit',
205 ApiBase
::PARAM_MIN
=> 1,
206 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
207 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
210 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
212 $this->titlesParam
=> [
213 ApiBase
::PARAM_ISMULTI
=> true,
216 ApiBase
::PARAM_DFLT
=> 'ascending',
217 ApiBase
::PARAM_TYPE
=> [
225 protected function getExamplesMessages() {
226 $name = $this->getModuleName();
227 $path = $this->getModulePath();
230 "action=query&prop={$name}&titles=Main%20Page"
231 => "apihelp-{$path}-example-simple",
232 "action=query&generator={$name}&titles=Main%20Page&prop=info"
233 => "apihelp-{$path}-example-generator",
234 "action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
235 => "apihelp-{$path}-example-namespaces",
239 public function getHelpUrls() {
240 return $this->helpUrl
;