call JS onload hooks at the end of the html body in all skins
[lhc/web/wiklou.git] / includes / Skin.php
index a66fe2e..a99ff0a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 if ( ! defined( 'MEDIAWIKI' ) )
-       die( -1 );
+       die( 1 );
 
 /**
  *
@@ -9,26 +9,6 @@ if ( ! defined( 'MEDIAWIKI' ) )
  */
 
 # See skin.txt
-require_once( 'Linker.php' );
-require_once( 'Image.php' );
-
-# Get a list of available skins
-# Build using the regular expression '^(.*).php$'
-# Array keys are all lower case, array value keep the case used by filename
-#
-
-$skinDir = dir( $wgStyleDirectory );
-
-# while code from www.php.net
-while (false !== ($file = $skinDir->read())) {
-       // Skip non-PHP files, hidden files, and '.dep' includes
-       if(preg_match('/^([^.]*)\.php$/',$file, $matches)) {
-               $aSkin = $matches[1];
-               $wgValidSkinNames[strtolower($aSkin)] = $aSkin;
-       }
-}
-$skinDir->close();
-unset($matches);
 
 /**
  * The main skin class that provide methods and properties for all other skins.
@@ -53,8 +33,30 @@ class Skin extends Linker {
         * @return array of strings
         * @static
         */
-       function getSkinNames() {
+       static function &getSkinNames() {
                global $wgValidSkinNames;
+               static $skinsInitialised = false;
+               if ( !$skinsInitialised ) {
+                       # Get a list of available skins
+                       # Build using the regular expression '^(.*).php$'
+                       # Array keys are all lower case, array value keep the case used by filename
+                       #
+                       wfProfileIn( __METHOD__ . '-init' );
+                       global $wgStyleDirectory;
+                       $skinDir = dir( $wgStyleDirectory );
+
+                       # while code from www.php.net
+                       while (false !== ($file = $skinDir->read())) {
+                               // Skip non-PHP files, hidden files, and '.dep' includes
+                               if(preg_match('/^([^.]*)\.php$/',$file, $matches)) {
+                                       $aSkin = $matches[1];
+                                       $wgValidSkinNames[strtolower($aSkin)] = $aSkin;
+                               }
+                       }
+                       $skinDir->close();
+                       $skinsInitialised = true;
+                       wfProfileOut( __METHOD__ . '-init' );
+               }
                return $wgValidSkinNames;
        }
 
@@ -66,7 +68,7 @@ class Skin extends Linker {
         * @return string
         * @static
         */
-       function normalizeKey( $key ) {
+       static function normalizeKey( $key ) {
                global $wgDefaultSkin;
                $skinNames = Skin::getSkinNames();
 
@@ -105,20 +107,20 @@ class Skin extends Linker {
         * @return Skin
         * @static
         */
-       function &newFromKey( $key ) {
+       static function &newFromKey( $key ) {
+               global $wgStyleDirectory;
+               
                $key = Skin::normalizeKey( $key );
 
                $skinNames = Skin::getSkinNames();
                $skinName = $skinNames[$key];
 
-               global $IP;
-
                # Grab the skin class and initialise it.
                wfSuppressWarnings();
                // Preload base classes to work around APC/PHP5 bug
-               include_once( $IP.'/skins/'.$skinName.'.deps.php' );
+               include_once( "{$wgStyleDirectory}/{$skinName}.deps.php" );
                wfRestoreWarnings();
-               require_once( $IP.'/skins/'.$skinName.'.php' );
+               require_once( "{$wgStyleDirectory}/{$skinName}.php" );
 
                # Check if we got if not failback to default skin
                $className = 'Skin'.$skinName;
@@ -129,9 +131,9 @@ class Skin extends Linker {
                        # is no longer valid.
                        wfDebug( "Skin class does not exist: $className\n" );
                        $className = 'SkinStandard';
-                       require_once( $IP.'/skins/Standard.php' );
+                       require_once( "{$wgStyleDirectory}/Standard.php" );
                }
-               $skin =& new $className;
+               $skin = new $className;
                return $skin;
        }
 
@@ -253,14 +255,63 @@ class Skin extends Linker {
 
                $out->out( $this->afterContent() );
 
+               $out->out( $this->bottomScripts() );
+
                $out->out( $out->reportTime() );
 
                $out->out( "\n</body></html>" );
        }
 
+       /*static*/ function makeGlobalVariablesScript( $data ) {
+               $r = '<script type= "' . $data['jsmimetype'] . '">
+                       var skin = "' . Xml::escapeJsString( $data['skinname'] ) . '";
+                       var stylepath = "' . Xml::escapeJsString( $data['stylepath'] ) . '";
+
+                       var wgArticlePath = "' . Xml::escapeJsString( $data['articlepath'] ) . '";
+                       var wgScriptPath = "' . Xml::escapeJsString( $data['scriptpath'] ) . '";
+                       var wgServer = "' . Xml::escapeJsString( $data['serverurl'] ) . '";
+                        
+                       var wgCanonicalNamespace = "' . Xml::escapeJsString( $data['nscanonical'] ) . '";
+                       var wgPageName = "' . Xml::escapeJsString( $data['titleprefixeddbkey'] ) . '";
+                       var wgTitle = "' . Xml::escapeJsString( $data['titletext'] ) . '";
+                       var wgArticleId = ' . (int)$data['articleid'] . ';
+                        
+                       var wgUserName = ' . ( $data['username'] == NULL ? 'null' : ( '"' . Xml::escapeJsString( $data['username'] ) . '"' ) ) . ';
+                       var wgUserLanguage = "' . Xml::escapeJsString( $data['userlang'] ) . '";
+                       var wgContentLanguage = "' . Xml::escapeJsString( $data['lang'] ) . '";
+               </script>
+               ';
+               
+               return $r;
+       }
+
        function getHeadScripts() {
                global $wgStylePath, $wgUser, $wgAllowUserJs, $wgJsMimeType;
-               $r = "<script type=\"{$wgJsMimeType}\" src=\"{$wgStylePath}/common/wikibits.js\"></script>\n";
+               global $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgLang;
+               global $wgTitle, $wgCanonicalNamespaceNames;
+
+               $nsname = @$wgCanonicalNamespaceNames[ $wgTitle->getNamespace() ];
+               if ( $nsname === NULL ) $nsname = $wgTitle->getNsText();
+
+               $vars = array( 
+                       'jsmimetype' => $wgJsMimeType,
+                       'skinname' => $this->getSkinName(),
+                       'stylepath' => $wgStylePath,
+                       'articlepath' => $wgArticlePath,
+                       'scriptpath' => $wgScriptPath,
+                       'serverurl' => $wgServer,
+                       'nscanonical' => $nsname,
+                       'titleprefixeddbkey' => $wgTitle->getPrefixedDBKey(),
+                       'titletext' => $wgTitle->getText(),
+                       'articleid' => $wgTitle->getArticleId(),
+                       'username' => $wgUser->isAnon() ? NULL : $wgUser->getName(),
+                       'userlang' => $wgLang->getCode(),
+                       'lang' => $wgContLang->getCode(),
+               );
+
+               $r = Skin::makeGlobalVariablesScript( $vars );
+                
+               $r .= "<script type=\"{$wgJsMimeType}\" src=\"{$wgStylePath}/common/wikibits.js\"></script>\n";
                if( $wgAllowUserJs && $wgUser->isLoggedIn() ) {
                        $userpage = $wgUser->getUserPage();
                        $userjs = htmlspecialchars( $this->makeUrl(
@@ -297,12 +348,11 @@ class Skin extends Linker {
 
        # get the user/site-specific stylesheet, SkinTemplate loads via RawPage.php (settings are cached that way)
        function getUserStylesheet() {
-               global $wgStylePath, $wgRequest, $wgLang, $wgContLang, $wgSquidMaxage;
+               global $wgStylePath, $wgRequest, $wgContLang, $wgSquidMaxage;
                $sheet = $this->getStylesheet();
                $action = $wgRequest->getText('action');
                $s = "@import \"$wgStylePath/$sheet\";\n";
-               if($wgLang->isRTL()) $s .= "@import \"$wgStylePath/common/common_rtl.css\";\n";
-               if($wgContLang->isRTL()) $s .= "@import \"$wgStylePath/common/common_content_rtl.css\";\n";
+               if($wgContLang->isRTL()) $s .= "@import \"$wgStylePath/common/common_rtl.css\";\n";
 
                $query = "action=raw&ctype=text/css&smaxage=$wgSquidMaxage";
                $s .= '@import "' . $this->makeNSUrl( 'Common.css', $query, NS_MEDIAWIKI ) . "\";\n" .
@@ -435,7 +485,7 @@ END;
        }
 
        function doBeforeContent() {
-               global $wgLang;
+               global $wgContLang;
                $fname = 'Skin::doBeforeContent';
                wfProfileIn( $fname );
 
@@ -456,7 +506,7 @@ END;
 
                $shove = ($qb != 0);
                $left = ($qb == 1 || $qb == 3);
-               if($wgLang->isRTL()) $left = !$left;
+               if($wgContLang->isRTL()) $left = !$left;
 
                if ( !$shove ) {
                        $s .= "<td class='top' align='left' valign='top' rowspan='{$rows}'>\n" .
@@ -464,13 +514,13 @@ END;
                } elseif( $left ) {
                        $s .= $this->getQuickbarCompensator( $rows );
                }
-               $l = $wgLang->isRTL() ? 'right' : 'left';
+               $l = $wgContLang->isRTL() ? 'right' : 'left';
                $s .= "<td {$borderhack} align='$l' valign='top'>\n";
 
                $s .= $this->topLinks() ;
                $s .= "<p class='subtitle'>" . $this->pageTitleLinks() . "</p>\n";
 
-               $r = $wgLang->isRTL() ? "left" : "right";
+               $r = $wgContLang->isRTL() ? "left" : "right";
                $s .= "</td>\n<td {$borderhack} valign='top' align='$r' nowrap='nowrap'>";
                $s .= $this->nameAndLogin();
                $s .= "\n<br />" . $this->searchForm() . "</td>";
@@ -572,14 +622,23 @@ END;
        }
 
        /**
-        * This gets called immediately before the \</body\> tag.
-        * @return String HTML to be put after \</body\> ???
+        * This gets called shortly before the \</body\> tag.
+        * @return String HTML to be put before \</body\> 
         */
        function afterContent() {
                $printfooter = "<div class=\"printfooter\">\n" . $this->printFooter() . "</div>\n";
                return $printfooter . $this->doAfterContent();
        }
 
+       /**
+        * This gets called shortly before the \</body\> tag.
+        * @return String HTML-wrapped JS code to be put before \</body\> 
+        */
+       function bottomScripts() {
+               global $wgJsMimeType;
+               return "\n\t\t<script type=\"$wgJsMimeType\">if (window.runOnloadHook) runOnloadHook();</script>\n";
+       }
+
        /** @return string Retrievied from HTML text */
        function printSource() {
                global $wgTitle;
@@ -802,7 +861,7 @@ END;
                  . '<input type="text" name="search" size="19" value="'
                  . htmlspecialchars(substr($search,0,256)) . "\" />\n"
                  . '<input type="submit" name="go" value="' . wfMsg ('go') . '" />&nbsp;'
-                 . '<input type="submit" name="fulltext" value="' . wfMsg ('search') . "\" />\n</form>";
+                 . '<input type="submit" name="fulltext" value="' . wfMsg ('searchbutton') . "\" />\n</form>";
 
                return $s;
        }
@@ -997,7 +1056,7 @@ END;
                if ( '' != $align ) { $a = " align='{$align}'"; }
                else { $a = ''; }
 
-               $mp = wfMsgForContent( 'mainpage' );
+               $mp = wfMsg( 'mainpage' );
                $titleObj = Title::newFromText( $mp );
                if ( is_object( $titleObj ) ) {
                        $url = $titleObj->escapeLocalURL();
@@ -1015,7 +1074,7 @@ END;
         * @TODO crash bug913. Need to be rewrote completly.
         */
        function specialPagesList() {
-               global $wgUser, $wgContLang, $wgServer, $wgRedirectScript, $wgAvailableRights;
+               global $wgUser, $wgContLang, $wgServer, $wgRedirectScript;
                require_once('SpecialPage.php');
                $a = array();
                $pages = SpecialPage::getPages();
@@ -1026,15 +1085,9 @@ END;
                }
 
                // Other special pages that are restricted.
-               // Copied from SpecialSpecialpages.php
-               foreach($wgAvailableRights as $right) {
-                       if( $wgUser->isAllowed($right) ) {
-                               /** Add all pages for this right */
-                               if(isset($pages[$right])) {
-                                       foreach($pages[$right] as $name => $page) {
-                                       $a[$name] = $page->getDescription();
-                                       }
-                               }
+               foreach ( $pages['restricted'] as $name => $page ) {
+                       if( $wgUser->isAllowed( $page->getRestriction() ) ) {
+                               $a[$name] = $page->getDescription();
                        }
                }
 
@@ -1252,7 +1305,7 @@ END;
        }
 
        function otherLanguages() {
-               global $wgOut, $wgLang, $wgHideInterlanguageLinks;
+               global $wgOut, $wgContLang, $wgHideInterlanguageLinks;
 
                if ( $wgHideInterlanguageLinks ) {
                        return '';
@@ -1265,20 +1318,20 @@ END;
 
                $s = wfMsg( 'otherlanguages' ) . ': ';
                $first = true;
-               if($wgLang->isRTL()) $s .= '<span dir="LTR">';
+               if($wgContLang->isRTL()) $s .= '<span dir="LTR">';
                foreach( $a as $l ) {
                        if ( ! $first ) { $s .= ' | '; }
                        $first = false;
 
                        $nt = Title::newFromText( $l );
                        $url = $nt->escapeFullURL();
-                       $text = $wgLang->getLanguageName( $nt->getInterwiki() );
+                       $text = $wgContLang->getLanguageName( $nt->getInterwiki() );
 
                        if ( '' == $text ) { $text = $l; }
                        $style = $this->getExternalLinkAttributes( $l, $text );
                        $s .= "<a href=\"{$url}\"{$style}>{$text}</a>";
                }
-               if($wgLang->isRTL()) $s .= '</span>';
+               if($wgContLang->isRTL()) $s .= '</span>';
                return $s;
        }
 
@@ -1443,7 +1496,7 @@ END;
         */
        function buildSidebar() {
                global $wgDBname, $parserMemc, $wgEnableSidebarCache;
-               global $wgLanguageCode, $wgContLanguageCode;
+               global $wgLang, $wgContLang;
 
                $fname = 'SkinTemplate::buildSidebar';
 
@@ -1451,7 +1504,7 @@ END;
 
                $key = "{$wgDBname}:sidebar";
                $cacheSidebar = $wgEnableSidebarCache &&
-                       ($wgLanguageCode == $wgContLanguageCode);
+                       ($wgLang->getCode() == $wgContLang->getCode());
                
                if ($cacheSidebar) {
                        $cachedsidebar = $parserMemc->get( $key );