From: lwelling Date: Fri, 3 May 2013 00:34:25 +0000 (-0400) Subject: Cache result of Language::isValidCode() to avoid regex processing X-Git-Tag: 1.31.0-rc.0~19785 X-Git-Url: http://git.cyclocoop.org/%22.%24image2.%22?a=commitdiff_plain;h=04a9d80e53f64ae97d75e746377f83f8519e416d;p=lhc%2Fweb%2Fwiklou.git Cache result of Language::isValidCode() to avoid regex processing The function can be called over 2000 times in generating a page. This way is significantly faster even for random invalid codes. For real use it should avoid the regex most of the time with no change in behavior. Change-Id: I9fcbae1770be0d3f405d3d12254c11943b0d5f46 --- diff --git a/languages/Language.php b/languages/Language.php index b9201d7ed4..84e7f37db8 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -326,13 +326,19 @@ class Language { * @return bool */ public static function isValidCode( $code ) { - return - // People think language codes are html safe, so enforce it. - // Ideally we should only allow a-zA-Z0-9- - // but, .+ and other chars are often used for {{int:}} hacks - // see bugs 37564, 37587, 36938 + static $cache = array(); + if( isset( $cache[$code] ) ) { + return $cache[$code]; + } + // People think language codes are html safe, so enforce it. + // Ideally we should only allow a-zA-Z0-9- + // but, .+ and other chars are often used for {{int:}} hacks + // see bugs 37564, 37587, 36938 + $return = strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code ) && !preg_match( Title::getTitleInvalidRegex(), $code ); + $cache[ $code ] = $return; + return $return; } /**