From 10a25c1cc9f4e9a53f3ce15f6708dc1fc7fa5ae9 Mon Sep 17 00:00:00 2001 From: "Amir E. Aharoni" Date: Mon, 17 Mar 2014 01:05:01 +0200 Subject: [PATCH] Allow always logging tofu detection This commit allows to log tofu detection always, even if the rest of the webfonts library is not loaded. By default this is disabled. To enable it, set the variable $wgULSTofuLoggingChance to a number between 0 and 100, indicating the chance of it getting logged. The idea of the lottery is that even a partial sample can be useful. Change-Id: Ia08fe0de348b7eebfa95498a6be58ae41608ba3b --- UniversalLanguageSelector.hooks.php | 10 ++++- UniversalLanguageSelector.php | 19 ++++++++++ resources/js/ext.uls.webfonts.js | 59 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/UniversalLanguageSelector.hooks.php b/UniversalLanguageSelector.hooks.php index acaaca4f..7cf460fb 100644 --- a/UniversalLanguageSelector.hooks.php +++ b/UniversalLanguageSelector.hooks.php @@ -273,8 +273,12 @@ class UniversalLanguageSelectorHooks { global $wgULSGeoService, $wgULSIMEEnabled, $wgULSWebfontsEnabled, $wgULSPosition, $wgULSNoWebfontsSelectors, - $wgULSAnonCanChangeLanguage, $wgULSEventLogging, $wgULSImeSelectors, - $wgULSNoImeSelectors, $wgULSFontRepositoryBasePath, $wgExtensionAssetsPath; + $wgULSAnonCanChangeLanguage, + $wgULSEventLogging, + $wgULSTofuLoggingChance, $wgULSTofuLoggingMaxTime, + $wgULSImeSelectors, $wgULSNoImeSelectors, + $wgULSFontRepositoryBasePath, + $wgExtensionAssetsPath; // Place constant stuff here (not depending on request context) if ( is_string( $wgULSGeoService ) ) { @@ -286,6 +290,8 @@ class UniversalLanguageSelectorHooks { $vars['wgULSPosition'] = $wgULSPosition; $vars['wgULSAnonCanChangeLanguage'] = $wgULSAnonCanChangeLanguage; $vars['wgULSEventLogging'] = $wgULSEventLogging; + $vars['wgULSTofuLoggingChance'] = $wgULSTofuLoggingChance; + $vars['wgULSTofuLoggingMaxTime'] = $wgULSTofuLoggingMaxTime; $vars['wgULSImeSelectors'] = $wgULSImeSelectors; $vars['wgULSNoImeSelectors'] = $wgULSNoImeSelectors; $vars['wgULSNoWebfontsSelectors'] = $wgULSNoWebfontsSelectors; diff --git a/UniversalLanguageSelector.php b/UniversalLanguageSelector.php index c7c78bb5..77d6a1a1 100644 --- a/UniversalLanguageSelector.php +++ b/UniversalLanguageSelector.php @@ -136,6 +136,25 @@ $GLOBALS['wgULSPosition'] = 'personal'; */ $GLOBALS['wgULSEventLogging'] = false; +/** + * How frequently to run tofu detection for all languages and to log them. + * This must be a number between 0 and 100, which signifies the percentage + * of pages for which this will be done. If it's 100, then it will be + * done for all pages. If it's 20, then it will be done on 20% of pages. + * The default is not to do it on any pages. + * @since 2014.04 + */ +$GLOBALS['wgULSTofuLoggingChance'] = 0; + +/** + * If tofu detection for all languages is enabled, and this value is non-zero + * this specifies the maximum time in milliseconds that the tofu detection + * is allowed to run before it's forced to stop. + * The default is 0 - not to force it to stop. + * @since 2014.04 + */ +$GLOBALS['wgULSTofuLoggingMaxTime'] = 0; + /** * Array of jQuery selectors of elements on which IME should be enabled. * diff --git a/resources/js/ext.uls.webfonts.js b/resources/js/ext.uls.webfonts.js index 96a9af94..ce673aa9 100644 --- a/resources/js/ext.uls.webfonts.js +++ b/resources/js/ext.uls.webfonts.js @@ -223,11 +223,70 @@ $( document ).ready( function () { mw.uls.init( function () { + var tofuStartTime, tofuEndTime, tofuTime; + mw.webfonts.preferences.load(); if ( mw.webfonts.preferences.isEnabled() ) { mw.loader.using( 'ext.uls.webfonts.fonts', mw.webfonts.setup ); } + + // If event logging is enabled and this page "wins" the tofu logging lottery + // try to detect tofu + if ( !mw.config.get( 'wgULSEventLogging' ) || + mw.config.get( 'wgULSTofuLoggingChance' ) < mw.config.get( 'wgArticleId' ) % 100 + ) { + return; + } + + setTimeout( function () { + var i, lang, text, + $langElements, $element, + maxTime = mw.config.get( 'wgULSTofuLoggingMaxTime' ); + + tofuStartTime = ( new Date() ).getTime(); + + // Check all elements that have the lang attribute, + // except the root - it has lang, + // but its text is not useful for testing. + // mw-content-text also always has lang, + // and its text is more relevant. + $langElements = $( 'body [lang]' ); + + for ( i = 0; i < $langElements.length; i++ ) { + $element = $( $langElements[ i ] ); + lang = $element.prop( 'lang' ); + + // Skip if this languages was already tested + if ( tofuLanguages[lang] !== undefined ) { + continue; + } + + text = $.trim( $element.text() ).substr( 0, 4 ); + + // Skip if the text only has basic ASCII and Latin + if ( !text.match( /[^\u0009-\u0200]/ ) ) { + continue; + } + + tofuLanguages[lang] = detectTofu( text ); + + if ( tofuLanguages[lang] ) { + mw.log( 'tofu detected for ' + lang ); + mw.hook( 'mw.uls.webfonts.tofudetected' ).fire( lang ); + } + + // Force to break the detection loop if it's taking too long + if ( maxTime && ( new Date() ).getTime() - tofuStartTime > maxTime ) { + mw.log( 'tofu detection max time reached. last lang: ' + lang ); + break; + } + } + + tofuEndTime = ( new Date() ).getTime(); + tofuTime = ( tofuEndTime - tofuStartTime ) / 1000; + mw.log( 'tofu detection took ' + tofuTime + ' seconds' ); + }, 1000 ); } ); } ); }( jQuery, mediaWiki ) );