Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.68% covered (danger)
23.68%
9 / 38
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
23.68% covered (danger)
23.68%
9 / 38
10.00% covered (danger)
10.00%
1 / 10
258.13
0.00% covered (danger)
0.00%
0 / 1
 onArticleFromTitle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 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 / 5
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
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
3.46
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\Content\Content;
20use MediaWiki\Context\IContextSource;
21use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
22use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
23use MediaWiki\Hook\LinksUpdateCompleteHook;
24use MediaWiki\Hook\TitleGetEditNoticesHook;
25use MediaWiki\Hook\TitleIsAlwaysKnownHook;
26use MediaWiki\Logging\ManualLogEntry;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Page\Article;
29use MediaWiki\Page\Hook\ArticleDeleteCompleteHook;
30use MediaWiki\Page\Hook\ArticleFromTitleHook;
31use MediaWiki\Page\Hook\WikiPageFactoryHook;
32use MediaWiki\Page\WikiPage;
33use MediaWiki\Revision\RevisionRecord;
34use MediaWiki\Storage\EditResult;
35use MediaWiki\Storage\Hook\PageSaveCompleteHook;
36use MediaWiki\Title\Title;
37use MediaWiki\User\User;
38use MediaWiki\User\UserIdentity;
39use MediaWiki\WikiMap\WikiMap;
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        // On the central wiki
92        return $wgGlobalUserPageDBname === WikiMap::getCurrentWikiId()
93            // is a user page
94            && $title->inNamespace( NS_USER )
95            // and is a root page.
96            && $title->getRootTitle()->equals( $title );
97    }
98
99    /**
100     * After a LinksUpdate runs for a user page, queue remote squid purges
101     *
102     * @param LinksUpdate $lu
103     * @param mixed $ticket
104     */
105    public function onLinksUpdateComplete( $lu, $ticket ) {
106        $title = $lu->getTitle();
107        if ( self::isGlobalUserPage( $title ) ) {
108            $inv = new CacheInvalidator( $title->getText() );
109            $inv->invalidate();
110        }
111    }
112
113    private function invalidCacheIfGlobal( WikiPage $page ): void {
114        $title = $page->getTitle();
115        if ( self::isGlobalUserPage( $title ) ) {
116            $inv = new CacheInvalidator( $title->getText(), [ 'links' ] );
117            $inv->invalidate();
118        }
119    }
120
121    /**
122     * Invalidate cache on remote wikis when a new page is created
123     *
124     * @param WikiPage $page
125     * @param UserIdentity $user
126     * @param string $summary
127     * @param int $flags
128     * @param RevisionRecord $revisionRecord
129     * @param EditResult $editResult
130     */
131    public function onPageSaveComplete( $page, $user, $summary, $flags, $revisionRecord, $editResult ) {
132        $this->invalidCacheIfGlobal( $page );
133    }
134
135    /**
136     * Invalidate cache on remote wikis when a user page is deleted
137     *
138     * @param WikiPage $page
139     * @param User $user
140     * @param string $reason
141     * @param int $id
142     * @param Content|null $content
143     * @param ManualLogEntry $logEntry
144     * @param int $archivedRevisionCount
145     */
146    public function onArticleDeleteComplete(
147        $page, $user, $reason, $id, $content, $logEntry, $archivedRevisionCount
148    ) {
149        $this->invalidCacheIfGlobal( $page );
150    }
151
152    /**
153     * Show an edit notice on user pages which displays global user pages
154     * or on the central global user page.
155     *
156     * @param Title $title
157     * @param int $oldid
158     * @param array &$notices
159     */
160    public function onTitleGetEditNotices( $title, $oldid, &$notices ) {
161        if ( !$title->exists() && GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
162            $notices['globaluserpage'] = '<p><strong>' .
163                wfMessage( 'globaluserpage-editnotice' )->parse()
164                . '</strong></p>';
165        } elseif ( self::isGlobalUserPage( $title ) ) {
166            $notices['centraluserpage'] = wfMessage( 'globaluserpage-central-editnotice' )->parseAsBlock();
167        }
168    }
169
170    /**
171     * @param array &$ids
172     */
173    public function onGetDoubleUnderscoreIDs( &$ids ) {
174        $ids[] = 'noglobal';
175    }
176
177    /**
178     * @param Title $title
179     * @param WikiPage &$page
180     * @return bool
181     */
182    public function onWikiPageFactory( $title, &$page ) {
183        if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
184            $page = new WikiGlobalUserPage(
185                $title,
186                MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'globaluserpage' )
187            );
188
189            return false;
190        }
191
192        return true;
193    }
194}