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