Revert Special:Log to r20745 with non-ugly form
[lhc/web/wiklou.git] / includes / Linker.php
index ad2f098..b216763 100644 (file)
@@ -4,7 +4,6 @@
  * These functions are used for primarily page content:
  * links, embedded images, table of contents. Links are
  * also used in the skin.
- * @package MediaWiki
  */
 
 /**
  * so that ever other bit of the wiki doesn't have to
  * go loading up Skin to get at it.
  *
- * @package MediaWiki
  */
 class Linker {
-       function Linker() {}
+       function __construct() {}
 
        /**
         * @deprecated
@@ -229,7 +227,7 @@ class Linker {
                        } else {
                                $threshold = $wgUser->getOption('stubthreshold') ;
                                if ( $threshold > 0 ) {
-                                       $dbr =& wfGetDB( DB_SLAVE );
+                                       $dbr = wfGetDB( DB_SLAVE );
                                        $s = $dbr->selectRow(
                                                array( 'page' ),
                                                array( 'page_len',
@@ -432,7 +430,7 @@ class Linker {
 
        /** @todo document */
        function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
-         $thumb = false, $manual_thumb = '', $page = null )
+         $thumb = false, $manual_thumb = '', $page = null, $valign = '' )
        {
                global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
 
@@ -533,6 +531,9 @@ class Linker {
                                 ( $width
                                        ? ( 'width="'.$width.'" height="'.$height.'" ' )
                                        : '' ) .
+                                ( $valign
+                                       ? ( 'style="vertical-align: '.$valign.'" ' )
+                                       : '' ) .
                                 'longdesc="'.$u.'" /></a>';
                }
                if ( '' != $align ) {
@@ -611,7 +612,7 @@ class Linker {
                        $thumbUrl = $img->getViewURL();
                        if( $boxheight == -1 ) {
                                // Approximate...
-                               $boxheight = intval( $height * $boxwidth / $width );
+                               $boxheight = round( $height * $boxwidth / $width );
                        }
                }
                if ( $error ) {
@@ -631,7 +632,7 @@ class Linker {
                                $zoomicon =  '<div class="magnify" style="float:'.$magnifyalign.'">'.
                                        '<a href="'.$u.'" class="internal" title="'.$more.'">'.
                                        '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
-                                       'width="15" height="11" alt="'.$more.'" /></a></div>';
+                                       'width="15" height="11" alt="" /></a></div>';
                        }
                }
                $s .= '  <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
@@ -680,8 +681,6 @@ class Linker {
         *
         * @param $title Title object.
         * @param $text  String: pre-sanitized HTML
-        * @param $nourl Boolean: Mask absolute URLs, so the parser doesn't
-        *                       linkify them (it is currently not context-aware)
         * @return string HTML
         *
         * @public
@@ -756,10 +755,10 @@ class Linker {
        /**
         * @param $userId Integer: user id in database.
         * @param $userText String: user name in database.
+        * @param $redContribsWhenNoEdits Bool: return a red contribs link when the user had no edits and this is true.
         * @return string HTML fragment with talk and/or block links
-        * @private
         */
-       function userToolLinks( $userId, $userText ) {
+       public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false ) {
                global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
                $talkable = !( $wgDisableAnonTalk && 0 == $userId );
                $blockable = ( $wgSysopUserBans || 0 == $userId );
@@ -769,9 +768,15 @@ class Linker {
                        $items[] = $this->userTalkLink( $userId, $userText );
                }
                if( $userId ) {
+                       // check if the user has an edit
+                       if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
+                               $style = "class='new'";
+                       } else {
+                               $style = '';
+                       }
                        $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
-                       $items[] = $this->makeKnownLinkObj( $contribsPage ,
-                               wfMsgHtml( 'contribslink' ) );
+
+                       $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
                }
                if( $blockable && $wgUser->isAllowed( 'block' ) ) {
                        $items[] = $this->blockLink( $userId, $userText );
@@ -784,6 +789,14 @@ class Linker {
                }
        }
 
+       /**
+        * Alias for userToolLinks( $userId, $userText, true );
+        */
+       public function userToolLinksRedContribs( $userId, $userText ) {
+               return $this->userToolLinks( $userId, $userText, true );
+       }
+
+
        /**
         * @param $userId Integer: user id in database.
         * @param $userText String: user name in database.
@@ -791,11 +804,8 @@ class Linker {
         * @private
         */
        function userTalkLink( $userId, $userText ) {
-               global $wgLang;
-               $talkname = $wgLang->getNsText( NS_TALK ); # use the shorter name
-
                $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
-               $userTalkLink = $this->makeLinkObj( $userTalkPage, $talkname );
+               $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
                return $userTalkLink;
        }
 
@@ -860,7 +870,7 @@ class Linker {
         * Since you can't set a default parameter for a reference, I've turned it
         * temporarily to a value pass. Should be adjusted further. --brion
         *
-        * $param string $comment
+        * @param string $comment
         * @param mixed $title Title object (to generate link to the section in autocomment) or null
         * @param bool $local Whether section links should refer to local page
         */
@@ -1013,7 +1023,7 @@ class Linker {
        /** @todo document */
        function tocList($toc) {
                global $wgJsMimeType;
-               $title =  wfMsgForContent('toc') ;
+               $title =  wfMsgHtml('toc') ;
                return
                   '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
                 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
@@ -1023,15 +1033,15 @@ class Linker {
                 . "</ul>\n</td></tr></table>"
                 . '<script type="' . $wgJsMimeType . '">'
                 . ' if (window.showTocToggle) {'
-                . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
-                . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
+                . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
+                . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
                 . ' showTocToggle();'
                 . ' } '
                 . "</script>\n";
        }
 
        /** @todo document */
-       function editSectionLinkForOther( $title, $section ) {
+       public function editSectionLinkForOther( $title, $section ) {
                global $wgContLang;
 
                $title = Title::newFromText( $title );
@@ -1047,7 +1057,7 @@ class Linker {
         * @param $section Integer: section number.
         * @param $hint Link String: title, or default if omitted or empty
         */
-       function editSectionLink( $nt, $section, $hint='' ) {
+       public function editSectionLink( $nt, $section, $hint='' ) {
                global $wgContLang;
 
                $editurl = '&section='.$section;
@@ -1057,6 +1067,22 @@ class Linker {
                return "<span class=\"editsection\">[".$url."]</span>";
        }
 
+       /**
+        * Create a headline for content
+        *
+        * @param int    $level   The level of the headline (1-6)
+        * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
+        *                        This *must* be at least '>' for no attribs
+        * @param string $anchor  The anchor to give the headline (the bit after the #)
+        * @param string $text    The text of the header
+        * @param string $link    HTML to add for the section edit link
+        *
+        * @return string HTML headline
+        */
+       public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
+               return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
+       }
+
        /**
         * Split a link trail, return the "inside" portion and the remainder of the trail
         * as a two-element array
@@ -1118,7 +1144,7 @@ class Linker {
                global $wgUser;
                wfProfileIn( __METHOD__ );
 
-               $sk =& $wgUser->getSkin();
+               $sk = $wgUser->getSkin();
 
                $outText = '';
                if ( count( $templates ) > 0 ) {
@@ -1131,20 +1157,114 @@ class Linker {
 
                        # Construct the HTML
                        $outText = '<div class="mw-templatesUsedExplanation">';
-                       if ($preview)
+                       if ( $preview ) {
                                $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
-                       elseif ($section)
+                       } elseif ( $section ) {
                                $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
-                       else
+                       } else {
                                $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
+                       }
                        $outText .= '</div><ul>';
+
                        foreach ( $templates as $titleObj ) {
-                               $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
+                               $r = $titleObj->getRestrictions( 'edit' );
+                               if ( in_array( 'sysop', $r ) ) { 
+                                       $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
+                               } elseif ( in_array( 'autoconfirmed', $r ) ) {
+                                       $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
+                               } else {
+                                       $protected = '';
+                               }
+                               $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
                        }
                        $outText .= '</ul>';
                }
                wfProfileOut( __METHOD__  );
                return $outText;
        }
+       
+       /**
+        * Format a size in bytes for output, using an appropriate
+        * unit (B, KB, MB or GB) according to the magnitude in question
+        *
+        * @param $size Size to format
+        * @return string
+        */
+       public function formatSize( $size ) {
+               global $wgLang;
+               // For small sizes no decimal places necessary
+               $round = 0;
+               if( $size > 1024 ) {
+                       $size = $size / 1024;
+                       if( $size > 1024 ) {
+                               $size = $size / 1024;
+                               // For MB and bigger two decimal places are smarter
+                               $round = 2;
+                               if( $size > 1024 ) {
+                                       $size = $size / 1024;
+                                       $msg = 'size-gigabytes';
+                               } else {
+                                       $msg = 'size-megabytes';
+                               }
+                       } else {
+                               $msg = 'size-kilobytes';
+                       }
+               } else {
+                       $msg = 'size-bytes';
+               }
+               $size = round( $size, $round );
+               return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
+       }
+
+       /**
+        * Given the id of an interface element, constructs the appropriate title
+        * and accesskey attributes from the system messages.  (Note, this is usu-
+        * ally the id but isn't always, because sometimes the accesskey needs to
+        * go on a different element than the id, for reverse-compatibility, etc.)
+        *
+        * @param string $name Id of the element, minus prefixes.
+        * @return string title and accesskey attributes, ready to drop in an
+        *   element (e.g., ' title="This does something [x]" accesskey="x"').
+        */
+       public function tooltipAndAccesskey($name) {
+               $out = '';
+
+               $tooltip = wfMsg('tooltip-'.$name);
+               if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
+                       // Compatibility: formerly some tooltips had [alt-.] hardcoded
+                       $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
+                       $out .= ' title="'.htmlspecialchars($tooltip);
+               }
+               $accesskey = wfMsg('accesskey-'.$name);
+               if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
+                       if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
+                       else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
+               } elseif ($out) {
+                       $out .= '"';
+               }
+               return $out;
+       }
+
+       /**
+        * Given the id of an interface element, constructs the appropriate title
+        * attribute from the system messages.  (Note, this is usually the id but
+        * isn't always, because sometimes the accesskey needs to go on a different
+        * element than the id, for reverse-compatibility, etc.)
+        *
+        * @param string $name Id of the element, minus prefixes.
+        * @return string title attribute, ready to drop in an element
+        * (e.g., ' title="This does something"').
+        */
+       public function tooltip($name) {
+               $out = '';
+
+               $tooltip = wfMsg('tooltip-'.$name);
+               if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
+                       $out = ' title="'.htmlspecialchars($tooltip).'"';
+               }
+
+               return $out;
+       }
 }
+
 ?>