From 896f835ea99d9bee03c2949436bd6a7371aef9e8 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Fri, 4 Jul 2014 22:19:48 +0200 Subject: [PATCH] Refactor: Use local variables for editsections in Parser In Parser.php an array was built and then the elements of that array were used, replaced this by local vars. In ParserOutput.php also use local vars to make the code more readable. Also inlined a private callback by using an anonymous function. Change-Id: I1c31c9e4855f93a8fb65e1c21faba46fcdcb1f4b --- includes/parser/Parser.php | 21 ++++++++------- includes/parser/ParserOutput.php | 46 +++++++++++++++----------------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 61fffc5564..8bf400a0cf 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4597,13 +4597,13 @@ class Parser { if ( $isTemplate ) { # Put a T flag in the section identifier, to indicate to extractSections() # that sections inside should be counted. - $editlinkArgs = array( $titleText, "T-$sectionIndex"/*, null */ ); + $editsectionPage = $titleText; + $editsectionSection = "T-$sectionIndex"; + $editsectionContent = null; } else { - $editlinkArgs = array( - $this->mTitle->getPrefixedText(), - $sectionIndex, - $headlineHint - ); + $editsectionPage = $this->mTitle->getPrefixedText(); + $editsectionSection = $sectionIndex; + $editsectionContent = $headlineHint; } // We use a bit of pesudo-xml for editsection markers. The // language converter is run later on. Using a UNIQ style marker @@ -4616,10 +4616,11 @@ class Parser { // important bits of data, but put the headline hint inside a // content block because the language converter is supposed to // be able to convert that piece of data. - $editlink = ''; + // Gets replaced with html in ParserOutput::getText + $editlink = ''; } else { $editlink .= '/>'; } diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index 7fa4436b8e..f939de35bb 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -75,8 +75,27 @@ class ParserOutput extends CacheTime { wfProfileIn( __METHOD__ ); $text = $this->mText; if ( $this->mEditSectionTokens ) { - $text = preg_replace_callback( ParserOutput::EDITSECTION_REGEX, - array( &$this, 'replaceEditSectionLinksCallback' ), $text ); + $text = preg_replace_callback( + ParserOutput::EDITSECTION_REGEX, + function ( $m ) { + global $wgOut, $wgLang; + $editsectionPage = Title::newFromText( htmlspecialchars_decode( $m[1] ) ); + $editsectionSection = htmlspecialchars_decode( $m[2] ); + $editsectionContent = isset( $m[4] ) ? $m[3] : null; + + if ( !is_object( $editsectionPage ) ) { + throw new MWException( "Bad parser output text." ); + } + + $skin = $wgOut->getSkin(); + return call_user_func_array( + array( $skin, 'doEditSectionLink' ), + array( $editsectionPage, $editsectionSection, + $editsectionContent, $wgLang->getCode() ) + ); + }, + $text + ); } else { $text = preg_replace( ParserOutput::EDITSECTION_REGEX, '', $text ); } @@ -95,29 +114,6 @@ class ParserOutput extends CacheTime { return $text; } - /** - * callback used by getText to replace editsection tokens - * @private - * @param array $m - * @throws MWException - * @return mixed - */ - public function replaceEditSectionLinksCallback( $m ) { - global $wgOut, $wgLang; - $args = array( - htmlspecialchars_decode( $m[1] ), - htmlspecialchars_decode( $m[2] ), - isset( $m[4] ) ? $m[3] : null, - ); - $args[0] = Title::newFromText( $args[0] ); - if ( !is_object( $args[0] ) ) { - throw new MWException( "Bad parser output text." ); - } - $args[] = $wgLang->getCode(); - $skin = $wgOut->getSkin(); - return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args ); - } - public function &getLanguageLinks() { return $this->mLanguageLinks; } -- 2.20.1