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