Merge "Correct case for the "MediaWiki:Revdelete-reason-dropdown" link in Special...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 */
26
27 /**
28 * Query module to enumerate links from all pages together.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'al' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param $resultPageSet ApiPageSet
52 * @return void
53 */
54 private function run( $resultPageSet = null ) {
55 $db = $this->getDB();
56 $params = $this->extractRequestParams();
57
58 $prop = array_flip( $params['prop'] );
59 $fld_ids = isset( $prop['ids'] );
60 $fld_title = isset( $prop['title'] );
61
62 if ( $params['unique'] ) {
63 if ( !is_null( $resultPageSet ) ) {
64 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
65 }
66 if ( $fld_ids ) {
67 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
68 }
69 $this->addOption( 'DISTINCT' );
70 }
71
72 $this->addTables( 'pagelinks' );
73 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
74
75 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
76 $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
77 }
78 if ( !is_null( $params['continue'] ) ) {
79 $continueArr = explode( '|', $params['continue'] );
80 if ( count( $continueArr ) != 2 ) {
81 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
82 }
83 $continueTitle = $db->addQuotes( $this->titleToKey( $continueArr[0] ) );
84 $continueFrom = intval( $continueArr[1] );
85 $this->addWhere(
86 "pl_title > $continueTitle OR " .
87 "(pl_title = $continueTitle AND " .
88 "pl_from > $continueFrom)"
89 );
90 }
91
92 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
93 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
94 $this->addWhereRange( 'pl_title', 'newer', $from, $to );
95
96 if ( isset( $params['prefix'] ) ) {
97 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
98 }
99
100 $this->addFields( 'pl_title' );
101 $this->addFieldsIf( 'pl_from', !$params['unique'] );
102
103 $this->addOption( 'USE INDEX', 'pl_namespace' );
104 $limit = $params['limit'];
105 $this->addOption( 'LIMIT', $limit + 1 );
106
107 if ( !$params['unique'] ) {
108 $this->addOption( 'ORDER BY', array(
109 'pl_title',
110 'pl_from'
111 ));
112 }
113
114 $res = $this->select( __METHOD__ );
115
116 $pageids = array();
117 $count = 0;
118 $result = $this->getResult();
119 foreach ( $res as $row ) {
120 if ( ++ $count > $limit ) {
121 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
122 // TODO: Security issue - if the user has no right to view next title, it will still be shown
123 if ( $params['unique'] ) {
124 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
125 } else {
126 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
127 }
128 break;
129 }
130
131 if ( is_null( $resultPageSet ) ) {
132 $vals = array();
133 if ( $fld_ids ) {
134 $vals['fromid'] = intval( $row->pl_from );
135 }
136 if ( $fld_title ) {
137 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
138 ApiQueryBase::addTitleInfo( $vals, $title );
139 }
140 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
141 if ( !$fit ) {
142 if ( $params['unique'] ) {
143 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
144 } else {
145 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
146 }
147 break;
148 }
149 } else {
150 $pageids[] = $row->pl_from;
151 }
152 }
153
154 if ( is_null( $resultPageSet ) ) {
155 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
156 } else {
157 $resultPageSet->populateFromPageIDs( $pageids );
158 }
159 }
160
161 public function getAllowedParams() {
162 return array(
163 'continue' => null,
164 'from' => null,
165 'to' => null,
166 'prefix' => null,
167 'unique' => false,
168 'prop' => array(
169 ApiBase::PARAM_ISMULTI => true,
170 ApiBase::PARAM_DFLT => 'title',
171 ApiBase::PARAM_TYPE => array(
172 'ids',
173 'title'
174 )
175 ),
176 'namespace' => array(
177 ApiBase::PARAM_DFLT => 0,
178 ApiBase::PARAM_TYPE => 'namespace'
179 ),
180 'limit' => array(
181 ApiBase::PARAM_DFLT => 10,
182 ApiBase::PARAM_TYPE => 'limit',
183 ApiBase::PARAM_MIN => 1,
184 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
185 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
186 )
187 );
188 }
189
190 public function getParamDescription() {
191 $p = $this->getModulePrefix();
192 return array(
193 'from' => 'The page title to start enumerating from',
194 'to' => 'The page title to stop enumerating at',
195 'prefix' => 'Search for all page titles that begin with this value',
196 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
197 'prop' => array(
198 'What pieces of information to include',
199 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
200 ' title - Adds the title of the link',
201 ),
202 'namespace' => 'The namespace to enumerate',
203 'limit' => 'How many total links to return',
204 'continue' => 'When more results are available, use this to continue',
205 );
206 }
207
208 public function getResultProperties() {
209 return array(
210 'ids' => array(
211 'fromid' => 'integer'
212 ),
213 'title' => array(
214 'ns' => 'namespace',
215 'title' => 'string'
216 )
217 );
218 }
219
220 public function getDescription() {
221 return 'Enumerate all links that point to a given namespace';
222 }
223
224 public function getPossibleErrors() {
225 $m = $this->getModuleName();
226 return array_merge( parent::getPossibleErrors(), array(
227 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
228 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
229 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
230 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
231 ) );
232 }
233
234 public function getExamples() {
235 return array(
236 'api.php?action=query&list=alllinks&alunique=&alfrom=B',
237 );
238 }
239
240 public function getHelpUrls() {
241 return 'https://www.mediawiki.org/wiki/API:Alllinks';
242 }
243
244 public function getVersion() {
245 return __CLASS__ . ': $Id$';
246 }
247 }