Merge "Revert "Live preview no longer experimental""
[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 switch ( $moduleName ) {
36 case 'alllinks':
37 $prefix = 'al';
38 $this->table = 'pagelinks';
39 $this->tablePrefix = 'pl_';
40 $this->dfltNamespace = NS_MAIN;
41 $this->indexTag = 'l';
42 $this->description = 'Enumerate all links that point to a given namespace';
43 $this->descriptionLink = 'link';
44 $this->descriptionLinked = 'linked';
45 $this->descriptionLinking = 'linking';
46 break;
47 case 'alltransclusions':
48 $prefix = 'at';
49 $this->table = 'templatelinks';
50 $this->tablePrefix = 'tl_';
51 $this->dfltNamespace = NS_TEMPLATE;
52 $this->indexTag = 't';
53 $this->description = 'List all transclusions (pages embedded using {{x}}), including non-existing';
54 $this->descriptionLink = 'transclusion';
55 $this->descriptionLinked = 'transcluded';
56 $this->descriptionLinking = 'transcluding';
57 break;
58 default:
59 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
60 }
61
62 parent::__construct( $query, $moduleName, $prefix );
63 }
64
65 public function execute() {
66 $this->run();
67 }
68
69 public function getCacheMode( $params ) {
70 return 'public';
71 }
72
73 public function executeGenerator( $resultPageSet ) {
74 $this->run( $resultPageSet );
75 }
76
77 /**
78 * @param $resultPageSet ApiPageSet
79 * @return void
80 */
81 private function run( $resultPageSet = null ) {
82 $db = $this->getDB();
83 $params = $this->extractRequestParams();
84
85 $pfx = $this->tablePrefix;
86 $prop = array_flip( $params['prop'] );
87 $fld_ids = isset( $prop['ids'] );
88 $fld_title = isset( $prop['title'] );
89
90 if ( $params['unique'] ) {
91 if ( $fld_ids ) {
92 $this->dieUsage(
93 "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionLink}s mode",
94 'params' );
95 }
96 $this->addOption( 'DISTINCT' );
97 }
98
99 $this->addTables( $this->table );
100 $this->addWhereFld( $pfx . 'namespace', $params['namespace'] );
101
102 $continue = !is_null( $params['continue'] );
103 if ( $continue ) {
104 $continueArr = explode( '|', $params['continue'] );
105 $op = $params['dir'] == 'descending' ? '<' : '>';
106 if ( $params['unique'] ) {
107 if ( count( $continueArr ) != 1 ) {
108 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
109 }
110 $continueTitle = $db->addQuotes( $continueArr[0] );
111 $this->addWhere( "{$pfx}title $op= $continueTitle" );
112 } else {
113 if ( count( $continueArr ) != 2 ) {
114 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
115 }
116 $continueTitle = $db->addQuotes( $continueArr[0] );
117 $continueFrom = intval( $continueArr[1] );
118 $this->addWhere(
119 "{$pfx}title $op $continueTitle OR " .
120 "({$pfx}title = $continueTitle AND " .
121 "{$pfx}from $op= $continueFrom)"
122 );
123 }
124 }
125
126 // 'continue' always overrides 'from'
127 $from = ( $continue || is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
128 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
129 $this->addWhereRange( $pfx . 'title', 'newer', $from, $to );
130
131 if ( isset( $params['prefix'] ) ) {
132 $this->addWhere( $pfx . 'title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
133 }
134
135 $this->addFields( array( 'pl_title' => $pfx . 'title' ) );
136 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
137
138 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
139 $limit = $params['limit'];
140 $this->addOption( 'LIMIT', $limit + 1 );
141
142 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
143 $orderBy = array();
144 $orderBy[] = $pfx . 'title' . $sort;
145 if ( !$params['unique'] ) {
146 $orderBy[] = $pfx . 'from' . $sort;
147 }
148 $this->addOption( 'ORDER BY', $orderBy );
149
150 $res = $this->select( __METHOD__ );
151
152 $pageids = array();
153 $titles = array();
154 $count = 0;
155 $result = $this->getResult();
156 foreach ( $res as $row ) {
157 if ( ++ $count > $limit ) {
158 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
159 if ( $params['unique'] ) {
160 $this->setContinueEnumParameter( 'continue', $row->pl_title );
161 } else {
162 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
163 }
164 break;
165 }
166
167 if ( is_null( $resultPageSet ) ) {
168 $vals = array();
169 if ( $fld_ids ) {
170 $vals['fromid'] = intval( $row->pl_from );
171 }
172 if ( $fld_title ) {
173 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
174 ApiQueryBase::addTitleInfo( $vals, $title );
175 }
176 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
177 if ( !$fit ) {
178 if ( $params['unique'] ) {
179 $this->setContinueEnumParameter( 'continue', $row->pl_title );
180 } else {
181 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
182 }
183 break;
184 }
185 } elseif ( $params['unique'] ) {
186 $titles[] = Title::makeTitle( $params['namespace'], $row->pl_title );
187 } else {
188 $pageids[] = $row->pl_from;
189 }
190 }
191
192 if ( is_null( $resultPageSet ) ) {
193 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag );
194 } elseif ( $params['unique'] ) {
195 $resultPageSet->populateFromTitles( $titles );
196 } else {
197 $resultPageSet->populateFromPageIDs( $pageids );
198 }
199 }
200
201 public function getAllowedParams() {
202 return array(
203 'continue' => null,
204 'from' => null,
205 'to' => null,
206 'prefix' => null,
207 'unique' => false,
208 'prop' => array(
209 ApiBase::PARAM_ISMULTI => true,
210 ApiBase::PARAM_DFLT => 'title',
211 ApiBase::PARAM_TYPE => array(
212 'ids',
213 'title'
214 )
215 ),
216 'namespace' => array(
217 ApiBase::PARAM_DFLT => $this->dfltNamespace,
218 ApiBase::PARAM_TYPE => 'namespace'
219 ),
220 'limit' => array(
221 ApiBase::PARAM_DFLT => 10,
222 ApiBase::PARAM_TYPE => 'limit',
223 ApiBase::PARAM_MIN => 1,
224 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
225 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
226 ),
227 'dir' => array(
228 ApiBase::PARAM_DFLT => 'ascending',
229 ApiBase::PARAM_TYPE => array(
230 'ascending',
231 'descending'
232 )
233 ),
234 );
235 }
236
237 public function getParamDescription() {
238 $p = $this->getModulePrefix();
239 $link = $this->descriptionLink;
240 $linking = $this->descriptionLinking;
241 return array(
242 'from' => "The title of the $link to start enumerating from",
243 'to' => "The title of the $link to stop enumerating at",
244 'prefix' => "Search for all $link titles that begin with this value",
245 'unique' => array(
246 "Only show distinct $link titles. Cannot be used with {$p}prop=ids.",
247 'When used as a generator, yields target pages instead of source pages.',
248 ),
249 'prop' => array(
250 'What pieces of information to include',
251 " ids - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
252 " title - Adds the title of the $link",
253 ),
254 'namespace' => 'The namespace to enumerate',
255 'limit' => "How many total items to return",
256 'continue' => 'When more results are available, use this to continue',
257 'dir' => 'The direction in which to list',
258 );
259 }
260
261 public function getResultProperties() {
262 return array(
263 'ids' => array(
264 'fromid' => 'integer'
265 ),
266 'title' => array(
267 'ns' => 'namespace',
268 'title' => 'string'
269 )
270 );
271 }
272
273 public function getDescription() {
274 return $this->description;
275 }
276
277 public function getPossibleErrors() {
278 $m = $this->getModuleName();
279 $link = $this->descriptionLink;
280 return array_merge( parent::getPossibleErrors(), array(
281 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique {$link}s mode" ),
282 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
283 ) );
284 }
285
286 public function getExamples() {
287 $p = $this->getModulePrefix();
288 $link = $this->descriptionLink;
289 $linked = $this->descriptionLinked;
290 return array(
291 "api.php?action=query&list=all{$link}s&{$p}from=B&{$p}prop=ids|title"
292 => "List $linked titles with page ids they are from, including missing ones. Start at B",
293 "api.php?action=query&list=all{$link}s&{$p}unique=&{$p}from=B"
294 => "List unique $linked titles",
295 "api.php?action=query&generator=all{$link}s&g{$p}unique=&g{$p}from=B"
296 => "Gets all $link targets, marking the missing ones",
297 "api.php?action=query&generator=all{$link}s&g{$p}from=B"
298 => "Gets pages containing the {$link}s",
299 );
300 }
301
302 public function getHelpUrls() {
303 return "https://www.mediawiki.org/wiki/API:All{$this->descriptionLink}s";
304 }
305
306 public function getVersion() {
307 return __CLASS__ . ': $Id$';
308 }
309 }