Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiQueryIWBacklinks.php
blob6e2fb67b8d99802ce434924e3f99f18b779ba858
1 <?php
2 /**
3 * API for MediaWiki 1.17+
5 * Created on May 14, 2010
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
28 /**
29 * This gives links pointing to the given interwiki
30 * @ingroup API
32 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'iwbl' );
38 public function execute() {
39 $this->run();
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
46 /**
47 * @param ApiPageSet $resultPageSet
48 * @return void
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
53 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
54 $this->dieWithError(
56 'apierror-invalidparammix-mustusewith',
57 $this->encodeParamName( 'title' ),
58 $this->encodeParamName( 'prefix' ),
60 'invalidparammix'
64 if ( !is_null( $params['continue'] ) ) {
65 $cont = explode( '|', $params['continue'] );
66 $this->dieContinueUsageIf( count( $cont ) != 3 );
68 $db = $this->getDB();
69 $op = $params['dir'] == 'descending' ? '<' : '>';
70 $prefix = $db->addQuotes( $cont[0] );
71 $title = $db->addQuotes( $cont[1] );
72 $from = intval( $cont[2] );
73 $this->addWhere(
74 "iwl_prefix $op $prefix OR " .
75 "(iwl_prefix = $prefix AND " .
76 "(iwl_title $op $title OR " .
77 "(iwl_title = $title AND " .
78 "iwl_from $op= $from)))"
82 $prop = array_flip( $params['prop'] );
83 $iwprefix = isset( $prop['iwprefix'] );
84 $iwtitle = isset( $prop['iwtitle'] );
86 $this->addTables( [ 'iwlinks', 'page' ] );
87 $this->addWhere( 'iwl_from = page_id' );
89 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
90 'iwl_from', 'iwl_prefix', 'iwl_title' ] );
92 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
93 if ( isset( $params['prefix'] ) ) {
94 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
95 if ( isset( $params['title'] ) ) {
96 $this->addWhereFld( 'iwl_title', $params['title'] );
97 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
98 } else {
99 $this->addOption( 'ORDER BY', [
100 'iwl_title' . $sort,
101 'iwl_from' . $sort
102 ] );
104 } else {
105 $this->addOption( 'ORDER BY', [
106 'iwl_prefix' . $sort,
107 'iwl_title' . $sort,
108 'iwl_from' . $sort
109 ] );
112 $this->addOption( 'LIMIT', $params['limit'] + 1 );
114 $res = $this->select( __METHOD__ );
116 $pages = [];
118 $count = 0;
119 $result = $this->getResult();
120 foreach ( $res as $row ) {
121 if ( ++$count > $params['limit'] ) {
122 // We've reached the one extra which shows that there are
123 // additional pages to be had. Stop here...
124 // Continue string preserved in case the redirect query doesn't
125 // pass the limit
126 $this->setContinueEnumParameter(
127 'continue',
128 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
130 break;
133 if ( !is_null( $resultPageSet ) ) {
134 $pages[] = Title::newFromRow( $row );
135 } else {
136 $entry = [ 'pageid' => $row->page_id ];
138 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
139 ApiQueryBase::addTitleInfo( $entry, $title );
141 if ( $row->page_is_redirect ) {
142 $entry['redirect'] = true;
145 if ( $iwprefix ) {
146 $entry['iwprefix'] = $row->iwl_prefix;
149 if ( $iwtitle ) {
150 $entry['iwtitle'] = $row->iwl_title;
153 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
154 if ( !$fit ) {
155 $this->setContinueEnumParameter(
156 'continue',
157 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
159 break;
164 if ( is_null( $resultPageSet ) ) {
165 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
166 } else {
167 $resultPageSet->populateFromTitles( $pages );
171 public function getCacheMode( $params ) {
172 return 'public';
175 public function getAllowedParams() {
176 return [
177 'prefix' => null,
178 'title' => null,
179 'continue' => [
180 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
182 'limit' => [
183 ApiBase::PARAM_DFLT => 10,
184 ApiBase::PARAM_TYPE => 'limit',
185 ApiBase::PARAM_MIN => 1,
186 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
187 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
189 'prop' => [
190 ApiBase::PARAM_ISMULTI => true,
191 ApiBase::PARAM_DFLT => '',
192 ApiBase::PARAM_TYPE => [
193 'iwprefix',
194 'iwtitle',
196 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
198 'dir' => [
199 ApiBase::PARAM_DFLT => 'ascending',
200 ApiBase::PARAM_TYPE => [
201 'ascending',
202 'descending'
208 protected function getExamplesMessages() {
209 return [
210 'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
211 => 'apihelp-query+iwbacklinks-example-simple',
212 'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
213 => 'apihelp-query+iwbacklinks-example-generator',
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Iwbacklinks';