From 1bb1ff8dd3e3777da88a20c598b331a16c21448f Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 31 Jul 2019 18:48:45 -0400 Subject: [PATCH] Short-circuit WebRequest::getGPCVal() for printable ASCII strings This avoids accessing the content Language instance in many cases Change-Id: I82bd66496180f947c1af16c2728a1548df03dcf9 --- includes/WebRequest.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 6593e49d8a..defe07eaee 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -395,18 +395,25 @@ class WebRequest { # https://www.php.net/variables.external#language.variables.external.dot-in-names # Work around PHP *feature* to avoid *bugs* elsewhere. $name = strtr( $name, '.', '_' ); - if ( isset( $arr[$name] ) ) { - $data = $arr[$name]; + + if ( !isset( $arr[$name] ) ) { + return $default; + } + + $data = $arr[$name]; + # Optimisation: Skip UTF-8 normalization and legacy transcoding for simple ASCII strings. + $isAsciiStr = ( is_string( $data ) && preg_match( '/[^\x20-\x7E]/', $data ) === 0 ); + if ( !$isAsciiStr ) { if ( isset( $_GET[$name] ) && is_string( $data ) ) { # Check for alternate/legacy character encoding. - $contLang = MediaWikiServices::getInstance()->getContentLanguage(); - $data = $contLang->checkTitleEncoding( $data ); + $data = MediaWikiServices::getInstance() + ->getContentLanguage() + ->checkTitleEncoding( $data ); } $data = $this->normalizeUnicode( $data ); - return $data; - } else { - return $default; } + + return $data; } /** -- 2.20.1