Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.45% covered (danger)
25.45%
14 / 55
9.09% covered (danger)
9.09%
1 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
25.45% covered (danger)
25.45%
14 / 55
9.09% covered (danger)
9.09%
1 / 11
306.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onRegistration
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 onArticleFromTitle
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 onLinkTargetIsAlwaysKnownBatch
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 onLinksUpdateComplete
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 invalidCacheIfGlobal
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 onPageSaveComplete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onPageDeleteComplete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onTitleGetEditNotices
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 onGetDoubleUnderscoreIDs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onWikiPageFactory
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2/**
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace MediaWiki\GlobalUserPage;
18
19use MediaWiki\Config\Config;
20use MediaWiki\Config\ConfigException;
21use MediaWiki\Config\ConfigFactory;
22use MediaWiki\Deferred\Hook\LinksUpdateCompleteHook;
23use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
24use MediaWiki\Hook\LinkTargetIsAlwaysKnownBatchHook;
25use MediaWiki\Hook\TitleGetEditNoticesHook;
26use MediaWiki\Http\HttpRequestFactory;
27use MediaWiki\JobQueue\JobQueueGroup;
28use MediaWiki\Logging\ManualLogEntry;
29use MediaWiki\Page\Hook\ArticleFromTitleHook;
30use MediaWiki\Page\Hook\PageDeleteCompleteHook;
31use MediaWiki\Page\Hook\WikiPageFactoryHook;
32use MediaWiki\Page\PageReference;
33use MediaWiki\Page\ProperPageIdentity;
34use MediaWiki\Permissions\Authority;
35use MediaWiki\Revision\RevisionRecord;
36use MediaWiki\Storage\Hook\PageSaveCompleteHook;
37use MediaWiki\Title\NamespaceInfo;
38use MediaWiki\Title\TitleValue;
39use MediaWiki\Utils\UrlUtils;
40use Wikimedia\ObjectCache\WANObjectCache;
41
42class Hooks implements
43    LinkTargetIsAlwaysKnownBatchHook,
44    ArticleFromTitleHook,
45    LinksUpdateCompleteHook,
46    PageSaveCompleteHook,
47    PageDeleteCompleteHook,
48    TitleGetEditNoticesHook,
49    GetDoubleUnderscoreIDsHook,
50    WikiPageFactoryHook
51{
52    private readonly Config $config;
53
54    public function __construct(
55        private readonly GlobalUserPageManager $manager,
56        ConfigFactory $configFactory,
57        private readonly Config $mainConfig,
58        private readonly WANObjectCache $mainWANObjectCache,
59        private readonly HttpRequestFactory $httpRequestFactory,
60        private readonly UrlUtils $urlUtils,
61        private readonly NamespaceInfo $namespaceInfo,
62        private readonly JobQueueGroup $jobQueueGroup,
63    ) {
64        $this->config = $configFactory->makeConfig( 'globaluserpage' );
65    }
66
67    public static function onRegistration(): void {
68        // Use globals instead of Config, accessing it so early does not work (T255704)
69        global $wgGlobalUserPageDBname, $wgDBname;
70
71        if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_QUIBBLE_CI' ) ) {
72            $wgGlobalUserPageDBname = $wgDBname;
73        }
74
75        if ( !$wgGlobalUserPageDBname ) {
76            throw new ConfigException( 'GlobalUserPage requires $wgGlobalUserPageDBname to be set' );
77        }
78    }
79
80    /**
81     * @inheritDoc
82     */
83    public function onArticleFromTitle( $title, &$article, $context ): void {
84        // If another extension's hook has already run, don't override it
85        if ( $article === null
86            && $title->inNamespace( NS_USER ) && !$title->exists()
87            && $this->manager->shouldDisplayGlobalPage( $title )
88        ) {
89            $article = new GlobalUserPage(
90                $title,
91                $this->config,
92                $this->mainWANObjectCache,
93                $this->manager,
94                $this->httpRequestFactory,
95                $this->urlUtils,
96                $this->namespaceInfo
97            );
98        }
99    }
100
101    /**
102     * Mark global user pages as known so they appear in blue, in batch.
103     *
104     * @inheritDoc
105     */
106    public function onLinkTargetIsAlwaysKnownBatch( array $links, array &$isAlwaysKnown ): void {
107        foreach ( $this->manager->shouldDisplayGlobalPageBatched( $links ) as $i => $shouldDisplay ) {
108            if ( $shouldDisplay ) {
109                $isAlwaysKnown[$i] = true;
110            }
111        }
112    }
113
114    /**
115     * After a LinksUpdate runs for a user page, queue remote squid purges
116     *
117     * @inheritDoc
118     */
119    public function onLinksUpdateComplete( $linksUpdate, $ticket ): void {
120        $title = $linksUpdate->getTitle();
121        if ( $this->manager->isGlobalPage( $title ) ) {
122            $inv = new CacheInvalidator(
123                $this->jobQueueGroup,
124                $this->mainConfig,
125                $title->getText()
126            );
127            $inv->invalidate();
128        }
129    }
130
131    private function invalidCacheIfGlobal( PageReference $page ): void {
132        if ( $this->manager->isGlobalPage( TitleValue::newFromPage( $page ) ) ) {
133            $inv = new CacheInvalidator(
134                $this->jobQueueGroup,
135                $this->mainConfig,
136                $page->getDBkey(),
137                [ 'links' ],
138            );
139            $inv->invalidate();
140        }
141    }
142
143    /**
144     * Invalidate cache on remote wikis when a new page is created
145     *
146     * @inheritDoc
147     */
148    public function onPageSaveComplete( $wikiPage, $user, $summary, $flags, $revisionRecord, $editResult ): void {
149        $this->invalidCacheIfGlobal( $wikiPage );
150    }
151
152    /**
153     * @inheritDoc
154     */
155    public function onPageDeleteComplete(
156        ProperPageIdentity $page, Authority $deleter, string $reason, int $pageID, RevisionRecord $deletedRev,
157        ManualLogEntry $logEntry, int $archivedRevisionCount
158    ): void {
159        $this->invalidCacheIfGlobal( $page );
160    }
161
162    /**
163     * Show an edit notice on user pages which displays global user pages
164     * or on the central global user page.
165     *
166     * @inheritDoc
167     */
168    public function onTitleGetEditNotices( $title, $oldid, &$notices ): void {
169        if ( !$title->exists() && $this->manager->shouldDisplayGlobalPage( $title ) ) {
170            $notices['globaluserpage'] = '<p><strong>' .
171                wfMessage( 'globaluserpage-editnotice' )->parse()
172                . '</strong></p>';
173        } elseif ( $this->manager->isGlobalPage( $title ) ) {
174            $notices['centraluserpage'] = wfMessage( 'globaluserpage-central-editnotice' )->parseAsBlock();
175        }
176    }
177
178    /**
179     * @inheritDoc
180     */
181    public function onGetDoubleUnderscoreIDs( &$doubleUnderscoreIDs ): void {
182        $doubleUnderscoreIDs[] = 'noglobal';
183    }
184
185    /**
186     * @inheritDoc
187     */
188    public function onWikiPageFactory( $title, &$page ): bool {
189        if ( $this->manager->shouldDisplayGlobalPage( $title ) ) {
190            $page = new WikiGlobalUserPage(
191                $title,
192                $this->config,
193                $this->mainWANObjectCache,
194                $this->httpRequestFactory,
195                $this->urlUtils
196            );
197
198            return false;
199        }
200
201        return true;
202    }
203}