Change default on $wgSysopUserBans and $wgSysopRangeBans to true
[lhc/web/wiklou.git] / includes / Linker.php
index 7a725c4..f830819 100644 (file)
@@ -1,11 +1,13 @@
 <?php
-
 /**
  * Split off some of the internal bits from Skin.php.
  * These functions are used for primarily page content:
  * links, embedded images, table of contents. Links are
  * also used in the skin.
- *
+ * @package MediaWiki
+ */
+
+/**
  * For the moment, Skin is a descendent class of Linker.
  * In the future, it should probably be further split
  * so that ever other bit of the wiki doesn't have to
  *
  * @package MediaWiki
  */
-
 class Linker {
        var $linktrail ; # linktrail regexp
        var $postParseLinkColour = false;
 
+       /** @todo document */
        function Linker() {
                global $wgContLang;
                $this->linktrail = $wgContLang->linkTrail();
-               
-               # Cache option lookups done very frequently
-               $options = array( 'highlightbroken', 'hover' );
-               foreach( $options as $opt ) {
-                       global $wgUser;
-                       $this->mOptions[$opt] = $wgUser->getOption( $opt );
-               }
        }
        
        /**
@@ -37,6 +32,7 @@ class Linker {
                return wfSetVar( $this->postParseLinkColour, $setting );
        }
 
+       /** @todo document */
        function getExternalLinkAttributes( $link, $text, $class='' ) {
                global $wgContLang;
 
@@ -48,12 +44,11 @@ class Linker {
 
                $r = ($class != '') ? " class='$class'" : " class='external'";
 
-               if( !$same && $this->mOptions['hover'] ) {
-                       $r .= " title=\"{$link}\"";
-               }
+               $r .= " title=\"{$link}\"";
                return $r;
        }
 
+       /** @todo document */
        function getInternalLinkAttributes( $link, $text, $broken = false ) {
                $link = urldecode( $link );
                $link = str_replace( '_', ' ', $link );
@@ -67,9 +62,7 @@ class Linker {
                        $r = '';
                }
 
-               if( $this->mOptions['hover'] ) {
-                       $r .= " title=\"{$link}\"";
-               }
+               $r .= " title=\"{$link}\"";
                return $r;
        }
 
@@ -85,9 +78,7 @@ class Linker {
                        $r = '';
                }
 
-               if( $this->mOptions['hover'] ) {
-                       $r .= ' title="' . $nt->getEscapedText() . '"';
-               }
+               $r .= ' title="' . $nt->getEscapedText() . '"';
                return $r;
        }
 
@@ -109,6 +100,7 @@ class Linker {
                return $result;
        }
 
+       /** @todo document */
        function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
                $nt = Title::newFromText( $title );
                if ($nt) {
@@ -119,6 +111,7 @@ class Linker {
                }
        }
 
+       /** @todo document */
        function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
                $nt = Title::newFromText( $title );
                if ($nt) {
@@ -129,6 +122,7 @@ class Linker {
                }
        }
 
+       /** @todo document */
        function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
                $nt = Title::newFromText( $title );
                if ($nt) {
@@ -219,11 +213,15 @@ class Linker {
                                $threshold = $wgUser->getOption('stubthreshold') ;
                                if ( $threshold > 0 ) {
                                        $dbr =& wfGetDB( DB_SLAVE );
-                                       $s = $dbr->selectRow( 'cur', array( 'LENGTH(cur_text) AS x', 'cur_namespace',
-                                               'cur_is_redirect' ), array( 'cur_id' => $aid ), $fname ) ;
+                                       $s = $dbr->selectRow(
+                                               array( 'page' ),
+                                               array( 'page_len',
+                                                       'page_namespace',
+                                                       'page_is_redirect' ),
+                                               array( 'page_id' => $aid ), $fname ) ;
                                        if ( $s !== false ) {
-                                               $size = $s->x;
-                                               if ( $s->cur_is_redirect OR $s->cur_namespace != NS_MAIN ) {
+                                               $size = $s->page_len;
+                                               if ( $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) {
                                                        $size = $threshold*2 ; # Really big
                                                }
                                        } else {
@@ -322,11 +320,7 @@ class Linker {
                                $trail = $m[2];
                        }
                }
-               if ( $this->mOptions['highlightbroken'] ) {
-                       $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
-               } else {
-                       $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>?</a>{$trail}";
-               }
+               $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
 
                wfProfileOut( $fname );
                return $s;
@@ -352,14 +346,11 @@ class Linker {
                                $trail = $m[2];
                        }
                }
-               if ( $this->mOptions['highlightbroken'] ) {
-                       $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
-               } else {
-                       $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>!</a>{$trail}";
-               }
+               $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
                return $s;
        }
 
+       /** @todo document */
        function makeSelfLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                $u = $nt->escapeLocalURL( $query );
                if ( '' == $text ) {
@@ -375,6 +366,7 @@ class Linker {
                return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
        }
 
+       /** @todo document */
        function fnamePart( $url ) {
                $basename = strrchr( $url, '/' );
                if ( false === $basename ) {
@@ -385,6 +377,7 @@ class Linker {
                return htmlspecialchars( $basename );
        }
 
+       /** @todo document */
        function makeImage( $url, $alt = '' ) {
                global $wgOut;
                if ( '' == $alt ) {
@@ -394,11 +387,13 @@ class Linker {
                return $s;
        }
 
+       /** @todo document */
        function makeImageLink( $name, $url, $alt = '' ) {
                $nt = Title::makeTitleSafe( NS_IMAGE, $name );
                return $this->makeImageLinkObj( $nt, $alt );
        }
 
+       /** @todo document */
        function makeImageLinkObj( $nt, $alt = '' ) {
                global $wgContLang, $wgUseImageResize;
                $img   = Image::newFromTitle( $nt );
@@ -502,9 +497,13 @@ class Linker {
                        if ( '' == $manual_thumb ) $url = $img->createThumb( $width );
                }
 
-               $alt = preg_replace( '/<[^>]*>/', '', $alt );
-               $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
-               $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
+               # FIXME: This is a gross hack using a global.
+               # Replace link color holders in the caption text so the
+               # text portion can be placed int the alt/title attributes.
+               global $wgParser;
+               $wgParser->replaceLinkHolders( $alt );
+               
+               $alt = Sanitizer::stripAllTags( $alt );
 
                $u = $nt->escapeLocalURL();
                if ( $url == '' ) {
@@ -530,9 +529,7 @@ class Linker {
                $url  = $img->getViewURL();
 
                #$label = htmlspecialchars( $label );
-               $alt = preg_replace( '/<[^>]*>/', '', $label);
-               $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
-               $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
+               $alt = Sanitizer::stripAllTags( $label );
 
                $width = $height = 0;
                if ( $img->exists() )
@@ -610,34 +607,45 @@ class Linker {
                return str_replace("\n", ' ', $s);
        }
 
+       /** @todo document */
        function makeMediaLink( $name, $url, $alt = '' ) {
                $nt = Title::makeTitleSafe( NS_IMAGE, $name );
                return $this->makeMediaLinkObj( $nt, $alt );
        }
 
-       function makeMediaLinkObj( $nt, $alt = '', $nourl=false ) {             
-               if ( ! isset( $nt ) )
-               {
+       /**
+        * Create a direct link to a given uploaded file.
+        *
+        * @param Title  $title
+        * @param string $text   pre-sanitized HTML
+        * @param bool   $nourl  Mask absolute URLs, so the parser doesn't
+        *                       linkify them (it is currently not context-aware)
+        * @return string HTML
+        *
+        * @access public
+        * @todo Handle invalid or missing images better.
+        */
+       function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
+               if( is_null( $title ) ) {
                        ### HOTFIX. Instead of breaking, return empty string.
-                       $s = $alt;
+                       return $text;
                } else {
-                       $name = $nt->getDBKey();        
-                       $img   = Image::newFromTitle( $nt );
-                       $url = $img->getURL();
-                       # $nourl can be set by the parser
-                       # this is a hack to mask absolute URLs, so the parser doesn't
-                       # linkify them (it is currently not context-aware)
-                       # 2004-10-25
-                       if ($nourl) { $url=str_replace("http://","http-noparse://",$url) ; }
-                       if ( empty( $alt ) ) {
-                               $alt = preg_replace( '/\.(.+?)^/', '', $name );
+                       $name = $title->getDBKey();     
+                       $img  = Image::newFromTitle( $title );
+                       $url  = $img->getURL();
+                       if( $nourl ) {
+                               $url = str_replace( "http://", "http-noparse://", $url );
+                       }
+                       $alt = htmlspecialchars( $title->getText() );
+                       if( $text == '' ) {
+                               $text = $alt;
                        }
                        $u = htmlspecialchars( $url );
-                       $s = "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$alt}</a>";                   
+                       return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";                        
                }
-               return $s;
        }
 
+       /** @todo document */
        function specialLink( $name, $key = '' ) {
                global $wgContLang;
 
@@ -647,8 +655,13 @@ class Linker {
                  wfMsg( $key ) );
        }
 
-       function makeExternalLink( $url, $text, $escape = true ) {
-               $style = $this->getExternalLinkAttributes( $url, $text );
+       /** @todo document */
+       function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
+               $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
+               global $wgNoFollowLinks;
+               if( $wgNoFollowLinks ) {
+                       $style .= ' rel="nofollow"';
+               }
                $url = htmlspecialchars( $url );
                if( $escape ) {
                        $text = htmlspecialchars( $text );
@@ -675,6 +688,7 @@ class Linker {
                wfProfileIn( $fname );
                
                global $wgContLang;
+               $comment = str_replace( "\n", " ", $comment );
                $comment = htmlspecialchars( $comment );
 
                # The pattern for autogen comments is / * foo * /, which makes for
@@ -705,7 +719,7 @@ class Linker {
 
                # format regular and media links - all other wiki formatting
                # is ignored
-               $medians = $wgContLang->getNsText(Namespace::getMedia()).':';
+               $medians = $wgContLang->getNsText( NS_MEDIA ) . ':';
                while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
                        # Handle link renaming [[foo|text]] will show link as "text"
                        if( "" != $match[3] ) {
@@ -735,10 +749,30 @@ class Linker {
                return $comment;
        }
        
+       /**
+        * Wrap a comment in standard punctuation and formatting if
+        * it's non-empty, otherwise return empty string.
+        *
+        * @param string $comment
+        * @param Title $title
+        * @return string
+        * @access public
+        */
+       function commentBlock( $comment, $title = NULL ) {
+               if( $comment == '' || $comment == '*' ) {
+                       return '';
+               } else {
+                       $formatted = $this->formatComment( $comment, $title );
+                       return " <span class='comment'>($formatted)</span>";
+               }
+       }
+
+       /** @todo document */
        function tocIndent() {
                return "\n<ul>";
        }
 
+       /** @todo document */
        function tocUnindent($level) {
                return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
        }
@@ -747,14 +781,18 @@ class Linker {
         * parameter level defines if we are on an indentation level
         */
        function tocLine( $anchor, $tocline, $tocnumber, $level ) {
-               return "\n<li class='toclevel-$level'><a href=\"#" . $anchor . '"><span class="tocnumber">' . $tocnumber . '</span> <span class="toctext">' . $tocline . '</span></a>';
+               return "\n<li class='toclevel-$level'><a href=\"#" .
+                       $anchor . '"><span class="tocnumber">' .
+                       $tocnumber . '</span> <span class="toctext">' .
+                       $tocline . '</span></a>';
        }
 
-       function tocLineEnd()
-       {
+       /** @todo document */
+       function tocLineEnd() {
                return "</li>\n";
        }
 
+       /** @todo document */
        function tocList($toc) {
                return "<div id='toc'>\n" 
                           . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
@@ -770,25 +808,7 @@ class Linker {
                                 . "<div class='visualClear'></div>\n";
        }
 
-       /**
-        * These two do not check for permissions: check $wgTitle->userCanEdit
-        * before calling them
-        */
-       function editSectionScriptForOther( $title, $section, $head ) {
-               $ttl = Title::newFromText( $title );
-               $url = $ttl->escapeLocalURL( 'action=edit&section='.$section );
-               return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
-       }
-
-       function editSectionScript( $nt, $section, $head ) {
-               global $wgRequest;
-               if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
-                       return $head;
-               }
-               $url = $nt->escapeLocalURL( 'action=edit&section='.$section );
-               return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
-       }
-
+       /** @todo document */
        function editSectionLinkForOther( $title, $section ) {
                global $wgRequest;
                global $wgContLang;
@@ -808,6 +828,7 @@ class Linker {
 
        }
 
+       /** @todo document */
        function editSectionLink( $nt, $section ) {
                global $wgRequest;
                global $wgContLang;
@@ -830,16 +851,6 @@ class Linker {
                        $nearside = 'left';
                }
                return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
-
        }
-
-       /**
-        * @access public
-        */
-       function suppressUrlExpansion() {
-               return false;
-       }
-
 }
-
-?>
\ No newline at end of file
+?>