Merge "(bug 12701) Use diff of all unseen revisions in the "new messages" bar."
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
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 * A query module to show basic page information.
29 *
30 * @ingroup API
31 */
32 class ApiQueryInfo extends ApiQueryBase {
33
34 private $fld_protection = false, $fld_talkid = false,
35 $fld_subjectid = false, $fld_url = false,
36 $fld_readable = false, $fld_watched = false, $fld_notificationtimestamp = false,
37 $fld_preload = false, $fld_displaytitle = false;
38
39 private $params, $titles, $missing, $everything, $pageCounter;
40
41 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
42 $pageLatest, $pageLength;
43
44 private $protections, $watched, $notificationtimestamps, $talkids, $subjectids, $displaytitles;
45
46 private $tokenFunctions;
47
48 public function __construct( $query, $moduleName ) {
49 parent::__construct( $query, $moduleName, 'in' );
50 }
51
52 /**
53 * @param $pageSet ApiPageSet
54 * @return void
55 */
56 public function requestExtraData( $pageSet ) {
57 global $wgDisableCounters;
58
59 $pageSet->requestField( 'page_restrictions' );
60 // when resolving redirects, no page will have this field
61 if( !$pageSet->isResolvingRedirects() ) {
62 $pageSet->requestField( 'page_is_redirect' );
63 }
64 $pageSet->requestField( 'page_is_new' );
65 if ( !$wgDisableCounters ) {
66 $pageSet->requestField( 'page_counter' );
67 }
68 $pageSet->requestField( 'page_touched' );
69 $pageSet->requestField( 'page_latest' );
70 $pageSet->requestField( 'page_len' );
71 }
72
73 /**
74 * Get an array mapping token names to their handler functions.
75 * The prototype for a token function is func($pageid, $title)
76 * it should return a token or false (permission denied)
77 * @return array array(tokenname => function)
78 */
79 protected function getTokenFunctions() {
80 // Don't call the hooks twice
81 if ( isset( $this->tokenFunctions ) ) {
82 return $this->tokenFunctions;
83 }
84
85 // If we're in JSON callback mode, no tokens can be obtained
86 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
87 return array();
88 }
89
90 $this->tokenFunctions = array(
91 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
92 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
93 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
94 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
95 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
96 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
97 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
98 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
99 'watch' => array( 'ApiQueryInfo', 'getWatchToken'),
100 );
101 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions ) );
102 return $this->tokenFunctions;
103 }
104
105 static $cachedTokens = array();
106
107 public static function resetTokenCache() {
108 ApiQueryInfo::$cachedTokens = array();
109 }
110
111 public static function getEditToken( $pageid, $title ) {
112 // We could check for $title->userCan('edit') here,
113 // but that's too expensive for this purpose
114 // and would break caching
115 global $wgUser;
116 if ( !$wgUser->isAllowed( 'edit' ) ) {
117 return false;
118 }
119
120 // The token is always the same, let's exploit that
121 if ( !isset( ApiQueryInfo::$cachedTokens[ 'edit' ] ) ) {
122 ApiQueryInfo::$cachedTokens[ 'edit' ] = $wgUser->getEditToken();
123 }
124
125 return ApiQueryInfo::$cachedTokens[ 'edit' ];
126 }
127
128 public static function getDeleteToken( $pageid, $title ) {
129 global $wgUser;
130 if ( !$wgUser->isAllowed( 'delete' ) ) {
131 return false;
132 }
133
134 // The token is always the same, let's exploit that
135 if ( !isset( ApiQueryInfo::$cachedTokens[ 'delete' ] ) ) {
136 ApiQueryInfo::$cachedTokens[ 'delete' ] = $wgUser->getEditToken();
137 }
138
139 return ApiQueryInfo::$cachedTokens[ 'delete' ];
140 }
141
142 public static function getProtectToken( $pageid, $title ) {
143 global $wgUser;
144 if ( !$wgUser->isAllowed( 'protect' ) ) {
145 return false;
146 }
147
148 // The token is always the same, let's exploit that
149 if ( !isset( ApiQueryInfo::$cachedTokens[ 'protect' ] ) ) {
150 ApiQueryInfo::$cachedTokens[ 'protect' ] = $wgUser->getEditToken();
151 }
152
153 return ApiQueryInfo::$cachedTokens[ 'protect' ];
154 }
155
156 public static function getMoveToken( $pageid, $title ) {
157 global $wgUser;
158 if ( !$wgUser->isAllowed( 'move' ) ) {
159 return false;
160 }
161
162 // The token is always the same, let's exploit that
163 if ( !isset( ApiQueryInfo::$cachedTokens[ 'move' ] ) ) {
164 ApiQueryInfo::$cachedTokens[ 'move' ] = $wgUser->getEditToken();
165 }
166
167 return ApiQueryInfo::$cachedTokens[ 'move' ];
168 }
169
170 public static function getBlockToken( $pageid, $title ) {
171 global $wgUser;
172 if ( !$wgUser->isAllowed( 'block' ) ) {
173 return false;
174 }
175
176 // The token is always the same, let's exploit that
177 if ( !isset( ApiQueryInfo::$cachedTokens[ 'block' ] ) ) {
178 ApiQueryInfo::$cachedTokens[ 'block' ] = $wgUser->getEditToken();
179 }
180
181 return ApiQueryInfo::$cachedTokens[ 'block' ];
182 }
183
184 public static function getUnblockToken( $pageid, $title ) {
185 // Currently, this is exactly the same as the block token
186 return self::getBlockToken( $pageid, $title );
187 }
188
189 public static function getEmailToken( $pageid, $title ) {
190 global $wgUser;
191 if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser() ) {
192 return false;
193 }
194
195 // The token is always the same, let's exploit that
196 if ( !isset( ApiQueryInfo::$cachedTokens[ 'email' ] ) ) {
197 ApiQueryInfo::$cachedTokens[ 'email' ] = $wgUser->getEditToken();
198 }
199
200 return ApiQueryInfo::$cachedTokens[ 'email' ];
201 }
202
203 public static function getImportToken( $pageid, $title ) {
204 global $wgUser;
205 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
206 return false;
207 }
208
209 // The token is always the same, let's exploit that
210 if ( !isset( ApiQueryInfo::$cachedTokens[ 'import' ] ) ) {
211 ApiQueryInfo::$cachedTokens[ 'import' ] = $wgUser->getEditToken();
212 }
213
214 return ApiQueryInfo::$cachedTokens[ 'import' ];
215 }
216
217 public static function getWatchToken( $pageid, $title ) {
218 global $wgUser;
219 if ( !$wgUser->isLoggedIn() ) {
220 return false;
221 }
222
223 // The token is always the same, let's exploit that
224 if ( !isset( ApiQueryInfo::$cachedTokens[ 'watch' ] ) ) {
225 ApiQueryInfo::$cachedTokens[ 'watch' ] = $wgUser->getEditToken( 'watch' );
226 }
227
228 return ApiQueryInfo::$cachedTokens[ 'watch' ];
229 }
230
231 public static function getOptionsToken( $pageid, $title ) {
232 global $wgUser;
233 if ( !$wgUser->isLoggedIn() ) {
234 return false;
235 }
236
237 // The token is always the same, let's exploit that
238 if ( !isset( ApiQueryInfo::$cachedTokens[ 'options' ] ) ) {
239 ApiQueryInfo::$cachedTokens[ 'options' ] = $wgUser->getEditToken();
240 }
241
242 return ApiQueryInfo::$cachedTokens[ 'options' ];
243 }
244
245 public function execute() {
246 $this->params = $this->extractRequestParams();
247 if ( !is_null( $this->params['prop'] ) ) {
248 $prop = array_flip( $this->params['prop'] );
249 $this->fld_protection = isset( $prop['protection'] );
250 $this->fld_watched = isset( $prop['watched'] );
251 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
252 $this->fld_talkid = isset( $prop['talkid'] );
253 $this->fld_subjectid = isset( $prop['subjectid'] );
254 $this->fld_url = isset( $prop['url'] );
255 $this->fld_readable = isset( $prop['readable'] );
256 $this->fld_preload = isset( $prop['preload'] );
257 $this->fld_displaytitle = isset( $prop['displaytitle'] );
258 }
259
260 $pageSet = $this->getPageSet();
261 $this->titles = $pageSet->getGoodTitles();
262 $this->missing = $pageSet->getMissingTitles();
263 $this->everything = $this->titles + $this->missing;
264 $result = $this->getResult();
265
266 uasort( $this->everything, array( 'Title', 'compare' ) );
267 if ( !is_null( $this->params['continue'] ) ) {
268 // Throw away any titles we're gonna skip so they don't
269 // clutter queries
270 $cont = explode( '|', $this->params['continue'] );
271 if ( count( $cont ) != 2 ) {
272 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
273 'value returned by the previous query', '_badcontinue' );
274 }
275 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
276 foreach ( $this->everything as $pageid => $title ) {
277 if ( Title::compare( $title, $conttitle ) >= 0 ) {
278 break;
279 }
280 unset( $this->titles[$pageid] );
281 unset( $this->missing[$pageid] );
282 unset( $this->everything[$pageid] );
283 }
284 }
285
286 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
287 // when resolving redirects, no page will have this field
288 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
289 ? $pageSet->getCustomField( 'page_is_redirect' )
290 : array();
291 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
292
293 global $wgDisableCounters;
294
295 if ( !$wgDisableCounters ) {
296 $this->pageCounter = $pageSet->getCustomField( 'page_counter' );
297 }
298 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
299 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
300 $this->pageLength = $pageSet->getCustomField( 'page_len' );
301
302 // Get protection info if requested
303 if ( $this->fld_protection ) {
304 $this->getProtectionInfo();
305 }
306
307 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
308 $this->getWatchedInfo();
309 }
310
311 // Run the talkid/subjectid query if requested
312 if ( $this->fld_talkid || $this->fld_subjectid ) {
313 $this->getTSIDs();
314 }
315
316 if ( $this->fld_displaytitle ) {
317 $this->getDisplayTitle();
318 }
319
320 foreach ( $this->everything as $pageid => $title ) {
321 $pageInfo = $this->extractPageInfo( $pageid, $title );
322 $fit = $result->addValue( array(
323 'query',
324 'pages'
325 ), $pageid, $pageInfo );
326 if ( !$fit ) {
327 $this->setContinueEnumParameter( 'continue',
328 $title->getNamespace() . '|' .
329 $title->getText() );
330 break;
331 }
332 }
333 }
334
335 /**
336 * Get a result array with information about a title
337 * @param $pageid int Page ID (negative for missing titles)
338 * @param $title Title object
339 * @return array
340 */
341 private function extractPageInfo( $pageid, $title ) {
342 $pageInfo = array();
343 $titleExists = $pageid > 0; //$title->exists() needs pageid, which is not set for all title objects
344 $ns = $title->getNamespace();
345 $dbkey = $title->getDBkey();
346 if ( $titleExists ) {
347 global $wgDisableCounters;
348
349 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
350 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
351 $pageInfo['counter'] = $wgDisableCounters
352 ? ""
353 : intval( $this->pageCounter[$pageid] );
354 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
355
356 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
357 $pageInfo['redirect'] = '';
358 }
359 if ( $this->pageIsNew[$pageid] ) {
360 $pageInfo['new'] = '';
361 }
362 }
363
364 if ( !is_null( $this->params['token'] ) ) {
365 $tokenFunctions = $this->getTokenFunctions();
366 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
367 foreach ( $this->params['token'] as $t ) {
368 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
369 if ( $val === false ) {
370 $this->setWarning( "Action '$t' is not allowed for the current user" );
371 } else {
372 $pageInfo[$t . 'token'] = $val;
373 }
374 }
375 }
376
377 if ( $this->fld_protection ) {
378 $pageInfo['protection'] = array();
379 if ( isset( $this->protections[$ns][$dbkey] ) ) {
380 $pageInfo['protection'] =
381 $this->protections[$ns][$dbkey];
382 }
383 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
384 }
385
386 if ( $this->fld_watched && isset( $this->watched[$ns][$dbkey] ) ) {
387 $pageInfo['watched'] = '';
388 }
389
390 if ( $this->fld_notificationtimestamp ) {
391 $pageInfo['notificationtimestamp'] = '';
392 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
393 $pageInfo['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
394 }
395 }
396
397 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
398 $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
399 }
400
401 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
402 $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
403 }
404
405 if ( $this->fld_url ) {
406 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
407 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
408 }
409 if ( $this->fld_readable && $title->userCan( 'read' ) ) {
410 $pageInfo['readable'] = '';
411 }
412
413 if ( $this->fld_preload ) {
414 if ( $titleExists ) {
415 $pageInfo['preload'] = '';
416 } else {
417 $text = null;
418 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
419
420 $pageInfo['preload'] = $text;
421 }
422 }
423
424 if ( $this->fld_displaytitle ) {
425 if ( isset( $this->displaytitles[$pageid] ) ) {
426 $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
427 } else {
428 $pageInfo['displaytitle'] = $title->getPrefixedText();
429 }
430 }
431
432 return $pageInfo;
433 }
434
435 /**
436 * Get information about protections and put it in $protections
437 */
438 private function getProtectionInfo() {
439 global $wgContLang;
440 $this->protections = array();
441 $db = $this->getDB();
442
443 // Get normal protections for existing titles
444 if ( count( $this->titles ) ) {
445 $this->resetQueryParams();
446 $this->addTables( 'page_restrictions' );
447 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
448 'pr_expiry', 'pr_cascade' ) );
449 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
450
451 $res = $this->select( __METHOD__ );
452 foreach ( $res as $row ) {
453 $title = $this->titles[$row->pr_page];
454 $a = array(
455 'type' => $row->pr_type,
456 'level' => $row->pr_level,
457 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
458 );
459 if ( $row->pr_cascade ) {
460 $a['cascade'] = '';
461 }
462 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
463 }
464 // Also check old restrictions
465 foreach( $this->titles as $pageId => $title ) {
466 if ( $this->pageRestrictions[$pageId] ) {
467 $namespace = $title->getNamespace();
468 $dbKey = $title->getDBkey();
469 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
470 foreach ( $restrictions as $restrict ) {
471 $temp = explode( '=', trim( $restrict ) );
472 if ( count( $temp ) == 1 ) {
473 // old old format should be treated as edit/move restriction
474 $restriction = trim( $temp[0] );
475
476 if ( $restriction == '' ) {
477 continue;
478 }
479 $this->protections[$namespace][$dbKey][] = array(
480 'type' => 'edit',
481 'level' => $restriction,
482 'expiry' => 'infinity',
483 );
484 $this->protections[$namespace][$dbKey][] = array(
485 'type' => 'move',
486 'level' => $restriction,
487 'expiry' => 'infinity',
488 );
489 } else {
490 $restriction = trim( $temp[1] );
491 if ( $restriction == '' ) {
492 continue;
493 }
494 $this->protections[$namespace][$dbKey][] = array(
495 'type' => $temp[0],
496 'level' => $restriction,
497 'expiry' => 'infinity',
498 );
499 }
500 }
501 }
502 }
503 }
504
505 // Get protections for missing titles
506 if ( count( $this->missing ) ) {
507 $this->resetQueryParams();
508 $lb = new LinkBatch( $this->missing );
509 $this->addTables( 'protected_titles' );
510 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
511 $this->addWhere( $lb->constructSet( 'pt', $db ) );
512 $res = $this->select( __METHOD__ );
513 foreach ( $res as $row ) {
514 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
515 'type' => 'create',
516 'level' => $row->pt_create_perm,
517 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
518 );
519 }
520 }
521
522 // Cascading protections
523 $images = $others = array();
524 foreach ( $this->everything as $title ) {
525 if ( $title->getNamespace() == NS_FILE ) {
526 $images[] = $title->getDBkey();
527 } else {
528 $others[] = $title;
529 }
530 }
531
532 if ( count( $others ) ) {
533 // Non-images: check templatelinks
534 $lb = new LinkBatch( $others );
535 $this->resetQueryParams();
536 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
537 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
538 'page_title', 'page_namespace',
539 'tl_title', 'tl_namespace' ) );
540 $this->addWhere( $lb->constructSet( 'tl', $db ) );
541 $this->addWhere( 'pr_page = page_id' );
542 $this->addWhere( 'pr_page = tl_from' );
543 $this->addWhereFld( 'pr_cascade', 1 );
544
545 $res = $this->select( __METHOD__ );
546 foreach ( $res as $row ) {
547 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
548 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
549 'type' => $row->pr_type,
550 'level' => $row->pr_level,
551 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
552 'source' => $source->getPrefixedText()
553 );
554 }
555 }
556
557 if ( count( $images ) ) {
558 // Images: check imagelinks
559 $this->resetQueryParams();
560 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
561 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
562 'page_title', 'page_namespace', 'il_to' ) );
563 $this->addWhere( 'pr_page = page_id' );
564 $this->addWhere( 'pr_page = il_from' );
565 $this->addWhereFld( 'pr_cascade', 1 );
566 $this->addWhereFld( 'il_to', $images );
567
568 $res = $this->select( __METHOD__ );
569 foreach ( $res as $row ) {
570 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
571 $this->protections[NS_FILE][$row->il_to][] = array(
572 'type' => $row->pr_type,
573 'level' => $row->pr_level,
574 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
575 'source' => $source->getPrefixedText()
576 );
577 }
578 }
579 }
580
581 /**
582 * Get talk page IDs (if requested) and subject page IDs (if requested)
583 * and put them in $talkids and $subjectids
584 */
585 private function getTSIDs() {
586 $getTitles = $this->talkids = $this->subjectids = array();
587
588 foreach ( $this->everything as $t ) {
589 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
590 if ( $this->fld_subjectid ) {
591 $getTitles[] = $t->getSubjectPage();
592 }
593 } elseif ( $this->fld_talkid ) {
594 $getTitles[] = $t->getTalkPage();
595 }
596 }
597 if ( !count( $getTitles ) ) {
598 return;
599 }
600
601 $db = $this->getDB();
602
603 // Construct a custom WHERE clause that matches
604 // all titles in $getTitles
605 $lb = new LinkBatch( $getTitles );
606 $this->resetQueryParams();
607 $this->addTables( 'page' );
608 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
609 $this->addWhere( $lb->constructSet( 'page', $db ) );
610 $res = $this->select( __METHOD__ );
611 foreach ( $res as $row ) {
612 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
613 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
614 intval( $row->page_id );
615 } else {
616 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
617 intval( $row->page_id );
618 }
619 }
620 }
621
622 private function getDisplayTitle() {
623 $this->displaytitles = array();
624
625 $pageIds = array_keys( $this->titles );
626
627 if ( !count( $pageIds ) ) {
628 return;
629 }
630
631 $this->resetQueryParams();
632 $this->addTables( 'page_props' );
633 $this->addFields( array( 'pp_page', 'pp_value' ) );
634 $this->addWhereFld( 'pp_page', $pageIds );
635 $this->addWhereFld( 'pp_propname', 'displaytitle' );
636 $res = $this->select( __METHOD__ );
637
638 foreach ( $res as $row ) {
639 $this->displaytitles[$row->pp_page] = $row->pp_value;
640 }
641 }
642
643 /**
644 * Get information about watched status and put it in $this->watched
645 * and $this->notificationtimestamps
646 */
647 private function getWatchedInfo() {
648 $user = $this->getUser();
649
650 if ( $user->isAnon() || count( $this->everything ) == 0 ) {
651 return;
652 }
653
654 $this->watched = array();
655 $this->notificationtimestamps = array();
656 $db = $this->getDB();
657
658 $lb = new LinkBatch( $this->everything );
659
660 $this->resetQueryParams();
661 $this->addTables( array( 'watchlist' ) );
662 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
663 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
664 $this->addWhere( array(
665 $lb->constructSet( 'wl', $db ),
666 'wl_user' => $user->getID()
667 ) );
668
669 $res = $this->select( __METHOD__ );
670
671 foreach ( $res as $row ) {
672 if ( $this->fld_watched ) {
673 $this->watched[$row->wl_namespace][$row->wl_title] = true;
674 }
675 if ( $this->fld_notificationtimestamp ) {
676 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
677 }
678 }
679 }
680
681 public function getCacheMode( $params ) {
682 $publicProps = array(
683 'protection',
684 'talkid',
685 'subjectid',
686 'url',
687 'preload',
688 'displaytitle',
689 );
690 if ( !is_null( $params['prop'] ) ) {
691 foreach ( $params['prop'] as $prop ) {
692 if ( !in_array( $prop, $publicProps ) ) {
693 return 'private';
694 }
695 }
696 }
697 if ( !is_null( $params['token'] ) ) {
698 return 'private';
699 }
700 return 'public';
701 }
702
703 public function getAllowedParams() {
704 return array(
705 'prop' => array(
706 ApiBase::PARAM_DFLT => null,
707 ApiBase::PARAM_ISMULTI => true,
708 ApiBase::PARAM_TYPE => array(
709 'protection',
710 'talkid',
711 'watched', # private
712 'notificationtimestamp', # private
713 'subjectid',
714 'url',
715 'readable', # private
716 'preload',
717 'displaytitle',
718 // If you add more properties here, please consider whether they
719 // need to be added to getCacheMode()
720 ) ),
721 'token' => array(
722 ApiBase::PARAM_DFLT => null,
723 ApiBase::PARAM_ISMULTI => true,
724 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
725 ),
726 'continue' => null,
727 );
728 }
729
730 public function getParamDescription() {
731 return array(
732 'prop' => array(
733 'Which additional properties to get:',
734 ' protection - List the protection level of each page',
735 ' talkid - The page ID of the talk page for each non-talk page',
736 ' watched - List the watched status of each page',
737 ' notificationtimestamp - The watchlist notification timestamp of each page',
738 ' subjectid - The page ID of the parent page for each talk page',
739 ' url - Gives a full URL to the page, and also an edit URL',
740 ' readable - Whether the user can read this page',
741 ' preload - Gives the text returned by EditFormPreloadText',
742 ' displaytitle - Gives the way the page title is actually displayed',
743 ),
744 'token' => 'Request a token to perform a data-modifying action on a page',
745 'continue' => 'When more results are available, use this to continue',
746 );
747 }
748
749 public function getResultProperties() {
750 $props = array(
751 ApiBase::PROP_LIST => false,
752 '' => array(
753 'touched' => 'timestamp',
754 'lastrevid' => 'integer',
755 'counter' => array(
756 ApiBase::PROP_TYPE => 'integer',
757 ApiBase::PROP_NULLABLE => true
758 ),
759 'length' => 'integer',
760 'redirect' => 'boolean',
761 'new' => 'boolean',
762 'starttimestamp' => array(
763 ApiBase::PROP_TYPE => 'timestamp',
764 ApiBase::PROP_NULLABLE => true
765 )
766 ),
767 'watched' => array(
768 'watched' => 'boolean'
769 ),
770 'notificationtimestamp' => array(
771 'notificationtimestamp' => array(
772 ApiBase::PROP_TYPE => 'timestamp',
773 ApiBase::PROP_NULLABLE => true
774 )
775 ),
776 'talkid' => array(
777 'talkid' => array(
778 ApiBase::PROP_TYPE => 'integer',
779 ApiBase::PROP_NULLABLE => true
780 )
781 ),
782 'subjectid' => array(
783 'subjectid' => array(
784 ApiBase::PROP_TYPE => 'integer',
785 ApiBase::PROP_NULLABLE => true
786 )
787 ),
788 'url' => array(
789 'fullurl' => 'string',
790 'editurl' => 'string'
791 ),
792 'readable' => array(
793 'readable' => 'boolean'
794 ),
795 'preload' => array(
796 'preload' => 'string'
797 ),
798 'displaytitle' => array(
799 'displaytitle' => 'string'
800 )
801 );
802
803 self::addTokenProperties( $props, $this->getTokenFunctions() );
804
805 return $props;
806 }
807
808 public function getDescription() {
809 return 'Get basic page information such as namespace, title, last touched date, ...';
810 }
811
812 public function getPossibleErrors() {
813 return array_merge( parent::getPossibleErrors(), array(
814 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
815 ) );
816 }
817
818 public function getExamples() {
819 return array(
820 'api.php?action=query&prop=info&titles=Main%20Page',
821 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
822 );
823 }
824
825 public function getHelpUrls() {
826 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
827 }
828
829 public function getVersion() {
830 return __CLASS__ . ': $Id$';
831 }
832 }