Merge "I18n of display of change tags"
[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 $op = $params['dir'] == 'descending' ? '<' : '>';
84 $continueTitle = $db->addQuotes( $this->titleToKey( $continueArr[0] ) );
85 $continueFrom = intval( $continueArr[1] );
86 $this->addWhere(
87 "pl_title $op $continueTitle OR " .
88 "(pl_title = $continueTitle AND " .
89 "pl_from $op= $continueFrom)"
90 );
91 }
92
93 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
94 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
95 $this->addWhereRange( 'pl_title', 'newer', $from, $to );
96
97 if ( isset( $params['prefix'] ) ) {
98 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
99 }
100
101 $this->addFields( 'pl_title' );
102 $this->addFieldsIf( 'pl_from', !$params['unique'] );
103
104 $this->addOption( 'USE INDEX', 'pl_namespace' );
105 $limit = $params['limit'];
106 $this->addOption( 'LIMIT', $limit + 1 );
107
108 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
109 $orderBy = array();
110 $orderBy[] = 'pl_title' . $sort;
111 if ( !$params['unique'] ) {
112 $orderBy[] = 'pl_from' . $sort;
113 }
114 $this->addOption( 'ORDER BY', $orderBy );
115
116 $res = $this->select( __METHOD__ );
117
118 $pageids = array();
119 $count = 0;
120 $result = $this->getResult();
121 foreach ( $res as $row ) {
122 if ( ++ $count > $limit ) {
123 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
124 // TODO: Security issue - if the user has no right to view next title, it will still be shown
125 if ( $params['unique'] ) {
126 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
127 } else {
128 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
129 }
130 break;
131 }
132
133 if ( is_null( $resultPageSet ) ) {
134 $vals = array();
135 if ( $fld_ids ) {
136 $vals['fromid'] = intval( $row->pl_from );
137 }
138 if ( $fld_title ) {
139 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
140 ApiQueryBase::addTitleInfo( $vals, $title );
141 }
142 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
143 if ( !$fit ) {
144 if ( $params['unique'] ) {
145 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
146 } else {
147 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
148 }
149 break;
150 }
151 } else {
152 $pageids[] = $row->pl_from;
153 }
154 }
155
156 if ( is_null( $resultPageSet ) ) {
157 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
158 } else {
159 $resultPageSet->populateFromPageIDs( $pageids );
160 }
161 }
162
163 public function getAllowedParams() {
164 return array(
165 'continue' => null,
166 'from' => null,
167 'to' => null,
168 'prefix' => null,
169 'unique' => false,
170 'prop' => array(
171 ApiBase::PARAM_ISMULTI => true,
172 ApiBase::PARAM_DFLT => 'title',
173 ApiBase::PARAM_TYPE => array(
174 'ids',
175 'title'
176 )
177 ),
178 'namespace' => array(
179 ApiBase::PARAM_DFLT => 0,
180 ApiBase::PARAM_TYPE => 'namespace'
181 ),
182 'limit' => array(
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
188 ),
189 'dir' => array(
190 ApiBase::PARAM_DFLT => 'ascending',
191 ApiBase::PARAM_TYPE => array(
192 'ascending',
193 'descending'
194 )
195 ),
196 );
197 }
198
199 public function getParamDescription() {
200 $p = $this->getModulePrefix();
201 return array(
202 'from' => 'The page title to start enumerating from',
203 'to' => 'The page title to stop enumerating at',
204 'prefix' => 'Search for all page titles that begin with this value',
205 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
206 'prop' => array(
207 'What pieces of information to include',
208 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
209 ' title - Adds the title of the link',
210 ),
211 'namespace' => 'The namespace to enumerate',
212 'limit' => 'How many total links to return',
213 'continue' => 'When more results are available, use this to continue',
214 'dir' => 'The direction in which to list',
215 );
216 }
217
218 public function getResultProperties() {
219 return array(
220 'ids' => array(
221 'fromid' => 'integer'
222 ),
223 'title' => array(
224 'ns' => 'namespace',
225 'title' => 'string'
226 )
227 );
228 }
229
230 public function getDescription() {
231 return 'Enumerate all links that point to a given namespace';
232 }
233
234 public function getPossibleErrors() {
235 $m = $this->getModuleName();
236 return array_merge( parent::getPossibleErrors(), array(
237 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
238 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
239 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
240 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
241 ) );
242 }
243
244 public function getExamples() {
245 return array(
246 'api.php?action=query&list=alllinks&alunique=&alfrom=B',
247 );
248 }
249
250 public function getHelpUrls() {
251 return 'https://www.mediawiki.org/wiki/API:Alllinks';
252 }
253
254 public function getVersion() {
255 return __CLASS__ . ': $Id$';
256 }
257 }