Title: Add scripts for generating/updating phpCharToUpper.js
authorEd Sanders <esanders@wikimedia.org>
Tue, 26 Mar 2019 13:42:29 +0000 (13:42 +0000)
committerEd Sanders <esanders@wikimedia.org>
Wed, 27 Mar 2019 22:15:20 +0000 (22:15 +0000)
Bug: T141723
Change-Id: I8355990f06b793e889d5279bada81e6ef3c242d1

maintenance/mediawiki.Title/generateJsToUpperCaseList.js [new file with mode: 0644]
maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php [new file with mode: 0755]

diff --git a/maintenance/mediawiki.Title/generateJsToUpperCaseList.js b/maintenance/mediawiki.Title/generateJsToUpperCaseList.js
new file mode 100644 (file)
index 0000000..fd742f6
--- /dev/null
@@ -0,0 +1,8 @@
+/* eslint-env node, es6 */
+var i, chars = [];
+
+for ( i = 0; i < 65536; i++ ) {
+       chars.push( String.fromCharCode( i ).toUpperCase() );
+}
+// eslint-disable-next-line no-console
+console.log( JSON.stringify( chars ) );
diff --git a/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php b/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php
new file mode 100755 (executable)
index 0000000..ff875d8
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/env php
+<?php
+/**
+ * Utility to generate mapping file used in phpCharToUpper.js
+ *
+ * Compares output of String.toUpperCase in JavaScript with
+ * mb_strtoupper in PHP, and outputs a list of lower:upper
+ * mappings where they differ. This is then used by Title.js
+ * to provide the same normalization in the client as on
+ * the server.
+ */
+
+$data = [];
+
+// phpcs:disable MediaWiki.Usage.ForbiddenFunctions.exec
+$jsUpperChars = json_decode( exec( 'node generateJsToUpperCaseList.js' ) );
+// phpcs:enable MediaWiki.Usage.ForbiddenFunctions.exec
+
+for ( $i = 0; $i < 65536; $i++ ) {
+       if ( $i >= 0xd800 && $i <= 0xdfff ) {
+               // Skip surrogate pairs
+               continue;
+       }
+       $char = mb_convert_encoding( '&#' . $i . ';', 'UTF-8', 'HTML-ENTITIES' );
+       $phpUpper = mb_strtoupper( $char );
+       $jsUpper = $jsUpperChars[$i];
+       if ( $jsUpper !== $phpUpper ) {
+               $data[$char] = $phpUpper;
+       }
+}
+
+echo 'var toUpperMapping = ' . str_replace( '"', "'",
+       str_replace( '    ', "\t",
+               json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
+       )
+) . ";\n";