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