raise timeout for CdbTest::testCdb()
[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 $this->dieContinueUsageIf( count( $cont ) != 2 );
272 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
273 foreach ( $this->everything as $pageid => $title ) {
274 if ( Title::compare( $title, $conttitle ) >= 0 ) {
275 break;
276 }
277 unset( $this->titles[$pageid] );
278 unset( $this->missing[$pageid] );
279 unset( $this->everything[$pageid] );
280 }
281 }
282
283 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
284 // when resolving redirects, no page will have this field
285 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
286 ? $pageSet->getCustomField( 'page_is_redirect' )
287 : array();
288 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
289
290 global $wgDisableCounters;
291
292 if ( !$wgDisableCounters ) {
293 $this->pageCounter = $pageSet->getCustomField( 'page_counter' );
294 }
295 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
296 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
297 $this->pageLength = $pageSet->getCustomField( 'page_len' );
298
299 // Get protection info if requested
300 if ( $this->fld_protection ) {
301 $this->getProtectionInfo();
302 }
303
304 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
305 $this->getWatchedInfo();
306 }
307
308 // Run the talkid/subjectid query if requested
309 if ( $this->fld_talkid || $this->fld_subjectid ) {
310 $this->getTSIDs();
311 }
312
313 if ( $this->fld_displaytitle ) {
314 $this->getDisplayTitle();
315 }
316
317 foreach ( $this->everything as $pageid => $title ) {
318 $pageInfo = $this->extractPageInfo( $pageid, $title );
319 $fit = $result->addValue( array(
320 'query',
321 'pages'
322 ), $pageid, $pageInfo );
323 if ( !$fit ) {
324 $this->setContinueEnumParameter( 'continue',
325 $title->getNamespace() . '|' .
326 $title->getText() );
327 break;
328 }
329 }
330 }
331
332 /**
333 * Get a result array with information about a title
334 * @param $pageid int Page ID (negative for missing titles)
335 * @param $title Title object
336 * @return array
337 */
338 private function extractPageInfo( $pageid, $title ) {
339 $pageInfo = array();
340 $titleExists = $pageid > 0; //$title->exists() needs pageid, which is not set for all title objects
341 $ns = $title->getNamespace();
342 $dbkey = $title->getDBkey();
343 if ( $titleExists ) {
344 global $wgDisableCounters;
345
346 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
347 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
348 $pageInfo['counter'] = $wgDisableCounters
349 ? ''
350 : intval( $this->pageCounter[$pageid] );
351 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
352
353 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
354 $pageInfo['redirect'] = '';
355 }
356 if ( $this->pageIsNew[$pageid] ) {
357 $pageInfo['new'] = '';
358 }
359 }
360
361 if ( !is_null( $this->params['token'] ) ) {
362 $tokenFunctions = $this->getTokenFunctions();
363 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
364 foreach ( $this->params['token'] as $t ) {
365 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
366 if ( $val === false ) {
367 $this->setWarning( "Action '$t' is not allowed for the current user" );
368 } else {
369 $pageInfo[$t . 'token'] = $val;
370 }
371 }
372 }
373
374 if ( $this->fld_protection ) {
375 $pageInfo['protection'] = array();
376 if ( isset( $this->protections[$ns][$dbkey] ) ) {
377 $pageInfo['protection'] =
378 $this->protections[$ns][$dbkey];
379 }
380 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
381 }
382
383 if ( $this->fld_watched && isset( $this->watched[$ns][$dbkey] ) ) {
384 $pageInfo['watched'] = '';
385 }
386
387 if ( $this->fld_notificationtimestamp ) {
388 $pageInfo['notificationtimestamp'] = '';
389 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
390 $pageInfo['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
391 }
392 }
393
394 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
395 $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
396 }
397
398 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
399 $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
400 }
401
402 if ( $this->fld_url ) {
403 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
404 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
405 }
406 if ( $this->fld_readable && $title->userCan( 'read', $this->getUser() ) ) {
407 $pageInfo['readable'] = '';
408 }
409
410 if ( $this->fld_preload ) {
411 if ( $titleExists ) {
412 $pageInfo['preload'] = '';
413 } else {
414 $text = null;
415 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
416
417 $pageInfo['preload'] = $text;
418 }
419 }
420
421 if ( $this->fld_displaytitle ) {
422 if ( isset( $this->displaytitles[$pageid] ) ) {
423 $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
424 } else {
425 $pageInfo['displaytitle'] = $title->getPrefixedText();
426 }
427 }
428
429 return $pageInfo;
430 }
431
432 /**
433 * Get information about protections and put it in $protections
434 */
435 private function getProtectionInfo() {
436 global $wgContLang;
437 $this->protections = array();
438 $db = $this->getDB();
439
440 // Get normal protections for existing titles
441 if ( count( $this->titles ) ) {
442 $this->resetQueryParams();
443 $this->addTables( 'page_restrictions' );
444 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
445 'pr_expiry', 'pr_cascade' ) );
446 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
447
448 $res = $this->select( __METHOD__ );
449 foreach ( $res as $row ) {
450 $title = $this->titles[$row->pr_page];
451 $a = array(
452 'type' => $row->pr_type,
453 'level' => $row->pr_level,
454 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
455 );
456 if ( $row->pr_cascade ) {
457 $a['cascade'] = '';
458 }
459 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
460 }
461 // Also check old restrictions
462 foreach( $this->titles as $pageId => $title ) {
463 if ( $this->pageRestrictions[$pageId] ) {
464 $namespace = $title->getNamespace();
465 $dbKey = $title->getDBkey();
466 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
467 foreach ( $restrictions as $restrict ) {
468 $temp = explode( '=', trim( $restrict ) );
469 if ( count( $temp ) == 1 ) {
470 // old old format should be treated as edit/move restriction
471 $restriction = trim( $temp[0] );
472
473 if ( $restriction == '' ) {
474 continue;
475 }
476 $this->protections[$namespace][$dbKey][] = array(
477 'type' => 'edit',
478 'level' => $restriction,
479 'expiry' => 'infinity',
480 );
481 $this->protections[$namespace][$dbKey][] = array(
482 'type' => 'move',
483 'level' => $restriction,
484 'expiry' => 'infinity',
485 );
486 } else {
487 $restriction = trim( $temp[1] );
488 if ( $restriction == '' ) {
489 continue;
490 }
491 $this->protections[$namespace][$dbKey][] = array(
492 'type' => $temp[0],
493 'level' => $restriction,
494 'expiry' => 'infinity',
495 );
496 }
497 }
498 }
499 }
500 }
501
502 // Get protections for missing titles
503 if ( count( $this->missing ) ) {
504 $this->resetQueryParams();
505 $lb = new LinkBatch( $this->missing );
506 $this->addTables( 'protected_titles' );
507 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
508 $this->addWhere( $lb->constructSet( 'pt', $db ) );
509 $res = $this->select( __METHOD__ );
510 foreach ( $res as $row ) {
511 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
512 'type' => 'create',
513 'level' => $row->pt_create_perm,
514 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
515 );
516 }
517 }
518
519 // Cascading protections
520 $images = $others = array();
521 foreach ( $this->everything as $title ) {
522 if ( $title->getNamespace() == NS_FILE ) {
523 $images[] = $title->getDBkey();
524 } else {
525 $others[] = $title;
526 }
527 }
528
529 if ( count( $others ) ) {
530 // Non-images: check templatelinks
531 $lb = new LinkBatch( $others );
532 $this->resetQueryParams();
533 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
534 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
535 'page_title', 'page_namespace',
536 'tl_title', 'tl_namespace' ) );
537 $this->addWhere( $lb->constructSet( 'tl', $db ) );
538 $this->addWhere( 'pr_page = page_id' );
539 $this->addWhere( 'pr_page = tl_from' );
540 $this->addWhereFld( 'pr_cascade', 1 );
541
542 $res = $this->select( __METHOD__ );
543 foreach ( $res as $row ) {
544 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
545 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
546 'type' => $row->pr_type,
547 'level' => $row->pr_level,
548 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
549 'source' => $source->getPrefixedText()
550 );
551 }
552 }
553
554 if ( count( $images ) ) {
555 // Images: check imagelinks
556 $this->resetQueryParams();
557 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
558 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
559 'page_title', 'page_namespace', 'il_to' ) );
560 $this->addWhere( 'pr_page = page_id' );
561 $this->addWhere( 'pr_page = il_from' );
562 $this->addWhereFld( 'pr_cascade', 1 );
563 $this->addWhereFld( 'il_to', $images );
564
565 $res = $this->select( __METHOD__ );
566 foreach ( $res as $row ) {
567 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
568 $this->protections[NS_FILE][$row->il_to][] = array(
569 'type' => $row->pr_type,
570 'level' => $row->pr_level,
571 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
572 'source' => $source->getPrefixedText()
573 );
574 }
575 }
576 }
577
578 /**
579 * Get talk page IDs (if requested) and subject page IDs (if requested)
580 * and put them in $talkids and $subjectids
581 */
582 private function getTSIDs() {
583 $getTitles = $this->talkids = $this->subjectids = array();
584
585 foreach ( $this->everything as $t ) {
586 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
587 if ( $this->fld_subjectid ) {
588 $getTitles[] = $t->getSubjectPage();
589 }
590 } elseif ( $this->fld_talkid ) {
591 $getTitles[] = $t->getTalkPage();
592 }
593 }
594 if ( !count( $getTitles ) ) {
595 return;
596 }
597
598 $db = $this->getDB();
599
600 // Construct a custom WHERE clause that matches
601 // all titles in $getTitles
602 $lb = new LinkBatch( $getTitles );
603 $this->resetQueryParams();
604 $this->addTables( 'page' );
605 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
606 $this->addWhere( $lb->constructSet( 'page', $db ) );
607 $res = $this->select( __METHOD__ );
608 foreach ( $res as $row ) {
609 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
610 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
611 intval( $row->page_id );
612 } else {
613 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
614 intval( $row->page_id );
615 }
616 }
617 }
618
619 private function getDisplayTitle() {
620 $this->displaytitles = array();
621
622 $pageIds = array_keys( $this->titles );
623
624 if ( !count( $pageIds ) ) {
625 return;
626 }
627
628 $this->resetQueryParams();
629 $this->addTables( 'page_props' );
630 $this->addFields( array( 'pp_page', 'pp_value' ) );
631 $this->addWhereFld( 'pp_page', $pageIds );
632 $this->addWhereFld( 'pp_propname', 'displaytitle' );
633 $res = $this->select( __METHOD__ );
634
635 foreach ( $res as $row ) {
636 $this->displaytitles[$row->pp_page] = $row->pp_value;
637 }
638 }
639
640 /**
641 * Get information about watched status and put it in $this->watched
642 * and $this->notificationtimestamps
643 */
644 private function getWatchedInfo() {
645 $user = $this->getUser();
646
647 if ( $user->isAnon() || count( $this->everything ) == 0 ) {
648 return;
649 }
650
651 $this->watched = array();
652 $this->notificationtimestamps = array();
653 $db = $this->getDB();
654
655 $lb = new LinkBatch( $this->everything );
656
657 $this->resetQueryParams();
658 $this->addTables( array( 'watchlist' ) );
659 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
660 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
661 $this->addWhere( array(
662 $lb->constructSet( 'wl', $db ),
663 'wl_user' => $user->getID()
664 ) );
665
666 $res = $this->select( __METHOD__ );
667
668 foreach ( $res as $row ) {
669 if ( $this->fld_watched ) {
670 $this->watched[$row->wl_namespace][$row->wl_title] = true;
671 }
672 if ( $this->fld_notificationtimestamp ) {
673 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
674 }
675 }
676 }
677
678 public function getCacheMode( $params ) {
679 $publicProps = array(
680 'protection',
681 'talkid',
682 'subjectid',
683 'url',
684 'preload',
685 'displaytitle',
686 );
687 if ( !is_null( $params['prop'] ) ) {
688 foreach ( $params['prop'] as $prop ) {
689 if ( !in_array( $prop, $publicProps ) ) {
690 return 'private';
691 }
692 }
693 }
694 if ( !is_null( $params['token'] ) ) {
695 return 'private';
696 }
697 return 'public';
698 }
699
700 public function getAllowedParams() {
701 return array(
702 'prop' => array(
703 ApiBase::PARAM_DFLT => null,
704 ApiBase::PARAM_ISMULTI => true,
705 ApiBase::PARAM_TYPE => array(
706 'protection',
707 'talkid',
708 'watched', # private
709 'notificationtimestamp', # private
710 'subjectid',
711 'url',
712 'readable', # private
713 'preload',
714 'displaytitle',
715 // If you add more properties here, please consider whether they
716 // need to be added to getCacheMode()
717 ) ),
718 'token' => array(
719 ApiBase::PARAM_DFLT => null,
720 ApiBase::PARAM_ISMULTI => true,
721 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
722 ),
723 'continue' => null,
724 );
725 }
726
727 public function getParamDescription() {
728 return array(
729 'prop' => array(
730 'Which additional properties to get:',
731 ' protection - List the protection level of each page',
732 ' talkid - The page ID of the talk page for each non-talk page',
733 ' watched - List the watched status of each page',
734 ' notificationtimestamp - The watchlist notification timestamp of each page',
735 ' subjectid - The page ID of the parent page for each talk page',
736 ' url - Gives a full URL to the page, and also an edit URL',
737 ' readable - Whether the user can read this page',
738 ' preload - Gives the text returned by EditFormPreloadText',
739 ' displaytitle - Gives the way the page title is actually displayed',
740 ),
741 'token' => 'Request a token to perform a data-modifying action on a page',
742 'continue' => 'When more results are available, use this to continue',
743 );
744 }
745
746 public function getResultProperties() {
747 $props = array(
748 ApiBase::PROP_LIST => false,
749 '' => array(
750 'touched' => 'timestamp',
751 'lastrevid' => 'integer',
752 'counter' => array(
753 ApiBase::PROP_TYPE => 'integer',
754 ApiBase::PROP_NULLABLE => true
755 ),
756 'length' => 'integer',
757 'redirect' => 'boolean',
758 'new' => 'boolean',
759 'starttimestamp' => array(
760 ApiBase::PROP_TYPE => 'timestamp',
761 ApiBase::PROP_NULLABLE => true
762 )
763 ),
764 'watched' => array(
765 'watched' => 'boolean'
766 ),
767 'notificationtimestamp' => array(
768 'notificationtimestamp' => array(
769 ApiBase::PROP_TYPE => 'timestamp',
770 ApiBase::PROP_NULLABLE => true
771 )
772 ),
773 'talkid' => array(
774 'talkid' => array(
775 ApiBase::PROP_TYPE => 'integer',
776 ApiBase::PROP_NULLABLE => true
777 )
778 ),
779 'subjectid' => array(
780 'subjectid' => array(
781 ApiBase::PROP_TYPE => 'integer',
782 ApiBase::PROP_NULLABLE => true
783 )
784 ),
785 'url' => array(
786 'fullurl' => 'string',
787 'editurl' => 'string'
788 ),
789 'readable' => array(
790 'readable' => 'boolean'
791 ),
792 'preload' => array(
793 'preload' => 'string'
794 ),
795 'displaytitle' => array(
796 'displaytitle' => 'string'
797 )
798 );
799
800 self::addTokenProperties( $props, $this->getTokenFunctions() );
801
802 return $props;
803 }
804
805 public function getDescription() {
806 return 'Get basic page information such as namespace, title, last touched date, ...';
807 }
808
809 public function getExamples() {
810 return array(
811 'api.php?action=query&prop=info&titles=Main%20Page',
812 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
813 );
814 }
815
816 public function getHelpUrls() {
817 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
818 }
819 }