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
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
;
34 * A query module to list all interwiki links on a page
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 === [] ) {
54 $params = $this->extractRequestParams();
55 $prop = array_fill_keys( (array)$params['prop'], true );
57 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
60 'apierror-invalidparammix-mustusewith',
61 $this->encodeParamName( 'title' ),
62 $this->encodeParamName( 'prefix' ),
68 // Handle deprecated param
69 $this->requireMaxOneParameter( $params, 'url', 'prop' );
70 if ( $params['url'] ) {
71 $prop = [ 'url' => 1 ];
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' ?
'<=' : '>=';
87 $this->addWhere( $db->buildComparison( $op, [
88 'iwl_from' => $cont[0],
89 'iwl_prefix' => $cont[1],
90 'iwl_title' => $cont[2],
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 );
101 $this->addOption( 'ORDER BY', [
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 );
111 $this->addOption( 'ORDER BY', [
113 'iwl_prefix' . $sort,
119 $this->addOption( 'LIMIT', $params['limit'] +
1 );
120 $res = $this->select( __METHOD__
);
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(
129 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
133 $entry = [ 'prefix' => $row->iwl_prefix
];
135 if ( isset( $prop['url'] ) ) {
136 $title = Title
::newFromText( "{$row->iwl_prefix}:{$row->iwl_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 );
145 $this->setContinueEnumParameter(
147 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
154 public function getCacheMode( $params ) {
158 public function getAllowedParams() {
161 ParamValidator
::PARAM_ISMULTI
=> true,
162 ParamValidator
::PARAM_TYPE
=> [
165 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
170 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
171 ParamValidator
::PARAM_TYPE
=> [
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
184 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
187 ParamValidator
::PARAM_DEFAULT
=> false,
188 ParamValidator
::PARAM_DEPRECATED
=> true,
193 protected function getExamplesMessages() {
194 $title = Title
::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
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' );