Merge "Check validity and availability of usernames during signup via AJAX"
[lhc/web/wiklou.git] / includes / api / ApiQueryIWBacklinks.php
1 <?php
2 /**
3 * API for MediaWiki 1.17+
4 *
5 * Created on May 14, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27
28 /**
29 * This gives links pointing to the given interwiki
30 * @ingroup API
31 */
32 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'iwbl' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
49 */
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
55 }
56
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 $this->dieContinueUsageIf( count( $cont ) != 3 );
60
61 $db = $this->getDB();
62 $op = $params['dir'] == 'descending' ? '<' : '>';
63 $prefix = $db->addQuotes( $cont[0] );
64 $title = $db->addQuotes( $cont[1] );
65 $from = intval( $cont[2] );
66 $this->addWhere(
67 "iwl_prefix $op $prefix OR " .
68 "(iwl_prefix = $prefix AND " .
69 "(iwl_title $op $title OR " .
70 "(iwl_title = $title AND " .
71 "iwl_from $op= $from)))"
72 );
73 }
74
75 $prop = array_flip( $params['prop'] );
76 $iwprefix = isset( $prop['iwprefix'] );
77 $iwtitle = isset( $prop['iwtitle'] );
78
79 $this->addTables( array( 'iwlinks', 'page' ) );
80 $this->addWhere( 'iwl_from = page_id' );
81
82 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
83 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
84
85 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
86 if ( isset( $params['prefix'] ) ) {
87 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
88 if ( isset( $params['title'] ) ) {
89 $this->addWhereFld( 'iwl_title', $params['title'] );
90 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
91 } else {
92 $this->addOption( 'ORDER BY', array(
93 'iwl_title' . $sort,
94 'iwl_from' . $sort
95 ) );
96 }
97 } else {
98 $this->addOption( 'ORDER BY', array(
99 'iwl_prefix' . $sort,
100 'iwl_title' . $sort,
101 'iwl_from' . $sort
102 ) );
103 }
104
105 $this->addOption( 'LIMIT', $params['limit'] + 1 );
106
107 $res = $this->select( __METHOD__ );
108
109 $pages = array();
110
111 $count = 0;
112 $result = $this->getResult();
113 foreach ( $res as $row ) {
114 if ( ++$count > $params['limit'] ) {
115 // We've reached the one extra which shows that there are
116 // additional pages to be had. Stop here...
117 // Continue string preserved in case the redirect query doesn't
118 // pass the limit
119 $this->setContinueEnumParameter(
120 'continue',
121 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
122 );
123 break;
124 }
125
126 if ( !is_null( $resultPageSet ) ) {
127 $pages[] = Title::newFromRow( $row );
128 } else {
129 $entry = array( 'pageid' => $row->page_id );
130
131 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
132 ApiQueryBase::addTitleInfo( $entry, $title );
133
134 if ( $row->page_is_redirect ) {
135 $entry['redirect'] = '';
136 }
137
138 if ( $iwprefix ) {
139 $entry['iwprefix'] = $row->iwl_prefix;
140 }
141
142 if ( $iwtitle ) {
143 $entry['iwtitle'] = $row->iwl_title;
144 }
145
146 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
147 if ( !$fit ) {
148 $this->setContinueEnumParameter(
149 'continue',
150 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
151 );
152 break;
153 }
154 }
155 }
156
157 if ( is_null( $resultPageSet ) ) {
158 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
159 } else {
160 $resultPageSet->populateFromTitles( $pages );
161 }
162 }
163
164 public function getCacheMode( $params ) {
165 return 'public';
166 }
167
168 public function getAllowedParams() {
169 return array(
170 'prefix' => null,
171 'title' => null,
172 'continue' => null,
173 'limit' => array(
174 ApiBase::PARAM_DFLT => 10,
175 ApiBase::PARAM_TYPE => 'limit',
176 ApiBase::PARAM_MIN => 1,
177 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
178 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
179 ),
180 'prop' => array(
181 ApiBase::PARAM_ISMULTI => true,
182 ApiBase::PARAM_DFLT => '',
183 ApiBase::PARAM_TYPE => array(
184 'iwprefix',
185 'iwtitle',
186 ),
187 ),
188 'dir' => array(
189 ApiBase::PARAM_DFLT => 'ascending',
190 ApiBase::PARAM_TYPE => array(
191 'ascending',
192 'descending'
193 )
194 ),
195 );
196 }
197
198 public function getParamDescription() {
199 return array(
200 'prefix' => 'Prefix for the interwiki',
201 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
202 'continue' => 'When more results are available, use this to continue',
203 'prop' => array(
204 'Which properties to get',
205 ' iwprefix - Adds the prefix of the interwiki',
206 ' iwtitle - Adds the title of the interwiki',
207 ),
208 'limit' => 'How many total pages to return',
209 'dir' => 'The direction in which to list',
210 );
211 }
212
213 public function getResultProperties() {
214 return array(
215 '' => array(
216 'pageid' => 'integer',
217 'ns' => 'namespace',
218 'title' => 'string',
219 'redirect' => 'boolean'
220 ),
221 'iwprefix' => array(
222 'iwprefix' => 'string'
223 ),
224 'iwtitle' => array(
225 'iwtitle' => 'string'
226 )
227 );
228 }
229
230 public function getDescription() {
231 return array( 'Find all pages that link to the given interwiki link.',
232 'Can be used to find all links with a prefix, or',
233 'all links to a title (with a given prefix).',
234 'Using neither parameter is effectively "All IW Links".',
235 );
236 }
237
238 public function getPossibleErrors() {
239 return array_merge( parent::getPossibleErrors(), array(
240 array( 'missingparam', 'prefix' ),
241 ) );
242 }
243
244 public function getExamples() {
245 return array(
246 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
247 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
248 );
249 }
250
251 public function getHelpUrls() {
252 return 'https://www.mediawiki.org/wiki/API:Iwbacklinks';
253 }
254 }