Debian Codegraph @

extensions/DiscussionTools/includes/ResourceLoaderData.php

raw symbols SCIP
1 <?php
2 /**
3 * Utilities for ResourceLoader modules used by DiscussionTools.
4 *
5 * @file
6 * @ingroup Extensions
7 * @license MIT
8 */
9
10 namespace MediaWiki\Extension\DiscussionTools;
11
12 use MediaWiki\Config\Config;
13 use MediaWiki\Extension\DiscussionTools\Hooks\HookRunner;
14 use MediaWiki\MediaWikiServices;
15 use MediaWiki\Registration\ExtensionRegistry;
16 use MediaWiki\ResourceLoader as RL;
17 use MediaWiki\Title\Title;
18 use MessageLocalizer;
19
20 class ResourceLoaderData {
21 /**
22 * Used for the 'ext.discussionTools.init' module and the test module.
23 *
24 * We need all of this data *in content language*. Some of it is already available in JS, but only
25 * in client language, so it's useless for us (e.g. digit transform table, month name messages).
26 */
27 public static function getLocalData(
28 RL\Context $context, Config $config, ?string $langCode = null
29 ): array {
30 $services = MediaWikiServices::getInstance();
31
32 if ( $langCode === null ) {
33 $langData = $services->getService( 'DiscussionTools.LanguageData' );
34 } else {
35 $langData = new LanguageData(
36 $services->getMainConfig(),
37 $services->getLanguageFactory()->getLanguage( $langCode ),
38 $services->getLanguageConverterFactory(),
39 $services->getSpecialPageFactory()
40 );
41 }
42
43 return $langData->getLocalData();
44 }
45
46 /**
47 * Return messages in content language, for use in a ResourceLoader module.
48 */
49 public static function getContentLanguageMessages(
50 RL\Context $context, Config $config, array $messagesKeys = []
51 ): array {
52 return array_combine(
53 $messagesKeys,
54 array_map( static function ( $key ) {
55 return wfMessage( $key )->inContentLanguage()->text();
56 }, $messagesKeys )
57 );
58 }
59
60 /**
61 * Return information about terms-of-use messages.
62 *
63 * @param MessageLocalizer $context
64 * @param Config $config
65 * @return array[] Map from internal name to array of parameters for MessageLocalizer::msg()
66 * @phan-return non-empty-array[]
67 */
68 private static function getTermsOfUseMessages(
69 MessageLocalizer $context, Config $config
70 ): array {
71 $messages = [
72 'reply' => [ 'discussiontools-replywidget-terms-click',
73 $context->msg( 'discussiontools-replywidget-reply' )->text() ],
74 'newtopic' => [ 'discussiontools-replywidget-terms-click',
75 $context->msg( 'discussiontools-replywidget-newtopic' )->text() ],
76 ];
77
78 $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
79 $hookRunner->onDiscussionToolsTermsOfUseMessages( $messages, $context, $config );
80
81 return $messages;
82 }
83
84 /**
85 * Return parsed terms-of-use messages, for use in a ResourceLoader module.
86 */
87 public static function getTermsOfUseMessagesParsed(
88 MessageLocalizer $context, Config $config
89 ): array {
90 $messages = static::getTermsOfUseMessages( $context, $config );
91 foreach ( $messages as &$msg ) {
92 $msg = $context->msg( ...$msg )->parse();
93 }
94 return $messages;
95 }
96
97 /**
98 * Return information about terms-of-use messages, for use in a ResourceLoader module as
99 * 'versionCallback'. This is to avoid calling the parser from version invalidation code.
100 */
101 public static function getTermsOfUseMessagesVersion(
102 MessageLocalizer $context, Config $config
103 ): array {
104 $messages = static::getTermsOfUseMessages( $context, $config );
105 foreach ( $messages as &$msg ) {
106 $message = $context->msg( ...$msg );
107 $msg = [
108 // Include the text of the message, in case the canonical translation changes
109 $message->plain(),
110 // Include the page touched time, in case the on-wiki override is invalidated
111 Title::makeTitle( NS_MEDIAWIKI, ucfirst( $message->getKey() ) )->getTouched(),
112 ];
113 }
114 return $messages;
115 }
116
117 /**
118 * Add optional array values to a ResourceLoader module definition depending on loaded extensions.
119 *
120 * "optional": {
121 * "MyExtension": {
122 * "dependencies": [ ... ],
123 * "messages": [ ... ],
124 * "packageFiles": [ ... ],
125 * ...
126 * }
127 * }
128 */
129 public static function addOptional( array $info ): RL\Module {
130 $extensionRegistry = ExtensionRegistry::getInstance();
131
132 if ( isset( $info['optional'] ) ) {
133 foreach ( $info['optional'] as $ext => $extOptions ) {
134 if ( $extensionRegistry->isLoaded( $ext ) ) {
135 foreach ( $extOptions as $key => $values ) {
136 if ( !isset( $info[$key] ) ) {
137 $info[$key] = [];
138 }
139 // TODO: Support non-array properties
140 $info[$key] = array_merge( $info[$key], (array)$values );
141 }
142 }
143 }
144 }
145
146 $class = $info['class'] ?? RL\FileModule::class;
147 return new $class( $info );
148 }
149
150 /**
151 * Generate the test module that includes all of the test data, based on the JSON files defining
152 * test cases.
153 */
154 public static function makeTestModule( array $info ): RL\Module {
155 // Some tests rely on PHP-only features or are too large for the Karma test runner.
156 // Skip them here. They are still tested in the PHP version.
157 $skipTests = [
158 'cases/modified.json' => [
159 // Too large, cause timeouts in Karma test runner
160 'enwiki oldparser',
161 'enwiki parsoid',
162 'enwiki oldparser (bullet indentation)',
163 'enwiki parsoid (bullet indentation)',
164 // These tests depend on #getTranscludedFrom(), which we didn't implement in JS
165 'arwiki no-paragraph parsoid',
166 'enwiki parsoid',
167 'Many comments consisting of a block template and a paragraph',
168 'Comment whose range almost exactly matches a template, but is not considered transcluded (T313100)',
169 'Accidental complex transclusion (T265528)',
170 'Accidental complex transclusion (T313093)',
171 ],
172 ];
173 $info['packageFiles'][] = [
174 'name' => 'skip.json',
175 'type' => 'data',
176 'content' => $skipTests,
177 ];
178
179 $keys = [ 'config', 'data', 'dom', 'expected' ];
180 foreach ( $info['testData'] as $path ) {
181 $info['packageFiles'][] = $path;
182 $localPath = $info['localBasePath'] . '/' . $path;
183 $data = json_decode( file_get_contents( $localPath ), true );
184 foreach ( $data as $case ) {
185 if ( isset( $case['name'] ) && in_array( $case['name'], $skipTests[$path] ?? [], true ) ) {
186 continue;
187 }
188 foreach ( $case as $key => $val ) {
189 if ( in_array( $key, $keys, true ) && is_string( $val ) ) {
190 if ( str_ends_with( $val, '.json' ) ) {
191 $info['packageFiles'][] = substr( $val, strlen( '../' ) );
192 } elseif ( str_ends_with( $val, '.html' ) ) {
193 $info['packageFiles'][] = [
194 'name' => $val,
195 'type' => 'data',
196 'callback' => static function () use ( $info, $val ) {
197 $localPath = $info['localBasePath'] . '/' . $val;
198 return file_get_contents( $localPath );
199 },
200 'versionCallback' => static function () use ( $val ) {
201 return new RL\FilePath( $val );
202 },
203 ];
204 }
205 }
206 }
207 }
208 }
209 $class = $info['class'] ?? RL\FileModule::class;
210 return new $class( $info );
211 }
212 }