| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 99 |
|
| 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 |
|
| 109 |
$message->plain(), |
| 110 |
|
| 111 |
Title::makeTitle( NS_MEDIAWIKI, ucfirst( $message->getKey() ) )->getTouched(), |
| 112 |
]; |
| 113 |
} |
| 114 |
return $messages; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
public static function makeTestModule( array $info ): RL\Module { |
| 155 |
|
| 156 |
|
| 157 |
$skipTests = [ |
| 158 |
'cases/modified.json' => [ |
| 159 |
|
| 160 |
'enwiki oldparser', |
| 161 |
'enwiki parsoid', |
| 162 |
'enwiki oldparser (bullet indentation)', |
| 163 |
'enwiki parsoid (bullet indentation)', |
| 164 |
|
| 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 |
} |