Use anonymous function in Linker::formatAutocomments/formatLinksInComment
authorumherirrender <umherirrender_de.wp@web.de>
Fri, 16 May 2014 19:16:09 +0000 (21:16 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 10 Jun 2014 20:32:23 +0000 (20:32 +0000)
A anonym function has no own scope, so it is not possible to call the
private callback, so moved the code of the callback into the function.

This avoids passing parameters with static variables to the callback.

Change-Id: I41ad85545301f6699817135b7e8ceba0f8b26851

includes/Linker.php

index aa5325d..7d88f25 100644 (file)
@@ -1317,12 +1317,6 @@ class Linker {
                return $comment;
        }
 
-       /** @var Title */
-       private static $autocommentTitle;
-
-       /** @var bool Whether section links should refer to local page */
-       private static $autocommentLocal;
-
        /**
         * Converts autogenerated comments in edit summaries into section links.
         * The pattern for autogen comments is / * foo * /, which makes for
@@ -1337,80 +1331,59 @@ class Linker {
         * @return string Formatted comment
         */
        private static function formatAutocomments( $comment, $title = null, $local = false ) {
-               // Bah!
-               self::$autocommentTitle = $title;
-               self::$autocommentLocal = $local;
-               $comment = preg_replace_callback(
+               return preg_replace_callback(
                        '!(.*)/\*\s*(.*?)\s*\*/(.*)!',
-                       array( 'Linker', 'formatAutocommentsCallback' ),
-                       $comment );
-               self::$autocommentTitle = null;
-               self::$autocommentLocal = null;
-               return $comment;
-       }
-
-       /**
-        * Helper function for Linker::formatAutocomments
-        * @param array $match
-        * @return string
-        */
-       private static function formatAutocommentsCallback( $match ) {
-               global $wgLang;
-               $title = self::$autocommentTitle;
-               $local = self::$autocommentLocal;
-
-               $pre = $match[1];
-               $auto = $match[2];
-               $post = $match[3];
-               $comment = null;
-               wfRunHooks( 'FormatAutocomments', array( &$comment, $pre, $auto, $post, $title, $local ) );
-               if ( $comment === null ) {
-                       $link = '';
-                       if ( $title ) {
-                               $section = $auto;
-
-                               # Remove links that a user may have manually put in the autosummary
-                               # This could be improved by copying as much of Parser::stripSectionName as desired.
-                               $section = str_replace( '[[:', '', $section );
-                               $section = str_replace( '[[', '', $section );
-                               $section = str_replace( ']]', '', $section );
-
-                               $section = Sanitizer::normalizeSectionNameWhitespace( $section ); # bug 22784
-                               if ( $local ) {
-                                       $sectionTitle = Title::newFromText( '#' . $section );
-                               } else {
-                                       $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
-                                               $title->getDBkey(), $section );
-                               }
-                               if ( $sectionTitle ) {
-                                       $link = self::link( $sectionTitle,
-                                               $wgLang->getArrow(), array(), array(),
-                                               'noclasses' );
-                               } else {
+                       function ( $match ) use ( $title, $local ) {
+                               global $wgLang;
+
+                               $pre = $match[1];
+                               $auto = $match[2];
+                               $post = $match[3];
+                               $comment = null;
+                               wfRunHooks( 'FormatAutocomments', array( &$comment, $pre, $auto, $post, $title, $local ) );
+                               if ( $comment === null ) {
                                        $link = '';
+                                       if ( $title ) {
+                                               $section = $auto;
+                                               # Remove links that a user may have manually put in the autosummary
+                                               # This could be improved by copying as much of Parser::stripSectionName as desired.
+                                               $section = str_replace( '[[:', '', $section );
+                                               $section = str_replace( '[[', '', $section );
+                                               $section = str_replace( ']]', '', $section );
+
+                                               $section = Sanitizer::normalizeSectionNameWhitespace( $section ); # bug 22784
+                                               if ( $local ) {
+                                                       $sectionTitle = Title::newFromText( '#' . $section );
+                                               } else {
+                                                       $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
+                                                               $title->getDBkey(), $section );
+                                               }
+                                               if ( $sectionTitle ) {
+                                                       $link = Linker::link( $sectionTitle,
+                                                               $wgLang->getArrow(), array(), array(),
+                                                               'noclasses' );
+                                               } else {
+                                                       $link = '';
+                                               }
+                                       }
+                                       if ( $pre ) {
+                                               # written summary $presep autocomment (summary /* section */)
+                                               $pre .= wfMessage( 'autocomment-prefix' )->inContentLanguage()->escaped();
+                                       }
+                                       if ( $post ) {
+                                               # autocomment $postsep written summary (/* section */ summary)
+                                               $auto .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped();
+                                       }
+                                       $auto = '<span class="autocomment">' . $auto . '</span>';
+                                       $comment = $pre . $link . $wgLang->getDirMark()
+                                               . '<span dir="auto">' . $auto . $post . '</span>';
                                }
-                       }
-                       if ( $pre ) {
-                               # written summary $presep autocomment (summary /* section */)
-                               $pre .= wfMessage( 'autocomment-prefix' )->inContentLanguage()->escaped();
-                       }
-                       if ( $post ) {
-                               # autocomment $postsep written summary (/* section */ summary)
-                               $auto .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped();
-                       }
-                       $auto = '<span class="autocomment">' . $auto . '</span>';
-                       $comment = $pre . $link . $wgLang->getDirMark()
-                               . '<span dir="auto">' . $auto . $post . '</span>';
-               }
-               return $comment;
+                               return $comment;
+               },
+                       $comment
+               );
        }
 
-       /** @var Title */
-       private static $commentContextTitle;
-
-       /** @var bool Whether section links should refer to local page */
-       private static $commentLocal;
-
        /**
         * Formats wiki links and media links in text; all other wiki formatting
         * is ignored
@@ -1422,9 +1395,7 @@ class Linker {
         * @return string
         */
        public static function formatLinksInComment( $comment, $title = null, $local = false ) {
-               self::$commentContextTitle = $title;
-               self::$commentLocal = $local;
-               $html = preg_replace_callback(
+               return preg_replace_callback(
                        '/
                                \[\[
                                :? # ignore optional leading colon
@@ -1437,88 +1408,79 @@ class Linker {
                                \]\]
                                ([^[]*) # 3. link trail (the text up until the next link)
                        /x',
-                       array( 'Linker', 'formatLinksInCommentCallback' ),
-                       $comment );
-               self::$commentContextTitle = null;
-               self::$commentLocal = null;
-               return $html;
-       }
-
-       /**
-        * @param array $match
-        * @return mixed
-        */
-       protected static function formatLinksInCommentCallback( $match ) {
-               global $wgContLang;
+                       function ( $match ) use ( $title, $local ) {
+                               global $wgContLang;
 
-               $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
-               $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
+                               $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
+                               $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
 
-               $comment = $match[0];
+                               $comment = $match[0];
 
-               # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
-               if ( strpos( $match[1], '%' ) !== false ) {
-                       $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), rawurldecode( $match[1] ) );
-               }
+                               # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
+                               if ( strpos( $match[1], '%' ) !== false ) {
+                                       $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), rawurldecode( $match[1] ) );
+                               }
 
-               # Handle link renaming [[foo|text]] will show link as "text"
-               if ( $match[2] != "" ) {
-                       $text = $match[2];
-               } else {
-                       $text = $match[1];
-               }
-               $submatch = array();
-               $thelink = null;
-               if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
-                       # Media link; trail not supported.
-                       $linkRegexp = '/\[\[(.*?)\]\]/';
-                       $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
-                       if ( $title ) {
-                               $thelink = self::makeMediaLinkObj( $title, $text );
-                       }
-               } else {
-                       # Other kind of link
-                       if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
-                               $trail = $submatch[1];
-                       } else {
-                               $trail = "";
-                       }
-                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
-                       if ( isset( $match[1][0] ) && $match[1][0] == ':' ) {
-                               $match[1] = substr( $match[1], 1 );
-                       }
-                       list( $inside, $trail ) = self::splitTrail( $trail );
-
-                       $linkText = $text;
-                       $linkTarget = self::normalizeSubpageLink( self::$commentContextTitle,
-                               $match[1], $linkText );
-
-                       $target = Title::newFromText( $linkTarget );
-                       if ( $target ) {
-                               if ( $target->getText() == '' && !$target->isExternal()
-                                       && !self::$commentLocal && self::$commentContextTitle
-                               ) {
-                                       $newTarget = clone ( self::$commentContextTitle );
-                                       $newTarget->setFragment( '#' . $target->getFragment() );
-                                       $target = $newTarget;
+                               # Handle link renaming [[foo|text]] will show link as "text"
+                               if ( $match[2] != "" ) {
+                                       $text = $match[2];
+                               } else {
+                                       $text = $match[1];
+                               }
+                               $submatch = array();
+                               $thelink = null;
+                               if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
+                                       # Media link; trail not supported.
+                                       $linkRegexp = '/\[\[(.*?)\]\]/';
+                                       $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
+                                       if ( $title ) {
+                                               $thelink = Linker::makeMediaLinkObj( $title, $text );
+                                       }
+                               } else {
+                                       # Other kind of link
+                                       if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
+                                               $trail = $submatch[1];
+                                       } else {
+                                               $trail = "";
+                                       }
+                                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                                       if ( isset( $match[1][0] ) && $match[1][0] == ':' ) {
+                                               $match[1] = substr( $match[1], 1 );
+                                       }
+                                       list( $inside, $trail ) = Linker::splitTrail( $trail );
+
+                                       $linkText = $text;
+                                       $linkTarget = Linker::normalizeSubpageLink( $title, $match[1], $linkText );
+
+                                       $target = Title::newFromText( $linkTarget );
+                                       if ( $target ) {
+                                               if ( $target->getText() == '' && !$target->isExternal()
+                                                       && !$local && $title
+                                               ) {
+                                                       $newTarget = clone ( $title );
+                                                       $newTarget->setFragment( '#' . $target->getFragment() );
+                                                       $target = $newTarget;
+                                               }
+                                               $thelink = Linker::link(
+                                                       $target,
+                                                       $linkText . $inside
+                                               ) . $trail;
+                                       }
+                               }
+                               if ( $thelink ) {
+                                       // If the link is still valid, go ahead and replace it in!
+                                       $comment = preg_replace(
+                                               $linkRegexp,
+                                               StringUtils::escapeRegexReplacement( $thelink ),
+                                               $comment,
+                                               1
+                                       );
                                }
-                               $thelink = self::link(
-                                       $target,
-                                       $linkText . $inside
-                               ) . $trail;
-                       }
-               }
-               if ( $thelink ) {
-                       // If the link is still valid, go ahead and replace it in!
-                       $comment = preg_replace(
-                               $linkRegexp,
-                               StringUtils::escapeRegexReplacement( $thelink ),
-                               $comment,
-                               1
-                       );
-               }
 
-               return $comment;
+                               return $comment;
+                       },
+                       $comment
+               );
        }
 
        /**