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 Wikimedia\ParamValidator\ParamValidator
;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
33 * This gives links pointing to the given interwiki
36 class ApiQueryIWBacklinks
extends ApiQueryGeneratorBase
{
38 public function __construct( ApiQuery
$query, string $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'iwbl' );
42 public function execute() {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param ApiPageSet|null $resultPageSet
54 public function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
57 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
60 'apierror-invalidparammix-mustusewith',
61 $this->encodeParamName( 'title' ),
62 $this->encodeParamName( 'prefix' ),
68 if ( $params['continue'] !== null ) {
69 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
71 $op = $params['dir'] == 'descending' ?
'<=' : '>=';
72 $this->addWhere( $db->buildComparison( $op, [
73 'iwl_prefix' => $cont[0],
74 'iwl_title' => $cont[1],
75 'iwl_from' => $cont[2],
79 $prop = array_fill_keys( $params['prop'], true );
80 $iwprefix = isset( $prop['iwprefix'] );
81 $iwtitle = isset( $prop['iwtitle'] );
83 $this->addTables( [ 'iwlinks', 'page' ] );
84 $this->addWhere( 'iwl_from = page_id' );
86 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
87 'iwl_from', 'iwl_prefix', 'iwl_title' ] );
89 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
90 if ( isset( $params['prefix'] ) ) {
91 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
92 if ( isset( $params['title'] ) ) {
93 $this->addWhereFld( 'iwl_title', $params['title'] );
94 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
96 $this->addOption( 'ORDER BY', [
102 $this->addOption( 'ORDER BY', [
103 'iwl_prefix' . $sort,
109 $this->addOption( 'LIMIT', $params['limit'] +
1 );
111 $res = $this->select( __METHOD__
);
116 $result = $this->getResult();
118 if ( $resultPageSet === null ) {
119 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
122 foreach ( $res as $row ) {
123 if ( ++
$count > $params['limit'] ) {
124 // We've reached the one extra which shows that there are
125 // additional pages to be had. Stop here...
126 // Continue string preserved in case the redirect query doesn't
128 $this->setContinueEnumParameter(
130 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
135 if ( $resultPageSet !== null ) {
136 $pages[] = Title
::newFromRow( $row );
138 $entry = [ 'pageid' => (int)$row->page_id
];
140 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
141 ApiQueryBase
::addTitleInfo( $entry, $title );
143 if ( $row->page_is_redirect
) {
144 $entry['redirect'] = true;
148 $entry['iwprefix'] = $row->iwl_prefix
;
152 $entry['iwtitle'] = $row->iwl_title
;
155 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
157 $this->setContinueEnumParameter(
159 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
166 if ( $resultPageSet === null ) {
167 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
169 $resultPageSet->populateFromTitles( $pages );
173 public function getCacheMode( $params ) {
177 public function getAllowedParams() {
182 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
185 ParamValidator
::PARAM_DEFAULT
=> 10,
186 ParamValidator
::PARAM_TYPE
=> 'limit',
187 IntegerDef
::PARAM_MIN
=> 1,
188 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
189 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
192 ParamValidator
::PARAM_ISMULTI
=> true,
193 ParamValidator
::PARAM_DEFAULT
=> '',
194 ParamValidator
::PARAM_TYPE
=> [
198 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
201 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
202 ParamValidator
::PARAM_TYPE
=> [
210 protected function getExamplesMessages() {
212 'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
213 => 'apihelp-query+iwbacklinks-example-simple',
214 'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
215 => 'apihelp-query+iwbacklinks-example-generator',
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwbacklinks';
224 /** @deprecated class alias since 1.43 */
225 class_alias( ApiQueryIWBacklinks
::class, 'ApiQueryIWBacklinks' );