summaryrefslogtreecommitdiff
blob: c5c64fbc1f1b0d9c9cc4697be29ce714d74647f9 (plain)
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
<?php
/**
 * Static functions for extension.
 *
 * @file
 * @author Robert Leverington
 * @license GPL-2.0-or-later
 */

use MediaWiki\MediaWikiServices;

/**
 * Static functions for Babel extension.
 */
class BabelStatic {
	/**
	 * Registers the parser function hook.
	 *
	 * @param Parser $parser
	 */
	public static function onParserFirstCallInit( Parser $parser ) {
		$parser->setFunctionHook( 'babel', [ 'Babel', 'Render' ] );
	}

	/**
	 * @param DatabaseUpdater $updater
	 */
	public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
		$dir = dirname( __DIR__ ) . '/sql/';
		$dbType = $updater->getDB()->getType();

		if ( $dbType === 'mysql' ) {
			$updater->addExtensionTable( 'babel',
				$dir . 'tables-generated.sql'
			);
			$updater->modifyExtensionField(
				'babel',
				'babel_lang',
				$dir . 'babel-babel_lang-length-type.sql'
			);
			$updater->modifyExtensionField(
				'babel',
				'babel_level',
				$dir . 'babel-babel_level-type.sql'
			);
		} elseif ( $dbType === 'sqlite' ) {
			$updater->addExtensionTable( 'babel',
				$dir . 'sqlite/tables-generated.sql'
			);

			$updater->modifyExtensionField(
				'babel',
				'babel_lang',
				$dir . 'sqlite/babel-babel_lang-length.sql'
			);
		} elseif ( $dbType === 'postgres' ) {
			$updater->addExtensionTable( 'babel',
				$dir . 'postgres/tables-generated.sql'
			);
		}
	}

	/**
	 * @param LinksUpdate $linksUpdate
	 */
	public static function onLinksUpdate( LinksUpdate $linksUpdate ) {
		global $wgBabelCentralDb;

		$title = $linksUpdate->getTitle();
		// Has to be a root userpage
		if ( !$title->inNamespace( NS_USER ) || !$title->getRootTitle()->equals( $title ) ) {
			return;
		}

		// And the user has to exist
		$user = User::newFromName( $title->getText() );
		if ( !$user || !$user->getId() ) {
			return;
		}

		$babelDB = new MediaWiki\Babel\Database();
		$data = $linksUpdate->getParserOutput()->getExtensionData( 'babel' ) ?: [];
		$changed = $babelDB->setForUser( $user->getId(), $data );
		if ( $changed ) {
			$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
			$cache->touchCheckKey( $cache->makeKey( 'babel-local-languages', $user->getId() ) );
			if ( $wgBabelCentralDb === wfWikiID() ) {
				// If this is the central wiki, invalidate all of the local caches
				if ( method_exists( MediaWikiServices::class, 'getCentralIdLookupFactory' ) ) {
					// MW1.37+
					$centralId = MediaWikiServices::getInstance()->getCentralIdLookupFactory()
						->getLookup()->centralIdFromLocalUser( $user );
				} else {
					$centralId = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
				}
				if ( $centralId ) {
					$cache->touchCheckKey( $cache->makeGlobalKey( 'babel-central-languages', $centralId ) );
				}
			}
		}
	}
}