Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
45.07% |
32 / 71 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
WikibaseLexemeHooks | |
45.07% |
32 / 71 |
|
16.67% |
1 / 6 |
64.90 | |
0.00% |
0 / 1 |
onCanonicalNamespaces | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
6 | |||
registerNamespace | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
onInfoAction | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
onScribuntoExternalLibraries | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
getLexemeViewLanguages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
onLoadExtensionSchemaUpdates | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme; |
4 | |
5 | use MediaWiki\Context\IContextSource; |
6 | use MediaWiki\Hook\CanonicalNamespacesHook; |
7 | use MediaWiki\Hook\InfoActionHook; |
8 | use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook; |
9 | use MediaWiki\MediaWikiServices; |
10 | use Wikibase\Client\WikibaseClient; |
11 | use Wikibase\Lexeme\Maintenance\FixPagePropsSortkey; |
12 | use Wikibase\Lexeme\MediaWiki\Actions\InfoActionHookHandler; |
13 | use Wikibase\Lexeme\MediaWiki\Scribunto\WikibaseLexemeEntityFormLibrary; |
14 | use Wikibase\Lexeme\MediaWiki\Scribunto\WikibaseLexemeEntityLexemeLibrary; |
15 | use Wikibase\Lexeme\MediaWiki\Scribunto\WikibaseLexemeEntitySenseLibrary; |
16 | use Wikibase\Lexeme\MediaWiki\Scribunto\WikibaseLexemeLibrary; |
17 | use Wikibase\Lib\WikibaseSettings; |
18 | use Wikibase\Repo\WikibaseRepo; |
19 | use Wikimedia\Assert\Assert; |
20 | |
21 | /** |
22 | * MediaWiki hook handlers for the Wikibase Lexeme extension. |
23 | * |
24 | * @license GPL-2.0-or-later |
25 | * @author Amir Sarabadani <ladsgroup@gmail.com> |
26 | */ |
27 | class WikibaseLexemeHooks implements |
28 | InfoActionHook, |
29 | CanonicalNamespacesHook, |
30 | LoadExtensionSchemaUpdatesHook |
31 | { |
32 | |
33 | /** |
34 | * Hook to register the default namespace names. |
35 | * |
36 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/CanonicalNamespaces |
37 | * |
38 | * @param string[] &$namespaces |
39 | */ |
40 | public function onCanonicalNamespaces( &$namespaces ) { |
41 | // XXX: ExtensionProcessor should define an extra config object for every extension. |
42 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
43 | |
44 | // Do not register lexeme namespaces when the repo is not enabled. |
45 | if ( !WikibaseSettings::isRepoEnabled() || !$config->get( 'LexemeEnableRepo' ) ) { |
46 | return; |
47 | } |
48 | |
49 | // Setting the namespace to false disabled automatic registration. |
50 | $lexemeNamespaceId = $config->get( 'LexemeNamespace' ); |
51 | if ( $lexemeNamespaceId !== false ) { |
52 | Assert::parameter( |
53 | is_int( $lexemeNamespaceId ) && |
54 | $lexemeNamespaceId >= 100 && |
55 | ( $lexemeNamespaceId % 2 ) === 0, |
56 | '$wgLexemeNamespace', |
57 | 'Namespace ID must be an even integer, at least 100' |
58 | ); |
59 | $lexemeNamespaceName = 'Lexeme'; |
60 | $namespaces = self::registerNamespace( |
61 | $namespaces, |
62 | $lexemeNamespaceId, |
63 | $lexemeNamespaceName |
64 | ); |
65 | |
66 | $talkNamespaceId = $lexemeNamespaceId + 1; |
67 | $talkNamespaceName = $lexemeNamespaceName . '_talk'; |
68 | $namespaces = self::registerNamespace( |
69 | $namespaces, |
70 | $talkNamespaceId, |
71 | $talkNamespaceName |
72 | ); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * @param string[] $namespaces |
78 | * @param int $namespaceId |
79 | * @param string $namespaceName |
80 | * |
81 | * @return string[] |
82 | * @throws \RuntimeException If namespace ID is already registered with another name |
83 | */ |
84 | private static function registerNamespace( array $namespaces, $namespaceId, $namespaceName ) { |
85 | if ( |
86 | isset( $namespaces[$namespaceId] ) && |
87 | $namespaces[$namespaceId] !== $namespaceName |
88 | ) { |
89 | throw new \RuntimeException( |
90 | "Tried to register `$namespaceName` namespace with ID `$namespaceId`, " . |
91 | "but ID was already occupied by `{$namespaces[$namespaceId]} namespace`" |
92 | ); |
93 | } |
94 | |
95 | $namespaces[$namespaceId] = $namespaceName; |
96 | |
97 | return $namespaces; |
98 | } |
99 | |
100 | /** |
101 | * Adds the Wikis using the entity in action=info |
102 | * |
103 | * @param IContextSource $context |
104 | * @param array[] &$pageInfo |
105 | */ |
106 | public function onInfoAction( $context, &$pageInfo ) { |
107 | $services = MediaWikiServices::getInstance(); |
108 | $config = $services->getMainConfig(); |
109 | if ( !$config->get( 'LexemeEnableRepo' ) ) { |
110 | return; |
111 | } |
112 | |
113 | $namespaceChecker = WikibaseRepo::getEntityNamespaceLookup(); |
114 | $infoActionHookHandler = new InfoActionHookHandler( |
115 | $namespaceChecker, |
116 | WikibaseRepo::getEntityIdLookup(), |
117 | $services->getPageProps(), |
118 | $context |
119 | ); |
120 | |
121 | $pageInfo = $infoActionHookHandler->handle( $context, $pageInfo ); |
122 | } |
123 | |
124 | /** @inheritDoc */ |
125 | public static function onScribuntoExternalLibraries( $engine, array &$extraLibraries ) { |
126 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
127 | if ( !$config->get( 'LexemeEnableDataTransclusion' ) ) { |
128 | return; |
129 | } |
130 | $clientSettings = WikibaseClient::getSettings(); |
131 | if ( !$clientSettings->getSetting( 'allowDataTransclusion' ) ) { |
132 | return; |
133 | } |
134 | |
135 | if ( $engine == 'lua' ) { |
136 | $extraLibraries['mw.wikibase.lexeme'] |
137 | = WikibaseLexemeLibrary::class; |
138 | $extraLibraries['mw.wikibase.lexeme.entity.lexeme'] = [ |
139 | 'class' => WikibaseLexemeEntityLexemeLibrary::class, |
140 | 'deferLoad' => true, |
141 | ]; |
142 | $extraLibraries['mw.wikibase.lexeme.entity.form'] = [ |
143 | 'class' => WikibaseLexemeEntityFormLibrary::class, |
144 | 'deferLoad' => true, |
145 | ]; |
146 | $extraLibraries['mw.wikibase.lexeme.entity.sense'] = [ |
147 | 'class' => WikibaseLexemeEntitySenseLibrary::class, |
148 | 'deferLoad' => true, |
149 | ]; |
150 | } |
151 | } |
152 | |
153 | public static function getLexemeViewLanguages(): array { |
154 | return [ |
155 | 'lexemeTermLanguages' => MediaWikiServices::getInstance() |
156 | ->getService( 'WikibaseLexemeTermLanguages' )->getLanguages(), |
157 | ]; |
158 | } |
159 | |
160 | /** @inheritDoc */ |
161 | public function onLoadExtensionSchemaUpdates( $updater ) { |
162 | $updater->addPostDatabaseUpdateMaintenance( FixPagePropsSortkey::class ); |
163 | } |
164 | |
165 | } |