Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryIWLinks.php
blob3697b4a0a4e147b779d42d4befd1a5f2eb7e64f5
1 <?php
2 /**
3 * API for MediaWiki 1.17+
5 * Copyright © 2010 Sam Reed
6 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
26 namespace MediaWiki\Api;
28 use MediaWiki\Title\Title;
29 use MediaWiki\Utils\UrlUtils;
30 use Wikimedia\ParamValidator\ParamValidator;
31 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
33 /**
34 * A query module to list all interwiki links on a page
36 * @ingroup API
38 class ApiQueryIWLinks extends ApiQueryBase {
40 private UrlUtils $urlUtils;
42 public function __construct( ApiQuery $query, string $moduleName, UrlUtils $urlUtils ) {
43 parent::__construct( $query, $moduleName, 'iw' );
45 $this->urlUtils = $urlUtils;
48 public function execute() {
49 $pages = $this->getPageSet()->getGoodPages();
50 if ( $pages === [] ) {
51 return;
54 $params = $this->extractRequestParams();
55 $prop = array_fill_keys( (array)$params['prop'], true );
57 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
58 $this->dieWithError(
60 'apierror-invalidparammix-mustusewith',
61 $this->encodeParamName( 'title' ),
62 $this->encodeParamName( 'prefix' ),
64 'invalidparammix'
68 // Handle deprecated param
69 $this->requireMaxOneParameter( $params, 'url', 'prop' );
70 if ( $params['url'] ) {
71 $prop = [ 'url' => 1 ];
74 $this->addFields( [
75 'iwl_from',
76 'iwl_prefix',
77 'iwl_title'
78 ] );
80 $this->addTables( 'iwlinks' );
81 $this->addWhereFld( 'iwl_from', array_keys( $pages ) );
83 if ( $params['continue'] !== null ) {
84 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'string' ] );
85 $op = $params['dir'] == 'descending' ? '<=' : '>=';
86 $db = $this->getDB();
87 $this->addWhere( $db->buildComparison( $op, [
88 'iwl_from' => $cont[0],
89 'iwl_prefix' => $cont[1],
90 'iwl_title' => $cont[2],
91 ] ) );
94 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
97 if ( isset( $params['title'] ) ) {
98 $this->addWhereFld( 'iwl_title', $params['title'] );
99 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
100 } else {
101 $this->addOption( 'ORDER BY', [
102 'iwl_from' . $sort,
103 'iwl_title' . $sort
104 ] );
106 } else {
107 // Don't order by iwl_from if it's constant in the WHERE clause
108 if ( count( $pages ) === 1 ) {
109 $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
110 } else {
111 $this->addOption( 'ORDER BY', [
112 'iwl_from' . $sort,
113 'iwl_prefix' . $sort,
114 'iwl_title' . $sort
115 ] );
119 $this->addOption( 'LIMIT', $params['limit'] + 1 );
120 $res = $this->select( __METHOD__ );
122 $count = 0;
123 foreach ( $res as $row ) {
124 if ( ++$count > $params['limit'] ) {
125 // We've reached the one extra which shows that
126 // there are additional pages to be had. Stop here...
127 $this->setContinueEnumParameter(
128 'continue',
129 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
131 break;
133 $entry = [ 'prefix' => $row->iwl_prefix ];
135 if ( isset( $prop['url'] ) ) {
136 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
137 if ( $title ) {
138 $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
142 ApiResult::setContentValue( $entry, 'title', $row->iwl_title );
143 $fit = $this->addPageSubItem( $row->iwl_from, $entry );
144 if ( !$fit ) {
145 $this->setContinueEnumParameter(
146 'continue',
147 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
149 break;
154 public function getCacheMode( $params ) {
155 return 'public';
158 public function getAllowedParams() {
159 return [
160 'prop' => [
161 ParamValidator::PARAM_ISMULTI => true,
162 ParamValidator::PARAM_TYPE => [
163 'url',
165 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
167 'prefix' => null,
168 'title' => null,
169 'dir' => [
170 ParamValidator::PARAM_DEFAULT => 'ascending',
171 ParamValidator::PARAM_TYPE => [
172 'ascending',
173 'descending'
176 'limit' => [
177 ParamValidator::PARAM_DEFAULT => 10,
178 ParamValidator::PARAM_TYPE => 'limit',
179 IntegerDef::PARAM_MIN => 1,
180 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
181 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
183 'continue' => [
184 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
186 'url' => [
187 ParamValidator::PARAM_DEFAULT => false,
188 ParamValidator::PARAM_DEPRECATED => true,
193 protected function getExamplesMessages() {
194 $title = Title::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
197 return [
198 "action=query&prop=iwlinks&titles={$mp}"
199 => 'apihelp-query+iwlinks-example-simple',
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks';
208 /** @deprecated class alias since 1.43 */
209 class_alias( ApiQueryIWLinks::class, 'ApiQueryIWLinks' );