43 private $languageConverter;
45 private $linkBatchFactory;
47 private $namespaceInfo;
49 private $titleFactory;
51 private $titleFormatter;
53 private $watchedItemStore;
55 private $restrictionStore;
57 private $linksMigration;
59 private $fld_protection =
false, $fld_talkid =
false,
60 $fld_subjectid =
false, $fld_url =
false,
61 $fld_readable =
false, $fld_watched =
false,
62 $fld_watchers =
false, $fld_visitingwatchers =
false,
63 $fld_notificationtimestamp =
false,
64 $fld_preload =
false, $fld_displaytitle =
false, $fld_varianttitles =
false;
70 private $fld_linkclasses =
false;
75 private $fld_associatedpage =
false;
86 private $pageIsRedir, $pageIsNew, $pageTouched,
87 $pageLatest, $pageLength;
89 private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
90 $notificationtimestamps, $talkids, $subjectids, $displaytitles, $variantTitles;
96 private $watchlistExpiries;
102 private $linkClasses;
104 private $showZeroWatchers =
false;
106 private $countTestedActions = 0;
134 parent::__construct( $queryModule, $moduleName,
'in' );
136 $this->linkBatchFactory = $linkBatchFactory;
137 $this->namespaceInfo = $namespaceInfo;
138 $this->titleFactory = $titleFactory;
139 $this->titleFormatter = $titleFormatter;
140 $this->watchedItemStore = $watchedItemStore;
141 $this->restrictionStore = $restrictionStore;
142 $this->linksMigration = $linksMigration;
153 $pageSet->requestField(
'page_is_redirect' );
154 $pageSet->requestField(
'page_is_new' );
156 $pageSet->requestField(
'page_touched' );
157 $pageSet->requestField(
'page_latest' );
158 $pageSet->requestField(
'page_len' );
159 $pageSet->requestField(
'page_content_model' );
160 if ( $config->get( MainConfigNames::PageLanguageUseDB ) ) {
161 $pageSet->requestField(
'page_lang' );
167 if ( $this->params[
'prop'] !==
null ) {
168 $prop = array_fill_keys( $this->params[
'prop'],
true );
169 $this->fld_protection = isset( $prop[
'protection'] );
170 $this->fld_watched = isset( $prop[
'watched'] );
171 $this->fld_watchers = isset( $prop[
'watchers'] );
172 $this->fld_visitingwatchers = isset( $prop[
'visitingwatchers'] );
173 $this->fld_notificationtimestamp = isset( $prop[
'notificationtimestamp'] );
174 $this->fld_talkid = isset( $prop[
'talkid'] );
175 $this->fld_subjectid = isset( $prop[
'subjectid'] );
176 $this->fld_url = isset( $prop[
'url'] );
177 $this->fld_readable = isset( $prop[
'readable'] );
178 $this->fld_preload = isset( $prop[
'preload'] );
179 $this->fld_displaytitle = isset( $prop[
'displaytitle'] );
180 $this->fld_varianttitles = isset( $prop[
'varianttitles'] );
181 $this->fld_linkclasses = isset( $prop[
'linkclasses'] );
182 $this->fld_associatedpage = isset( $prop[
'associatedpage'] );
186 $this->titles = $pageSet->getGoodTitles();
187 $this->missing = $pageSet->getMissingTitles();
188 $this->everything = $this->titles + $this->missing;
191 uasort( $this->everything, [ Title::class,
'compare' ] );
192 if ( $this->params[
'continue'] !==
null ) {
196 $conttitle = $this->titleFactory->makeTitleSafe( $cont[0], $cont[1] );
198 foreach ( $this->everything as $pageid =>
$title ) {
199 if ( Title::compare(
$title, $conttitle ) >= 0 ) {
202 unset( $this->titles[$pageid] );
203 unset( $this->missing[$pageid] );
204 unset( $this->everything[$pageid] );
209 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
210 ? $pageSet->getCustomField(
'page_is_redirect' )
212 $this->pageIsNew = $pageSet->getCustomField(
'page_is_new' );
214 $this->pageTouched = $pageSet->getCustomField(
'page_touched' );
215 $this->pageLatest = $pageSet->getCustomField(
'page_latest' );
216 $this->pageLength = $pageSet->getCustomField(
'page_len' );
219 if ( $this->fld_protection ) {
220 $this->getProtectionInfo();
223 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
224 $this->getWatchedInfo();
227 if ( $this->fld_watchers ) {
228 $this->getWatcherInfo();
231 if ( $this->fld_visitingwatchers ) {
232 $this->getVisitingWatcherInfo();
236 if ( $this->fld_talkid || $this->fld_subjectid ) {
240 if ( $this->fld_displaytitle ) {
241 $this->getDisplayTitle();
244 if ( $this->fld_varianttitles ) {
245 $this->getVariantTitles();
248 if ( $this->fld_linkclasses ) {
249 $this->getLinkClasses( $this->params[
'linkcontext'] );
253 foreach ( $this->everything as $pageid =>
$title ) {
254 $pageInfo = $this->extractPageInfo( $pageid,
$title );
255 $fit = $pageInfo !==
null && $result->addValue( [
258 ], $pageid, $pageInfo );
261 $title->getNamespace() .
'|' .
274 private function extractPageInfo( $pageid,
$title ) {
277 $titleExists = $pageid > 0;
278 $ns =
$title->getNamespace();
279 $dbkey =
$title->getDBkey();
281 $pageInfo[
'contentmodel'] =
$title->getContentModel();
283 $pageLanguage =
$title->getPageLanguage();
284 $pageInfo[
'pagelanguage'] = $pageLanguage->getCode();
285 $pageInfo[
'pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
286 $pageInfo[
'pagelanguagedir'] = $pageLanguage->getDir();
288 if ( $titleExists ) {
289 $pageInfo[
'touched'] =
wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
290 $pageInfo[
'lastrevid'] = (int)$this->pageLatest[$pageid];
291 $pageInfo[
'length'] = (int)$this->pageLength[$pageid];
293 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
294 $pageInfo[
'redirect'] =
true;
296 if ( $this->pageIsNew[$pageid] ) {
297 $pageInfo[
'new'] =
true;
301 if ( $this->fld_protection ) {
302 $pageInfo[
'protection'] = [];
303 if ( isset( $this->protections[$ns][$dbkey] ) ) {
304 $pageInfo[
'protection'] =
305 $this->protections[$ns][$dbkey];
309 $pageInfo[
'restrictiontypes'] = [];
310 if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
311 $pageInfo[
'restrictiontypes'] =
312 $this->restrictionTypes[$ns][$dbkey];
317 if ( $this->fld_watched ) {
318 $pageInfo[
'watched'] =
false;
320 if ( isset( $this->watched[$ns][$dbkey] ) ) {
321 $pageInfo[
'watched'] = $this->watched[$ns][$dbkey];
324 if ( isset( $this->watchlistExpiries[$ns][$dbkey] ) ) {
325 $pageInfo[
'watchlistexpiry'] = $this->watchlistExpiries[$ns][$dbkey];
329 if ( $this->fld_watchers ) {
330 if ( $this->watchers !==
null && $this->watchers[$ns][$dbkey] !== 0 ) {
331 $pageInfo[
'watchers'] = $this->watchers[$ns][$dbkey];
332 } elseif ( $this->showZeroWatchers ) {
333 $pageInfo[
'watchers'] = 0;
337 if ( $this->fld_visitingwatchers ) {
338 if ( $this->visitingwatchers !==
null && $this->visitingwatchers[$ns][$dbkey] !== 0 ) {
339 $pageInfo[
'visitingwatchers'] = $this->visitingwatchers[$ns][$dbkey];
340 } elseif ( $this->showZeroWatchers ) {
341 $pageInfo[
'visitingwatchers'] = 0;
345 if ( $this->fld_notificationtimestamp ) {
346 $pageInfo[
'notificationtimestamp'] =
'';
347 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
348 $pageInfo[
'notificationtimestamp'] =
349 wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
353 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
354 $pageInfo[
'talkid'] = $this->talkids[$ns][$dbkey];
357 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
358 $pageInfo[
'subjectid'] = $this->subjectids[$ns][$dbkey];
361 if ( $this->fld_associatedpage && $ns >=
NS_MAIN ) {
362 $pageInfo[
'associatedpage'] = $this->titleFormatter->getPrefixedText(
363 $this->namespaceInfo->getAssociatedPage(
$title )
367 if ( $this->fld_url ) {
372 if ( $this->fld_readable ) {
373 $pageInfo[
'readable'] = $this->
getAuthority()->definitelyCan(
'read', $title );
376 if ( $this->fld_preload ) {
377 if ( $titleExists ) {
378 $pageInfo[
'preload'] =
'';
384 $pageInfo[
'preload'] = $text;
388 if ( $this->fld_displaytitle ) {
389 $pageInfo[
'displaytitle'] = $this->displaytitles[$pageid] ??
390 htmlspecialchars(
$title->getPrefixedText(), ENT_NOQUOTES );
393 if ( $this->fld_varianttitles && isset( $this->variantTitles[$pageid] ) ) {
394 $pageInfo[
'varianttitles'] = $this->variantTitles[$pageid];
397 if ( $this->fld_linkclasses && isset( $this->linkClasses[$pageid] ) ) {
398 $pageInfo[
'linkclasses'] = $this->linkClasses[$pageid];
401 if ( $this->params[
'testactions'] ) {
403 if ( $this->countTestedActions >= $limit ) {
407 $detailLevel = $this->params[
'testactionsdetail'];
409 if ( $errorFormatter->getFormat() ===
'bc' ) {
411 $errorFormatter = $errorFormatter->newWithFormat(
'plaintext' );
414 $pageInfo[
'actions'] = [];
415 foreach ( $this->params[
'testactions'] as $action ) {
416 $this->countTestedActions++;
418 if ( $detailLevel ===
'boolean' ) {
419 $pageInfo[
'actions'][$action] = $this->
getAuthority()->authorizeRead( $action,
$title );
422 if ( $detailLevel ===
'quick' ) {
428 $pageInfo[
'actions'][$action] = $errorFormatter->arrayFromStatus( $status );
439 private function getProtectionInfo() {
440 $this->protections = [];
441 $db = $this->
getDB();
444 if ( count( $this->titles ) ) {
447 $this->
addFields( [
'pr_page',
'pr_type',
'pr_level',
448 'pr_expiry',
'pr_cascade' ] );
449 $this->
addWhereFld(
'pr_page', array_keys( $this->titles ) );
452 foreach (
$res as $row ) {
454 $title = $this->titles[$row->pr_page];
456 'type' => $row->pr_type,
457 'level' => $row->pr_level,
460 if ( $row->pr_cascade ) {
461 $a[
'cascade'] =
true;
463 $this->protections[
$title->getNamespace()][
$title->getDBkey()][] = $a;
468 if ( count( $this->missing ) ) {
470 $lb = $this->linkBatchFactory->newLinkBatch( $this->missing );
472 $this->
addFields( [
'pt_title',
'pt_namespace',
'pt_create_perm',
'pt_expiry' ] );
473 $this->
addWhere( $lb->constructSet(
'pt', $db ) );
475 foreach (
$res as $row ) {
476 $this->protections[$row->pt_namespace][$row->pt_title][] = [
478 'level' => $row->pt_create_perm,
486 $images = $others = [];
487 foreach ( $this->everything as
$title ) {
489 $images[] =
$title->getDBkey();
494 $this->restrictionTypes[
$title->getNamespace()][
$title->getDBkey()] =
495 array_values( $this->restrictionStore->listApplicableRestrictionTypes(
$title ) );
498 [ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields(
'templatelinks' );
499 $queryInfo = $this->linksMigration->getQueryInfo(
'templatelinks' );
501 if ( count( $others ) ) {
503 $lb = $this->linkBatchFactory->newLinkBatch( $others );
505 $this->
addTables( array_merge( [
'page_restrictions',
'page' ], $queryInfo[
'tables'] ) );
507 $this->
addOption(
'USE INDEX', [
'templatelinks' =>
'PRIMARY' ] );
508 $this->
addFields( [
'pr_type',
'pr_level',
'pr_expiry',
509 'page_title',
'page_namespace',
510 $blNamespace, $blTitle ] );
511 $this->
addWhere( $lb->constructSet(
'tl', $db ) );
512 $this->
addWhere(
'pr_page = page_id' );
513 $this->
addWhere(
'pr_page = tl_from' );
518 foreach (
$res as $row ) {
519 $this->protections[$row->$blNamespace][$row->$blTitle][] = [
520 'type' => $row->pr_type,
521 'level' => $row->pr_level,
523 'source' => $this->titleFormatter->formatTitle( $row->page_namespace, $row->page_title ),
528 if ( count( $images ) ) {
531 $this->
addTables( [
'page_restrictions',
'page',
'imagelinks' ] );
532 $this->
addFields( [
'pr_type',
'pr_level',
'pr_expiry',
533 'page_title',
'page_namespace',
'il_to' ] );
534 $this->
addWhere(
'pr_page = page_id' );
535 $this->
addWhere(
'pr_page = il_from' );
540 foreach (
$res as $row ) {
541 $this->protections[
NS_FILE][$row->il_to][] = [
542 'type' => $row->pr_type,
543 'level' => $row->pr_level,
545 'source' => $this->titleFormatter->formatTitle( $row->page_namespace, $row->page_title ),
555 private function getTSIDs() {
556 $getTitles = $this->talkids = $this->subjectids = [];
557 $nsInfo = $this->namespaceInfo;
560 foreach ( $this->everything as
$t ) {
561 if ( $nsInfo->isTalk(
$t->getNamespace() ) ) {
562 if ( $this->fld_subjectid ) {
563 $getTitles[] =
$t->getSubjectPage();
565 } elseif ( $this->fld_talkid ) {
566 $getTitles[] =
$t->getTalkPage();
569 if ( $getTitles === [] ) {
573 $db = $this->
getDB();
577 $lb = $this->linkBatchFactory->newLinkBatch( $getTitles );
580 $this->
addFields( [
'page_title',
'page_namespace',
'page_id' ] );
581 $this->
addWhere( $lb->constructSet(
'page', $db ) );
583 foreach (
$res as $row ) {
584 if ( $nsInfo->isTalk( $row->page_namespace ) ) {
585 $this->talkids[$nsInfo->getSubject( $row->page_namespace )][$row->page_title] =
586 (int)( $row->page_id );
588 $this->subjectids[$nsInfo->getTalk( $row->page_namespace )][$row->page_title] =
589 (int)( $row->page_id );
594 private function getDisplayTitle() {
595 $this->displaytitles = [];
597 $pageIds = array_keys( $this->titles );
599 if ( $pageIds === [] ) {
605 $this->
addFields( [
'pp_page',
'pp_value' ] );
607 $this->
addWhereFld(
'pp_propname',
'displaytitle' );
610 foreach (
$res as $row ) {
611 $this->displaytitles[$row->pp_page] = $row->pp_value;
622 private function getLinkClasses( ?
LinkTarget $context_title =
null ) {
623 if ( $this->titles === [] ) {
634 foreach ( $this->titles as $pageId =>
$title ) {
635 $pdbk =
$title->getPrefixedDBkey();
636 $pagemap[$pageId] = $pdbk;
637 $classes[$pdbk] =
$title->isRedirect() ?
'mw-redirect' :
'';
640 $context_title = $this->titleFactory->newFromLinkTarget(
641 $context_title ?? $this->titleFactory->newMainPage()
644 $pagemap, $classes, $context_title
651 $this->linkClasses = [];
652 foreach ( $this->titles as $pageId =>
$title ) {
653 $pdbk =
$title->getPrefixedDBkey();
654 $this->linkClasses[$pageId] = preg_split(
655 '/\s+/', $classes[$pdbk] ??
'', -1, PREG_SPLIT_NO_EMPTY
660 private function getVariantTitles() {
661 if ( $this->titles === [] ) {
664 $this->variantTitles = [];
665 foreach ( $this->titles as $pageId =>
$t ) {
666 $this->variantTitles[$pageId] = isset( $this->displaytitles[$pageId] )
667 ? $this->getAllVariants( $this->displaytitles[$pageId] )
668 : $this->getAllVariants(
$t->getText(),
$t->getNamespace() );
672 private function getAllVariants( $text, $ns =
NS_MAIN ) {
674 foreach ( $this->languageConverter->getVariants() as $variant ) {
675 $convertTitle = $this->languageConverter->autoConvert( $text, $variant );
677 $convertNs = $this->languageConverter->convertNamespace( $ns, $variant );
678 $convertTitle = $convertNs .
':' . $convertTitle;
680 $result[$variant] = $convertTitle;
689 private function getWatchedInfo() {
692 if ( !$user->isRegistered() || count( $this->everything ) == 0
693 || !$this->
getAuthority()->isAllowed(
'viewmywatchlist' )
699 $this->watchlistExpiries = [];
700 $this->notificationtimestamps = [];
703 $items = $this->watchedItemStore->loadWatchedItemsBatch( $user, $this->everything );
705 foreach ( $items as $item ) {
706 $nsId = $item->getTarget()->getNamespace();
707 $dbKey = $item->getTarget()->getDBkey();
709 if ( $this->fld_watched ) {
710 $this->watched[$nsId][$dbKey] =
true;
712 $expiry = $item->getExpiry( TS_ISO_8601 );
714 $this->watchlistExpiries[$nsId][$dbKey] = $expiry;
718 if ( $this->fld_notificationtimestamp ) {
719 $this->notificationtimestamps[$nsId][$dbKey] = $item->getNotificationTimestamp();
727 private function getWatcherInfo() {
728 if ( count( $this->everything ) == 0 ) {
732 $canUnwatchedpages = $this->
getAuthority()->isAllowed(
'unwatchedpages' );
733 $unwatchedPageThreshold =
734 $this->
getConfig()->get( MainConfigNames::UnwatchedPageThreshold );
735 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
739 $this->showZeroWatchers = $canUnwatchedpages;
742 if ( !$canUnwatchedpages ) {
743 $countOptions[
'minimumWatchers'] = $unwatchedPageThreshold;
746 $this->watchers = $this->watchedItemStore->countWatchersMultiple(
758 private function getVisitingWatcherInfo() {
760 $db = $this->
getDB();
762 $canUnwatchedpages = $this->
getAuthority()->isAllowed(
'unwatchedpages' );
763 $unwatchedPageThreshold = $config->get( MainConfigNames::UnwatchedPageThreshold );
764 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
768 $this->showZeroWatchers = $canUnwatchedpages;
770 $titlesWithThresholds = [];
771 if ( $this->titles ) {
772 $lb = $this->linkBatchFactory->newLinkBatch( $this->titles );
776 $this->
addTables( [
'page',
'revision' ] );
777 $this->
addFields( [
'page_namespace',
'page_title',
'rev_timestamp' ] );
779 'page_latest = rev_id',
780 $lb->constructSet(
'page', $db ),
782 $this->
addOption(
'GROUP BY', [
'page_namespace',
'page_title' ] );
783 $timestampRes = $this->
select( __METHOD__ );
785 $age = $config->get( MainConfigNames::WatchersMaxAge );
787 foreach ( $timestampRes as $row ) {
788 $revTimestamp =
wfTimestamp( TS_UNIX, (
int)$row->rev_timestamp );
789 $timestamps[$row->page_namespace][$row->page_title] = (int)$revTimestamp - $age;
791 $titlesWithThresholds = array_map(
792 static function (
LinkTarget $target ) use ( $timestamps ) {
801 if ( $this->missing ) {
802 $titlesWithThresholds = array_merge(
803 $titlesWithThresholds,
806 return [ $target, null ];
812 $this->visitingwatchers = $this->watchedItemStore->countVisitingWatchersMultiple(
813 $titlesWithThresholds,
814 !$canUnwatchedpages ? $unwatchedPageThreshold : null
830 if ( array_diff( (array)$params[
'prop'], $publicProps ) ) {
835 if ( $params[
'testactions'] ) {
845 ParamValidator::PARAM_ISMULTI =>
true,
846 ParamValidator::PARAM_TYPE => [
850 'watchers', #
private
851 'visitingwatchers', #
private
852 'notificationtimestamp', #
private
856 'readable', #
private
860 'linkclasses', #
private: stub length (and possibly hook colors)
865 EnumDef::PARAM_DEPRECATED_VALUES => [
870 ParamValidator::PARAM_TYPE =>
'title',
871 ParamValidator::PARAM_DEFAULT => $this->titleFactory->newMainPage()->getPrefixedText(),
872 TitleDef::PARAM_RETURN_OBJECT =>
true,
875 ParamValidator::PARAM_TYPE =>
'string',
876 ParamValidator::PARAM_ISMULTI =>
true,
878 'testactionsdetail' => [
879 ParamValidator::PARAM_TYPE => [
'boolean',
'full',
'quick' ],
880 ParamValidator::PARAM_DEFAULT =>
'boolean',
890 $title = Title::newMainPage()->getPrefixedText();
891 $mp = rawurlencode(
$title );
894 "action=query&prop=info&titles={$mp}"
895 =>
'apihelp-query+info-example-simple',
896 "action=query&prop=info&inprop=protection&titles={$mp}"
897 =>
'apihelp-query+info-example-protection',
902 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';