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