Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 10
552
0.00% covered (danger)
0.00%
0 / 1
 onArticleFromTitle
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 onTitleIsAlwaysKnown
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 isGlobalUserPage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 onLinksUpdateComplete
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 invalidCacheIfGlobal
0.00% covered (danger)
0.00%
0 / 4
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
 onArticleDeleteComplete
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
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
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 Article;
20use Content;
21use IContextSource;
22use ManualLogEntry;
23use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
24use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
25use MediaWiki\Hook\LinksUpdateCompleteHook;
26use MediaWiki\Hook\TitleGetEditNoticesHook;
27use MediaWiki\Hook\TitleIsAlwaysKnownHook;
28use MediaWiki\MediaWikiServices;
29use MediaWiki\Page\Hook\ArticleDeleteCompleteHook;
30use MediaWiki\Page\Hook\ArticleFromTitleHook;
31use MediaWiki\Page\Hook\WikiPageFactoryHook;
32use MediaWiki\Revision\RevisionRecord;
33use MediaWiki\Storage\EditResult;
34use MediaWiki\Storage\Hook\PageSaveCompleteHook;
35use MediaWiki\Title\Title;
36use MediaWiki\User\User;
37use MediaWiki\User\UserIdentity;
38use MediaWiki\WikiMap\WikiMap;
39use WikiPage;
40
41class Hooks implements
42    TitleIsAlwaysKnownHook,
43    ArticleFromTitleHook,
44    LinksUpdateCompleteHook,
45    PageSaveCompleteHook,
46    ArticleDeleteCompleteHook,
47    TitleGetEditNoticesHook,
48    GetDoubleUnderscoreIDsHook,
49    WikiPageFactoryHook
50{
51
52    /**
53     * @param Title $title
54     * @param Article|null &$page
55     * @param IContextSource $context
56     */
57    public function onArticleFromTitle( $title, &$page, $context ) {
58        // If another extension's hook has already run, don't override it
59        if ( $page === null
60            && $title->inNamespace( NS_USER ) && !$title->exists()
61            && GlobalUserPage::shouldDisplayGlobalPage( $title )
62        ) {
63            $page = new GlobalUserPage(
64                $title,
65                MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'globaluserpage' )
66            );
67        }
68    }
69
70    /**
71     * Mark global user pages as known so they appear in blue
72     *
73     * @param Title $title title to check
74     * @param bool &$isKnown Whether the page should be considered known
75     */
76    public function onTitleIsAlwaysKnown( $title, &$isKnown ) {
77        if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
78            $isKnown = true;
79        }
80    }
81
82    /**
83     * Whether a page is the global user page on the central wiki
84     *
85     * @param Title $title
86     * @return bool
87     */
88    protected static function isGlobalUserPage( Title $title ) {
89        global $wgGlobalUserPageDBname;
90
91        return $wgGlobalUserPageDBname === WikiMap::getCurrentWikiId() // On the central wiki
92            && $title->inNamespace( NS_USER ) // is a user page
93            && $title->getRootTitle()->equals( $title ); // and is a root page.
94    }
95
96    /**
97     * After a LinksUpdate runs for a user page, queue remote squid purges
98     *
99     * @param LinksUpdate $lu
100     * @param mixed $ticket
101     */
102    public function onLinksUpdateComplete( $lu, $ticket ) {
103        $title = $lu->getTitle();
104        if ( self::isGlobalUserPage( $title ) ) {
105            $inv = new CacheInvalidator( $title->getText() );
106            $inv->invalidate();
107        }
108    }
109
110    private function invalidCacheIfGlobal( WikiPage $page ): void {
111        $title = $page->getTitle();
112        if ( self::isGlobalUserPage( $title ) ) {
113            $inv = new CacheInvalidator( $title->getText(), [ 'links' ] );
114            $inv->invalidate();
115        }
116    }
117
118    /**
119     * Invalidate cache on remote wikis when a new page is created
120     *
121     * @param WikiPage $page
122     * @param UserIdentity $user
123     * @param string $summary
124     * @param int $flags
125     * @param RevisionRecord $revisionRecord
126     * @param EditResult $editResult
127     */
128    public function onPageSaveComplete( $page, $user, $summary, $flags, $revisionRecord, $editResult ) {
129        $this->invalidCacheIfGlobal( $page );
130    }
131
132    /**
133     * Invalidate cache on remote wikis when a user page is deleted
134     *
135     * @param WikiPage $page
136     * @param User $user
137     * @param string $reason
138     * @param int $id
139     * @param Content|null $content
140     * @param ManualLogEntry $logEntry
141     * @param int $archivedRevisionCount
142     */
143    public function onArticleDeleteComplete(
144        $page, $user, $reason, $id, $content, $logEntry, $archivedRevisionCount
145    ) {
146        $this->invalidCacheIfGlobal( $page );
147    }
148
149    /**
150     * Show an edit notice on user pages which displays global user pages
151     * or on the central global user page.
152     *
153     * @param Title $title
154     * @param int $oldid
155     * @param array &$notices
156     */
157    public function onTitleGetEditNotices( $title, $oldid, &$notices ) {
158        if ( !$title->exists() && GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
159            $notices['globaluserpage'] = '<p><strong>' .
160                wfMessage( 'globaluserpage-editnotice' )->parse()
161                . '</strong></p>';
162        } elseif ( self::isGlobalUserPage( $title ) ) {
163            $notices['centraluserpage'] = wfMessage( 'globaluserpage-central-editnotice' )->parseAsBlock();
164        }
165    }
166
167    /**
168     * @param array &$ids
169     */
170    public function onGetDoubleUnderscoreIDs( &$ids ) {
171        $ids[] = 'noglobal';
172    }
173
174    /**
175     * @param Title $title
176     * @param WikiPage &$page
177     * @return bool
178     */
179    public function onWikiPageFactory( $title, &$page ) {
180        if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
181            $page = new WikiGlobalUserPage(
182                $title,
183                MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'globaluserpage' )
184            );
185
186            return false;
187        }
188
189        return true;
190    }
191}