LanguageNameIndexer: Generate PHP file instead of serialized file.
Serialized format is no longer in style for data. PHP files can take advantage of AutoLoader and caching so they can even be faster than serialized files. As side bonus we can have readable diffs for updates. Only downside is that the file generation takes about ten lines of ugly string manipulation. Change-Id: If09704d1172daa13c72a308814534cac1fe9899f
This commit is contained in:
committed by
Santhosh
parent
8bb0c2f683
commit
b3ba423354
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,5 +6,4 @@
|
|||||||
.bundle/
|
.bundle/
|
||||||
node_modules/
|
node_modules/
|
||||||
composer.lock
|
composer.lock
|
||||||
langnames.ser
|
|
||||||
vendor/
|
vendor/
|
||||||
|
|||||||
@@ -44,7 +44,34 @@ class LanguageNameIndexer extends Maintenance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->output( 'Total buckets: ' . count( $buckets ) . "\n" );
|
$this->output( 'Total buckets: ' . count( $buckets ) . "\n" );
|
||||||
file_put_contents( 'langnames.ser', serialize( $buckets ) );
|
$this->generateFile( $buckets );
|
||||||
|
}
|
||||||
|
|
||||||
|
private function generateFile( array $buckets ) {
|
||||||
|
$template = <<<PHP
|
||||||
|
<?php
|
||||||
|
// This file is generated by script!
|
||||||
|
class LanguageNameSearchData {
|
||||||
|
public static \$buckets = ___;
|
||||||
|
}
|
||||||
|
|
||||||
|
PHP;
|
||||||
|
|
||||||
|
// Format for short array format
|
||||||
|
$data = var_export( $buckets, true );
|
||||||
|
$data = str_replace( "array (", '[', $data );
|
||||||
|
$data = str_replace( "),", '],', $data );
|
||||||
|
// Closing of the array, add correct indendation
|
||||||
|
$data = preg_replace( "/\)$/", "\t]", $data );
|
||||||
|
// Remove newlines after =>s
|
||||||
|
$data = preg_replace( '/(=>)\s+(\[)/m', '\1 \2', $data );
|
||||||
|
// Convert spaces to tabs. Since we are not top-level need more tabs.
|
||||||
|
$data = preg_replace( '/^ /m', "\t\t\t", $data );
|
||||||
|
$data = preg_replace( '/^ /m', "\t\t", $data );
|
||||||
|
|
||||||
|
$template = str_replace( '___', $data, $template );
|
||||||
|
|
||||||
|
file_put_contents( __DIR__ . '/LanguageNameSearchData.php', $template );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,17 +18,7 @@
|
|||||||
* @licence MIT License
|
* @licence MIT License
|
||||||
*/
|
*/
|
||||||
class LanguageNameSearch {
|
class LanguageNameSearch {
|
||||||
protected static $languagenames;
|
|
||||||
|
|
||||||
public static function init() {
|
|
||||||
self::$languagenames = unserialize( file_get_contents( __DIR__ . '/langnames.ser' ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function search( $searchKey, $typos = 0 ) {
|
public static function search( $searchKey, $typos = 0 ) {
|
||||||
if ( self::$languagenames === null ) {
|
|
||||||
self::init();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use code's mb_strtolower compatibily code for MW < 1.27
|
// Use code's mb_strtolower compatibily code for MW < 1.27
|
||||||
$language = Language::factory( 'en' );
|
$language = Language::factory( 'en' );
|
||||||
|
|
||||||
@@ -36,11 +26,11 @@ class LanguageNameSearch {
|
|||||||
$searchKey = $language->lc( $searchKey );
|
$searchKey = $language->lc( $searchKey );
|
||||||
$index = self::getIndex( $searchKey );
|
$index = self::getIndex( $searchKey );
|
||||||
|
|
||||||
if ( !isset( self::$languagenames[$index] ) ) {
|
if ( !isset( LanguageNameSearchData::$buckets[$index] ) ) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$bucket = self::$languagenames[$index];
|
$bucket = LanguageNameSearchData::$buckets[$index];
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
foreach ( $bucket as $name => $code ) {
|
foreach ( $bucket as $name => $code ) {
|
||||||
|
|||||||
36643
data/LanguageNameSearchData.php
Normal file
36643
data/LanguageNameSearchData.php
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -44,6 +44,7 @@
|
|||||||
"ApiULSLocalization": "api/ApiULSLocalization.php",
|
"ApiULSLocalization": "api/ApiULSLocalization.php",
|
||||||
"FontRepoCompiler": "includes/FontRepoCompiler.php",
|
"FontRepoCompiler": "includes/FontRepoCompiler.php",
|
||||||
"LanguageNameSearch": "data/LanguageNameSearch.php",
|
"LanguageNameSearch": "data/LanguageNameSearch.php",
|
||||||
|
"LanguageNameSearchData": "data/LanguageNameSearchData.php",
|
||||||
"ResourceLoaderULSJsonMessageModule": "includes/ResourceLoaderULSJsonMessageModule.php",
|
"ResourceLoaderULSJsonMessageModule": "includes/ResourceLoaderULSJsonMessageModule.php",
|
||||||
"ResourceLoaderULSModule": "includes/ResourceLoaderULSModule.php",
|
"ResourceLoaderULSModule": "includes/ResourceLoaderULSModule.php",
|
||||||
"ULSJsonMessageLoader": "includes/ULSJsonMessageLoader.php",
|
"ULSJsonMessageLoader": "includes/ULSJsonMessageLoader.php",
|
||||||
|
|||||||
Reference in New Issue
Block a user