| 1 |
<?php |
| 2 |
|
| 3 |
/* phpcs:disable MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic */ |
| 4 |
/* phpcs:disable Generic.Files.LineLength.TooLong */ |
| 5 |
/* phpcs:disable MediaWiki.Commenting.FunctionComment.WrongStyle */ |
| 6 |
|
| 7 |
/* ------------------------------------------------------------------------------ |
| 8 |
* This script is meant to be used as a tool to process Cite's i18n messages used |
| 9 |
* for formatting Cite numbers and generate equivalent CSS rules that generate |
| 10 |
* almost equivalent output. These rules are meant to be tweaked before being added |
| 11 |
* to a wiki's MediaWiki:Common.css file. |
| 12 |
* |
| 13 |
* Run this script as: php tools/processCiteMessages.php <optional-args> |
| 14 |
* |
| 15 |
* This script assumes a checkout of the Cite extension in ../../core/extensions/Cite |
| 16 |
* If not, pass the cite directory path as ARGV[1] |
| 17 |
* |
| 18 |
* This script assumes the presence of a number of JSON files: |
| 19 |
* - JSON output of https://global-search.toolforge.org/?namespaces=8&q=.%2A®ex=1&title=Cite%20reference.%2A |
| 20 |
* stored in "./cite.references.messages.json" OR passed in via ARGV[2] |
| 21 |
* - JSON output of https://global-search.toolforge.org/?namespaces=8&q=.%2A®ex=1&title=Cite%20link%20label.%2A |
| 22 |
* stored in "/cite.link_label_groups.messages.json" OR passed in via ARGV[3] |
| 23 |
* - Localized separators stored in "./localized.separators.json" extracted |
| 24 |
* from the separators arrays in the various language message files in |
| 25 |
* mediawiki core repo. |
| 26 |
* - Message fallback chains in "./language.fallbacks.json" |
| 27 |
* |
| 28 |
* It dumps the CSS rules per wiki (found in the JSON files) to stdout. |
| 29 |
* ------------------------------------------------------------------------------ */ |
| 30 |
|
| 31 |
// Message fallback chain generated with this CLI in MW core's |
| 32 |
// languages/messages/ directory and edited lightly. |
| 33 |
// |
| 34 |
// git grep '$fallback ' | sed 's/^Messages\(.*\)\..* '"'"'\(.*\).;.*/\L"\1":["\2"]/;s/_/-/;s/, /", "/g;s/$/,/g;' > /tmp/language.fallbacks.json |
| 35 |
|
| 36 |
function wfJsonKeyToTitleSuffix( string $key ): string { |
| 37 |
return preg_replace( '/cite_/', '', $key ); |
| 38 |
} |
| 39 |
|
| 40 |
function wfTitleToKey( string $title ): string { |
| 41 |
return strtr( preg_replace( '/^.*?:Cite /', '', $title ), ' ', '_' ); |
| 42 |
} |
| 43 |
|
| 44 |
function wfFetchRevisionContent( string $wiki, string $title ): string { |
| 45 |
$out = []; |
| 46 |
$cache = __DIR__ . "/../cached_titles"; |
| 47 |
$cachedFile = "$cache/$wiki." . wfTitleToKey( $title ); |
| 48 |
if ( file_exists( $cachedFile ) ) { |
| 49 |
$res = file_get_contents( $cachedFile ); |
| 50 |
} else { |
| 51 |
$path = __DIR__ . "/../FetchWt.php"; |
| 52 |
$command = "php $path --domain $wiki.org --title '$title'"; |
| 53 |
/* phpcs:disable MediaWiki.Usage.ForbiddenFunctions.exec */ |
| 54 |
exec( $command, $out ); |
| 55 |
$res = implode( $out ); |
| 56 |
file_put_contents( $cachedFile, $res ); |
| 57 |
} |
| 58 |
# error_log("GOT $res instead of missing for $wiki:$title"); |
| 59 |
return $res; |
| 60 |
} |
| 61 |
|
| 62 |
function wfProcessRow( array $row, array &$wikiInfo ): void { |
| 63 |
$wiki = $row['wiki']; |
| 64 |
# Debug |
| 65 |
# if ( $wiki !== "vec.wikisource" ) { |
| 66 |
# return; |
| 67 |
# } |
| 68 |
$msgKey = $row['title']; |
| 69 |
$content = trim( $row['source_text'] ); |
| 70 |
if ( !$content ) { |
| 71 |
# error_log("MISSING content for $wiki:$msgKey"); |
| 72 |
$content = wfFetchRevisionContent( $wiki, $msgKey ); |
| 73 |
} |
| 74 |
$msgKey = wfTitleToKey( $msgKey ); |
| 75 |
// \u0026amp;#32 =>   => ' ' |
| 76 |
$content = preg_replace( '/\s+/', ' ', |
| 77 |
html_entity_decode( |
| 78 |
preg_replace( "#^<span[^>]*>([\s\S]*)</span>$#", "$1", html_entity_decode( $content ) ) |
| 79 |
) |
| 80 |
); |
| 81 |
# error_log("wiki:$wiki; msg: $msgKey; content: $content"); |
| 82 |
$wikiInfo[$wiki][$msgKey] = $content; |
| 83 |
} |
| 84 |
|
| 85 |
function wfAddCSSForIBTagsAndProcessMsg( array &$cssRules, string &$msg ): void { |
| 86 |
if ( preg_match( "/<b>/", $msg ) ) { |
| 87 |
$cssRules[] = "font-weight: bold;"; |
| 88 |
} |
| 89 |
if ( preg_match( "/<i>/", $msg ) ) { |
| 90 |
$cssRules[] = "font-style: italic;"; |
| 91 |
} |
| 92 |
$msg = preg_replace( "/<\/?(i|b)>/", "", $msg ); |
| 93 |
} |
| 94 |
|
| 95 |
function wfCheckIfAlphabetic( array $symbols ): array { |
| 96 |
$base = []; |
| 97 |
$prefix = ""; |
| 98 |
$i = -1; |
| 99 |
foreach ( $symbols as $s ) { |
| 100 |
if ( mb_strlen( $s ) === 1 ) { |
| 101 |
$base[] = $s; |
| 102 |
} else { |
| 103 |
if ( $i === count( $base ) ) { |
| 104 |
return $base; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $prefix === "" ) { |
| 108 |
$i = 0; |
| 109 |
$prefix = mb_substr( $s, 0, 1 ); |
| 110 |
if ( !$base || mb_substr( $s, 1 ) !== $base[$i] ) { |
| 111 |
$base[] = $s; |
| 112 |
$prefix = ""; |
| 113 |
continue; |
| 114 |
} |
| 115 |
} elseif ( $prefix !== mb_substr( $s, 0, 1 ) || mb_substr( $s, 1 ) !== $base[$i] ) { |
| 116 |
return []; |
| 117 |
} |
| 118 |
$i++; |
| 119 |
} |
| 120 |
} |
| 121 |
return $prefix !== "" ? $base : []; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* Next 2 arrays are extracted from https://www.w3.org/TR/predefined-counter-styles/ |
| 126 |
* Intersected with https://www.w3.org/International/i18n-tests/results/predefined-counter-styles |
| 127 |
* where all major browsers have support with a green cell. |
| 128 |
*/ |
| 129 |
$wgW3cDecimalCounters = [ |
| 130 |
"decimal", |
| 131 |
"arabic-indic", |
| 132 |
"bengali", |
| 133 |
"cambodian", |
| 134 |
"devanagari", |
| 135 |
"gujarati", |
| 136 |
"gurmukhi", |
| 137 |
"kannada", |
| 138 |
"khmer", |
| 139 |
"lao", |
| 140 |
"lepcha", |
| 141 |
"malayalam", |
| 142 |
"myanmar", /* w3c page says suffix, but testing shows no suffix! */ |
| 143 |
"mongolian", |
| 144 |
"oriya", |
| 145 |
"persian", |
| 146 |
"tamil", |
| 147 |
"telugu", |
| 148 |
"thai", |
| 149 |
]; |
| 150 |
|
| 151 |
$wgW3cCounterTypeMaps = [ |
| 152 |
/* numeric - no suffixes or prefixes */ |
| 153 |
"0123456789" => "decimal", |
| 154 |
"٠١٢٣٤٥٦٧٨٩" => "arabic-indic", |
| 155 |
"০১২৩৪৫৬৭৮৯" => "bengali", |
| 156 |
"០១២៣៤៥៦៧៨៩" => "cambodian", |
| 157 |
"०१२३४५६७८९" => "devanagari", |
| 158 |
"૦૧૨૩૪૫૬૭૮૯" => "gujarati", |
| 159 |
"੦੧੨੩੪੫੬੭੮੯" => "gurmukhi", |
| 160 |
"೦೧೨೩೪೫೬೭೮೯" => "kannada", |
| 161 |
"០១២៣៤៥៦៧៨៩" => "khmer", |
| 162 |
"໐໑໒໓໔໕໖໗໘໙" => "lao", |
| 163 |
"᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉" => "lepcha", |
| 164 |
"൦൧൨൩൪൫൬൭൮൯" => "malayalam", |
| 165 |
"၀၁၂၃၄၅၆၇၈၉" => "myanmar", /* w3c page says suffix, but testing shows no suffix! */ |
| 166 |
"᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙" => "mongolian", |
| 167 |
"୦୧୨୩୪୫୬୭୮୯" => "oriya", |
| 168 |
"۰۱۲۳۴۵۶۷۸۹" => "persian", |
| 169 |
"௦௧௨௩௪௫௬௭௮௯" => "tamil", |
| 170 |
"౦౧౨౩౪౫౬౭౮౯" => "telugu", |
| 171 |
"๐๑๒๓๔๕๖๗๘๙" => "thai", |
| 172 |
|
| 173 |
/* alphabetic - no suffixes or prefixes */ |
| 174 |
"a-z" => "lower-alpha", |
| 175 |
"A-Z" => "upper-alpha", |
| 176 |
"αβγδεζηθικλμνξοπρστυφχψω" => "lower-greek", |
| 177 |
"abcdefghijklmnopqrstuvwxyz" => "lower-alpha", |
| 178 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" => "upper-alpha", |
| 179 |
|
| 180 |
/* ---- Not sure we can use these as is ---- |
| 181 |
// alphabetic - WITH suffixes and/or prefixes. |
| 182 |
|
| 183 |
"子丑寅卯辰巳午未申酉戌亥"=>"cjk-earthly-branch", |
| 184 |
"甲乙丙丁戊己庚辛壬癸"=>"cjk-heavenly-stem", |
| 185 |
"㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉"=>"circled-ideograph", |
| 186 |
"あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをん"=>"hiragana", |
| 187 |
"いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす"=>"hiragana-iroha", |
| 188 |
"アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヰヱヲン"=>"katakana", |
| 189 |
"イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス"=>"katakana-iroha", |
| 190 |
*/ |
| 191 |
]; |
| 192 |
|
| 193 |
function wfIsNonDecimalCounter( string $counterName ): bool { |
| 194 |
global $wgW3cDecimalCounters; |
| 195 |
|
| 196 |
return array_search( $counterName, $wgW3cDecimalCounters, false ) === false; |
| 197 |
} |
| 198 |
|
| 199 |
function wfGetCSS( string $selector, array $rules ): string { |
| 200 |
return "$selector {\n\t" . implode( "\n\t", $rules ) . "\n}"; |
| 201 |
} |
| 202 |
|
| 203 |
function wfEmitCSS( string $selector, array $rules ): void { |
| 204 |
print wfGetCSS( $selector, $rules ) . "\n"; |
| 205 |
} |
| 206 |
|
| 207 |
function wfLoadJson( string $file ): array { |
| 208 |
return json_decode( file_get_contents( $file ), true ); |
| 209 |
} |
| 210 |
|
| 211 |
function wfProcessMessages( string $file, ?array &$wikiInfo ): void { |
| 212 |
$jsonObj = wfLoadJson( $file ); |
| 213 |
foreach ( $jsonObj["hits"] as $row ) { |
| 214 |
wfProcessRow( $row, $wikiInfo ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
function wfDetectCounterType( string $msg ): ?string { |
| 219 |
global $wgW3cCounterTypeMaps; |
| 220 |
|
| 221 |
/* Hacky heuristic that should work */ |
| 222 |
if ( preg_match( '/^([αβγδεζηθικλμνξοπρστυφχψω]*( |$))*$/u', $msg ) ) { |
| 223 |
return "lower-greek"; |
| 224 |
} elseif ( preg_match( '/^([ivxlcdm]*( |$))*$/u', $msg ) ) { |
| 225 |
return "lower-roman"; |
| 226 |
} elseif ( preg_match( '/^([IVXLCDM]*( |$))*$/u', $msg ) ) { |
| 227 |
return "upper-roman"; |
| 228 |
} else { |
| 229 |
foreach ( $wgW3cCounterTypeMaps as $digits => $type ) { |
| 230 |
if ( preg_match( "/^([$digits]*( |$))*$/u", $msg ) ) { |
| 231 |
return $type; |
| 232 |
} |
| 233 |
} |
| 234 |
return null; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
function wfBackfill( string $citeI18nDir, string $lang, array &$messages ): void { |
| 239 |
$jsonMsgKeys = [ |
| 240 |
"cite_reference_link", |
| 241 |
"cite_references_link_one", |
| 242 |
"cite_references_link_many_format_backlink_labels", |
| 243 |
"cite_references_link_many_format", |
| 244 |
"cite_references_link_many", |
| 245 |
"cite_references_link_many_and", |
| 246 |
"cite_references_link_many_sep", |
| 247 |
]; |
| 248 |
$langJsonFile = "$citeI18nDir$lang.json"; |
| 249 |
if ( file_exists( $langJsonFile ) ) { |
| 250 |
$langMsgs = wfLoadJson( $langJsonFile ); |
| 251 |
foreach ( $jsonMsgKeys as $key ) { |
| 252 |
$msgTitleKey = wfJsonKeyToTitleSuffix( $key ); |
| 253 |
if ( !isset( $messages[$msgTitleKey] ) && isset( $langMsgs[$key] ) ) { |
| 254 |
$messages[$msgTitleKey] = $langMsgs[$key]; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
$wikiInfo = []; |
| 261 |
$citeRepo = $argv[1] ?? ( __DIR__ . "/../../../core/extensions/Cite" ); |
| 262 |
|
| 263 |
// This file is the exported JSON of https://global-search.toolforge.org/?namespaces=8&q=.%2A®ex=1&title=Cite%20reference.%2A |
| 264 |
$jsonFile = $argv[2] ?? ( __DIR__ . "/cite.reference.messages.json" ); |
| 265 |
wfProcessMessages( $jsonFile, $wikiInfo ); |
| 266 |
|
| 267 |
// This file is the exported JSON of https://global-search.toolforge.org/?namespaces=8&q=.%2A®ex=1&title=Cite%20link%20label.%2A |
| 268 |
$jsonFile = $argv[3] ?? ( __DIR__ . "/cite.link_label_groups.messages.json" ); |
| 269 |
wfProcessMessages( $jsonFile, $wikiInfo ); |
| 270 |
|
| 271 |
// Language fallback chain |
| 272 |
$langFallbacks = wfLoadJson( __DIR__ . "/language.fallbacks.json" ); |
| 273 |
|
| 274 |
// Localized digits & separators |
| 275 |
$localizedDigits = wfLoadJson( __DIR__ . "/localized.digits.json" ); // Extracted from languages/messages/Message$lang.php |
| 276 |
$localizedSeps = wfLoadJson( __DIR__ . "/localized.separators.json" ); // Extracted from languages/messages/Message$lang.php |
| 277 |
$digitLocalizationEnabled = wfLoadJson( __DIR__ . "/digits.localization.config.json" ); // Extracted from wmf-config/$wgTranslateNumerals |
| 278 |
|
| 279 |
// Wiki language exceptions |
| 280 |
$wikiLangExceptions = wfLoadJson( __DIR__ . "/wikilang.exceptions.json" ); |
| 281 |
foreach ( $wikiLangExceptions as $wiki => $lang ) { |
| 282 |
$wikiInfo[$wiki] ??= []; // init |
| 283 |
} |
| 284 |
|
| 285 |
// Backfill from lang-specific i18n file |
| 286 |
$citeI18nDir = $citeRepo . "/i18n/"; |
| 287 |
foreach ( $wikiInfo as $wiki => &$messages ) { |
| 288 |
$lang = $wikiLangExceptions[$wiki] ?? preg_replace( "/\..*$/", "", $wiki ); |
| 289 |
wfBackfill( $citeI18nDir, $lang, $messages ); |
| 290 |
} |
| 291 |
|
| 292 |
// Backfill from language fallback chains |
| 293 |
foreach ( $wikiInfo as $wiki => &$messages ) { |
| 294 |
$lang = $wikiLangExceptions[$wiki] ?? preg_replace( "/\..*$/", "", $wiki ); |
| 295 |
if ( $lang !== 'en' ) { |
| 296 |
// last fallback is 'en' |
| 297 |
$len = count( $langFallbacks[$lang] ?? [] ); |
| 298 |
if ( $len === 0 || ( $langFallbacks[$lang][$len - 1] !== 'en' ) ) { |
| 299 |
$langFallbacks[$lang][] = 'en'; |
| 300 |
} |
| 301 |
foreach ( $langFallbacks[$lang] ?? [] as $fbLang ) { |
| 302 |
wfBackfill( $citeI18nDir, $fbLang, $messages ); |
| 303 |
if ( !isset( $localizedDigits[$lang] ) && isset( $localizedDigits[$fbLang] ) ) { |
| 304 |
$localizedDigits[$lang] = $localizedDigits[$fbLang]; |
| 305 |
} |
| 306 |
if ( !isset( $localizedSeps[$lang] ) && isset( $localizedSeps[$fbLang] ) ) { |
| 307 |
$localizedSeps[$lang] = $localizedSeps[$fbLang]; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// Process messages |
| 314 |
foreach ( $wikiInfo as $wiki => &$messages ) { |
| 315 |
# error_log( "$wiki has " . count($messages) . " messages "); |
| 316 |
|
| 317 |
// Preprocess all messages |
| 318 |
foreach ( $messages as $key => $msg ) { |
| 319 |
$msg = preg_replace( "/strong/", "b", $msg ); |
| 320 |
$msg = preg_replace( "/'''([^']+)'''/", "<b>$1</b>", $msg ); |
| 321 |
$msg = preg_replace( "/''([^']+)''/", "<i>$1</i>", $msg ); |
| 322 |
$msg = html_entity_decode( $msg ); |
| 323 |
|
| 324 |
switch ( $key ) { |
| 325 |
case "reference_link": |
| 326 |
$msg = preg_replace( "/<(span|nowiki|sup)[^>]*>|<\/(span|sup|nowiki)>/", "", $msg ); |
| 327 |
$msg = preg_replace( "/\[\[\#\\$2\||\]\]/", "", $msg ); |
| 328 |
break; |
| 329 |
|
| 330 |
case "references_link_one": |
| 331 |
$msg = preg_replace( "/<(li|span)[^>]*>|<\/(li|span)>/", "", $msg ); |
| 332 |
$msg = preg_replace( "/\[\[\#\\$2\||\]\]|\\$3/", "", $msg ); |
| 333 |
break; |
| 334 |
|
| 335 |
case "references_link_many": |
| 336 |
$msg = preg_replace( "/<(li|span|nowiki)[^>]*>|<\/(li|span|nowiki)>/", "", $msg ); |
| 337 |
$msg = preg_replace( "/\\$2 \\$3/", "", $msg ); |
| 338 |
break; |
| 339 |
|
| 340 |
case "references_link_many_format": |
| 341 |
$msg = preg_replace( "/\[\[\#\\$1\||<(span|sup)[^>]*>|<\/(span|sup)>|\]\]/", "", $msg ); |
| 342 |
break; |
| 343 |
|
| 344 |
case "references_link_many_sep": |
| 345 |
$msg = preg_replace( "/|<sup[^>]*>|<\/sup>/", "", $msg ); |
| 346 |
break; |
| 347 |
|
| 348 |
case "references_link_many_and": |
| 349 |
$msg = preg_replace( "/|<sup[^>]*>|<\/sup>/", "", $msg ); |
| 350 |
$msg = preg_replace( "/|<sup[^>]*>|<\/sup>/", "", $msg ); |
| 351 |
break; |
| 352 |
|
| 353 |
default: |
| 354 |
break; |
| 355 |
} |
| 356 |
$messages[$key] = $msg; |
| 357 |
# error_log("$key = " . json_encode($msg) ); |
| 358 |
} |
| 359 |
|
| 360 |
print "--- EMITTING CSS for $wiki ---\n"; |
| 361 |
|
| 362 |
// Generate any required custom counters first |
| 363 |
$lang = $wikiLangExceptions[$wiki] ?? preg_replace( "/\..*$/", "", $wiki ); |
| 364 |
$digits = $localizedDigits[$lang] ?? null; |
| 365 |
if ( $digits === null ) { |
| 366 |
$langCounterType = "decimal"; // default |
| 367 |
} else { |
| 368 |
$langCounterType = wfDetectCounterType( implode( ' ', $digits ) ) ?? "$lang-counter"; |
| 369 |
} |
| 370 |
// check if localization is disabled! |
| 371 |
if ( $langCounterType !== 'decimal' && !( $digitLocalizationEnabled[$wiki] ?? true ) ) { |
| 372 |
$refCounterType = 'decimal'; |
| 373 |
$linkbackCounterType = 'decimal'; |
| 374 |
$langSep = '.'; |
| 375 |
$resetRefCounterTypes = true; |
| 376 |
} else { |
| 377 |
$refCounterType = $langCounterType; |
| 378 |
$linkbackCounterType = $langCounterType; |
| 379 |
$langSep = $localizedSeps[$lang] ?? '.'; // default |
| 380 |
$resetRefCounterTypes = false; |
| 381 |
} |
| 382 |
|
| 383 |
$keys = array_keys( $messages ); |
| 384 |
$groupLabels = []; |
| 385 |
$initCustomLinkbackCounter = ''; |
| 386 |
foreach ( $messages as $key => $msg ) { |
| 387 |
// Skip error-test groups |
| 388 |
if ( preg_match( '/error-test/', $key ) ) { |
| 389 |
continue; |
| 390 |
} |
| 391 |
|
| 392 |
if ( preg_match( '/^link_label_group-/', $key ) ) { |
| 393 |
$group = substr( $key, 17 /* strlen( "link_label_group-" ) */ ); |
| 394 |
$groupCounterType = wfDetectCounterType( $msg ); |
| 395 |
if ( !$groupCounterType ) { |
| 396 |
// emit custom counter definitions |
| 397 |
$groupCounterType = "custom-group-label-$group"; |
| 398 |
$cssSel = "@counter-style $groupCounterType"; |
| 399 |
$cssRules = []; |
| 400 |
$symbols = preg_split( "/\s+/", $msg ); |
| 401 |
$alphabeticBase = wfCheckIfAlphabetic( $symbols ); |
| 402 |
if ( $alphabeticBase ) { |
| 403 |
$cssRules[] = "system: alphabetic;"; |
| 404 |
$cssRules[] = "symbols: " . preg_replace( "/([^\s]+)/", "'$1'", implode( " ", $alphabeticBase ) ) . ";"; |
| 405 |
} else { |
| 406 |
$cssRules[] = "system: fixed;"; |
| 407 |
$cssRules[] = "symbols: " . preg_replace( "/([^\s]+)/", "'$1'", $msg ) . ";"; |
| 408 |
} |
| 409 |
wfEmitCSS( $cssSel, $cssRules ); |
| 410 |
} |
| 411 |
$groupLabels[$group] = $groupCounterType; |
| 412 |
} elseif ( $key === "references_link_many_format_backlink_labels" ) { |
| 413 |
$linkbackCounterType = wfDetectCounterType( $msg ); |
| 414 |
if ( !$linkbackCounterType ) { |
| 415 |
$linkbackCounterType = "custom-backlink"; |
| 416 |
$cssSel = "@counter-style custom-backlink"; |
| 417 |
$cssRules = []; |
| 418 |
$symbols = preg_split( "/\s+/", $msg ); |
| 419 |
$alphabeticBase = wfCheckIfAlphabetic( $symbols ); |
| 420 |
if ( $alphabeticBase ) { |
| 421 |
$cssRules[] = "system: alphabetic;"; |
| 422 |
$cssRules[] = "symbols: " . preg_replace( "/([^\s]+)/", "'$1'", implode( " ", $alphabeticBase ) ) . ";"; |
| 423 |
} else { |
| 424 |
$cssRules[] = "system: fixed;"; |
| 425 |
$cssRules[] = "symbols: " . preg_replace( "/([^\s]+)/", "'$1'", $msg ) . ";"; |
| 426 |
} |
| 427 |
|
| 428 |
// Some wikis may define custom messages for backlinks |
| 429 |
// but not actually use them in references_link_many_format |
| 430 |
// by using default messages! So, we cannot init them here! |
| 431 |
$initCustomLinkbackCounter = wfGetCSS( $cssSel, $cssRules ); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
// Generate other CSS rules |
| 437 |
foreach ( $messages as $key => $msg ) { |
| 438 |
switch ( $key ) { |
| 439 |
case "reference_link": |
| 440 |
// "[$3]" is the effective default Parsoid CSS output |
| 441 |
if ( $msg !== "[$3]" ) { |
| 442 |
$cssSel = '.mw-ref > a::after'; |
| 443 |
$parts = preg_split( "/\\$3/", $msg ); |
| 444 |
$rule = "content:"; |
| 445 |
if ( $parts[0] !== "" ) { |
| 446 |
$rule .= " '" . $parts[0] . "'"; |
| 447 |
} |
| 448 |
// FIXME: the counter is language-specific |
| 449 |
// but editors can fix this or we can edit it manually |
| 450 |
$rule .= " counter( mw-Ref, $refCounterType )"; |
| 451 |
if ( $parts[1] !== "" ) { |
| 452 |
$rule .= " '" . $parts[1] . "'"; |
| 453 |
} |
| 454 |
$rule .= ";"; |
| 455 |
wfEmitCSS( $cssSel, [ $rule ] ); |
| 456 |
|
| 457 |
// Add default CSS rule for groups |
| 458 |
$cssSel = ".mw-ref > a[data-mw-group]::after"; |
| 459 |
$baseRule = $rule; |
| 460 |
$newRule = preg_replace( "/counter\(/", "attr(data-mw-group) ' ' counter(", $baseRule ); |
| 461 |
wfEmitCSS( $cssSel, [ $newRule ] ); |
| 462 |
|
| 463 |
// Add CSS rules for groups with custom counters |
| 464 |
$baseRule = $rule; |
| 465 |
foreach ( $groupLabels as $group => $groupCounterType ) { |
| 466 |
$cssSel = ".mw-ref > a[data-mw-group=$group]::after"; |
| 467 |
$newRule = preg_replace( "/$refCounterType/", "$groupCounterType", $baseRule ); |
| 468 |
wfEmitCSS( $cssSel, [ $newRule ] ); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* // This won't execute because $refCountertype is known to be 'decimal' |
| 473 |
* // but leaving behind as documentation |
| 474 |
* if ( $resetRefCounterTypes ) { |
| 475 |
* $cssSel = ".mw-ref > a[ data-mw-group ]::after"; |
| 476 |
* $rule = preg_replace( "/$refCounterType/", "decimal", $baseRule ); |
| 477 |
* wfEmitCSS( $cssSel, [ $rule ] ); |
| 478 |
* } |
| 479 |
*/ |
| 480 |
} else { |
| 481 |
if ( $resetRefCounterTypes ) { |
| 482 |
wfEmitCSS( |
| 483 |
".mw-ref > a::after", |
| 484 |
[ "content: '[' counter( mw-Ref, decimal ) ']';" ] |
| 485 |
); |
| 486 |
|
| 487 |
wfEmitCSS( |
| 488 |
".mw-ref > a[ data-mw-group ]::after", |
| 489 |
[ "content: '[' attr( data-mw-group ) ' ' counter( mw-Ref, decimal ) ']';" ] |
| 490 |
); |
| 491 |
} |
| 492 |
// Add CSS rules for ref-groups |
| 493 |
foreach ( $groupLabels as $group => $groupCounterType ) { |
| 494 |
$cssSel = ".mw-ref > a[data-mw-group=$group]::after"; |
| 495 |
$rule = "content: '[' counter( mw-Ref, $groupCounterType ) ']';"; |
| 496 |
wfEmitCSS( $cssSel, [ $rule ] ); |
| 497 |
} |
| 498 |
} |
| 499 |
break; |
| 500 |
|
| 501 |
case "references_link_one": |
| 502 |
// "↑ " is the effective default Parsoid CSS output |
| 503 |
$msg = rtrim( $msg ); |
| 504 |
if ( $msg !== "↑" ) { |
| 505 |
$cssSel = 'a[ rel="mw:referencedBy" ]::before'; |
| 506 |
$cssRules = []; |
| 507 |
wfAddCSSForIBTagsAndProcessMsg( $cssRules, $msg ); |
| 508 |
$cssRules[] = 'content: "' . $msg . '";'; |
| 509 |
wfEmitCSS( $cssSel, $cssRules ); |
| 510 |
} |
| 511 |
break; |
| 512 |
|
| 513 |
case "references_link_many": |
| 514 |
if ( preg_match( "/<sup>/", $msg ) ) { |
| 515 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a::after'; |
| 516 |
$cssRules = [ 'font-size: smaller;' ]; |
| 517 |
wfEmitCSS( $cssSel, $cssRules ); |
| 518 |
|
| 519 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a:nth-last-child(2)::after'; |
| 520 |
$cssRules = [ "vertical-align: super;" ]; |
| 521 |
wfEmitCSS( $cssSel, $cssRules ); |
| 522 |
} |
| 523 |
|
| 524 |
$msg = preg_replace( "/<\/?sup[^>]*>/", "", $msg ); |
| 525 |
$msg = preg_replace( "/\\$2 \\$3/", "", $msg ); |
| 526 |
|
| 527 |
if ( $msg !== "↑ " ) { // "↑ " is the default |
| 528 |
$cssSel = 'span[ rel="mw:referencedBy" ]::before'; |
| 529 |
$cssRules = []; |
| 530 |
wfAddCSSForIBTagsAndProcessMsg( $cssRules, $msg ); |
| 531 |
$cssRules[] = 'content: "' . $msg . '";'; |
| 532 |
wfEmitCSS( $cssSel, $cssRules ); |
| 533 |
} |
| 534 |
break; |
| 535 |
|
| 536 |
case "references_link_many_format": |
| 537 |
// "$2" is the effective default Parsoid CSS output |
| 538 |
// with decimal as the counter type and "." as the separator. |
| 539 |
if ( $msg !== "$2" || ( $langCounterType !== "decimal" && $langSep !== "." ) ) { |
| 540 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a::before'; |
| 541 |
$cssRules = []; |
| 542 |
wfAddCSSForIBTagsAndProcessMsg( $cssRules, $msg ); |
| 543 |
|
| 544 |
$counterRef = preg_match( "/\\$2/", $msg ) ? "\\$2" : "\\$3"; |
| 545 |
$parts = preg_split( "/$counterRef/", $msg ); |
| 546 |
$rule = 'content:'; |
| 547 |
if ( $parts[0] !== "" ) { |
| 548 |
$rule .= " '" . $parts[0] . "'"; |
| 549 |
} |
| 550 |
|
| 551 |
// Default is Y where Y is mw-ref-linkback counter |
| 552 |
if ( $counterRef === "\\$2" ) { |
| 553 |
$counter = $resetRefCounterTypes ? "decimal" : $langCounterType; |
| 554 |
// X.Y where X is mw-references counter and Y is mw-ref-linkback counter |
| 555 |
$linkbackRule = |
| 556 |
"counter( mw-references, $counter )" . " '$langSep' " . |
| 557 |
"counter( mw-ref-linkback, $counter )"; |
| 558 |
} else { // implicilty assumed to be "\\$3" |
| 559 |
// Init custom counter types |
| 560 |
if ( $initCustomLinkbackCounter ) { |
| 561 |
print $initCustomLinkbackCounter . "\n"; |
| 562 |
} |
| 563 |
// These aren't 2.0, 2.1 ... etc. style counters |
| 564 |
// but more like 1,2,3 style counters so we need |
| 565 |
// to override counter-reset to 0. |
| 566 |
wfEmitCSS( |
| 567 |
'span[ rel="mw:referencedBy" ]', |
| 568 |
[ 'counter-reset: mw-ref-linkback 0;' ] |
| 569 |
); |
| 570 |
$linkbackRule = "counter( mw-ref-linkback, $linkbackCounterType )"; |
| 571 |
} |
| 572 |
|
| 573 |
$rule .= " $linkbackRule"; |
| 574 |
if ( count( $parts ) > 1 && $parts[1] !== "" ) { |
| 575 |
$rule .= " '" . $parts[1] . "'"; |
| 576 |
} |
| 577 |
$rule .= ";"; |
| 578 |
$cssRules[] = $rule; |
| 579 |
wfEmitCSS( $cssSel, $cssRules ); |
| 580 |
} elseif ( $resetRefCounterTypes ) { |
| 581 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a::before'; |
| 582 |
$linkbackRule = 'content: ' . |
| 583 |
"counter( mw-references, decimal )" . " '$langSep' " . |
| 584 |
"counter( mw-ref-linkback, decimal )"; |
| 585 |
$cssRules = [ $linkbackRule ]; |
| 586 |
wfEmitCSS( $cssSel, $cssRules ); |
| 587 |
} |
| 588 |
break; |
| 589 |
|
| 590 |
case "references_link_many_and": |
| 591 |
if ( $msg !== ' ' ) { // ' ' is the default |
| 592 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a:nth-last-child(2)::after'; |
| 593 |
$cssRules = []; |
| 594 |
wfAddCSSForIBTagsAndProcessMsg( $cssRules, $msg ); |
| 595 |
$cssRules[] = 'content: "' . $msg . '";'; |
| 596 |
wfEmitCSS( $cssSel, $cssRules ); |
| 597 |
} |
| 598 |
break; |
| 599 |
|
| 600 |
case "references_link_many_sep": |
| 601 |
if ( $msg !== ' ' ) { // ' ' is the default |
| 602 |
$cssSel = 'span[ rel="mw:referencedBy" ] > a::after'; |
| 603 |
$cssRules = []; |
| 604 |
wfAddCSSForIBTagsAndProcessMsg( $cssRules, $msg ); |
| 605 |
$cssRules[] = 'content: "' . $msg . '";'; |
| 606 |
wfEmitCSS( $cssSel, $cssRules ); |
| 607 |
} |
| 608 |
break; |
| 609 |
|
| 610 |
default: |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
} |