Merge "Check for error before outputting srcset urls"
[lhc/web/wiklou.git] / includes / Linker.php
index 7724548..f0b16ab 100644 (file)
@@ -25,7 +25,7 @@
  * for primarily page content: links, embedded images, table of contents. Links
  * are also used in the skin.
  *
- * @todo: turn this into a legacy interface for HtmlPageLinkRenderer and similar services.
+ * @todo turn this into a legacy interface for HtmlPageLinkRenderer and similar services.
  *
  * @ingroup Skins
  */
@@ -259,6 +259,7 @@ class Linker {
 
        /**
         * Identical to link(), except $options defaults to 'known'.
+        * @see Linker::link
         * @return string
         */
        public static function linkKnown(
@@ -272,7 +273,7 @@ class Linker {
         * Returns the Url used to link to a Title
         *
         * @param Title $target
-        * @param array $query query parameters
+        * @param array $query Query parameters
         * @param array $options
         * @return string
         */
@@ -408,6 +409,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 +609,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 +620,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;
-                                       }
                                }
                        }
                }
@@ -703,6 +703,7 @@ class Linker {
         * frame parameters supplied by the Parser.
         * @param array $frameParams The frame parameters
         * @param string $query An optional query string to add to description page links
+        * @param Parser|null $parser
         * @return array
         */
        private static function getImageLinkMTOParams( $frameParams, $query = '', $parser = null ) {
@@ -771,7 +772,6 @@ class Linker {
        public static function makeThumbLink2( Title $title, $file, $frameParams = array(),
                $handlerParams = array(), $time = false, $query = ""
        ) {
-               global $wgStylePath, $wgContLang;
                $exists = $file && $file->exists();
 
                # Shortcuts
@@ -880,12 +880,7 @@ class Linker {
                                                'href' => $url,
                                                'class' => 'internal',
                                                'title' => wfMessage( 'thumbnail-more' )->text() ),
-                                               Html::element( 'img', array(
-                                                       'src' => $wgStylePath . '/common/images/magnify-clip'
-                                                               . ( $wgContLang->isRTL() ? '-rtl' : '' ) . '.png',
-                                                       'width' => 15,
-                                                       'height' => 11,
-                                                       'alt' => "" ) ) ) );
+                                               "" ) );
                        }
                }
                $s .= '  <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
@@ -914,10 +909,10 @@ class Linker {
 
                        $thumb15 = $file->transform( $hp15 );
                        $thumb20 = $file->transform( $hp20 );
-                       if ( $thumb15 && $thumb15->getUrl() !== $thumb->getUrl() ) {
+                       if ( $thumb15 && !$thumb15->isError() && $thumb15->getUrl() !== $thumb->getUrl() ) {
                                $thumb->responsiveUrls['1.5'] = $thumb15->getUrl();
                        }
-                       if ( $thumb20 && $thumb20->getUrl() !== $thumb->getUrl() ) {
+                       if ( $thumb20 && !$thumb20->isError() && $thumb20->getUrl() !== $thumb->getUrl() ) {
                                $thumb->responsiveUrls['2'] = $thumb20->getUrl();
                        }
                }
@@ -938,7 +933,7 @@ class Linker {
                $query = '', $unused1 = '', $unused2 = '', $time = false
        ) {
                global $wgEnableUploads, $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
-               if ( ! $title instanceof Title ) {
+               if ( !$title instanceof Title ) {
                        return "<!-- ERROR -->" . htmlspecialchars( $label );
                }
                wfProfileIn( __METHOD__ );
@@ -1318,12 +1313,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 +1327,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 +1391,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 +1404,83 @@ 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
+               );
        }
 
        /**
@@ -2237,7 +2198,7 @@ class Linker {
         *
         * @param User $user
         * @param Revision $rev
-        * @param Revision $title
+        * @param Title $title
         * @return string HTML fragment
         */
        public static function getRevDeleteLink( User $user, Revision $rev, Title $title ) {