Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
8.00% |
2 / 25 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
8.00% |
2 / 25 |
|
66.67% |
2 / 3 |
144.60 | |
0.00% |
0 / 1 |
onParserFirstCallInit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onLinksUpdate | |
0.00% |
0 / 23 |
|
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\Deferred\LinksUpdate\LinksUpdate; |
13 | use MediaWiki\Hook\LinksUpdateHook; |
14 | use MediaWiki\Hook\ParserFirstCallInitHook; |
15 | use MediaWiki\MediaWikiServices; |
16 | use MediaWiki\User\Hook\UserGetReservedNamesHook; |
17 | use MediaWiki\WikiMap\WikiMap; |
18 | use Parser; |
19 | |
20 | /** |
21 | * Hook handler functions for Babel extension. |
22 | */ |
23 | class Hooks implements |
24 | ParserFirstCallInitHook, |
25 | LinksUpdateHook, |
26 | UserGetReservedNamesHook |
27 | { |
28 | /** |
29 | * Registers the parser function hook. |
30 | * |
31 | * @param Parser $parser |
32 | */ |
33 | public function onParserFirstCallInit( $parser ): void { |
34 | $parser->setFunctionHook( 'babel', [ Babel::class, 'Render' ] ); |
35 | } |
36 | |
37 | /** |
38 | * @param LinksUpdate $linksUpdate |
39 | */ |
40 | public function onLinksUpdate( $linksUpdate ): void { |
41 | global $wgBabelCentralDb; |
42 | |
43 | $title = $linksUpdate->getTitle(); |
44 | $toCreate = $linksUpdate->getParserOutput()->getExtensionData( 'babel-tocreate' ) ?: []; |
45 | |
46 | // Create categories |
47 | foreach ( $toCreate as $category => $value ) { |
48 | $text = $linksUpdate->getParserOutput()->getExtensionData( "babel-category-text-{$category}" ); |
49 | BabelAutoCreate::create( $category, $text ); |
50 | } |
51 | |
52 | // Has to be a root userpage |
53 | if ( !$title->inNamespace( NS_USER ) || !$title->getRootTitle()->equals( $title ) ) { |
54 | return; |
55 | } |
56 | |
57 | $mwInstance = MediaWikiServices::getInstance(); |
58 | $userIdentityLookup = $mwInstance->getUserIdentityLookup(); |
59 | // And the user has to exist |
60 | $userIdentity = $userIdentityLookup->getUserIdentityByName( $title->getText() ); |
61 | if ( $userIdentity === null || !$userIdentity->isRegistered() ) { |
62 | return; |
63 | } |
64 | |
65 | $babelDB = new Database(); |
66 | $data = $linksUpdate->getParserOutput()->getExtensionData( 'babel' ) ?: []; |
67 | $changed = $babelDB->setForUser( $userIdentity->getId(), $data ); |
68 | if ( $changed ) { |
69 | $cache = $mwInstance->getMainWANObjectCache(); |
70 | $cache->touchCheckKey( $cache->makeKey( 'babel-local-languages', $userIdentity->getId() ) ); |
71 | if ( $wgBabelCentralDb === WikiMap::getCurrentWikiId() ) { |
72 | // If this is the central wiki, invalidate all of the local caches |
73 | $centralId = $mwInstance->getCentralIdLookupFactory() |
74 | ->getLookup()->centralIdFromLocalUser( $userIdentity ); |
75 | if ( $centralId ) { |
76 | $cache->touchCheckKey( $cache->makeGlobalKey( 'babel-central-languages', $centralId ) ); |
77 | } |
78 | } |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * @param array &$names |
84 | */ |
85 | public function onUserGetReservedNames( &$names ): void { |
86 | $names[] = 'msg:' . BabelAutoCreate::MSG_USERNAME; |
87 | } |
88 | } |