Add class to non-content links
[lhc/web/wiklou.git] / includes / Linker.php
index 105e24b..3e755a5 100644 (file)
@@ -79,22 +79,21 @@ class Linker {
        /**
         * Return the CSS colour of a known link
         *
-        * @param mixed $s
+        * @param Title $t
         * @param integer $threshold user defined threshold
         * @return string CSS class
         */
-       function getLinkColour( $s, $threshold ) {
-               if( $s === false ) {
-                       return '';
-               }
-
+       function getLinkColour( $t, $threshold ) {
                $colour = '';
-               if ( !empty( $s->page_is_redirect ) ) {
+               if ( $t->isRedirect() ) {
                        # Page is a redirect
                        $colour = 'mw-redirect';
-               } elseif ( $threshold > 0 && $s->page_len < $threshold && Namespace::isContent( $s->page_namespace ) ) {
+               } elseif ( $threshold > 0 && $t->getLength() < $threshold && MWNamespace::isContent( $t->getNamespace() ) ) {
                        # Page is a stub
                        $colour = 'stub';
+               } elseif ( !MWNamespace::isContent( $t->getNamespace() ) ) {
+                       # Non-article page
+                       $colour = 'mw-noncontent';
                }
                return $colour;
        }
@@ -127,7 +126,7 @@ class Linker {
        /**
         * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
         * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
-        * 
+        *
         * @param $title String: the text of the title
         * @param $text  String: link text
         * @param $query String: optional query part
@@ -148,7 +147,7 @@ class Linker {
        /**
         * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
         * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
-        * 
+        *
         * @param string $title The text of the title
         * @param string $text Link text
         * @param string $query Optional query part
@@ -168,10 +167,10 @@ 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.
-        * 
+        *
         * @param $title String: the text of the title
         * @param $text  String: link text
         * @param $query String: optional query part
@@ -193,7 +192,7 @@ class Linker {
         * Make a link for a title which may or may not be in the database. If you need to
         * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
         * call to this will result in a DB query.
-        * 
+        *
         * @param $nt     Title: the title object to make the link from, e.g. from
         *                      Title::newFromText.
         * @param $text  String: link text
@@ -232,7 +231,7 @@ class Linker {
                        wfProfileOut( __METHOD__ );
                        return $t;
                } elseif ( $nt->isAlwaysKnown() ) {
-                       # Image links, special page links and self-links with fragements are always known.
+                       # Image links, special page links and self-links with fragments are always known.
                        $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
                } else {
                        wfProfileIn( __METHOD__.'-immediate' );
@@ -256,15 +255,8 @@ class Linker {
                        } else {
                                $colour = '';
                                if ( $nt->isContentPage() ) {
-                                       # FIXME: This is stupid, we should combine this query with
-                                       # the Title::getArticleID() query above.
                                        $threshold = $wgUser->getOption('stubthreshold');
-                                       $dbr = wfGetDB( DB_SLAVE );
-                                       $s = $dbr->selectRow(
-                                               array( 'page' ),
-                                               array( 'page_len', 'page_is_redirect', 'page_namespace' ),
-                                               array( 'page_id' => $aid ), __METHOD__ ) ;
-                                       $colour = $this->getLinkColour( $s, $threshold );
+                                       $colour = $this->getLinkColour( $nt, $threshold );
                                }
                                $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
                        }
@@ -324,7 +316,7 @@ class Linker {
 
        /**
         * Make a red link to the edit page of a given title.
-        * 
+        *
         * @param $nt Title object of the target page
         * @param $text  String: Link text
         * @param $query String: Optional query part
@@ -356,8 +348,9 @@ class Linker {
                }
                $titleAttr = wfMsg( 'red-link-title', $titleText );
                $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new', $titleAttr );
-
                list( $inside, $trail ) = Linker::splitTrail( $trail );
+
+               wfRunHooks( 'BrokenLink', array( &$this, $nt, $query, &$u, &$style, &$prefix, &$text, &$inside, &$trail ) );
                $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
 
                wfProfileOut( __METHOD__ );
@@ -366,9 +359,9 @@ class Linker {
 
        /**
         * @deprecated use makeColouredLinkObj
-        * 
+        *
         * Make a brown link to a short article.
-        * 
+        *
         * @param $nt Title object of the target page
         * @param $text  String: link text
         * @param $query String: optional query part
@@ -382,7 +375,7 @@ class Linker {
 
        /**
         * Make a coloured link.
-        * 
+        *
         * @param $nt Title object of the target page
         * @param $colour Integer: colour of the link
         * @param $text   String:  link text
@@ -418,7 +411,7 @@ class Linker {
                return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
        }
 
-       /** 
+       /**
         * Make appropriate markup for a link to the current article. This is currently rendered
         * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
         * despite $query not being used.
@@ -456,7 +449,7 @@ class Linker {
                return $s;
        }
 
-       /** 
+       /**
         * Creates the HTML source for images
         * @deprecated use makeImageLink2
         *
@@ -503,7 +496,7 @@ class Linker {
         * @param File $file File object, or false if it doesn't exist
         *
         * @param array $frameParams Associative array of parameters external to the media handler.
-        *     Boolean parameters are indicated by presence or absence, the value is arbitrary and 
+        *     Boolean parameters are indicated by presence or absence, the value is arbitrary and
         *     will often be false.
         *          thumbnail       If present, downscale and frame
         *          manualthumb     Image name to use as a thumbnail, instead of automatic scaling
@@ -513,13 +506,13 @@ class Linker {
         *          upright_factor  Fudge factor for "upright" tweak (default 0.75)
         *          border          If present, show a border around the image
         *          align           Horizontal alignment (left, right, center, none)
-        *          valign          Vertical alignment (baseline, sub, super, top, text-top, middle, 
+        *          valign          Vertical alignment (baseline, sub, super, top, text-top, middle,
         *                          bottom, text-bottom)
         *          alt             Alternate text for image (i.e. alt attribute). Plain text.
         *          caption         HTML for image caption.
         *
-        * @param array $handlerParams Associative array of media handler parameters, to be passed 
-        *       to transform(). Typical keys are "width" and "page". 
+        * @param array $handlerParams Associative array of media handler parameters, to be passed
+        *       to transform(). Typical keys are "width" and "page".
         * @param string $time, timestamp of the file, set as false for current
         * @return string HTML for an image, with links, wrappers, etc.
         */
@@ -569,8 +562,8 @@ class Linker {
                                }
                                // 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'], -1 ) : 
+                               $prefWidth = isset( $fp['upright'] ) ?
+                                       round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) :
                                        $wgThumbLimits[$wopt];
                                if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
                                        $hp['width'] = $prefWidth;
@@ -626,11 +619,11 @@ class Linker {
 
        /**
         * Make HTML for a thumbnail including image, border and caption
-        * @param Title $title 
+        * @param Title $title
         * @param File $file File object or false if it doesn't exist
         */
        function makeThumbLinkObj( Title $title, $file, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manualthumb = "" ) {
-               $frameParams = array( 
+               $frameParams = array(
                        'alt' => $alt,
                        'caption' => $label,
                        'align' => $align
@@ -654,7 +647,7 @@ class Linker {
                if ( !isset( $fp['caption'] ) ) $fp['caption'] = '';
 
                if ( empty( $hp['width'] ) ) {
-                       // Reduce width for upright images when parameter 'upright' is used 
+                       // Reduce width for upright images when parameter 'upright' is used
                        $hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
                }
                $thumb = false;
@@ -744,9 +737,9 @@ class Linker {
                                if( $text == '' )
                                        $text = htmlspecialchars( $title->getPrefixedText() );
                                $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
-                               if( $redir ) {  
+                               if( $redir ) {
                                        return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
-                               } 
+                               }
                                $q = 'wpDestFile=' . $title->getPartialUrl();
                                if( $query != '' )
                                        $q .= '&' . $query;
@@ -853,9 +846,10 @@ class Linker {
         * @param string $userText User name or IP address
         * @param bool $redContribsWhenNoEdits Should the contributions link be red if the user has no edits?
         * @param int $flags Customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
+        * @param int $edits, user edit count (optional, for performance)
         * @return string
         */
-       public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0 ) {
+       public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits=null ) {
                global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
                $talkable = !( $wgDisableAnonTalk && 0 == $userId );
                $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
@@ -866,8 +860,9 @@ class Linker {
                }
                if( $userId ) {
                        // check if the user has an edit
-                       if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
-                               $style = " class='new'";
+                       if( $redContribsWhenNoEdits ) {
+                               $count = !is_null($edits) ? $edits : User::edits( $userId );
+                               $style = ($count == 0) ? " class='new'" : '';
                        } else {
                                $style = '';
                        }
@@ -888,9 +883,12 @@ class Linker {
 
        /**
         * Alias for userToolLinks( $userId, $userText, true );
+        * @param int $userId User identifier
+        * @param string $userText User name or IP address
+        * @param int $edits, user edit count (optional, for performance)
         */
-       public function userToolLinksRedContribs( $userId, $userText ) {
-               return $this->userToolLinks( $userId, $userText, true );
+       public function userToolLinksRedContribs( $userId, $userText, $edits=null ) {
+               return $this->userToolLinks( $userId, $userText, true, 0, $edits );
        }
 
 
@@ -918,14 +916,17 @@ class Linker {
                        wfMsgHtml( 'blocklink' ) );
                return $blockLink;
        }
-       
+
        /**
         * Generate a user link if the current user is allowed to view it
         * @param $rev Revision object.
+        * @param $isPublic, bool, show only if all users can see it
         * @return string HTML
         */
-       function revUserLink( $rev ) {
-               if( $rev->userCan( Revision::DELETED_USER ) ) {
+       function revUserLink( $rev, $isPublic = false ) {
+               if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
+                       $link = wfMsgHtml( 'rev-deleted-user' );
+               } else if( $rev->userCan( Revision::DELETED_USER ) ) {
                        $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
                } else {
                        $link = wfMsgHtml( 'rev-deleted-user' );
@@ -939,22 +940,24 @@ class Linker {
        /**
         * Generate a user tool link cluster if the current user is allowed to view it
         * @param $rev Revision object.
+        * @param $isPublic, bool, show only if all users can see it
         * @return string HTML
         */
-       function revUserTools( $rev ) {
-               if( $rev->userCan( Revision::DELETED_USER ) ) {
+       function revUserTools( $rev, $isPublic = false ) {
+               if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
+                       $link = wfMsgHtml( 'rev-deleted-user' );
+               } else if( $rev->userCan( Revision::DELETED_USER ) ) {
                        $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
-                               ' ' .
-                               $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
+                       ' ' . $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
                } else {
                        $link = wfMsgHtml( 'rev-deleted-user' );
                }
                if( $rev->isDeleted( Revision::DELETED_USER ) ) {
-                       return '<span class="history-deleted">' . $link . '</span>';
+                       return ' <span class="history-deleted">' . $link . '</span>';
                }
                return $link;
        }
-       
+
        /**
         * This function is called by all recent changes variants, by the page history,
         * and by the user contributions list. It is responsible for formatting edit
@@ -1027,10 +1030,12 @@ class Linker {
                        }
                        $auto = $link . $auto;
                        if( $pre ) {
-                               $auto = '- ' . $auto; # written summary $presep autocomment (summary /* section */)
+                               # written summary $presep autocomment (summary /* section */)
+                               $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
                        }
                        if( $post ) {
-                               $auto .= ': '; # autocomment $postsep written summary (/* section */ summary)
+                               # autocomment $postsep written summary (/* section */ summary)
+                               $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
                        }
                        $auto = '<span class="autocomment">' . $auto . '</span>';
                        $comment = $pre . $auto . $post;
@@ -1053,13 +1058,13 @@ class Linker {
                        array( $this, 'formatLinksInCommentCallback' ),
                        $comment );
        }
-       
+
        protected function formatLinksInCommentCallback( $match ) {
                global $wgContLang;
 
-               $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
+               $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
                $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
-               
+
                $comment = $match[0];
 
                # Handle link renaming [[foo|text]] will show link as "text"
@@ -1118,14 +1123,16 @@ class Linker {
         *
         * @param Revision $rev
         * @param bool $local Whether section links should refer to local page
+        * @param $isPublic, show only if all users can see it
         * @return string HTML
         */
-       function revComment( Revision $rev, $local = false ) {
-               if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
+       function revComment( Revision $rev, $local = false, $isPublic = false ) {
+               if( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic ) {
+                       $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
+               } else if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
                        $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
                } else {
-                       $block = " <span class=\"comment\">" .
-                               wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
+                       $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
                }
                if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
                        return " <span class=\"history-deleted\">$block</span>";
@@ -1229,7 +1236,7 @@ class Linker {
                } elseif( $hook == 'EditSectionLinkForOther' ) {
                        wfRunHooks( 'EditSectionLinkForOther', array( &$this, $nt, $section, $url, &$result ) );
                }
-               
+
                // For reverse compatibility, add the brackets *after* the hook is run,
                // and even add them to hook-provided text.
                if( is_null( $result ) ) {
@@ -1297,7 +1304,7 @@ class Linker {
                        . $this->buildRollbackLink( $rev )
                        . ']</span>';
        }
-       
+
        /**
         * Build a raw rollback link, useful for collections of "tool" links
         *
@@ -1314,7 +1321,7 @@ class Linker {
                        $title,
                        wfMsgHtml( 'rollbacklink' ),
                        'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extra
-               );              
+               );
        }
 
        /**
@@ -1354,7 +1361,7 @@ class Linker {
 
                        foreach ( $templates as $titleObj ) {
                                $r = $titleObj->getRestrictions( 'edit' );
-                               if ( in_array( 'sysop', $r ) ) { 
+                               if ( in_array( 'sysop', $r ) ) {
                                        $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
                                } elseif ( in_array( 'autoconfirmed', $r ) ) {
                                        $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );