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