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