Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.84% covered (danger)
47.84%
277 / 579
17.65% covered (danger)
17.65%
3 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryInfo
47.84% covered (danger)
47.84%
277 / 579
17.65% covered (danger)
17.65%
3 / 17
3003.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 requestExtraData
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 execute
70.27% covered (warning)
70.27%
52 / 74
0.00% covered (danger)
0.00%
0 / 1
36.90
 extractPageInfo
46.75% covered (danger)
46.75%
72 / 154
0.00% covered (danger)
0.00%
0 / 1
494.22
 getProtectionInfo
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 1
156
 getTSIDs
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
72
 getDisplayTitle
33.33% covered (danger)
33.33%
4 / 12
0.00% covered (danger)
0.00%
0 / 1
5.67
 getLinkClasses
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 getVariantTitles
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getAllVariants
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getWatchedInfo
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
8.01
 getWatcherInfo
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 getVisitingWatcherInfo
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
56
 getCacheMode
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
 getAllowedParams
100.00% covered (success)
100.00%
75 / 75
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22use MediaWiki\Cache\LinkBatchFactory;
23use MediaWiki\EditPage\IntroMessageBuilder;
24use MediaWiki\EditPage\PreloadedContentBuilder;
25use MediaWiki\Languages\LanguageConverterFactory;
26use MediaWiki\Linker\LinksMigration;
27use MediaWiki\Linker\LinkTarget;
28use MediaWiki\MainConfigNames;
29use MediaWiki\Page\PageIdentity;
30use MediaWiki\Page\PageReference;
31use MediaWiki\ParamValidator\TypeDef\TitleDef;
32use MediaWiki\Permissions\PermissionStatus;
33use MediaWiki\Permissions\RestrictionStore;
34use MediaWiki\Revision\RevisionLookup;
35use MediaWiki\Title\NamespaceInfo;
36use MediaWiki\Title\Title;
37use MediaWiki\Title\TitleFactory;
38use MediaWiki\Title\TitleFormatter;
39use MediaWiki\Title\TitleValue;
40use MediaWiki\User\TempUser\TempUserCreator;
41use MediaWiki\User\UserFactory;
42use MediaWiki\Utils\UrlUtils;
43use Wikimedia\ParamValidator\ParamValidator;
44use Wikimedia\ParamValidator\TypeDef\EnumDef;
45
46/**
47 * A query module to show basic page information.
48 *
49 * @ingroup API
50 */
51class ApiQueryInfo extends ApiQueryBase {
52
53    private ILanguageConverter $languageConverter;
54    private LinkBatchFactory $linkBatchFactory;
55    private NamespaceInfo $namespaceInfo;
56    private TitleFactory $titleFactory;
57    private TitleFormatter $titleFormatter;
58    private WatchedItemStore $watchedItemStore;
59    private RestrictionStore $restrictionStore;
60    private LinksMigration $linksMigration;
61    private TempUserCreator $tempUserCreator;
62    private UserFactory $userFactory;
63    private IntroMessageBuilder $introMessageBuilder;
64    private PreloadedContentBuilder $preloadedContentBuilder;
65    private RevisionLookup $revisionLookup;
66    private UrlUtils $urlUtils;
67
68    private $fld_protection = false, $fld_talkid = false,
69        $fld_subjectid = false, $fld_url = false,
70        $fld_readable = false, $fld_watched = false,
71        $fld_watchers = false, $fld_visitingwatchers = false,
72        $fld_notificationtimestamp = false,
73        $fld_preload = false, $fld_preloadcontent = false, $fld_editintro = false,
74        $fld_displaytitle = false, $fld_varianttitles = false;
75
76    /**
77     * @var bool Whether to include link class information for the
78     *    given page titles.
79     */
80    private $fld_linkclasses = false;
81
82    /**
83     * @var bool Whether to include the name of the associated page
84     */
85    private $fld_associatedpage = false;
86
87    private $params;
88
89    /** @var PageIdentity[] */
90    private $titles;
91    /** @var PageIdentity[] */
92    private $missing;
93    /** @var PageIdentity[] */
94    private $everything;
95
96    private $pageIsRedir, $pageIsNew, $pageTouched,
97        $pageLatest, $pageLength;
98
99    private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
100        $notificationtimestamps, $talkids, $subjectids, $displaytitles, $variantTitles;
101
102    /**
103     * Watchlist expiries that corresponds with the $watched property. Keyed by namespace and title.
104     * @var array<int,array<string,string>>
105     */
106    private $watchlistExpiries;
107
108    /**
109     * @var array<int,string[]> Mapping of page id to list of 'extra link
110     *   classes' for the given page
111     */
112    private $linkClasses;
113
114    private $showZeroWatchers = false;
115
116    private $countTestedActions = 0;
117
118    /**
119     * @param ApiQuery $queryModule
120     * @param string $moduleName
121     * @param Language $contentLanguage
122     * @param LinkBatchFactory $linkBatchFactory
123     * @param NamespaceInfo $namespaceInfo
124     * @param TitleFactory $titleFactory
125     * @param TitleFormatter $titleFormatter
126     * @param WatchedItemStore $watchedItemStore
127     * @param LanguageConverterFactory $languageConverterFactory
128     * @param RestrictionStore $restrictionStore
129     * @param LinksMigration $linksMigration
130     * @param TempUserCreator $tempUserCreator
131     * @param UserFactory $userFactory
132     * @param IntroMessageBuilder $introMessageBuilder
133     * @param PreloadedContentBuilder $preloadedContentBuilder
134     * @param RevisionLookup $revisionLookup
135     * @param UrlUtils $urlUtils
136     */
137    public function __construct(
138        ApiQuery $queryModule,
139        $moduleName,
140        Language $contentLanguage,
141        LinkBatchFactory $linkBatchFactory,
142        NamespaceInfo $namespaceInfo,
143        TitleFactory $titleFactory,
144        TitleFormatter $titleFormatter,
145        WatchedItemStore $watchedItemStore,
146        LanguageConverterFactory $languageConverterFactory,
147        RestrictionStore $restrictionStore,
148        LinksMigration $linksMigration,
149        TempUserCreator $tempUserCreator,
150        UserFactory $userFactory,
151        IntroMessageBuilder $introMessageBuilder,
152        PreloadedContentBuilder $preloadedContentBuilder,
153        RevisionLookup $revisionLookup,
154        UrlUtils $urlUtils
155    ) {
156        parent::__construct( $queryModule, $moduleName, 'in' );
157        $this->languageConverter = $languageConverterFactory->getLanguageConverter( $contentLanguage );
158        $this->linkBatchFactory = $linkBatchFactory;
159        $this->namespaceInfo = $namespaceInfo;
160        $this->titleFactory = $titleFactory;
161        $this->titleFormatter = $titleFormatter;
162        $this->watchedItemStore = $watchedItemStore;
163        $this->restrictionStore = $restrictionStore;
164        $this->linksMigration = $linksMigration;
165        $this->tempUserCreator = $tempUserCreator;
166        $this->userFactory = $userFactory;
167        $this->introMessageBuilder = $introMessageBuilder;
168        $this->preloadedContentBuilder = $preloadedContentBuilder;
169        $this->revisionLookup = $revisionLookup;
170        $this->urlUtils = $urlUtils;
171    }
172
173    /**
174     * @param ApiPageSet $pageSet
175     * @return void
176     */
177    public function requestExtraData( $pageSet ) {
178        // If the pageset is resolving redirects we won't get page_is_redirect.
179        // But we can't know for sure until the pageset is executed (revids may
180        // turn it off), so request it unconditionally.
181        $pageSet->requestField( 'page_is_redirect' );
182        $pageSet->requestField( 'page_is_new' );
183        $config = $this->getConfig();
184        $pageSet->requestField( 'page_touched' );
185        $pageSet->requestField( 'page_latest' );
186        $pageSet->requestField( 'page_len' );
187        $pageSet->requestField( 'page_content_model' );
188        if ( $config->get( MainConfigNames::PageLanguageUseDB ) ) {
189            $pageSet->requestField( 'page_lang' );
190        }
191    }
192
193    public function execute() {
194        $this->params = $this->extractRequestParams();
195        if ( $this->params['prop'] !== null ) {
196            $prop = array_fill_keys( $this->params['prop'], true );
197            $this->fld_protection = isset( $prop['protection'] );
198            $this->fld_watched = isset( $prop['watched'] );
199            $this->fld_watchers = isset( $prop['watchers'] );
200            $this->fld_visitingwatchers = isset( $prop['visitingwatchers'] );
201            $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
202            $this->fld_talkid = isset( $prop['talkid'] );
203            $this->fld_subjectid = isset( $prop['subjectid'] );
204            $this->fld_url = isset( $prop['url'] );
205            $this->fld_readable = isset( $prop['readable'] );
206            $this->fld_preload = isset( $prop['preload'] );
207            $this->fld_preloadcontent = isset( $prop['preloadcontent'] );
208            $this->fld_editintro = isset( $prop['editintro'] );
209            $this->fld_displaytitle = isset( $prop['displaytitle'] );
210            $this->fld_varianttitles = isset( $prop['varianttitles'] );
211            $this->fld_linkclasses = isset( $prop['linkclasses'] );
212            $this->fld_associatedpage = isset( $prop['associatedpage'] );
213        }
214
215        $pageSet = $this->getPageSet();
216        $this->titles = $pageSet->getGoodPages();
217        $this->missing = $pageSet->getMissingPages();
218        $this->everything = $this->titles + $this->missing;
219        $result = $this->getResult();
220
221        if (
222            ( $this->fld_preloadcontent || $this->fld_editintro ) &&
223            ( count( $this->everything ) > 1 || count( $this->getPageSet()->getRevisionIDs() ) > 1 )
224        ) {
225            // This is relatively slow, so disallow doing it for multiple pages, just in case.
226            // (Also, handling multiple revisions would be tricky.)
227            $this->dieWithError(
228                [ 'apierror-info-singlepagerevision', $this->getModulePrefix() ], 'invalidparammix'
229            );
230        }
231
232        uasort( $this->everything, [ Title::class, 'compare' ] );
233        if ( $this->params['continue'] !== null ) {
234            // Throw away any titles we're gonna skip so they don't
235            // clutter queries
236            $cont = $this->parseContinueParamOrDie( $this->params['continue'], [ 'int', 'string' ] );
237            $conttitle = $this->titleFactory->makeTitleSafe( $cont[0], $cont[1] );
238            $this->dieContinueUsageIf( !$conttitle );
239            foreach ( $this->everything as $pageid => $page ) {
240                if ( Title::compare( $page, $conttitle ) >= 0 ) {
241                    break;
242                }
243                unset( $this->titles[$pageid] );
244                unset( $this->missing[$pageid] );
245                unset( $this->everything[$pageid] );
246            }
247        }
248
249        // when resolving redirects, no page will have this field
250        $this->pageIsRedir = !$pageSet->isResolvingRedirects()
251            ? $pageSet->getCustomField( 'page_is_redirect' )
252            : [];
253        $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
254
255        $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
256        $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
257        $this->pageLength = $pageSet->getCustomField( 'page_len' );
258
259        // Get protection info if requested
260        if ( $this->fld_protection ) {
261            $this->getProtectionInfo();
262        }
263
264        if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
265            $this->getWatchedInfo();
266        }
267
268        if ( $this->fld_watchers ) {
269            $this->getWatcherInfo();
270        }
271
272        if ( $this->fld_visitingwatchers ) {
273            $this->getVisitingWatcherInfo();
274        }
275
276        // Run the talkid/subjectid query if requested
277        if ( $this->fld_talkid || $this->fld_subjectid ) {
278            $this->getTSIDs();
279        }
280
281        if ( $this->fld_displaytitle ) {
282            $this->getDisplayTitle();
283        }
284
285        if ( $this->fld_varianttitles ) {
286            $this->getVariantTitles();
287        }
288
289        if ( $this->fld_linkclasses ) {
290            $this->getLinkClasses( $this->params['linkcontext'] );
291        }
292
293        /** @var PageIdentity $page */
294        foreach ( $this->everything as $pageid => $page ) {
295            $pageInfo = $this->extractPageInfo( $pageid, $page );
296            $fit = $pageInfo !== null && $result->addValue( [
297                'query',
298                'pages'
299            ], $pageid, $pageInfo );
300            if ( !$fit ) {
301                $this->setContinueEnumParameter( 'continue',
302                    $page->getNamespace() . '|' .
303                    $this->titleFormatter->getText( $page ) );
304                break;
305            }
306        }
307    }
308
309    /**
310     * Get a result array with information about a title
311     * @param int $pageid Page ID (negative for missing titles)
312     * @param PageIdentity $page
313     * @return array|null
314     */
315    private function extractPageInfo( $pageid, $page ) {
316        $title = $this->titleFactory->newFromPageIdentity( $page );
317        $pageInfo = [];
318        // $page->exists() needs pageid, which is not set for all title objects
319        $pageExists = $pageid > 0;
320        $ns = $page->getNamespace();
321        $dbkey = $page->getDBkey();
322
323        $pageInfo['contentmodel'] = $title->getContentModel();
324
325        $pageLanguage = $title->getPageLanguage();
326        $pageInfo['pagelanguage'] = $pageLanguage->getCode();
327        $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
328        $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
329
330        if ( $pageExists ) {
331            $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
332            $pageInfo['lastrevid'] = (int)$this->pageLatest[$pageid];
333            $pageInfo['length'] = (int)$this->pageLength[$pageid];
334
335            if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
336                $pageInfo['redirect'] = true;
337            }
338            if ( $this->pageIsNew[$pageid] ) {
339                $pageInfo['new'] = true;
340            }
341        }
342
343        if ( $this->fld_protection ) {
344            $pageInfo['protection'] = [];
345            if ( isset( $this->protections[$ns][$dbkey] ) ) {
346                $pageInfo['protection'] =
347                    $this->protections[$ns][$dbkey];
348            }
349            ApiResult::setIndexedTagName( $pageInfo['protection'], 'pr' );
350
351            $pageInfo['restrictiontypes'] = [];
352            if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
353                $pageInfo['restrictiontypes'] =
354                    $this->restrictionTypes[$ns][$dbkey];
355            }
356            ApiResult::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
357        }
358
359        if ( $this->fld_watched ) {
360            $pageInfo['watched'] = false;
361
362            if ( isset( $this->watched[$ns][$dbkey] ) ) {
363                $pageInfo['watched'] = $this->watched[$ns][$dbkey];
364            }
365
366            if ( isset( $this->watchlistExpiries[$ns][$dbkey] ) ) {
367                $pageInfo['watchlistexpiry'] = $this->watchlistExpiries[$ns][$dbkey];
368            }
369        }
370
371        if ( $this->fld_watchers ) {
372            if ( $this->watchers !== null && $this->watchers[$ns][$dbkey] !== 0 ) {
373                $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
374            } elseif ( $this->showZeroWatchers ) {
375                $pageInfo['watchers'] = 0;
376            }
377        }
378
379        if ( $this->fld_visitingwatchers ) {
380            if ( $this->visitingwatchers !== null && $this->visitingwatchers[$ns][$dbkey] !== 0 ) {
381                $pageInfo['visitingwatchers'] = $this->visitingwatchers[$ns][$dbkey];
382            } elseif ( $this->showZeroWatchers ) {
383                $pageInfo['visitingwatchers'] = 0;
384            }
385        }
386
387        if ( $this->fld_notificationtimestamp ) {
388            $pageInfo['notificationtimestamp'] = '';
389            if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
390                $pageInfo['notificationtimestamp'] =
391                    wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
392            }
393        }
394
395        if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
396            $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
397        }
398
399        if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
400            $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
401        }
402
403        if ( $this->fld_associatedpage && $ns >= NS_MAIN ) {
404            $pageInfo['associatedpage'] = $this->titleFormatter->getPrefixedText(
405                $this->namespaceInfo->getAssociatedPage( TitleValue::newFromPage( $page ) )
406            );
407        }
408
409        if ( $this->fld_url ) {
410            $pageInfo['fullurl'] = (string)$this->urlUtils->expand(
411                $title->getFullURL(), PROTO_CURRENT
412            );
413            $pageInfo['editurl'] = (string)$this->urlUtils->expand(
414                $title->getFullURL( 'action=edit' ), PROTO_CURRENT
415            );
416            $pageInfo['canonicalurl'] = (string)$this->urlUtils->expand(
417                $title->getFullURL(), PROTO_CANONICAL
418            );
419        }
420        if ( $this->fld_readable ) {
421            $pageInfo['readable'] = $this->getAuthority()->definitelyCan( 'read', $page );
422        }
423
424        if ( $this->fld_preload ) {
425            if ( $pageExists ) {
426                $pageInfo['preload'] = '';
427            } else {
428                $text = null;
429                // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
430                $this->getHookRunner()->onEditFormPreloadText( $text, $title );
431
432                $pageInfo['preload'] = $text;
433            }
434        }
435
436        if ( $this->fld_preloadcontent ) {
437            $newSection = $this->params['preloadnewsection'];
438            // Preloaded content is not supported for already existing pages or sections.
439            // The actual page/section content should be shown for editing (from prop=revisions API).
440            if ( !$pageExists || $newSection ) {
441                $content = $this->preloadedContentBuilder->getPreloadedContent(
442                    $title->toPageIdentity(),
443                    $this->getAuthority(),
444                    $this->params['preloadcustom'],
445                    $this->params['preloadparams'] ?? [],
446                    $newSection ? 'new' : null
447                );
448                $defaultContent = $newSection ? null :
449                    $this->preloadedContentBuilder->getDefaultContent( $title->toPageIdentity() );
450                $contentIsDefault = $defaultContent ? $content->equals( $defaultContent ) : $content->isEmpty();
451                // Adapted from ApiQueryRevisionsBase::extractAllSlotInfo.
452                // The preloaded content fills the main slot.
453                $pageInfo['preloadcontent']['contentmodel'] = $content->getModel();
454                $pageInfo['preloadcontent']['contentformat'] = $content->getDefaultFormat();
455                ApiResult::setContentValue( $pageInfo['preloadcontent'], 'content', $content->serialize() );
456                // If the preloaded content generated from these parameters is the same as
457                // the default page content, the user should be discouraged from saving the page
458                // (e.g. by disabling the save button until changes are made, or displaying a warning).
459                $pageInfo['preloadisdefault'] = $contentIsDefault;
460            }
461        }
462
463        if ( $this->fld_editintro ) {
464            // Use $page as the context page in every processed message (T300184)
465            $localizerWithPage = new class( $this, $page ) implements MessageLocalizer {
466                private MessageLocalizer $base;
467                private PageReference $page;
468
469                public function __construct( MessageLocalizer $base, PageReference $page ) {
470                    $this->base = $base;
471                    $this->page = $page;
472                }
473
474                /**
475                 * @inheritDoc
476                 */
477                public function msg( $key, ...$params ) {
478                    return $this->base->msg( $key, ...$params )->page( $this->page );
479                }
480            };
481
482            $styleParamMap = [
483                'lessframes' => IntroMessageBuilder::LESS_FRAMES,
484                'moreframes' => IntroMessageBuilder::MORE_FRAMES,
485            ];
486            // If we got here, there is exactly one page and revision in the query
487            $revId = array_key_first( $this->getPageSet()->getLiveRevisionIDs() );
488            $revRecord = $revId ? $this->revisionLookup->getRevisionById( $revId ) : null;
489
490            $messages = $this->introMessageBuilder->getIntroMessages(
491                $styleParamMap[ $this->params['editintrostyle'] ],
492                $this->params['editintroskip'] ?? [],
493                $localizerWithPage,
494                $title->toPageIdentity(),
495                $revRecord,
496                $this->getAuthority(),
497                $this->params['editintrocustom'],
498                // Maybe expose these as parameters in the future, but for now it doesn't seem worth it:
499                null,
500                false
501            );
502            ApiResult::setIndexedTagName( $messages, 'ei' );
503            ApiResult::setArrayType( $messages, 'kvp', 'key' );
504
505            $pageInfo['editintro'] = $messages;
506        }
507
508        if ( $this->fld_displaytitle ) {
509            $pageInfo['displaytitle'] = $this->displaytitles[$pageid] ??
510                htmlspecialchars( $this->titleFormatter->getPrefixedText( $page ), ENT_NOQUOTES );
511        }
512
513        if ( $this->fld_varianttitles && isset( $this->variantTitles[$pageid] ) ) {
514            $pageInfo['varianttitles'] = $this->variantTitles[$pageid];
515        }
516
517        if ( $this->fld_linkclasses && isset( $this->linkClasses[$pageid] ) ) {
518            $pageInfo['linkclasses'] = $this->linkClasses[$pageid];
519        }
520
521        if ( $this->params['testactions'] ) {
522            $limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_SML2 : self::LIMIT_SML1;
523            if ( $this->countTestedActions >= $limit ) {
524                return null; // force a continuation
525            }
526
527            $detailLevel = $this->params['testactionsdetail'];
528            $errorFormatter = $this->getErrorFormatter();
529            if ( $errorFormatter->getFormat() === 'bc' ) {
530                // Eew, no. Use a more modern format here.
531                $errorFormatter = $errorFormatter->newWithFormat( 'plaintext' );
532            }
533
534            $pageInfo['actions'] = [];
535            if ( $this->params['testactionsautocreate'] ) {
536                $pageInfo['wouldautocreate'] = [];
537            }
538
539            foreach ( $this->params['testactions'] as $action ) {
540                $this->countTestedActions++;
541
542                $shouldAutoCreate = $this->tempUserCreator->shouldAutoCreate( $this->getUser(), $action );
543
544                if ( $shouldAutoCreate ) {
545                    $authority = $this->userFactory->newTempPlaceholder();
546                } else {
547                    $authority = $this->getAuthority();
548                }
549
550                if ( $detailLevel === 'boolean' ) {
551                    $pageInfo['actions'][$action] = $authority->authorizeRead( $action, $page );
552                } else {
553                    $status = new PermissionStatus();
554                    if ( $detailLevel === 'quick' ) {
555                        $authority->probablyCan( $action, $page, $status );
556                    } else {
557                        $authority->definitelyCan( $action, $page, $status );
558                    }
559                    $this->addBlockInfoToStatus( $status );
560                    $pageInfo['actions'][$action] = $errorFormatter->arrayFromStatus( $status );
561                }
562
563                if ( $this->params['testactionsautocreate'] ) {
564                    $pageInfo['wouldautocreate'][$action] = $shouldAutoCreate;
565                }
566            }
567        }
568
569        return $pageInfo;
570    }
571
572    /**
573     * Get information about protections and put it in $protections
574     */
575    private function getProtectionInfo() {
576        $this->protections = [];
577        $db = $this->getDB();
578
579        // Get normal protections for existing titles
580        if ( count( $this->titles ) ) {
581            $this->resetQueryParams();
582            $this->addTables( 'page_restrictions' );
583            $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
584                'pr_expiry', 'pr_cascade' ] );
585            $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
586
587            $res = $this->select( __METHOD__ );
588            foreach ( $res as $row ) {
589                /** @var PageReference $page */
590                $page = $this->titles[$row->pr_page];
591                $a = [
592                    'type' => $row->pr_type,
593                    'level' => $row->pr_level,
594                    'expiry' => ApiResult::formatExpiry( $row->pr_expiry )
595                ];
596                if ( $row->pr_cascade ) {
597                    $a['cascade'] = true;
598                }
599                $this->protections[$page->getNamespace()][$page->getDBkey()][] = $a;
600            }
601        }
602
603        // Get protections for missing titles
604        if ( count( $this->missing ) ) {
605            $this->resetQueryParams();
606            $lb = $this->linkBatchFactory->newLinkBatch( $this->missing );
607            $this->addTables( 'protected_titles' );
608            $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
609            $this->addWhere( $lb->constructSet( 'pt', $db ) );
610            $res = $this->select( __METHOD__ );
611            foreach ( $res as $row ) {
612                $this->protections[$row->pt_namespace][$row->pt_title][] = [
613                    'type' => 'create',
614                    'level' => $row->pt_create_perm,
615                    'expiry' => ApiResult::formatExpiry( $row->pt_expiry )
616                ];
617            }
618        }
619
620        // Separate good and missing titles into files and other pages
621        // and populate $this->restrictionTypes
622        $images = $others = [];
623        foreach ( $this->everything as $page ) {
624            if ( $page->getNamespace() === NS_FILE ) {
625                $images[] = $page->getDBkey();
626            } else {
627                $others[] = $page;
628            }
629            // Applicable protection types
630            $this->restrictionTypes[$page->getNamespace()][$page->getDBkey()] =
631                array_values( $this->restrictionStore->listApplicableRestrictionTypes( $page ) );
632        }
633
634        [ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields( 'templatelinks' );
635        $queryInfo = $this->linksMigration->getQueryInfo( 'templatelinks' );
636
637        if ( count( $others ) ) {
638            // Non-images: check templatelinks
639            $lb = $this->linkBatchFactory->newLinkBatch( $others );
640            $this->resetQueryParams();
641            $this->addTables( array_merge( [ 'page_restrictions', 'page' ], $queryInfo['tables'] ) );
642            // templatelinks must use PRIMARY index and not the tl_target_id.
643            $this->addOption( 'USE INDEX', [ 'templatelinks' => 'PRIMARY' ] );
644            $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
645                'page_title', 'page_namespace',
646                $blNamespace, $blTitle ] );
647            $this->addWhere( $lb->constructSet( 'tl', $db ) );
648            $this->addWhere( 'pr_page = page_id' );
649            $this->addWhere( 'pr_page = tl_from' );
650            $this->addWhereFld( 'pr_cascade', 1 );
651            $this->addJoinConds( $queryInfo['joins'] );
652
653            $res = $this->select( __METHOD__ );
654            foreach ( $res as $row ) {
655                $this->protections[$row->$blNamespace][$row->$blTitle][] = [
656                    'type' => $row->pr_type,
657                    'level' => $row->pr_level,
658                    'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
659                    'source' => $this->titleFormatter->formatTitle( $row->page_namespace, $row->page_title ),
660                ];
661            }
662        }
663
664        if ( count( $images ) ) {
665            // Images: check imagelinks
666            $this->resetQueryParams();
667            $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
668            $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
669                'page_title', 'page_namespace', 'il_to' ] );
670            $this->addWhere( 'pr_page = page_id' );
671            $this->addWhere( 'pr_page = il_from' );
672            $this->addWhereFld( 'pr_cascade', 1 );
673            $this->addWhereFld( 'il_to', $images );
674
675            $res = $this->select( __METHOD__ );
676            foreach ( $res as $row ) {
677                $this->protections[NS_FILE][$row->il_to][] = [
678                    'type' => $row->pr_type,
679                    'level' => $row->pr_level,
680                    'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
681                    'source' => $this->titleFormatter->formatTitle( $row->page_namespace, $row->page_title ),
682                ];
683            }
684        }
685    }
686
687    /**
688     * Get talk page IDs (if requested) and subject page IDs (if requested)
689     * and put them in $talkids and $subjectids
690     */
691    private function getTSIDs() {
692        $getTitles = $this->talkids = $this->subjectids = [];
693        $nsInfo = $this->namespaceInfo;
694
695        /** @var PageReference $page */
696        foreach ( $this->everything as $page ) {
697            if ( $nsInfo->isTalk( $page->getNamespace() ) ) {
698                if ( $this->fld_subjectid ) {
699                    $getTitles[] = $nsInfo->getSubjectPage( TitleValue::newFromPage( $page ) );
700                }
701            } elseif ( $this->fld_talkid ) {
702                $getTitles[] = $nsInfo->getTalkPage( TitleValue::newFromPage( $page ) );
703            }
704        }
705        if ( $getTitles === [] ) {
706            return;
707        }
708
709        $db = $this->getDB();
710
711        // Construct a custom WHERE clause that matches
712        // all titles in $getTitles
713        $lb = $this->linkBatchFactory->newLinkBatch( $getTitles );
714        $this->resetQueryParams();
715        $this->addTables( 'page' );
716        $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
717        $this->addWhere( $lb->constructSet( 'page', $db ) );
718        $res = $this->select( __METHOD__ );
719        foreach ( $res as $row ) {
720            if ( $nsInfo->isTalk( $row->page_namespace ) ) {
721                $this->talkids[$nsInfo->getSubject( $row->page_namespace )][$row->page_title] =
722                    (int)( $row->page_id );
723            } else {
724                $this->subjectids[$nsInfo->getTalk( $row->page_namespace )][$row->page_title] =
725                    (int)( $row->page_id );
726            }
727        }
728    }
729
730    private function getDisplayTitle() {
731        $this->displaytitles = [];
732
733        $pageIds = array_keys( $this->titles );
734
735        if ( $pageIds === [] ) {
736            return;
737        }
738
739        $this->resetQueryParams();
740        $this->addTables( 'page_props' );
741        $this->addFields( [ 'pp_page', 'pp_value' ] );
742        $this->addWhereFld( 'pp_page', $pageIds );
743        $this->addWhereFld( 'pp_propname', 'displaytitle' );
744        $res = $this->select( __METHOD__ );
745
746        foreach ( $res as $row ) {
747            $this->displaytitles[$row->pp_page] = $row->pp_value;
748        }
749    }
750
751    /**
752     * Fetch the set of extra link classes associated with links to the
753     * set of titles ("link colours"), as they would appear on the
754     * given context page.
755     * @param ?LinkTarget $context_title The page context in which link
756     *   colors are determined.
757     */
758    private function getLinkClasses( ?LinkTarget $context_title = null ) {
759        if ( $this->titles === [] ) {
760            return;
761        }
762        // For compatibility with legacy GetLinkColours hook:
763        // $pagemap maps from page id to title (as prefixed db key)
764        // $classes maps from title (prefixed db key) to a space-separated
765        //   list of link classes ("link colours").
766        // The hook should not modify $pagemap, and should only append to
767        // $classes (being careful to maintain space separation).
768        $classes = [];
769        $pagemap = [];
770        foreach ( $this->titles as $pageId => $page ) {
771            $pdbk = $this->titleFormatter->getPrefixedDBkey( $page );
772            $pagemap[$pageId] = $pdbk;
773            $classes[$pdbk] = isset( $this->pageIsRedir[$pageId] ) && $this->pageIsRedir[$pageId] ? 'mw-redirect' : '';
774        }
775        // legacy hook requires a real Title, not a LinkTarget
776        $context_title = $this->titleFactory->newFromLinkTarget(
777            $context_title ?? $this->titleFactory->newMainPage()
778        );
779        $this->getHookRunner()->onGetLinkColours(
780            $pagemap, $classes, $context_title
781        );
782
783        // This API class expects the class list to be:
784        //  (a) indexed by pageid, not title, and
785        //  (b) a proper array of strings (possibly zero-length),
786        //      not a single space-separated string (possibly the empty string)
787        $this->linkClasses = [];
788        foreach ( $this->titles as $pageId => $page ) {
789            $pdbk = $this->titleFormatter->getPrefixedDBkey( $page );
790            $this->linkClasses[$pageId] = preg_split(
791                '/\s+/', $classes[$pdbk] ?? '', -1, PREG_SPLIT_NO_EMPTY
792            );
793        }
794    }
795
796    private function getVariantTitles() {
797        if ( $this->titles === [] ) {
798            return;
799        }
800        $this->variantTitles = [];
801        foreach ( $this->titles as $pageId => $page ) {
802            $this->variantTitles[$pageId] = isset( $this->displaytitles[$pageId] )
803                ? $this->getAllVariants( $this->displaytitles[$pageId] )
804                : $this->getAllVariants( $this->titleFormatter->getText( $page ), $page->getNamespace() );
805        }
806    }
807
808    private function getAllVariants( $text, $ns = NS_MAIN ) {
809        $result = [];
810        foreach ( $this->languageConverter->getVariants() as $variant ) {
811            $convertTitle = $this->languageConverter->autoConvert( $text, $variant );
812            if ( $ns !== NS_MAIN ) {
813                $convertNs = $this->languageConverter->convertNamespace( $ns, $variant );
814                $convertTitle = $convertNs . ':' . $convertTitle;
815            }
816            $result[$variant] = $convertTitle;
817        }
818        return $result;
819    }
820
821    /**
822     * Get information about watched status and put it in $this->watched
823     * and $this->notificationtimestamps
824     */
825    private function getWatchedInfo() {
826        $user = $this->getUser();
827
828        if ( !$user->isRegistered() || count( $this->everything ) == 0
829            || !$this->getAuthority()->isAllowed( 'viewmywatchlist' )
830        ) {
831            return;
832        }
833
834        $this->watched = [];
835        $this->watchlistExpiries = [];
836        $this->notificationtimestamps = [];
837
838        /** @var WatchedItem[] $items */
839        $items = $this->watchedItemStore->loadWatchedItemsBatch( $user, $this->everything );
840
841        foreach ( $items as $item ) {
842            $nsId = $item->getTarget()->getNamespace();
843            $dbKey = $item->getTarget()->getDBkey();
844
845            if ( $this->fld_watched ) {
846                $this->watched[$nsId][$dbKey] = true;
847
848                $expiry = $item->getExpiry( TS_ISO_8601 );
849                if ( $expiry ) {
850                    $this->watchlistExpiries[$nsId][$dbKey] = $expiry;
851                }
852            }
853
854            if ( $this->fld_notificationtimestamp ) {
855                $this->notificationtimestamps[$nsId][$dbKey] = $item->getNotificationTimestamp();
856            }
857        }
858    }
859
860    /**
861     * Get the count of watchers and put it in $this->watchers
862     */
863    private function getWatcherInfo() {
864        if ( count( $this->everything ) == 0 ) {
865            return;
866        }
867
868        $canUnwatchedpages = $this->getAuthority()->isAllowed( 'unwatchedpages' );
869        $unwatchedPageThreshold =
870            $this->getConfig()->get( MainConfigNames::UnwatchedPageThreshold );
871        if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
872            return;
873        }
874
875        $this->showZeroWatchers = $canUnwatchedpages;
876
877        $countOptions = [];
878        if ( !$canUnwatchedpages ) {
879            $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
880        }
881
882        $this->watchers = $this->watchedItemStore->countWatchersMultiple(
883            $this->everything,
884            $countOptions
885        );
886    }
887
888    /**
889     * Get the count of watchers who have visited recent edits and put it in
890     * $this->visitingwatchers
891     *
892     * Based on InfoAction::pageCounts
893     */
894    private function getVisitingWatcherInfo() {
895        $config = $this->getConfig();
896        $db = $this->getDB();
897
898        $canUnwatchedpages = $this->getAuthority()->isAllowed( 'unwatchedpages' );
899        $unwatchedPageThreshold = $config->get( MainConfigNames::UnwatchedPageThreshold );
900        if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
901            return;
902        }
903
904        $this->showZeroWatchers = $canUnwatchedpages;
905
906        $titlesWithThresholds = [];
907        if ( $this->titles ) {
908            $lb = $this->linkBatchFactory->newLinkBatch( $this->titles );
909
910            // Fetch last edit timestamps for pages
911            $this->resetQueryParams();
912            $this->addTables( [ 'page', 'revision' ] );
913            $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
914            $this->addWhere( [
915                'page_latest = rev_id',
916                $lb->constructSet( 'page', $db ),
917            ] );
918            $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
919            $timestampRes = $this->select( __METHOD__ );
920
921            $age = $config->get( MainConfigNames::WatchersMaxAge );
922            $timestamps = [];
923            foreach ( $timestampRes as $row ) {
924                $revTimestamp = wfTimestamp( TS_UNIX, (int)$row->rev_timestamp );
925                $timestamps[$row->page_namespace][$row->page_title] = (int)$revTimestamp - $age;
926            }
927            $titlesWithThresholds = array_map(
928                static function ( PageReference $target ) use ( $timestamps ) {
929                    return [
930                        $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
931                    ];
932                },
933                $this->titles
934            );
935        }
936
937        if ( $this->missing ) {
938            $titlesWithThresholds = array_merge(
939                $titlesWithThresholds,
940                array_map(
941                    static function ( PageReference $target ) {
942                        return [ $target, null ];
943                    },
944                    $this->missing
945                )
946            );
947        }
948        $this->visitingwatchers = $this->watchedItemStore->countVisitingWatchersMultiple(
949            $titlesWithThresholds,
950            !$canUnwatchedpages ? $unwatchedPageThreshold : null
951        );
952    }
953
954    public function getCacheMode( $params ) {
955        // Other props depend on something about the current user
956        $publicProps = [
957            'protection',
958            'talkid',
959            'subjectid',
960            'associatedpage',
961            'url',
962            'preload',
963            'displaytitle',
964            'varianttitles',
965        ];
966        if ( array_diff( (array)$params['prop'], $publicProps ) ) {
967            return 'private';
968        }
969
970        // testactions also depends on the current user
971        if ( $params['testactions'] ) {
972            return 'private';
973        }
974
975        return 'public';
976    }
977
978    public function getAllowedParams() {
979        return [
980            'prop' => [
981                ParamValidator::PARAM_ISMULTI => true,
982                ParamValidator::PARAM_TYPE => [
983                    'protection',
984                    'talkid',
985                    'watched', # private
986                    'watchers', # private
987                    'visitingwatchers', # private
988                    'notificationtimestamp', # private
989                    'subjectid',
990                    'associatedpage',
991                    'url',
992                    'readable', # private
993                    'preload',
994                    'preloadcontent', # private: checks current user's permissions
995                    'editintro', # private: checks current user's permissions
996                    'displaytitle',
997                    'varianttitles',
998                    'linkclasses', # private: stub length (and possibly hook colors)
999                    // If you add more properties here, please consider whether they
1000                    // need to be added to getCacheMode()
1001                ],
1002                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
1003                EnumDef::PARAM_DEPRECATED_VALUES => [
1004                    'readable' => true, // Since 1.32
1005                    'preload' => true, // Since 1.41
1006                ],
1007            ],
1008            'linkcontext' => [
1009                ParamValidator::PARAM_TYPE => 'title',
1010                ParamValidator::PARAM_DEFAULT => $this->titleFactory->newMainPage()->getPrefixedText(),
1011                TitleDef::PARAM_RETURN_OBJECT => true,
1012            ],
1013            'testactions' => [
1014                ParamValidator::PARAM_TYPE => 'string',
1015                ParamValidator::PARAM_ISMULTI => true,
1016            ],
1017            'testactionsdetail' => [
1018                ParamValidator::PARAM_TYPE => [ 'boolean', 'full', 'quick' ],
1019                ParamValidator::PARAM_DEFAULT => 'boolean',
1020                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
1021            ],
1022            'testactionsautocreate' => false,
1023            'preloadcustom' => [
1024                // This should be a valid and existing page title, but we don't want to validate it here,
1025                // because it's usually someone else's fault. It could emit a warning in the future.
1026                ParamValidator::PARAM_TYPE => 'string',
1027                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'preloadcontentonly' ] ],
1028            ],
1029            'preloadparams' => [
1030                ParamValidator::PARAM_ISMULTI => true,
1031                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'preloadcontentonly' ] ],
1032            ],
1033            'preloadnewsection' => [
1034                ParamValidator::PARAM_TYPE => 'boolean',
1035                ParamValidator::PARAM_DEFAULT => false,
1036                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'preloadcontentonly' ] ],
1037            ],
1038            'editintrostyle' => [
1039                ParamValidator::PARAM_TYPE => [ 'lessframes', 'moreframes' ],
1040                ParamValidator::PARAM_DEFAULT => 'moreframes',
1041                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'editintroonly' ] ],
1042            ],
1043            'editintroskip' => [
1044                ParamValidator::PARAM_TYPE => 'string',
1045                ParamValidator::PARAM_ISMULTI => true,
1046                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'editintroonly' ] ],
1047            ],
1048            'editintrocustom' => [
1049                // This should be a valid and existing page title, but we don't want to validate it here,
1050                // because it's usually someone else's fault. It could emit a warning in the future.
1051                ParamValidator::PARAM_TYPE => 'string',
1052                ApiBase::PARAM_HELP_MSG_INFO => [ [ 'editintroonly' ] ],
1053            ],
1054            'continue' => [
1055                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
1056            ],
1057        ];
1058    }
1059
1060    protected function getExamplesMessages() {
1061        $title = Title::newMainPage()->getPrefixedText();
1062        $mp = rawurlencode( $title );
1063
1064        return [
1065            "action=query&prop=info&titles={$mp}"
1066                => 'apihelp-query+info-example-simple',
1067            "action=query&prop=info&inprop=protection&titles={$mp}"
1068                => 'apihelp-query+info-example-protection',
1069        ];
1070    }
1071
1072    public function getHelpUrls() {
1073        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';
1074    }
1075}