Merge "Allow local interwiki links with an empty title part"
[lhc/web/wiklou.git] / includes / Linker.php
index 7724548..7d88f25 100644 (file)
@@ -408,6 +408,11 @@ class Linker {
         * @return string
         */
        public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) {
+               $ret = "<strong class=\"selflink\">{$prefix}{$html}</strong>{$trail}";
+               if ( !wfRunHooks( 'SelfLinkBegin', array( $nt, &$html, &$trail, &$prefix, &$ret ) ) ) {
+                       return $ret;
+               }
+
                if ( $html == '' ) {
                        $html = htmlspecialchars( $nt->getPrefixedText() );
                }
@@ -603,7 +608,6 @@ class Linker {
                                }
 
                                // Reduce width for upright images when parameter 'upright' is used
-                               $useSquare = !isset( $fp['upright'] );
                                if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
                                        $fp['upright'] = $wgThumbUpright;
                                }
@@ -615,16 +619,11 @@ class Linker {
                                        round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
                                        $wgThumbLimits[$widthOption];
 
-                               // Use whichever is smaller: real image width or user preference width
+                               // Use width which is smaller: real image width or user preference width
                                // Unless image is scalable vector.
                                if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
-                                               $prefWidth < $hp['width'] ||
-                                               ( $useSquare && $prefWidth < $file->getHeight( $page ) ) ||
-                                               $file->isVectorized() ) ) {
+                                               $prefWidth < $hp['width'] || $file->isVectorized() ) ) {
                                        $hp['width'] = $prefWidth;
-                                       if ( $useSquare ) {
-                                               $hp['height'] = $prefWidth;
-                                       }
                                }
                        }
                }
@@ -1318,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
@@ -1338,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
@@ -1423,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
@@ -1438,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;
-       }
+                       function ( $match ) use ( $title, $local ) {
+                               global $wgContLang;
 
-       /**
-        * @param array $match
-        * @return mixed
-        */
-       protected static function formatLinksInCommentCallback( $match ) {
-               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
+               );
        }
 
        /**