Inserted two hooks for extension-defined colour codes, and simplified the way link...
[lhc/web/wiklou.git] / includes / Linker.php
index bede54b..9917c88 100644 (file)
@@ -52,15 +52,13 @@ class Linker {
        }
 
        /** @todo document */
-       function getInternalLinkAttributes( $link, $text, $broken = false ) {
+       function getInternalLinkAttributes( $link, $text, $class = false ) {
                $link = urldecode( $link );
                $link = str_replace( '_', ' ', $link );
                $link = htmlspecialchars( $link );
 
-               if( $broken == 'stub' ) {
-                       $r = ' class="stub"';
-               } else if ( $broken == 'yes' ) {
-                       $r = ' class="new"';
+               if( $class ) {
+                       $r = ' class="'.$class.'"';
                } else {
                        $r = '';
                }
@@ -74,11 +72,9 @@ class Linker {
         * @param $text String: FIXME
         * @param $broken Boolean: FIXME, default 'false'.
         */
-       function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
-               if( $broken == 'stub' ) {
-                       $r = ' class="stub"';
-               } else if ( $broken == 'yes' ) {
-                       $r = ' class="new"';
+       function getInternalLinkAttributesObj( &$nt, $text, $class = false ) {
+               if( $class ) {
+                       $r = ' class="'.$class.'"';
                } else {
                        $r = '';
                }
@@ -99,16 +95,16 @@ class Linker {
         *                      the end of the link.
         */
        function makeLink( $title, $text = '', $query = '', $trail = '' ) {
-               wfProfileIn( 'Linker::makeLink' );
+               wfProfileIn( __METHOD__ );
                $nt = Title::newFromText( $title );
-               if ($nt) {
+               if ( $nt instanceof Title ) {
                        $result = $this->makeLinkObj( $nt, $text, $query, $trail );
                } else {
                        wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
                        $result = $text == "" ? $title : $text;
                }
 
-               wfProfileOut( 'Linker::makeLink' );
+               wfProfileOut( __METHOD__ );
                return $result;
        }
 
@@ -125,8 +121,8 @@ class Linker {
         */
        function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
                $nt = Title::newFromText( $title );
-               if ($nt) {
-                       return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
+               if ( $nt instanceof Title ) {
+                       return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
                } else {
                        wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
@@ -146,8 +142,8 @@ class Linker {
         */
        function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
                $nt = Title::newFromText( $title );
-               if ($nt) {
-                       return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
+               if ( $nt instanceof Title ) {
+                       return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
                } else {
                        wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
@@ -155,6 +151,8 @@ class Linker {
        }
 
        /**
+        * @deprecated use makeColouredLinkObj
+        * 
         * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
         * it if you already have a title object handy. See makeStubLinkObj for further documentation.
         * 
@@ -167,8 +165,8 @@ class Linker {
         */
        function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
                $nt = Title::newFromText( $title );
-               if ($nt) {
-                       return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
+               if ( $nt instanceof Title ) {
+                       return $this->makeStubLinkObj( $nt, $text, $query, $trail );
                } else {
                        wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
@@ -191,13 +189,11 @@ class Linker {
         */
        function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
                global $wgUser;
-               $fname = 'Linker::makeLinkObj';
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
 
-               # Fail gracefully
-               if ( ! is_object($nt) ) {
-                       # throw new MWException();
-                       wfProfileOut( $fname );
+               if ( !$nt instanceof Title ) {
+                       # Fail gracefully
+                       wfProfileOut( __METHOD__ );
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
 
@@ -217,13 +213,13 @@ class Linker {
                        }
                        $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
 
-                       wfProfileOut( $fname );
+                       wfProfileOut( __METHOD__ );
                        return $t;
                } elseif ( $nt->isAlwaysKnown() ) {
                        # Image links, special page links and self-links with fragements are always known.
                        $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
                } else {
-                       wfProfileIn( $fname.'-immediate' );
+                       wfProfileIn( __METHOD__.'-immediate' );
 
                        # Handles links to special pages wich do not exist in the database:
                        if( $nt->getNamespace() == NS_SPECIAL ) {
@@ -232,8 +228,8 @@ class Linker {
                                } else {
                                        $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
                                }
-                               wfProfileOut( $fname.'-immediate' );
-                               wfProfileOut( $fname );
+                               wfProfileOut( __METHOD__.'-immediate' );
+                               wfProfileOut( __METHOD__ );
                                return $retVal;
                        }
 
@@ -242,7 +238,7 @@ class Linker {
                        if ( 0 == $aid ) {
                                $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
                        } else {
-                               $stub = false;
+                               $colour = 1;
                                if ( $nt->isContentPage() ) {
                                        $threshold = $wgUser->getOption('stubthreshold');
                                        if ( $threshold > 0 ) {
@@ -251,20 +247,18 @@ class Linker {
                                                        array( 'page' ),
                                                        array( 'page_len',
                                                               'page_is_redirect' ),
-                                                       array( 'page_id' => $aid ), $fname ) ;
+                                                       array( 'page_id' => $aid ), __METHOD__ ) ;
                                                $stub = ( $s !== false && !$s->page_is_redirect &&
                                                          $s->page_len < $threshold );
+                                               $colour = $stub ? 2 : 1;
+                                               wfRunHooks( 'GetLinkColour', array( $dbr, $aid, &$colour ) );
                                        }
                                }
-                               if ( $stub ) {
-                                       $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
-                               } else {
-                                       $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
-                               }
+                               $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
                        }
-                       wfProfileOut( $fname.'-immediate' );
+                       wfProfileOut( __METHOD__.'-immediate' );
                }
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return $retVal;
        }
 
@@ -283,13 +277,12 @@ class Linker {
         * @return the a-element
         */
        function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
+               wfProfileIn( __METHOD__ );
 
-               $fname = 'Linker::makeKnownLinkObj';
-               wfProfileIn( $fname );
-
-               if ( !is_object( $nt ) ) {
-                       wfProfileOut( $fname );
-                       return $text;
+               if ( !$nt instanceof Title ) {
+                       # Fail gracefully
+                       wfProfileOut( __METHOD__ );
+                       return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
 
                $u = $nt->escapeLocalURL( $query );
@@ -313,7 +306,7 @@ class Linker {
 
                list( $inside, $trail ) = Linker::splitTrail( $trail );
                $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return $r;
        }
 
@@ -328,15 +321,14 @@ class Linker {
         *                      the end of the link.
         */
        function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               # Fail gracefully
-               if ( ! isset($nt) ) {
-                       # throw new MWException();
+               wfProfileIn( __METHOD__ );
+
+               if ( !$nt instanceof Title ) {
+                       # Fail gracefully
+                       wfProfileOut( __METHOD__ );
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
 
-               $fname = 'Linker::makeBrokenLinkObj';
-               wfProfileIn( $fname );
-
                if( $nt->getNamespace() == NS_SPECIAL ) {
                        $q = $query;
                } else if ( '' == $query ) {
@@ -349,16 +341,18 @@ class Linker {
                if ( '' == $text ) {
                        $text = htmlspecialchars( $nt->getPrefixedText() );
                }
-               $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
+               $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new' );
 
                list( $inside, $trail ) = Linker::splitTrail( $trail );
                $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
 
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return $s;
        }
 
        /**
+        * @deprecated use makeColouredLinkObj
+        * 
         * Make a brown link to a short article.
         * 
         * @param $title String: the text of the title
@@ -369,7 +363,36 @@ class Linker {
         *                      the end of the link.
         */
        function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
+               makeColouredLinkObj( $nt, 2, $text, $query, $trail, $prefix );
+       }
+
+       /**
+        * Make a coloured link.
+        * 
+        * @param $title  String:  the text of the title
+        * @param $colour Integer: colour of the link
+        * @param $text   String:  link text
+        * @param $query  String:  optional query part
+        * @param $trail  String:  optional trail. Alphabetic characters at the start of this string will
+        *                      be included in the link text. Other characters will be appended after
+        *                      the end of the link.
+        */
+       function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
+
+               // convert colour into class name
+               $colourcode = array(
+                       0 => 'new',
+                       1 => '',
+                       2 => 'stub'
+               );
+               // allow alternative colour code
+               wfRunHooks( 'GetLinkColourCode', array( &$colourcode ) );
+
+               $class = $colourcode[$colour];
+
+               if($class){
+                       $style = $this->getInternalLinkAttributesObj( $nt, $text, $class );
+               } else $style = '';
                return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
        }
 
@@ -388,11 +411,8 @@ class Linker {
        function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                global $wgUser;
                $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
-               if( $size < $threshold ) {
-                       return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
-               } else {
-                       return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
-               }
+               $colour = ( $size < $threshold ) ? 2 : 1;
+               return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
        }
 
        /** 
@@ -459,7 +479,7 @@ class Linker {
                        $frameParams['framed'] = true;
                }
                if ( $thumb ) {
-                       $frameParams['thumb'] = true;
+                       $frameParams['thumbnail'] = true;
                }
                if ( $manualthumb ) {
                        $frameParams['manualthumb'] = $manualthumb;
@@ -468,7 +488,7 @@ class Linker {
                        $frameParams['valign'] = $valign;
                }
                $file = wfFindFile( $title, $time );
-               return $this->makeImageLink2( $title, $file, $label, $alt, $frameParams, $handlerParams );
+               return $this->makeImageLink2( $title, $file, $frameParams, $handlerParams );
        }
 
        /**
@@ -530,13 +550,13 @@ class Linker {
                                }
 
                                // Reduce width for upright images when parameter 'upright' is used
-                               if ( !isset( $fp['upright_factor'] ) || $fp['upright_factor'] == 0 ) {
-                                       $fp['upright_factor'] = $wgThumbUpright;
+                               if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
+                                       $fp['upright'] = $wgThumbUpright;
                                }
                                // Use width which is smaller: real image width or user preference width
                                // For caching health: If width scaled down due to upright parameter, round to full __0 pixel to avoid the creation of a lot of odd thumbs
                                $prefWidth = isset( $fp['upright'] ) ? 
-                                       round( $wgThumbLimits[$wopt] * $fp['upright_factor'], -1 ) : 
+                                       round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) : 
                                        $wgThumbLimits[$wopt];
                                if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
                                        $hp['width'] = $prefWidth;
@@ -544,7 +564,7 @@ class Linker {
                        }
                }
 
-               if ( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) ) {
+               if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
 
                        # Create a thumbnail. Alignment depends on language
                        # writing direction, # right aligned for left-to-right-
@@ -566,33 +586,14 @@ class Linker {
                        $thumb = false;
                }
 
-               if ( $page ) {
-                       $query = 'page=' . urlencode( $page );
-               } else {
-                       $query = '';
-               }
-               $url = $title->getLocalURL( $query );
-               $imgAttribs = array(
-                       'alt' => $fp['alt'],
-                       'longdesc' => $url
-               );
-
-               if ( isset( $fp['valign'] ) ) {
-                       $imgAttribs['style'] = "vertical-align: {$fp['valign']}";
-               }
-               if ( isset( $fp['border'] ) ) {
-                       $imgAttribs['class'] = "thumbborder";
-               }
-               $linkAttribs = array(
-                       'href' => $url,
-                       'class' => 'image',
-                       'title' => $fp['alt']
-               );
-
                if ( !$thumb ) {
                        $s = $this->makeBrokenImageLinkObj( $title );
                } else {
-                       $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
+                       $s = $thumb->toHtml( array(
+                               'desc-link' => true,
+                               'alt' => $fp['alt'],
+                               'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
+                               'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false ) );
                }
                if ( '' != $fp['align'] ) {
                        $s = "<div class=\"float{$fp['align']}\"><span>{$s}</span></div>";
@@ -684,18 +685,10 @@ class Linker {
                        $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
                        $zoomicon = '';
                } else {
-                       $imgAttribs = array(
+                       $s .= $thumb->toHtml( array(
                                'alt' => $fp['alt'],
-                               'longdesc' => $url,
-                               'class' => 'thumbimage'
-                       );
-                       $linkAttribs = array(
-                               'href' => $url,
-                               'class' => 'internal',
-                               'title' => $fp['alt']
-                       );
-                       
-                       $s .= $thumb->toHtml( $imgAttribs, $linkAttribs );
+                               'img-class' => 'thumbimage',
+                               'desc-link' => true ) );
                        if ( isset( $fp['framed'] ) ) {
                                $zoomicon="";
                        } else {
@@ -731,7 +724,7 @@ class Linker {
                                if( $query != '' )
                                        $q .= '&' . $query;
                                list( $inside, $trail ) = self::splitTrail( $trail );
-                               $style = $this->getInternalLinkAttributesObj( $title, $text, 'yes' );
+                               $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
                                wfProfileOut( __METHOD__ );
                                return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
                                        . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
@@ -1019,42 +1012,49 @@ class Linker {
         * Formats wiki links and media links in text; all other wiki formatting
         * is ignored
         *
+        * @fixme doesn't handle sub-links as in image thumb texts like the main parser
         * @param string $comment Text to format links in
         * @return string
         */
        public function formatLinksInComment( $comment ) {
+               return preg_replace_callback(
+                       '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
+                       array( $this, 'formatLinksInCommentCallback' ),
+                       $comment );
+       }
+       
+       protected function formatLinksInCommentCallback( $match ) {
                global $wgContLang;
 
                $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
                $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
+               
+               $comment = $match[0];
 
-               $match = array();
-               while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
-                       # Handle link renaming [[foo|text]] will show link as "text"
-                       if( "" != $match[3] ) {
-                               $text = $match[3];
-                       } else {
-                               $text = $match[1];
-                       }
-                       $submatch = array();
-                       if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
-                               # Media link; trail not supported.
-                               $linkRegexp = '/\[\[(.*?)\]\]/';
-                               $thelink = $this->makeMediaLink( $submatch[1], "", $text );
+               # Handle link renaming [[foo|text]] will show link as "text"
+               if( "" != $match[3] ) {
+                       $text = $match[3];
+               } else {
+                       $text = $match[1];
+               }
+               $submatch = array();
+               if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
+                       # Media link; trail not supported.
+                       $linkRegexp = '/\[\[(.*?)\]\]/';
+                       $thelink = $this->makeMediaLink( $submatch[1], "", $text );
+               } else {
+                       # Other kind of link
+                       if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
+                               $trail = $submatch[1];
                        } else {
-                               # Other kind of link
-                               if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
-                                       $trail = $submatch[1];
-                               } else {
-                                       $trail = "";
-                               }
-                               $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
-                               if (isset($match[1][0]) && $match[1][0] == ':')
-                                       $match[1] = substr($match[1], 1);
-                               $thelink = $this->makeLink( $match[1], $text, "", $trail );
+                               $trail = "";
                        }
-                       $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
+                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                       if (isset($match[1][0]) && $match[1][0] == ':')
+                               $match[1] = substr($match[1], 1);
+                       $thelink = $this->makeLink( $match[1], $text, "", $trail );
                }
+               $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
 
                return $comment;
        }
@@ -1130,7 +1130,7 @@ class Linker {
        /** @todo document */
        function tocList($toc) {
                global $wgJsMimeType;
-               $title =  wfMsgHtml('toc') ;
+               $title = wfMsgHtml('toc') ;
                return
                   '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
                 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
@@ -1361,6 +1361,8 @@ class Linker {
         *   element (e.g., ' title="This does something [x]" accesskey="x"').
         */
        public function tooltipAndAccesskey($name) {
+               $fname="Linker::tooltipAndAccesskey";
+               wfProfileIn($fname);
                $out = '';
 
                $tooltip = wfMsg('tooltip-'.$name);
@@ -1376,6 +1378,7 @@ class Linker {
                } elseif ($out) {
                        $out .= '"';
                }
+               wfProfileOut($fname);
                return $out;
        }