Merge "SkinFactory: Improve documentation and comments"
[lhc/web/wiklou.git] / includes / api / ApiQueryRedirects.php
1 <?php
2 /**
3 * API module to return redirects to a page
4 *
5 * Created on Dec 30, 2013
6 *
7 * Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @since 1.23
26 */
27
28 /**
29 * This query lists redirects to the given pages.
30 *
31 * @ingroup API
32 */
33 class ApiQueryRedirects extends ApiQueryGeneratorBase {
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'rd' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
47 /**
48 * @param ApiPageSet $resultPageSet
49 */
50 private function run( ApiPageSet $resultPageSet = null ) {
51 $db = $this->getDB();
52 $params = $this->extractRequestParams();
53 $emptyString = $db->addQuotes( '' );
54
55 $pageSet = $this->getPageSet();
56 $titles = $pageSet->getGoodTitles() + $pageSet->getMissingTitles();
57
58 if ( !is_null( $params['continue'] ) ) {
59 $cont = explode( '|', $params['continue'] );
60 $this->dieContinueUsageIf( count( $cont ) != 3 );
61 $rd_namespace = (int)$cont[0];
62 $this->dieContinueUsageIf( $rd_namespace != $cont[0] );
63 $rd_title = $db->addQuotes( $cont[1] );
64 $rd_from = (int)$cont[2];
65 $this->dieContinueUsageIf( $rd_from != $cont[2] );
66 $this->addWhere(
67 "rd_namespace > $rd_namespace OR " .
68 "(rd_namespace = $rd_namespace AND " .
69 "(rd_title > $rd_title OR " .
70 "(rd_title = $rd_title AND " .
71 "rd_from >= $rd_from)))"
72 );
73
74 // Remove titles that we're past already
75 $titles = array_filter( $titles, function ( $t ) use ( $rd_namespace, $rd_title ) {
76 $ns = $t->getNamespace();
77 return ( $ns > $rd_namespace ||
78 $ns == $rd_namespace && $t->getDBKey() >= $rd_title
79 );
80 } );
81 }
82
83 if ( !$titles ) {
84 return; // nothing to do
85 }
86
87 $this->addTables( array( 'redirect', 'page' ) );
88 $this->addFields( array(
89 'rd_from',
90 'rd_namespace',
91 'rd_title',
92 ) );
93
94 if ( is_null( $resultPageSet ) ) {
95 $prop = array_flip( $params['prop'] );
96 $fld_pageid = isset( $prop['pageid'] );
97 $fld_title = isset( $prop['title'] );
98 $fld_fragment = isset( $prop['fragment'] );
99
100 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
101 $this->addFieldsIf( array( 'page_namespace', 'page_title' ), $fld_title );
102 } else {
103 $this->addFields( array( 'page_namespace', 'page_title' ) );
104 }
105
106 $lb = new LinkBatch( $titles );
107 $this->addWhere( array(
108 'rd_from = page_id',
109 "rd_interwiki = $emptyString OR rd_interwiki IS NULL",
110 $lb->constructSet( 'rd', $db ),
111 ) );
112
113 if ( $params['show'] !== null ) {
114 $show = array_flip( $params['show'] );
115 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) {
116 $this->dieUsageMsg( 'show' );
117 }
118 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
119 $this->addWhereIf(
120 "rd_fragment = $emptyString OR rd_fragment IS NULL",
121 isset( $show['!fragment'] )
122 );
123 }
124
125 $map = $pageSet->getAllTitlesByNamespace();
126
127 // Why, MySQL? Why do you do this to us?
128 $sortby = array();
129 if ( count( $map ) > 1 ) {
130 $sortby[] = 'rd_namespace';
131 }
132 $theTitle = null;
133 foreach ( $map as $nsTitles ) {
134 reset( $nsTitles );
135 $key = key( $nsTitles );
136 if ( $theTitle === null ) {
137 $theTitle = $key;
138 }
139 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
140 $sortby[] = 'rd_title';
141 break;
142 }
143 }
144 $sortby[] = 'rd_from';
145 $this->addOption( 'ORDER BY', $sortby );
146
147 $this->addOption( 'LIMIT', $params['limit'] + 1 );
148
149 $res = $this->select( __METHOD__ );
150
151 if ( is_null( $resultPageSet ) ) {
152 $count = 0;
153 foreach ( $res as $row ) {
154 if ( ++$count > $params['limit'] ) {
155 // We've reached the one extra which shows that
156 // there are additional pages to be had. Stop here...
157 $this->setContinueEnumParameter( 'continue',
158 "$row->rd_namespace|$row->rd_title|$row->rd_from"
159 );
160 break;
161 }
162
163 # Get the ID of the current page
164 $id = $map[$row->rd_namespace][$row->rd_title];
165
166 $vals = array();
167 if ( $fld_pageid ) {
168 $vals['pageid'] = $row->rd_from;
169 }
170 if ( $fld_title ) {
171 ApiQueryBase::addTitleInfo( $vals,
172 Title::makeTitle( $row->page_namespace, $row->page_title )
173 );
174 }
175 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
176 $vals['fragment'] = $row->rd_fragment;
177 }
178 $fit = $this->addPageSubItem( $id, $vals );
179 if ( !$fit ) {
180 $this->setContinueEnumParameter( 'continue',
181 "$row->rd_namespace|$row->rd_title|$row->rd_from"
182 );
183 break;
184 }
185 }
186 } else {
187 $titles = array();
188 $count = 0;
189 foreach ( $res as $row ) {
190 if ( ++$count > $params['limit'] ) {
191 // We've reached the one extra which shows that
192 // there are additional pages to be had. Stop here...
193 $this->setContinueEnumParameter( 'continue',
194 "$row->rd_namespace|$row->rd_title|$row->rd_from"
195 );
196 break;
197 }
198 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
199 }
200 $resultPageSet->populateFromTitles( $titles );
201 }
202 }
203
204 public function getCacheMode( $params ) {
205 return 'public';
206 }
207
208 public function getAllowedParams() {
209 return array(
210 'prop' => array(
211 ApiBase::PARAM_TYPE => array(
212 'pageid',
213 'title',
214 'fragment',
215 ),
216 ApiBase::PARAM_ISMULTI => true,
217 ApiBase::PARAM_DFLT => 'pageid|title',
218 ),
219 'show' => array(
220 ApiBase::PARAM_TYPE => array(
221 'fragment', '!fragment',
222 ),
223 ApiBase::PARAM_ISMULTI => true,
224 ),
225 'limit' => array(
226 ApiBase::PARAM_DFLT => 10,
227 ApiBase::PARAM_TYPE => 'limit',
228 ApiBase::PARAM_MIN => 1,
229 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
230 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
231 ),
232 'continue' => null,
233 );
234 }
235
236 public function getParamDescription() {
237 return array(
238 'prop' => array(
239 'Which properties to get:',
240 ' pageid - Page id of each redirect',
241 ' title - Title of each redirect',
242 ' fragment - Fragment of each redirect, if any',
243 ),
244 'show' => array(
245 'Show only items that meet this criteria.',
246 ' fragment - Only show redirects with a fragment',
247 ' !fragment - Only show redirects without a fragment',
248 ),
249 'limit' => 'How many redirects to return',
250 'continue' => 'When more results are available, use this to continue',
251 );
252 }
253
254 public function getDescription() {
255 return 'Returns all redirects to the given page(s).';
256 }
257
258 public function getExamples() {
259 return array(
260 'api.php?action=query&prop=redirects&titles=Main%20Page'
261 => 'Get a list of redirects to the [[Main Page]]',
262 'api.php?action=query&generator=redirects&titles=Main%20Page&prop=info'
263 => 'Get information about all redirects to the [[Main Page]]',
264 );
265 }
266
267 public function getHelpUrls() {
268 return 'https://www.mediawiki.org/wiki/API:Properties#redirects_.2F_rd';
269 }
270 }