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