be1de9385b0bfb89cc4c0908c61b6ebc8138a0ed
[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,
46 $talkids, $subjectids, $displaytitles;
47 private $showZeroWatchers = false;
48
49 private $tokenFunctions;
50
51 public function __construct( ApiQuery $query, $moduleName ) {
52 parent::__construct( $query, $moduleName, 'in' );
53 }
54
55 /**
56 * @param ApiPageSet $pageSet
57 * @return void
58 */
59 public function requestExtraData( $pageSet ) {
60 $pageSet->requestField( 'page_restrictions' );
61 // when resolving redirects, no page will have this field
62 if ( !$pageSet->isResolvingRedirects() ) {
63 $pageSet->requestField( 'page_is_redirect' );
64 }
65 $pageSet->requestField( 'page_is_new' );
66 $config = $this->getConfig();
67 if ( !$config->get( 'DisableCounters' ) ) {
68 $pageSet->requestField( 'page_counter' );
69 }
70 $pageSet->requestField( 'page_touched' );
71 $pageSet->requestField( 'page_latest' );
72 $pageSet->requestField( 'page_len' );
73 if ( $config->get( 'ContentHandlerUseDB' ) ) {
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
108 return $this->tokenFunctions;
109 }
110
111 static protected $cachedTokens = array();
112
113 public static function resetTokenCache() {
114 ApiQueryInfo::$cachedTokens = array();
115 }
116
117 public static function getEditToken( $pageid, $title ) {
118 // We could check for $title->userCan('edit') here,
119 // but that's too expensive for this purpose
120 // and would break caching
121 global $wgUser;
122 if ( !$wgUser->isAllowed( 'edit' ) ) {
123 return false;
124 }
125
126 // The token is always the same, let's exploit that
127 if ( !isset( ApiQueryInfo::$cachedTokens['edit'] ) ) {
128 ApiQueryInfo::$cachedTokens['edit'] = $wgUser->getEditToken();
129 }
130
131 return ApiQueryInfo::$cachedTokens['edit'];
132 }
133
134 public static function getDeleteToken( $pageid, $title ) {
135 global $wgUser;
136 if ( !$wgUser->isAllowed( 'delete' ) ) {
137 return false;
138 }
139
140 // The token is always the same, let's exploit that
141 if ( !isset( ApiQueryInfo::$cachedTokens['delete'] ) ) {
142 ApiQueryInfo::$cachedTokens['delete'] = $wgUser->getEditToken();
143 }
144
145 return ApiQueryInfo::$cachedTokens['delete'];
146 }
147
148 public static function getProtectToken( $pageid, $title ) {
149 global $wgUser;
150 if ( !$wgUser->isAllowed( 'protect' ) ) {
151 return false;
152 }
153
154 // The token is always the same, let's exploit that
155 if ( !isset( ApiQueryInfo::$cachedTokens['protect'] ) ) {
156 ApiQueryInfo::$cachedTokens['protect'] = $wgUser->getEditToken();
157 }
158
159 return ApiQueryInfo::$cachedTokens['protect'];
160 }
161
162 public static function getMoveToken( $pageid, $title ) {
163 global $wgUser;
164 if ( !$wgUser->isAllowed( 'move' ) ) {
165 return false;
166 }
167
168 // The token is always the same, let's exploit that
169 if ( !isset( ApiQueryInfo::$cachedTokens['move'] ) ) {
170 ApiQueryInfo::$cachedTokens['move'] = $wgUser->getEditToken();
171 }
172
173 return ApiQueryInfo::$cachedTokens['move'];
174 }
175
176 public static function getBlockToken( $pageid, $title ) {
177 global $wgUser;
178 if ( !$wgUser->isAllowed( 'block' ) ) {
179 return false;
180 }
181
182 // The token is always the same, let's exploit that
183 if ( !isset( ApiQueryInfo::$cachedTokens['block'] ) ) {
184 ApiQueryInfo::$cachedTokens['block'] = $wgUser->getEditToken();
185 }
186
187 return ApiQueryInfo::$cachedTokens['block'];
188 }
189
190 public static function getUnblockToken( $pageid, $title ) {
191 // Currently, this is exactly the same as the block token
192 return self::getBlockToken( $pageid, $title );
193 }
194
195 public static function getEmailToken( $pageid, $title ) {
196 global $wgUser;
197 if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser() ) {
198 return false;
199 }
200
201 // The token is always the same, let's exploit that
202 if ( !isset( ApiQueryInfo::$cachedTokens['email'] ) ) {
203 ApiQueryInfo::$cachedTokens['email'] = $wgUser->getEditToken();
204 }
205
206 return ApiQueryInfo::$cachedTokens['email'];
207 }
208
209 public static function getImportToken( $pageid, $title ) {
210 global $wgUser;
211 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
212 return false;
213 }
214
215 // The token is always the same, let's exploit that
216 if ( !isset( ApiQueryInfo::$cachedTokens['import'] ) ) {
217 ApiQueryInfo::$cachedTokens['import'] = $wgUser->getEditToken();
218 }
219
220 return ApiQueryInfo::$cachedTokens['import'];
221 }
222
223 public static function getWatchToken( $pageid, $title ) {
224 global $wgUser;
225 if ( !$wgUser->isLoggedIn() ) {
226 return false;
227 }
228
229 // The token is always the same, let's exploit that
230 if ( !isset( ApiQueryInfo::$cachedTokens['watch'] ) ) {
231 ApiQueryInfo::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
232 }
233
234 return ApiQueryInfo::$cachedTokens['watch'];
235 }
236
237 public static function getOptionsToken( $pageid, $title ) {
238 global $wgUser;
239 if ( !$wgUser->isLoggedIn() ) {
240 return false;
241 }
242
243 // The token is always the same, let's exploit that
244 if ( !isset( ApiQueryInfo::$cachedTokens['options'] ) ) {
245 ApiQueryInfo::$cachedTokens['options'] = $wgUser->getEditToken();
246 }
247
248 return ApiQueryInfo::$cachedTokens['options'];
249 }
250
251 public function execute() {
252 $this->params = $this->extractRequestParams();
253 if ( !is_null( $this->params['prop'] ) ) {
254 $prop = array_flip( $this->params['prop'] );
255 $this->fld_protection = isset( $prop['protection'] );
256 $this->fld_watched = isset( $prop['watched'] );
257 $this->fld_watchers = isset( $prop['watchers'] );
258 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
259 $this->fld_talkid = isset( $prop['talkid'] );
260 $this->fld_subjectid = isset( $prop['subjectid'] );
261 $this->fld_url = isset( $prop['url'] );
262 $this->fld_readable = isset( $prop['readable'] );
263 $this->fld_preload = isset( $prop['preload'] );
264 $this->fld_displaytitle = isset( $prop['displaytitle'] );
265 }
266
267 $pageSet = $this->getPageSet();
268 $this->titles = $pageSet->getGoodTitles();
269 $this->missing = $pageSet->getMissingTitles();
270 $this->everything = $this->titles + $this->missing;
271 $result = $this->getResult();
272
273 uasort( $this->everything, array( 'Title', 'compare' ) );
274 if ( !is_null( $this->params['continue'] ) ) {
275 // Throw away any titles we're gonna skip so they don't
276 // clutter queries
277 $cont = explode( '|', $this->params['continue'] );
278 $this->dieContinueUsageIf( count( $cont ) != 2 );
279 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
280 foreach ( $this->everything as $pageid => $title ) {
281 if ( Title::compare( $title, $conttitle ) >= 0 ) {
282 break;
283 }
284 unset( $this->titles[$pageid] );
285 unset( $this->missing[$pageid] );
286 unset( $this->everything[$pageid] );
287 }
288 }
289
290 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
291 // when resolving redirects, no page will have this field
292 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
293 ? $pageSet->getCustomField( 'page_is_redirect' )
294 : array();
295 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
296
297 if ( !$this->getConfig()->get( 'DisableCounters' ) ) {
298 $this->pageCounter = $pageSet->getCustomField( 'page_counter' );
299 }
300 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
301 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
302 $this->pageLength = $pageSet->getCustomField( 'page_len' );
303
304 // Get protection info if requested
305 if ( $this->fld_protection ) {
306 $this->getProtectionInfo();
307 }
308
309 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
310 $this->getWatchedInfo();
311 }
312
313 if ( $this->fld_watchers ) {
314 $this->getWatcherInfo();
315 }
316
317 // Run the talkid/subjectid query if requested
318 if ( $this->fld_talkid || $this->fld_subjectid ) {
319 $this->getTSIDs();
320 }
321
322 if ( $this->fld_displaytitle ) {
323 $this->getDisplayTitle();
324 }
325
326 /** @var $title Title */
327 foreach ( $this->everything as $pageid => $title ) {
328 $pageInfo = $this->extractPageInfo( $pageid, $title );
329 $fit = $result->addValue( array(
330 'query',
331 'pages'
332 ), $pageid, $pageInfo );
333 if ( !$fit ) {
334 $this->setContinueEnumParameter( 'continue',
335 $title->getNamespace() . '|' .
336 $title->getText() );
337 break;
338 }
339 }
340 }
341
342 /**
343 * Get a result array with information about a title
344 * @param int $pageid Page ID (negative for missing titles)
345 * @param Title $title
346 * @return array
347 */
348 private function extractPageInfo( $pageid, $title ) {
349 $pageInfo = array();
350 // $title->exists() needs pageid, which is not set for all title objects
351 $titleExists = $pageid > 0;
352 $ns = $title->getNamespace();
353 $dbkey = $title->getDBkey();
354
355 $pageInfo['contentmodel'] = $title->getContentModel();
356 $pageInfo['pagelanguage'] = $title->getPageLanguage()->getCode();
357
358 if ( $titleExists ) {
359 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
360 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
361 $pageInfo['counter'] = $this->getConfig()->get( 'DisableCounters' )
362 ? ''
363 : intval( $this->pageCounter[$pageid] );
364 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
365
366 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
367 $pageInfo['redirect'] = '';
368 }
369 if ( $this->pageIsNew[$pageid] ) {
370 $pageInfo['new'] = '';
371 }
372 }
373
374 if ( !is_null( $this->params['token'] ) ) {
375 $tokenFunctions = $this->getTokenFunctions();
376 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
377 foreach ( $this->params['token'] as $t ) {
378 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
379 if ( $val === false ) {
380 $this->setWarning( "Action '$t' is not allowed for the current user" );
381 } else {
382 $pageInfo[$t . 'token'] = $val;
383 }
384 }
385 }
386
387 if ( $this->fld_protection ) {
388 $pageInfo['protection'] = array();
389 if ( isset( $this->protections[$ns][$dbkey] ) ) {
390 $pageInfo['protection'] =
391 $this->protections[$ns][$dbkey];
392 }
393 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
394 }
395
396 if ( $this->fld_watched && isset( $this->watched[$ns][$dbkey] ) ) {
397 $pageInfo['watched'] = '';
398 }
399
400 if ( $this->fld_watchers ) {
401 if ( isset( $this->watchers[$ns][$dbkey] ) ) {
402 $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
403 } elseif ( $this->showZeroWatchers ) {
404 $pageInfo['watchers'] = 0;
405 }
406 }
407
408 if ( $this->fld_notificationtimestamp ) {
409 $pageInfo['notificationtimestamp'] = '';
410 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
411 $pageInfo['notificationtimestamp'] =
412 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 || !$user->isAllowed( 'viewmywatchlist' )
673 ) {
674 return;
675 }
676
677 $this->watched = array();
678 $this->notificationtimestamps = array();
679 $db = $this->getDB();
680
681 $lb = new LinkBatch( $this->everything );
682
683 $this->resetQueryParams();
684 $this->addTables( array( 'watchlist' ) );
685 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
686 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
687 $this->addWhere( array(
688 $lb->constructSet( 'wl', $db ),
689 'wl_user' => $user->getID()
690 ) );
691
692 $res = $this->select( __METHOD__ );
693
694 foreach ( $res as $row ) {
695 if ( $this->fld_watched ) {
696 $this->watched[$row->wl_namespace][$row->wl_title] = true;
697 }
698 if ( $this->fld_notificationtimestamp ) {
699 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] =
700 $row->wl_notificationtimestamp;
701 }
702 }
703 }
704
705 /**
706 * Get the count of watchers and put it in $this->watchers
707 */
708 private function getWatcherInfo() {
709 if ( count( $this->everything ) == 0 ) {
710 return;
711 }
712
713 $user = $this->getUser();
714 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
715 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
716 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
717 return;
718 }
719
720 $this->watchers = array();
721 $this->showZeroWatchers = $canUnwatchedpages;
722 $db = $this->getDB();
723
724 $lb = new LinkBatch( $this->everything );
725
726 $this->resetQueryParams();
727 $this->addTables( array( 'watchlist' ) );
728 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
729 $this->addWhere( array(
730 $lb->constructSet( 'wl', $db )
731 ) );
732 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
733 if ( !$canUnwatchedpages ) {
734 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
735 }
736
737 $res = $this->select( __METHOD__ );
738
739 foreach ( $res as $row ) {
740 $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
741 }
742 }
743
744 public function getCacheMode( $params ) {
745 $publicProps = array(
746 'protection',
747 'talkid',
748 'subjectid',
749 'url',
750 'preload',
751 'displaytitle',
752 );
753 if ( !is_null( $params['prop'] ) ) {
754 foreach ( $params['prop'] as $prop ) {
755 if ( !in_array( $prop, $publicProps ) ) {
756 return 'private';
757 }
758 }
759 }
760 if ( !is_null( $params['token'] ) ) {
761 return 'private';
762 }
763
764 return 'public';
765 }
766
767 public function getAllowedParams() {
768 return array(
769 'prop' => array(
770 ApiBase::PARAM_DFLT => null,
771 ApiBase::PARAM_ISMULTI => true,
772 ApiBase::PARAM_TYPE => array(
773 'protection',
774 'talkid',
775 'watched', # private
776 'watchers', # private
777 'notificationtimestamp', # private
778 'subjectid',
779 'url',
780 'readable', # private
781 'preload',
782 'displaytitle',
783 // If you add more properties here, please consider whether they
784 // need to be added to getCacheMode()
785 ) ),
786 'token' => array(
787 ApiBase::PARAM_DFLT => null,
788 ApiBase::PARAM_ISMULTI => true,
789 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
790 ),
791 'continue' => null,
792 );
793 }
794
795 public function getParamDescription() {
796 return array(
797 'prop' => array(
798 'Which additional properties to get:',
799 ' protection - List the protection level of each page',
800 ' talkid - The page ID of the talk page for each non-talk page',
801 ' watched - List the watched status of each page',
802 ' watchers - The number of watchers, if allowed',
803 ' notificationtimestamp - The watchlist notification timestamp of each page',
804 ' subjectid - The page ID of the parent page for each talk page',
805 ' url - Gives a full URL to the page, and also an edit URL',
806 ' readable - Whether the user can read this page',
807 ' preload - Gives the text returned by EditFormPreloadText',
808 ' displaytitle - Gives the way the page title is actually displayed',
809 ),
810 'token' => 'Request a token to perform a data-modifying action on a page',
811 'continue' => 'When more results are available, use this to continue',
812 );
813 }
814
815 public function getDescription() {
816 return 'Get basic page information such as namespace, title, last touched date, ...';
817 }
818
819 public function getExamples() {
820 return array(
821 'api.php?action=query&prop=info&titles=Main%20Page',
822 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
823 );
824 }
825
826 public function getHelpUrls() {
827 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
828 }
829 }