f75b54e3d00ff06bc92c3606f3129c4f44df75f3
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
1 <?php
2
3 /**
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * A query module to show basic page information.
33 *
34 * @ingroup API
35 */
36 class ApiQueryInfo extends ApiQueryBase {
37
38 private $fld_protection = false, $fld_talkid = false,
39 $fld_subjectid = false, $fld_url = false,
40 $fld_readable = false, $fld_watched = false,
41 $fld_preload = false;
42
43 public function __construct( $query, $moduleName ) {
44 parent::__construct( $query, $moduleName, 'in' );
45 }
46
47 public function requestExtraData( $pageSet ) {
48 $pageSet->requestField( 'page_restrictions' );
49 $pageSet->requestField( 'page_is_redirect' );
50 $pageSet->requestField( 'page_is_new' );
51 $pageSet->requestField( 'page_counter' );
52 $pageSet->requestField( 'page_touched' );
53 $pageSet->requestField( 'page_latest' );
54 $pageSet->requestField( 'page_len' );
55 }
56
57 /**
58 * Get an array mapping token names to their handler functions.
59 * The prototype for a token function is func($pageid, $title)
60 * it should return a token or false (permission denied)
61 * @return array(tokenname => function)
62 */
63 protected function getTokenFunctions() {
64 // Don't call the hooks twice
65 if ( isset( $this->tokenFunctions ) ) {
66 return $this->tokenFunctions;
67 }
68
69 // If we're in JSON callback mode, no tokens can be obtained
70 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
71 return array();
72 }
73
74 $this->tokenFunctions = array(
75 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
76 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
77 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
78 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
79 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
80 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
81 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
82 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
83 );
84 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions ) );
85 return $this->tokenFunctions;
86 }
87
88 public static function getEditToken( $pageid, $title ) {
89 // We could check for $title->userCan('edit') here,
90 // but that's too expensive for this purpose
91 // and would break caching
92 global $wgUser;
93 if ( !$wgUser->isAllowed( 'edit' ) ) {
94 return false;
95 }
96
97 // The edit token is always the same, let's exploit that
98 static $cachedEditToken = null;
99 if ( !is_null( $cachedEditToken ) ) {
100 return $cachedEditToken;
101 }
102
103 $cachedEditToken = $wgUser->editToken();
104 return $cachedEditToken;
105 }
106
107 public static function getDeleteToken( $pageid, $title ) {
108 global $wgUser;
109 if ( !$wgUser->isAllowed( 'delete' ) ) {
110 return false;
111 }
112
113 static $cachedDeleteToken = null;
114 if ( !is_null( $cachedDeleteToken ) ) {
115 return $cachedDeleteToken;
116 }
117
118 $cachedDeleteToken = $wgUser->editToken();
119 return $cachedDeleteToken;
120 }
121
122 public static function getProtectToken( $pageid, $title ) {
123 global $wgUser;
124 if ( !$wgUser->isAllowed( 'protect' ) ) {
125 return false;
126 }
127
128 static $cachedProtectToken = null;
129 if ( !is_null( $cachedProtectToken ) ) {
130 return $cachedProtectToken;
131 }
132
133 $cachedProtectToken = $wgUser->editToken();
134 return $cachedProtectToken;
135 }
136
137 public static function getMoveToken( $pageid, $title ) {
138 global $wgUser;
139 if ( !$wgUser->isAllowed( 'move' ) ) {
140 return false;
141 }
142
143 static $cachedMoveToken = null;
144 if ( !is_null( $cachedMoveToken ) ) {
145 return $cachedMoveToken;
146 }
147
148 $cachedMoveToken = $wgUser->editToken();
149 return $cachedMoveToken;
150 }
151
152 public static function getBlockToken( $pageid, $title ) {
153 global $wgUser;
154 if ( !$wgUser->isAllowed( 'block' ) ) {
155 return false;
156 }
157
158 static $cachedBlockToken = null;
159 if ( !is_null( $cachedBlockToken ) ) {
160 return $cachedBlockToken;
161 }
162
163 $cachedBlockToken = $wgUser->editToken();
164 return $cachedBlockToken;
165 }
166
167 public static function getUnblockToken( $pageid, $title ) {
168 // Currently, this is exactly the same as the block token
169 return self::getBlockToken( $pageid, $title );
170 }
171
172 public static function getEmailToken( $pageid, $title ) {
173 global $wgUser;
174 if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser() ) {
175 return false;
176 }
177
178 static $cachedEmailToken = null;
179 if ( !is_null( $cachedEmailToken ) ) {
180 return $cachedEmailToken;
181 }
182
183 $cachedEmailToken = $wgUser->editToken();
184 return $cachedEmailToken;
185 }
186
187 public static function getImportToken( $pageid, $title ) {
188 global $wgUser;
189 if ( !$wgUser->isAllowed( 'import' ) ) {
190 return false;
191 }
192
193 static $cachedImportToken = null;
194 if ( !is_null( $cachedImportToken ) ) {
195 return $cachedImportToken;
196 }
197
198 $cachedImportToken = $wgUser->editToken();
199 return $cachedImportToken;
200 }
201
202 public function execute() {
203 $this->params = $this->extractRequestParams();
204 if ( !is_null( $this->params['prop'] ) ) {
205 $prop = array_flip( $this->params['prop'] );
206 $this->fld_protection = isset( $prop['protection'] );
207 $this->fld_watched = isset( $prop['watched'] );
208 $this->fld_talkid = isset( $prop['talkid'] );
209 $this->fld_subjectid = isset( $prop['subjectid'] );
210 $this->fld_url = isset( $prop['url'] );
211 $this->fld_readable = isset( $prop['readable'] );
212 $this->fld_preload = isset( $prop['preload'] );
213 $this->fld_displaytitle = isset( $prop['displaytitle'] );
214 }
215
216 $pageSet = $this->getPageSet();
217 $this->titles = $pageSet->getGoodTitles();
218 $this->missing = $pageSet->getMissingTitles();
219 $this->everything = $this->titles + $this->missing;
220 $result = $this->getResult();
221
222 uasort( $this->everything, array( 'Title', 'compare' ) );
223 if ( !is_null( $this->params['continue'] ) ) {
224 // Throw away any titles we're gonna skip so they don't
225 // clutter queries
226 $cont = explode( '|', $this->params['continue'] );
227 if ( count( $cont ) != 2 ) {
228 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
229 'value returned by the previous query', '_badcontinue' );
230 }
231 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
232 foreach ( $this->everything as $pageid => $title ) {
233 if ( Title::compare( $title, $conttitle ) >= 0 ) {
234 break;
235 }
236 unset( $this->titles[$pageid] );
237 unset( $this->missing[$pageid] );
238 unset( $this->everything[$pageid] );
239 }
240 }
241
242 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
243 $this->pageIsRedir = $pageSet->getCustomField( 'page_is_redirect' );
244 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
245 $this->pageCounter = $pageSet->getCustomField( 'page_counter' );
246 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
247 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
248 $this->pageLength = $pageSet->getCustomField( 'page_len' );
249
250 $db = $this->getDB();
251 // Get protection info if requested
252 if ( $this->fld_protection ) {
253 $this->getProtectionInfo();
254 }
255
256 if ( $this->fld_watched ) {
257 $this->getMain()->setVaryCookie();
258 $this->getWatchedInfo();
259 }
260
261 // Run the talkid/subjectid query if requested
262 if ( $this->fld_talkid || $this->fld_subjectid ) {
263 $this->getTSIDs();
264 }
265
266 foreach ( $this->everything as $pageid => $title ) {
267 $pageInfo = $this->extractPageInfo( $pageid, $title );
268 $fit = $result->addValue( array(
269 'query',
270 'pages'
271 ), $pageid, $pageInfo );
272 if ( !$fit ) {
273 $this->setContinueEnumParameter( 'continue',
274 $title->getNamespace() . '|' .
275 $title->getText() );
276 break;
277 }
278 }
279 }
280
281 /**
282 * Get a result array with information about a title
283 * @param $pageid int Page ID (negative for missing titles)
284 * @param $title Title object
285 * @return array
286 */
287 private function extractPageInfo( $pageid, $title ) {
288 $pageInfo = array();
289 if ( $title->exists() ) {
290 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
291 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
292 $pageInfo['counter'] = intval( $this->pageCounter[$pageid] );
293 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
294
295 if ( $this->pageIsRedir[$pageid] ) {
296 $pageInfo['redirect'] = '';
297 }
298 if ( $this->pageIsNew[$pageid] ) {
299 $pageInfo['new'] = '';
300 }
301 }
302
303 if ( !is_null( $this->params['token'] ) ) {
304 // Don't cache tokens
305 $this->getMain()->setCachePrivate();
306
307 $tokenFunctions = $this->getTokenFunctions();
308 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
309 foreach ( $this->params['token'] as $t ) {
310 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
311 if ( $val === false ) {
312 $this->setWarning( "Action '$t' is not allowed for the current user" );
313 } else {
314 $pageInfo[$t . 'token'] = $val;
315 }
316 }
317 }
318
319 if ( $this->fld_protection ) {
320 $pageInfo['protection'] = array();
321 if ( isset( $this->protections[$title->getNamespace()][$title->getDBkey()] ) ) {
322 $pageInfo['protection'] =
323 $this->protections[$title->getNamespace()][$title->getDBkey()];
324 }
325 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
326 }
327
328 if ( $this->fld_watched && isset( $this->watched[$title->getNamespace()][$title->getDBkey()] ) ) {
329 $pageInfo['watched'] = '';
330 }
331
332 if ( $this->fld_talkid && isset( $this->talkids[$title->getNamespace()][$title->getDBkey()] ) ) {
333 $pageInfo['talkid'] = $this->talkids[$title->getNamespace()][$title->getDBkey()];
334 }
335
336 if ( $this->fld_subjectid && isset( $this->subjectids[$title->getNamespace()][$title->getDBkey()] ) ) {
337 $pageInfo['subjectid'] = $this->subjectids[$title->getNamespace()][$title->getDBkey()];
338 }
339
340 if ( $this->fld_url ) {
341 $pageInfo['fullurl'] = $title->getFullURL();
342 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
343 }
344 if ( $this->fld_readable && $title->userCanRead() ) {
345 $pageInfo['readable'] = '';
346 }
347
348 if ( $this->fld_preload ) {
349 if ( $title->exists() ) {
350 $pageInfo['preload'] = '';
351 } else {
352 $text = null;
353 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
354
355 $pageInfo['preload'] = $text;
356 }
357 }
358
359 if ( $this->fld_displaytitle ) {
360
361 }
362
363 return $pageInfo;
364 }
365
366 /**
367 * Get information about protections and put it in $protections
368 */
369 private function getProtectionInfo() {
370 $this->protections = array();
371 $db = $this->getDB();
372
373 // Get normal protections for existing titles
374 if ( count( $this->titles ) ) {
375 $this->resetQueryParams();
376 $this->addTables( array( 'page_restrictions', 'page' ) );
377 $this->addWhere( 'page_id=pr_page' );
378 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
379 'pr_expiry', 'pr_cascade', 'page_namespace',
380 'page_title' ) );
381 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
382
383 $res = $this->select( __METHOD__ );
384 foreach ( $res as $row ) {
385 $a = array(
386 'type' => $row->pr_type,
387 'level' => $row->pr_level,
388 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
389 );
390 if ( $row->pr_cascade ) {
391 $a['cascade'] = '';
392 }
393 $this->protections[$row->page_namespace][$row->page_title][] = $a;
394
395 // Also check old restrictions
396 if ( $this->pageRestrictions[$row->pr_page] ) {
397 $restrictions = explode( ':', trim( $this->pageRestrictions[$row->pr_page] ) );
398 foreach ( $restrictions as $restrict ) {
399 $temp = explode( '=', trim( $restrict ) );
400 if ( count( $temp ) == 1 ) {
401 // old old format should be treated as edit/move restriction
402 $restriction = trim( $temp[0] );
403
404 if ( $restriction == '' ) {
405 continue;
406 }
407 $this->protections[$row->page_namespace][$row->page_title][] = array(
408 'type' => 'edit',
409 'level' => $restriction,
410 'expiry' => 'infinity',
411 );
412 $this->protections[$row->page_namespace][$row->page_title][] = array(
413 'type' => 'move',
414 'level' => $restriction,
415 'expiry' => 'infinity',
416 );
417 } else {
418 $restriction = trim( $temp[1] );
419 if ( $restriction == '' ) {
420 continue;
421 }
422 $this->protections[$row->page_namespace][$row->page_title][] = array(
423 'type' => $temp[0],
424 'level' => $restriction,
425 'expiry' => 'infinity',
426 );
427 }
428 }
429 }
430 }
431 }
432
433 // Get protections for missing titles
434 if ( count( $this->missing ) ) {
435 $this->resetQueryParams();
436 $lb = new LinkBatch( $this->missing );
437 $this->addTables( 'protected_titles' );
438 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
439 $this->addWhere( $lb->constructSet( 'pt', $db ) );
440 $res = $this->select( __METHOD__ );
441 foreach ( $res as $row ) {
442 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
443 'type' => 'create',
444 'level' => $row->pt_create_perm,
445 'expiry' => Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 )
446 );
447 }
448 }
449
450 // Cascading protections
451 $images = $others = array();
452 foreach ( $this->everything as $title ) {
453 if ( $title->getNamespace() == NS_FILE ) {
454 $images[] = $title->getDBkey();
455 } else {
456 $others[] = $title;
457 }
458 }
459
460 if ( count( $others ) ) {
461 // Non-images: check templatelinks
462 $lb = new LinkBatch( $others );
463 $this->resetQueryParams();
464 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
465 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
466 'page_title', 'page_namespace',
467 'tl_title', 'tl_namespace' ) );
468 $this->addWhere( $lb->constructSet( 'tl', $db ) );
469 $this->addWhere( 'pr_page = page_id' );
470 $this->addWhere( 'pr_page = tl_from' );
471 $this->addWhereFld( 'pr_cascade', 1 );
472
473 $res = $this->select( __METHOD__ );
474 foreach ( $res as $row ) {
475 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
476 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
477 'type' => $row->pr_type,
478 'level' => $row->pr_level,
479 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
480 'source' => $source->getPrefixedText()
481 );
482 }
483 }
484
485 if ( count( $images ) ) {
486 // Images: check imagelinks
487 $this->resetQueryParams();
488 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
489 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
490 'page_title', 'page_namespace', 'il_to' ) );
491 $this->addWhere( 'pr_page = page_id' );
492 $this->addWhere( 'pr_page = il_from' );
493 $this->addWhereFld( 'pr_cascade', 1 );
494 $this->addWhereFld( 'il_to', $images );
495
496 $res = $this->select( __METHOD__ );
497 foreach ( $res as $row ) {
498 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
499 $this->protections[NS_FILE][$row->il_to][] = array(
500 'type' => $row->pr_type,
501 'level' => $row->pr_level,
502 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
503 'source' => $source->getPrefixedText()
504 );
505 }
506 }
507 }
508
509 /**
510 * Get talk page IDs (if requested) and subject page IDs (if requested)
511 * and put them in $talkids and $subjectids
512 */
513 private function getTSIDs() {
514 $getTitles = $this->talkids = $this->subjectids = array();
515 $db = $this->getDB();
516 foreach ( $this->everything as $t ) {
517 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
518 if ( $this->fld_subjectid ) {
519 $getTitles[] = $t->getSubjectPage();
520 }
521 } elseif ( $this->fld_talkid ) {
522 $getTitles[] = $t->getTalkPage();
523 }
524 }
525 if ( !count( $getTitles ) ) {
526 return;
527 }
528
529 // Construct a custom WHERE clause that matches
530 // all titles in $getTitles
531 $lb = new LinkBatch( $getTitles );
532 $this->resetQueryParams();
533 $this->addTables( 'page' );
534 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
535 $this->addWhere( $lb->constructSet( 'page', $db ) );
536 $res = $this->select( __METHOD__ );
537 foreach ( $res as $row ) {
538 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
539 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
540 intval( $row->page_id );
541 } else {
542 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
543 intval( $row->page_id );
544 }
545 }
546 }
547
548 /**
549 * Get information about watched status and put it in $this->watched
550 */
551 private function getWatchedInfo() {
552 global $wgUser;
553
554 if ( $wgUser->isAnon() || count( $this->titles ) == 0 ) {
555 return;
556 }
557
558 $this->watched = array();
559 $db = $this->getDB();
560
561 $lb = new LinkBatch( $this->titles );
562
563 $this->resetQueryParams();
564 $this->addTables( array( 'page', 'watchlist' ) );
565 $this->addFields( array( 'page_title', 'page_namespace' ) );
566 $this->addWhere( array(
567 $lb->constructSet( 'page', $db ),
568 'wl_namespace=page_namespace',
569 'wl_title=page_title',
570 'wl_user' => $wgUser->getID()
571 ) );
572
573 $res = $this->select( __METHOD__ );
574
575 foreach ( $res as $row ) {
576 $this->watched[$row->page_namespace][$row->page_title] = true;
577 }
578 }
579
580 public function getAllowedParams() {
581 return array(
582 'prop' => array(
583 ApiBase::PARAM_DFLT => null,
584 ApiBase::PARAM_ISMULTI => true,
585 ApiBase::PARAM_TYPE => array(
586 'protection',
587 'talkid',
588 'watched',
589 'subjectid',
590 'url',
591 'readable',
592 'preload'
593 ) ),
594 'token' => array(
595 ApiBase::PARAM_DFLT => null,
596 ApiBase::PARAM_ISMULTI => true,
597 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
598 ),
599 'continue' => null,
600 );
601 }
602
603 public function getParamDescription() {
604 return array(
605 'prop' => array(
606 'Which additional properties to get:',
607 ' protection - List the protection level of each page',
608 ' talkid - The page ID of the talk page for each non-talk page',
609 ' watched - List the watched status of each page',
610 ' subjectid - The page ID of the parent page for each talk page',
611 ' url - Gives a full URL to the page, and also an edit URL',
612 ' readable - Whether the user can read this page',
613 ' preload - Gives the text returned by EditFormPreloadText',
614 ' displaytitle - Gives the way the page title is actually displayed',
615 ),
616 'token' => 'Request a token to perform a data-modifying action on a page',
617 'continue' => 'When more results are available, use this to continue',
618 );
619 }
620
621 public function getDescription() {
622 return 'Get basic page information such as namespace, title, last touched date, ...';
623 }
624
625 public function getPossibleErrors() {
626 return array_merge( parent::getPossibleErrors(), array(
627 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
628 ) );
629 }
630
631 protected function getExamples() {
632 return array(
633 'api.php?action=query&prop=info&titles=Main%20Page',
634 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
635 );
636 }
637
638 public function getVersion() {
639 return __CLASS__ . ': $Id$';
640 }
641 }