/
Hooks.php
191 lines (175 loc) · 5.36 KB
/
Hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace MediaWiki\GlobalUserPage;
use Article;
use Content;
use IContextSource;
use ManualLogEntry;
use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
use MediaWiki\Hook\LinksUpdateCompleteHook;
use MediaWiki\Hook\TitleGetEditNoticesHook;
use MediaWiki\Hook\TitleIsAlwaysKnownHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\Hook\ArticleDeleteCompleteHook;
use MediaWiki\Page\Hook\ArticleFromTitleHook;
use MediaWiki\Page\Hook\WikiPageFactoryHook;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Storage\EditResult;
use MediaWiki\Storage\Hook\PageSaveCompleteHook;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\User\UserIdentity;
use MediaWiki\WikiMap\WikiMap;
use WikiPage;
class Hooks implements
TitleIsAlwaysKnownHook,
ArticleFromTitleHook,
LinksUpdateCompleteHook,
PageSaveCompleteHook,
ArticleDeleteCompleteHook,
TitleGetEditNoticesHook,
GetDoubleUnderscoreIDsHook,
WikiPageFactoryHook
{
/**
* @param Title $title
* @param Article|null &$page
* @param IContextSource $context
*/
public function onArticleFromTitle( $title, &$page, $context ) {
// If another extension's hook has already run, don't override it
if ( $page === null
&& $title->inNamespace( NS_USER ) && !$title->exists()
&& GlobalUserPage::shouldDisplayGlobalPage( $title )
) {
$page = new GlobalUserPage(
$title,
MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'globaluserpage' )
);
}
}
/**
* Mark global user pages as known so they appear in blue
*
* @param Title $title title to check
* @param bool &$isKnown Whether the page should be considered known
*/
public function onTitleIsAlwaysKnown( $title, &$isKnown ) {
if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
$isKnown = true;
}
}
/**
* Whether a page is the global user page on the central wiki
*
* @param Title $title
* @return bool
*/
protected static function isGlobalUserPage( Title $title ) {
global $wgGlobalUserPageDBname;
return $wgGlobalUserPageDBname === WikiMap::getCurrentWikiId() // On the central wiki
&& $title->inNamespace( NS_USER ) // is a user page
&& $title->getRootTitle()->equals( $title ); // and is a root page.
}
/**
* After a LinksUpdate runs for a user page, queue remote squid purges
*
* @param LinksUpdate $lu
* @param mixed $ticket
*/
public function onLinksUpdateComplete( $lu, $ticket ) {
$title = $lu->getTitle();
if ( self::isGlobalUserPage( $title ) ) {
$inv = new CacheInvalidator( $title->getText() );
$inv->invalidate();
}
}
private function invalidCacheIfGlobal( WikiPage $page ): void {
$title = $page->getTitle();
if ( self::isGlobalUserPage( $title ) ) {
$inv = new CacheInvalidator( $title->getText(), [ 'links' ] );
$inv->invalidate();
}
}
/**
* Invalidate cache on remote wikis when a new page is created
*
* @param WikiPage $page
* @param UserIdentity $user
* @param string $summary
* @param int $flags
* @param RevisionRecord $revisionRecord
* @param EditResult $editResult
*/
public function onPageSaveComplete( $page, $user, $summary, $flags, $revisionRecord, $editResult ) {
$this->invalidCacheIfGlobal( $page );
}
/**
* Invalidate cache on remote wikis when a user page is deleted
*
* @param WikiPage $page
* @param User $user
* @param string $reason
* @param int $id
* @param Content|null $content
* @param ManualLogEntry $logEntry
* @param int $archivedRevisionCount
*/
public function onArticleDeleteComplete(
$page, $user, $reason, $id, $content, $logEntry, $archivedRevisionCount
) {
$this->invalidCacheIfGlobal( $page );
}
/**
* Show an edit notice on user pages which displays global user pages
* or on the central global user page.
*
* @param Title $title
* @param int $oldid
* @param array &$notices
*/
public function onTitleGetEditNotices( $title, $oldid, &$notices ) {
if ( !$title->exists() && GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
$notices['globaluserpage'] = '<p><strong>' .
wfMessage( 'globaluserpage-editnotice' )->parse()
. '</strong></p>';
} elseif ( self::isGlobalUserPage( $title ) ) {
$notices['centraluserpage'] = wfMessage( 'globaluserpage-central-editnotice' )->parseAsBlock();
}
}
/**
* @param array &$ids
*/
public function onGetDoubleUnderscoreIDs( &$ids ) {
$ids[] = 'noglobal';
}
/**
* @param Title $title
* @param WikiPage &$page
* @return bool
*/
public function onWikiPageFactory( $title, &$page ) {
if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
$page = new WikiGlobalUserPage(
$title,
MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'globaluserpage' )
);
return false;
}
return true;
}
}