PHPTAL 1.0/PHP 5 fix: missing 'viewcount' sometimes.
[lhc/web/wiklou.git] / includes / SkinPHPTal.php
index 0a5fe47..862bbcf 100644 (file)
 if( defined( 'MEDIAWIKI' ) ) {
 
 require_once 'GlobalFunctions.php';
-global $IP;
-require_once $IP.'/PHPTAL-NP-0.7.0/libs/PHPTAL.php';
+
+if( version_compare( phpversion(), "5.0", "lt" ) ) {
+       define( 'OLD_PHPTAL', true );
+       global $IP;
+       require_once $IP.'/PHPTAL-NP-0.7.0/libs/PHPTAL.php';
+} else {
+       define( 'NEW_PHPTAL', true );
+       # For now, PHPTAL 1.0.x must be installed via PEAR in system dir.
+       require_once 'PEAR.php';
+       require_once 'PHPTAL.php';
+       require_once 'PHPTAL/Attribute.php';
+       require_once 'PHPTAL/Attribute/I18N/Attributes.php';
+}
 
 /**
  * @todo document
  * @package MediaWiki
  */
-class MediaWiki_I18N extends PHPTAL_I18N {
+//class MediaWiki_I18N extends PHPTAL_I18N {
+class MediaWiki_I18N {
        var $_context = array();
 
        function set($varName, $value) {
@@ -63,8 +75,18 @@ class MediaWiki_I18N extends PHPTAL_I18N {
                }
                return $value;
        }
-}
 
+       /* For PHPTAL 1.0: */
+       function setLanguage( $langCode ) {
+       }
+       
+       function setDomain( $domain ) {
+       }
+       
+       function setVar( $key, $value ) {
+               $this->set( $key, $value );
+       }
+}
 /**
  *
  * @package MediaWiki
@@ -102,20 +124,34 @@ class SkinPHPTal extends Skin {
                $this->template  = 'MonoBook';
        }
 
+       /**
+        * If using PHPTAL 0.7 on PHP 4.x, return a PHPTAL template object.
+        * If using PHPTAL 1.0 on PHP 5.x, return a bridge object.
+        * @return object
+        * @access private
+        */
+       function &setupTemplate( $file, $repository=false, $cache_dir=false ) {
+               if( defined( 'NEW_PHPTAL' ) && NEW_PHPTAL ) {
+                       return new PHPTAL_version_bridge( $file, $repository, $cache_dir );
+               } else {
+                       return new PHPTAL( $file, $repository, $cache_dir );
+               }
+       }
+       
        /**
         * initialize various variables and generate the template
         */
        function outputPage( &$out ) {
-               global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgOut;
-               global $wgScript, $wgStylePath, $wgLanguageCode, $wgUseNewInterlanguage;
+               global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgContLang, $wgOut;
+               global $wgScript, $wgStylePath, $wgLanguageCode, $wgContLanguageCode, $wgUseNewInterlanguage;
                global $wgMimeType, $wgOutputEncoding, $wgUseDatabaseMessages, $wgRequest;
                global $wgDisableCounters, $wgLogo, $action, $wgFeedClasses, $wgSiteNotice;
-                       global $wgMaxCredits, $wgShowCreditsIfMax;
+               global $wgMaxCredits, $wgShowCreditsIfMax;
 
                extract( $wgRequest->getValues( 'oldid', 'diff' ) );
 
                $this->initPage( $out );
-               $tpl = new PHPTAL($this->template . '.pt', 'skins');
+               $tpl =& $this->setupTemplate( $this->template . '.pt', 'skins' );
 
                #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
                $tpl->setTranslator(new MediaWiki_I18N());
@@ -127,7 +163,7 @@ class SkinPHPTal extends Skin {
                $this->iscontent = ($wgTitle->getNamespace() != Namespace::getSpecial() );
                $this->iseditable = ($this->iscontent and !($action == 'edit' or $action == 'submit'));
                $this->username = $wgUser->getName();
-               $this->userpage = $wgLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
+               $this->userpage = $wgContLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
                $this->userpageUrlDetails = $this->makeUrlDetails($this->userpage);
 
                $this->usercss =  $this->userjs = $this->userjsprev = false;
@@ -163,6 +199,8 @@ class SkinPHPTal extends Skin {
                                );
                        }
                        $tpl->setRef( 'feeds', $feeds );
+               } else {
+                       $tpl->set( 'feeds', false );
                }
                $tpl->setRef( 'mimetype', $wgMimeType );
                $tpl->setRef( 'charset', $wgOutputEncoding );
@@ -172,6 +210,7 @@ class SkinPHPTal extends Skin {
                $tpl->setRef( 'stylename', $this->stylename );
                $tpl->setRef( 'loggedin', $this->loggedin );
                $tpl->set('nsclass', 'ns-'.$wgTitle->getNamespace());
+               $tpl->set('notspecialpage', $wgTitle->getNamespace() != NS_SPECIAL);
                /* XXX currently unused, might get useful later
                $tpl->set( "editable", ($wgTitle->getNamespace() != NS_SPECIAL ) );
                $tpl->set( "exists", $wgTitle->getArticleID() != 0 );
@@ -183,10 +222,10 @@ class SkinPHPTal extends Skin {
                $tpl->set( 'searchaction', $this->escapeSearchLink() );
                $tpl->setRef( 'stylepath', $wgStylePath );
                $tpl->setRef( 'logopath', $wgLogo );
-               $tpl->setRef( "lang", $wgLanguageCode );
-               $tpl->set( 'dir', $wgLang->isRTL() ? "rtl" : "ltr" );
-               $tpl->set( 'rtl', $wgLang->isRTL() );
-               $tpl->set( 'langname', $wgLang->getLanguageName( $wgLanguageCode ) );
+               $tpl->setRef( "lang", $wgContLanguageCode );
+               $tpl->set( 'dir', $wgContLang->isRTL() ? "rtl" : "ltr" );
+               $tpl->set( 'rtl', $wgContLang->isRTL() );
+               $tpl->set( 'langname', $wgContLang->getLanguageName( $wgContLanguageCode ) );
                $tpl->setRef( 'username', $this->username );
                $tpl->setRef( 'userpage', $this->userpage);
                $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href']);
@@ -205,7 +244,7 @@ class SkinPHPTal extends Skin {
 
                                $ntl = wfMsg( 'newmessages',
                                $this->makeKnownLink(
-                                       $wgLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
+                                       $wgContLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
                                        . ':' . $this->username,
                                        wfMsg('newmessageslink') )
                                );
@@ -224,22 +263,32 @@ class SkinPHPTal extends Skin {
                                $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
                                if ( $viewcount ) {
                                        $tpl->set('viewcount', wfMsg( "viewcount", $viewcount ));
+                               } else {
+                                       $tpl->set('viewcount', false);
                                }
                        }
                        $tpl->set('lastmod', $this->lastModified());
-                               $tpl->set('copyright',$this->getCopyright());
+                       $tpl->set('copyright',$this->getCopyright());
 
-                               $this->credits = false;
+                       $this->credits = false;
 
-                               if (isset($wgMaxCredits) && $wgMaxCredits != 0) {
-                                       require_once("Credits.php");
-                                       $this->credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
-                               }
+                       if (isset($wgMaxCredits) && $wgMaxCredits != 0) {
+                               require_once("Credits.php");
+                               $this->credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
+                       }
 
-                                       $tpl->setRef( 'credits', $this->credits );
+                       $tpl->setRef( 'credits', $this->credits );
 
                } elseif ( isset( $oldid ) && !isset( $diff ) ) {
                        $tpl->set('copyright', $this->getCopyright());
+                       $tpl->set('viewcount', false);
+                       $tpl->set('lastmod', false);
+                       $tpl->set('credits', false);
+               } else {
+                       $tpl->set('copyright', false);
+                       $tpl->set('viewcount', false);
+                       $tpl->set('lastmod', false);
+                       $tpl->set('credits', false);
                }
 
                $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
@@ -255,28 +304,32 @@ class SkinPHPTal extends Skin {
                $out->mBodytext .= $printfooter ;
                $tpl->setRef( 'bodytext', $out->mBodytext );
 
+               # Language links
                $language_urls = array();
                foreach( $wgOut->getLanguageLinks() as $l ) {
                        $nt = Title::newFromText( $l );
                        $language_urls[] = array('href' => $nt->getFullURL(),
-                       'text' => ($wgLang->getLanguageName( $nt->getInterwiki()) != ''?$wgLang->getLanguageName( $nt->getInterwiki()) : $l),
-                       'class' => $wgLang->isRTL() ? 'rtl' : 'ltr');
+                       'text' => ($wgContLang->getLanguageName( $nt->getInterwiki()) != ''?$wgContLang->getLanguageName( $nt->getInterwiki()) : $l),
+                       'class' => $wgContLang->isRTL() ? 'rtl' : 'ltr');
                }
                if(count($language_urls)) {
                        $tpl->setRef( 'language_urls', $language_urls);
                } else {
                        $tpl->set('language_urls', false);
                }
+
+               # Personal toolbar
                $tpl->set('personal_urls', $this->buildPersonalUrls());
                $content_actions = $this->buildContentActionUrls();
                $tpl->setRef('content_actions', $content_actions);
                // XXX: attach this from javascript, same with section editing
                if($this->iseditable && $wgUser->getOption("editondblclick") )
                {
-                       $tpl->set('body-ondblclick', 'document.location = "' .$content_actions['edit']['href'] .'";');
+                       $tpl->set('body_ondblclick', 'document.location = "' .$content_actions['edit']['href'] .'";');
                } else {
-                       $tpl->set('body-ondblclick', false);
+                       $tpl->set('body_ondblclick', false);
                }
+               $tpl->set( 'navigation_urls', $this->buildNavigationUrls() );
                $tpl->set( 'nav_urls', $this->buildNavUrls() );
 
                // execute template
@@ -511,6 +564,22 @@ class SkinPHPTal extends Skin {
                return $content_actions;
        }
 
+       /**
+        * build array of global navigation links
+        */ 
+       function buildNavigationUrls () {
+               global $wgNavigationLinks;
+               $result = array();
+               foreach ( $wgNavigationLinks as $link ) {
+                       $result[] = array(
+                               'text' => wfMsg( $link['text'] ),
+                               'href' => $this->makeInternalOrExternalUrl( wfMsgForContent( $link['href'] ) ),
+                               'id' => 'n-'.$link['text']
+                       );
+               }
+               return $result;
+       }
+
        /**
         * build array of common navigation links
         */
@@ -523,21 +592,26 @@ class SkinPHPTal extends Skin {
                $diff = $wgRequest->getVal( 'diff' );
                // XXX: remove htmlspecialchars when tal:attributes works with i18n:attributes
                $nav_urls = array();
-               $nav_urls['mainpage'] = array('href' => htmlspecialchars( $this->makeI18nUrl('mainpage')));
-               $nav_urls['randompage'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Randompage')));
-               $nav_urls['recentchanges'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchanges')));
-               $nav_urls['whatlinkshere'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Whatlinkshere', 'target='.urlencode( $this->thispage ))));
-               $nav_urls['currentevents'] = (wfMsg('currentevents') != '-') ? array('href' => htmlspecialchars( $this->makeI18nUrl('currentevents'))) : false;
-               $nav_urls['portal'] = (wfMsg('portal') != '-') ? array('href' => htmlspecialchars( $this->makeI18nUrl('portal-url'))) : false;
-               $nav_urls['recentchangeslinked'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchangeslinked', 'target='.urlencode( $this->thispage ))));
-               $nav_urls['bugreports'] = array('href' => htmlspecialchars( $this->makeI18nUrl('bugreportspage')));
-               // $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $this->makeI18nUrl('sitesupportpage')));
-               $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $wgSiteSupportPage));
-               $nav_urls['help'] = array('href' => htmlspecialchars( $this->makeI18nUrl('helppage')));
+               $nav_urls['mainpage'] = array('href' => $this->makeI18nUrl('mainpage'));
+               $nav_urls['randompage'] = array('href' => $this->makeSpecialUrl('Randompage'));
+               $nav_urls['recentchanges'] = array('href' => $this->makeSpecialUrl('Recentchanges'));
+               $nav_urls['currentevents'] = (wfMsg('currentevents') != '-') ? array('href' => $this->makeI18nUrl('currentevents')) : false;
+               $nav_urls['portal'] = (wfMsg('portal') != '-') ? array('href' => $this->makeI18nUrl('portal-url')) : false;
+               $nav_urls['bugreports'] = array('href' => $this->makeI18nUrl('bugreportspage'));
+               // $nav_urls['sitesupport'] = array('href' => $this->makeI18nUrl('sitesupportpage'));
+               $nav_urls['sitesupport'] = array('href' => $wgSiteSupportPage);
+               $nav_urls['help'] = array('href' => $this->makeI18nUrl('helppage'));
                if( $this->loggedin && !$wgDisableUploads ) {
-                       $nav_urls['upload'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Upload')));
+                       $nav_urls['upload'] = array('href' => $this->makeSpecialUrl('Upload'));
+               } else {
+                       $nav_urls['upload'] = false;
+               }
+               $nav_urls['specialpages'] = array('href' => $this->makeSpecialUrl('Specialpages'));
+
+               if( $wgTitle->getNamespace() != NS_SPECIAL) {
+               $nav_urls['whatlinkshere'] = array('href' => $this->makeSpecialUrl('Whatlinkshere', 'target='.urlencode( $this->thispage)));
+               $nav_urls['recentchangeslinked'] = array('href' => $this->makeSpecialUrl('Recentchangeslinked', 'target='.urlencode( $this->thispage)));
                }
-               $nav_urls['specialpages'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Specialpages')));
 
                if( $wgTitle->getNamespace() == NS_USER || $wgTitle->getNamespace() == NS_USER_TALK ) {
                        $id = User::idFromName($wgTitle->getText());
@@ -549,18 +623,20 @@ class SkinPHPTal extends Skin {
 
                if($id || $ip) { # both anons and non-anons have contri list
                        $nav_urls['contributions'] = array(
-                               'href' => htmlspecialchars( $this->makeSpecialUrl('Contributions', "target=" . $wgTitle->getPartialURL() ) )
+                               'href' => $this->makeSpecialUrl('Contributions', "target=" . $wgTitle->getPartialURL() )
                        );
+               } else {
+                       $nav_urls['contributions'] = false;
                }
+               $nav_urls['emailuser'] = false;
                if ( 0 != $wgUser->getID() ) { # show only to signed in users
                        if($id) {       # can only email non-anons
                                $nav_urls['emailuser'] = array(
-                                       'href' => htmlspecialchars( $this->makeSpecialUrl('Emailuser', "target=" . $wgTitle->getPartialURL() ) )
+                                       'href' => $this->makeSpecialUrl('Emailuser', "target=" . $wgTitle->getPartialURL() )
                                );
                        }
                }
 
-
                return $nav_urls;
        }
 
@@ -633,17 +709,21 @@ class SkinPHPTal extends Skin {
                                $this->userjs = $this->makeUrl($this->userpage.'/'.$this->skinname.'.js', 'action=raw&ctype=text/javascript&dontcountme=s');
                        }
                }
+               $this->usercss = '/*<![CDATA[*/ ' . $this->usercss . ' /*]]>*/';
+               if( $this->userjsprev ) {
+                       $this->userjsprev = '/*<![CDATA[*/ ' . $this->userjsprev . ' /*]]>*/';
+               }
        }
        
        /**
         * returns css with user-specific options
         */
        function getUserStylesheet() {
-               global $wgUser, $wgRequest, $wgTitle, $wgLang, $wgSquidMaxage, $wgStylePath;
+               global $wgUser, $wgRequest, $wgTitle, $wgContLang, $wgSquidMaxage, $wgStylePath;
                $action = $wgRequest->getText('action');
                $maxage = $wgRequest->getText('maxage');
                $s = "/* generated user stylesheet */\n";
-               if($wgLang->isRTL()) $s .= '@import "'.$wgStylePath.'/'.$this->stylename.'/rtl.css";'."\n";
+               if($wgContLang->isRTL()) $s .= '@import "'.$wgStylePath.'/'.$this->stylename.'/rtl.css";'."\n";
                $s .= '@import "'.
                $this->makeNSUrl(ucfirst($this->skinname).'.css', 'action=raw&ctype=text/css&smaxage='.$wgSquidMaxage, NS_MEDIAWIKI)."\";\n";
                if($wgUser->getID() != 0) {
@@ -675,5 +755,41 @@ class SkinPHPTal extends Skin {
        }
 }
 
+class PHPTAL_version_bridge {
+       function PHPTAL_version_bridge( $file, $repository=false, $cache_dir=false ) {
+               $this->tpl =& new PHPTAL( $file );
+               if( $repository ) {
+                       $this->tpl->setTemplateRepository( $repository );
+               }
+       }
+       
+       function set( $name, $value ) {
+               $this->tpl->$name = $value;
+       }
+       
+       function setRef($name, &$value) {
+               $this->set( $name, $value );
+       }
+       
+       function setTranslator( &$t ) {
+               $this->tpl->setTranslator( $t );
+       }
+       
+       function execute() {
+               /*
+               try {
+               */
+                       return $this->tpl->execute();
+               /*
+               }
+               catch (Exception $e) {
+                       echo "<div class='error' style='background: white; white-space: pre; position: absolute; z-index: 9999; border: solid 2px black; padding: 4px;'>We caught an exception...\n ";
+                       echo $e;
+                       echo "</div>";
+               }
+               */
+       }
+}
+
 } // end of if( defined( 'MEDIAWIKI' ) ) 
 ?>