Category namespaces
[lhc/web/wiklou.git] / includes / Parser.php
index c8c4219..c120ad1 100644 (file)
@@ -47,8 +47,8 @@ define( "UNIQ_PREFIX", "NaodW29");
 class Parser
 {
        # Cleared with clearState():
-       var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState = array();
-       var $mVariables, $mIncludeCount, $mArgStack;
+       var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
+       var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
 
        # Temporary:
        var $mOptions, $mTitle, $mOutputType;
@@ -91,14 +91,12 @@ class Parser
                $stripState = NULL;
                $text = $this->strip( $text, $this->mStripState );
                $text = $this->internalParse( $text, $linestart );
-               # only once and next-to-last
                $text = $this->unstrip( $text, $this->mStripState );
-               $text = $this->doBlockLevels( $text, $linestart );
-               # Clean up special characters, only run once and last
+               # Clean up special characters, only run once, next-to-last before doBlockLevels
                $fixtags = array(
                        "/<hr *>/i" => '<hr/>',
                        "/<br *>/i" => '<br/>',
-                       "/<center *>/i"=>'<div style="text-align:center;">',
+                       "/<center *>/i"=>'<div class="center">',
                        "/<\\/center *>/i" => '</div>',
                        # Clean up spare ampersands; note that we probably ought to be
                        # more careful about named entities.
@@ -106,6 +104,9 @@ class Parser
                );
                $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
 
+               # only once and last
+               $text = $this->doBlockLevels( $text, $linestart );
+
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
                return $this->mOutput;
@@ -260,18 +261,15 @@ class Parser
                global $wgLang , $wgUser ;
                if ( !$this->mOptions->getUseCategoryMagic() ) return ;
                $id = $this->mTitle->getArticleID() ;
-               $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
+               $cns = Namespace::getCategory() ;
+               if ( $this->mTitle->getNamespace() != $cns ) return "" ;
                $ti = $this->mTitle->getText() ;
-               $ti = explode ( ":" , $ti , 2 ) ;
-               if ( $cat != $ti[0] ) return "" ;
-               $r = '<br style="clear:both;"/>\n';
+               $r = "<br style=\"clear:both;\"/>\n";
 
                $articles = array() ;
                $parents = array () ;
                $children = array() ;
 
-
-#              $sk =& $this->mGetSkin();
                $sk =& $wgUser->getSkin() ;
 
                $data = array () ;
@@ -284,16 +282,14 @@ class Parser
                $res = wfQuery ( $sql2, DB_READ ) ;
                while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
 
-
                foreach ( $data AS $x )
                {
                        $t = $wgLang->getNsText ( $x->cur_namespace ) ;
                        if ( $t != "" ) $t .= ":" ;
                        $t .= $x->cur_title ;
 
-                       $y = explode ( ":" , $t , 2 ) ;
-                       if ( count ( $y ) == 2 && $y[0] == $cat ) {
-                               array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
+                       if ( $x->cur_namespace == $cns ) {
+                               array_push ( $children , $sk->makeLink ( $t ) ) ;
                        } else {
                                array_push ( $articles , $sk->makeLink ( $t ) ) ;
                        }
@@ -312,7 +308,7 @@ class Parser
                if ( count ( $articles ) > 0 )
                {
                        asort ( $articles ) ;
-                       $h =  wfMsg( "category_header", $ti[1] );
+                       $h =  wfMsg( "category_header", $ti );
                        $r .= "<h2>{$h}</h2>\n" ;
                        $r .= implode ( ", " , $articles ) ;
                }
@@ -370,7 +366,7 @@ class Parser
                        $ltr = array () ; # tr attributes
                        foreach ( $t AS $k => $x )
                        {
-                               $x = rtrim ( $x ) ;
+                               $x = trim ( $x ) ;
                                $fc = substr ( $x , 0 , 1 ) ;
                                if ( "{|" == substr ( $x , 0 , 2 ) )
                                {
@@ -483,7 +479,10 @@ class Parser
                $sk =& $this->mOptions->getSkin();
                $text = $sk->transformContent( $text );
 
-               $text .= $this->categoryMagic () ;
+               if ( !isset ( $this->categoryMagicDone ) ) {
+                  $text .= $this->categoryMagic () ;
+                  $this->categoryMagicDone = true ;
+                  }
 
                wfProfileOut( $fname );
                return $text;
@@ -828,7 +827,7 @@ class Parser
                if ( !$image ) { $image = Namespace::getImage(); }
                if ( !$special ) { $special = Namespace::getSpecial(); }
                if ( !$media ) { $media = Namespace::getMedia(); }
-               if ( !$category ) { $category = wfMsg ( "category" ) ; }
+               if ( !$category ) { $category = Namespace::getCategory(); ; }
 
                $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
 
@@ -891,6 +890,14 @@ class Parser
                                $wgLinkCache->addImageLinkObj( $nt );
                                return $s;
                        }
+                       if ( $ns == $category ) {
+                               $t = $nt->getText() ;
+                               $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
+                               $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
+                               $this->mOutput->mCategoryLinks[] = $t ;
+                               $s .= $prefix . $trail ;
+                               return $s ;
+                       }
                }
                if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
                    ( strpos( $link, "#" ) == FALSE ) ) {
@@ -898,23 +905,6 @@ class Parser
                        return $s;
                }
 
-               # Category feature
-               $catns = strtoupper ( $nt->getDBkey () ) ;
-               $catns = explode ( ":" , $catns ) ;
-               if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
-               else $catns = "" ;
-               if ( $catns == strtoupper($category) && $this->mOptions->getUseCategoryMagic() ) {
-                       $t = explode ( ":" , $nt->getText() ) ;
-                       array_shift ( $t ) ;
-                       $t = implode ( ":" , $t ) ;
-                       $t = $wgLang->ucFirst ( $t ) ;
-                       $nnt = Title::newFromText ( $category.":".$t ) ;
-                       $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
-                       $this->mOutput->mCategoryLinks[] = $t ;
-                       $s .= $prefix . $trail ;
-                       return $s ;
-               }
-
                if( $ns == $media ) {
                        $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
                        $wgLinkCache->addImageLinkObj( $nt );
@@ -937,6 +927,7 @@ class Parser
                if ( '' != $this->mLastSection ) {
                        $result = "</" . $this->mLastSection  . ">\n";
                }
+               $this->mInPre = false;
                $this->mLastSection = "";
                return $result;
        }
@@ -1025,10 +1016,21 @@ class Parser
                foreach ( $a as $t ) {
                        $oLine = $t;
                        $opl = strlen( $lastPref );
-                       $npl = strspn( $t, "*#:;" );
-                       $pref = substr( $t, 0, $npl );
-                       $pref2 = str_replace( ";", ":", $pref );
-                       $t = substr( $t, $npl );
+                       $preCloseMatch = preg_match("/<\\/pre/i", $t );
+                       $preOpenMatch = preg_match("/<pre/i", $t );
+                       if (!$this->mInPre) {
+                               $this->mInPre = !empty($preOpenMatch);
+                       }
+                       if ( !$this->mInPre ) {
+                               $npl = strspn( $t, "*#:;" );
+                               $pref = substr( $t, 0, $npl );
+                               $pref2 = str_replace( ";", ":", $pref );
+                               $t = substr( $t, $npl );
+                       } else {
+                               $npl = 0;
+                               $pref = $pref2 = '';
+                       }
+
                        // list generation
                        if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
                                $text .= $this->nextItem( substr( $pref, -1 ) );
@@ -1079,6 +1081,9 @@ class Parser
                                if ( $openmatch or $closematch ) {
                                        if ( $pstack ) { $pstack = false; }
                                        $text .= $this->closeParagraph();
+                                       if($preOpenMatch and !$preCloseMatch) {
+                                               $this->mInPre = true;   
+                                       }
                                        if ( $closematch  ) {
                                                $inBlockElem = false;
                                        } else {
@@ -1415,7 +1420,7 @@ class Parser
                $fname = "Parser::removeHTMLtags";
                wfProfileIn( $fname );
                $htmlpairs = array( # Tags that must be closed
-                       "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
+                       "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
                        "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
                        "strike", "strong", "tt", "var", "div", "center",
                        "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
@@ -1989,7 +1994,7 @@ class ParserOptions
        /* static */ function newFromUser( &$user )
        {
                $popts = new ParserOptions;
-               $popts->initialiseFromUser( &$user );
+               $popts->initialiseFromUser( $user );
                return $popts;
        }