Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
22.22% |
6 / 27 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
22.22% |
6 / 27 |
|
75.00% |
3 / 4 |
106.22 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
onParserFirstCallInit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onLinksUpdate | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
132 | |||
onUserGetReservedNames | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * @file |
4 | * @author Robert Leverington |
5 | * @license GPL-2.0-or-later |
6 | */ |
7 | |
8 | declare( strict_types = 1 ); |
9 | |
10 | namespace MediaWiki\Babel; |
11 | |
12 | use MediaWiki\Config\Config; |
13 | use MediaWiki\Deferred\LinksUpdate\LinksUpdate; |
14 | use MediaWiki\Hook\LinksUpdateHook; |
15 | use MediaWiki\Hook\ParserFirstCallInitHook; |
16 | use MediaWiki\User\CentralId\CentralIdLookupFactory; |
17 | use MediaWiki\User\Hook\UserGetReservedNamesHook; |
18 | use MediaWiki\User\UserIdentityLookup; |
19 | use MediaWiki\WikiMap\WikiMap; |
20 | use Parser; |
21 | use WANObjectCache; |
22 | |
23 | /** |
24 | * Hook handler functions for Babel extension. |
25 | */ |
26 | class Hooks implements |
27 | ParserFirstCallInitHook, |
28 | LinksUpdateHook, |
29 | UserGetReservedNamesHook |
30 | { |
31 | |
32 | private Config $config; |
33 | private UserIdentityLookup $userIdentityLookup; |
34 | private CentralIdLookupFactory $centralIdLookupFactory; |
35 | private WANObjectCache $mainWANObjectCache; |
36 | |
37 | public function __construct( |
38 | Config $config, |
39 | UserIdentityLookup $userIdentityLookup, |
40 | CentralIdLookupFactory $centralIdLookupFactory, |
41 | WANObjectCache $mainWANObjectCache |
42 | ) { |
43 | $this->config = $config; |
44 | $this->userIdentityLookup = $userIdentityLookup; |
45 | $this->centralIdLookupFactory = $centralIdLookupFactory; |
46 | $this->mainWANObjectCache = $mainWANObjectCache; |
47 | } |
48 | |
49 | /** |
50 | * Registers the parser function hook. |
51 | * |
52 | * @param Parser $parser |
53 | */ |
54 | public function onParserFirstCallInit( $parser ): void { |
55 | $parser->setFunctionHook( 'babel', [ Babel::class, 'Render' ] ); |
56 | } |
57 | |
58 | /** |
59 | * @param LinksUpdate $linksUpdate |
60 | */ |
61 | public function onLinksUpdate( $linksUpdate ): void { |
62 | $title = $linksUpdate->getTitle(); |
63 | $toCreate = $linksUpdate->getParserOutput()->getExtensionData( 'babel-tocreate' ) ?: []; |
64 | |
65 | // Create categories |
66 | foreach ( $toCreate as $category => $value ) { |
67 | $text = $linksUpdate->getParserOutput()->getExtensionData( "babel-category-text-{$category}" ); |
68 | BabelAutoCreate::create( $category, $text ); |
69 | } |
70 | |
71 | // Has to be a root userpage |
72 | if ( !$title->inNamespace( NS_USER ) || !$title->getRootTitle()->equals( $title ) ) { |
73 | return; |
74 | } |
75 | |
76 | // And the user has to exist |
77 | $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $title->getText() ); |
78 | if ( $userIdentity === null || !$userIdentity->isRegistered() ) { |
79 | return; |
80 | } |
81 | |
82 | $babelDB = new Database(); |
83 | $data = $linksUpdate->getParserOutput()->getExtensionData( 'babel' ) ?: []; |
84 | $changed = $babelDB->setForUser( $userIdentity->getId(), $data ); |
85 | if ( $changed ) { |
86 | $localLangKey = $this->mainWANObjectCache->makeKey( 'babel-local-languages', $userIdentity->getId() ); |
87 | $this->mainWANObjectCache->touchCheckKey( $localLangKey ); |
88 | if ( $this->config->get( 'BabelCentralDb' ) === WikiMap::getCurrentWikiId() ) { |
89 | // If this is the central wiki, invalidate all of the local caches |
90 | $centralId = $this->centralIdLookupFactory->getLookup()->centralIdFromLocalUser( $userIdentity ); |
91 | if ( $centralId ) { |
92 | $centralLangKey = $this->mainWANObjectCache->makeGlobalKey( 'babel-central-languages', $centralId ); |
93 | $this->mainWANObjectCache->touchCheckKey( $centralLangKey ); |
94 | } |
95 | } |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * @param array &$names |
101 | */ |
102 | public function onUserGetReservedNames( &$names ): void { |
103 | $names[] = 'msg:' . BabelAutoCreate::MSG_USERNAME; |
104 | } |
105 | } |