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