Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryLinks.php
blobe25b791ce320ffcca16f7d22d5ef6c7dd275551e
1 <?php
2 /**
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
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Cache\LinkBatchFactory;
26 use MediaWiki\Linker\LinksMigration;
27 use MediaWiki\ParamValidator\TypeDef\NamespaceDef;
28 use MediaWiki\Title\Title;
29 use Wikimedia\ParamValidator\ParamValidator;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
32 /**
33 * A query module to list all wiki links on a given set of pages.
35 * @ingroup API
37 class ApiQueryLinks extends ApiQueryGeneratorBase {
39 private const LINKS = 'links';
40 private const TEMPLATES = 'templates';
42 private string $table;
43 private string $prefix;
44 private string $titlesParam;
45 private string $helpUrl;
47 private LinkBatchFactory $linkBatchFactory;
48 private LinksMigration $linksMigration;
50 public function __construct(
51 ApiQuery $query,
52 string $moduleName,
53 LinkBatchFactory $linkBatchFactory,
54 LinksMigration $linksMigration
55 ) {
56 switch ( $moduleName ) {
57 case self::LINKS:
58 $this->table = 'pagelinks';
59 $this->prefix = 'pl';
60 $this->titlesParam = 'titles';
61 $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
62 break;
63 case self::TEMPLATES:
64 $this->table = 'templatelinks';
65 $this->prefix = 'tl';
66 $this->titlesParam = 'templates';
67 $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
68 break;
69 default:
70 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
73 parent::__construct( $query, $moduleName, $this->prefix );
74 $this->linkBatchFactory = $linkBatchFactory;
75 $this->linksMigration = $linksMigration;
78 public function execute() {
79 $this->run();
82 public function getCacheMode( $params ) {
83 return 'public';
86 public function executeGenerator( $resultPageSet ) {
87 $this->run( $resultPageSet );
90 /**
91 * @param ApiPageSet|null $resultPageSet
93 private function run( $resultPageSet = null ) {
94 $pages = $this->getPageSet()->getGoodPages();
95 if ( $pages === [] ) {
96 return; // nothing to do
99 $params = $this->extractRequestParams();
101 if ( isset( $this->linksMigration::$mapping[$this->table] ) ) {
102 [ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table );
103 $queryInfo = $this->linksMigration->getQueryInfo( $this->table );
104 $this->addTables( $queryInfo['tables'] );
105 $this->addJoinConds( $queryInfo['joins'] );
106 } else {
107 $this->addTables( $this->table );
108 $nsField = $this->prefix . '_namespace';
109 $titleField = $this->prefix . '_title';
112 $this->addFields( [
113 'pl_from' => $this->prefix . '_from',
114 'pl_namespace' => $nsField,
115 'pl_title' => $titleField,
116 ] );
118 $this->addWhereFld( $this->prefix . '_from', array_keys( $pages ) );
120 $multiNS = true;
121 $multiTitle = true;
122 if ( $params[$this->titlesParam] ) {
123 // Filter the titles in PHP so our ORDER BY bug avoidance below works right.
124 $filterNS = $params['namespace'] ? array_fill_keys( $params['namespace'], true ) : false;
126 $lb = $this->linkBatchFactory->newLinkBatch();
127 foreach ( $params[$this->titlesParam] as $t ) {
128 $title = Title::newFromText( $t );
129 if ( !$title || $title->isExternal() ) {
130 $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
131 } elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) {
132 $lb->addObj( $title );
135 if ( $lb->isEmpty() ) {
136 // No titles, no results!
137 return;
139 $cond = $lb->constructSet( $this->prefix, $this->getDB() );
140 $this->addWhere( $cond );
141 $multiNS = count( $lb->data ) !== 1;
142 $multiTitle = count( array_merge( ...$lb->data ) ) !== 1;
143 } elseif ( $params['namespace'] ) {
144 $this->addWhereFld( $nsField, $params['namespace'] );
145 $multiNS = $params['namespace'] === null || count( $params['namespace'] ) !== 1;
148 if ( $params['continue'] !== null ) {
149 $db = $this->getDB();
150 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int', 'string' ] );
151 $op = $params['dir'] == 'descending' ? '<=' : '>=';
152 $this->addWhere( $db->buildComparison( $op, [
153 "{$this->prefix}_from" => $cont[0],
154 $nsField => $cont[1],
155 $titleField => $cont[2],
156 ] ) );
159 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
160 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
161 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
162 // but instead goes and filesorts, because the index for foo was used
163 // already. To work around this, we drop constant fields in the WHERE
164 // clause from the ORDER BY clause
165 $order = [];
166 if ( count( $pages ) !== 1 ) {
167 $order[] = $this->prefix . '_from' . $sort;
169 if ( $multiNS ) {
170 $order[] = $nsField . $sort;
172 if ( $multiTitle ) {
173 $order[] = $titleField . $sort;
175 if ( $order ) {
176 $this->addOption( 'ORDER BY', $order );
178 $this->addOption( 'LIMIT', $params['limit'] + 1 );
180 $res = $this->select( __METHOD__ );
182 if ( $resultPageSet === null ) {
183 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pl' );
185 $count = 0;
186 foreach ( $res as $row ) {
187 if ( ++$count > $params['limit'] ) {
188 // We've reached the one extra which shows that
189 // there are additional pages to be had. Stop here...
190 $this->setContinueEnumParameter( 'continue',
191 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
192 break;
194 $vals = [];
195 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
196 $fit = $this->addPageSubItem( $row->pl_from, $vals );
197 if ( !$fit ) {
198 $this->setContinueEnumParameter( 'continue',
199 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
200 break;
203 } else {
204 $titles = [];
205 $count = 0;
206 foreach ( $res as $row ) {
207 if ( ++$count > $params['limit'] ) {
208 // We've reached the one extra which shows that
209 // there are additional pages to be had. Stop here...
210 $this->setContinueEnumParameter( 'continue',
211 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
212 break;
214 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
216 $resultPageSet->populateFromTitles( $titles );
220 public function getAllowedParams() {
221 return [
222 'namespace' => [
223 ParamValidator::PARAM_TYPE => 'namespace',
224 ParamValidator::PARAM_ISMULTI => true,
225 NamespaceDef::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
227 'limit' => [
228 ParamValidator::PARAM_DEFAULT => 10,
229 ParamValidator::PARAM_TYPE => 'limit',
230 IntegerDef::PARAM_MIN => 1,
231 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
232 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
234 'continue' => [
235 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
237 $this->titlesParam => [
238 ParamValidator::PARAM_ISMULTI => true,
240 'dir' => [
241 ParamValidator::PARAM_DEFAULT => 'ascending',
242 ParamValidator::PARAM_TYPE => [
243 'ascending',
244 'descending'
250 protected function getExamplesMessages() {
251 $name = $this->getModuleName();
252 $path = $this->getModulePath();
253 $title = Title::newMainPage()->getPrefixedText();
254 $mp = rawurlencode( $title );
256 return [
257 "action=query&prop={$name}&titles={$mp}"
258 => "apihelp-{$path}-example-simple",
259 "action=query&generator={$name}&titles={$mp}&prop=info"
260 => "apihelp-{$path}-example-generator",
261 "action=query&prop={$name}&titles={$mp}&{$this->prefix}namespace=2|10"
262 => "apihelp-{$path}-example-namespaces",
266 public function getHelpUrls() {
267 return $this->helpUrl;
271 /** @deprecated class alias since 1.43 */
272 class_alias( ApiQueryLinks::class, 'ApiQueryLinks' );