Add failing test for bug 14404.
[lhc/web/wiklou.git] / includes / Skin.php
index a461b7d..fe57e5f 100644 (file)
@@ -41,7 +41,7 @@ class Skin extends Linker {
                global $wgValidSkinNames;
                static $skinsInitialised = false;
 
-               if ( !$skinsInitialised ) {
+               if ( !$skinsInitialised || !count( $wgValidSkinNames ) ) {
                        # 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
@@ -142,7 +142,7 @@ class Skin extends Linker {
 
                $skinNames = Skin::getSkinNames();
                $skinName = $skinNames[$key];
-               $className = 'Skin' . ucfirst( $key );
+               $className = "Skin{$skinName}";
 
                # Grab the skin class and initialise it.
                if ( !class_exists( $className ) ) {
@@ -192,7 +192,7 @@ class Skin extends Linker {
        }
 
        function initPage( OutputPage $out ) {
-               global $wgFavicon, $wgAppleTouchIcon;
+               global $wgFavicon, $wgAppleTouchIcon, $wgEnableAPI;
 
                wfProfileIn( __METHOD__ );
 
@@ -216,6 +216,18 @@ class Skin extends Linker {
                        'title' => wfMsgForContent( 'opensearch-desc' ),
                ) );
 
+               if ( $wgEnableAPI ) {
+                       # Real Simple Discovery link, provides auto-discovery information
+                       # for the MediaWiki API (and potentially additional custom API
+                       # support such as WordPress or Twitter-compatible APIs for a
+                       # blogging extension, etc)
+                       $out->addLink( array(
+                               'rel' => 'EditURI',
+                               'type' => 'application/rsd+xml',
+                               'href' => wfExpandUrl( wfAppendQuery( wfScript( 'api' ), array( 'action' => 'rsd' ) ) ),
+                       ) );
+               }
+
                $this->addMetadataLinks( $out );
 
                $this->mRevisionId = $out->mRevisionId;
@@ -244,16 +256,28 @@ class Skin extends Linker {
                }
 
                $lb = new LinkBatch( $titles );
+               $lb->setCaller( __METHOD__ );
                $lb->execute();
        }
 
        /**
-        * Adds metadata links (Creative Commons/Dublin Core/copyright) to the HTML
-        * output.
+        * Adds metadata links below to the HTML output.
+        * <ol>
+        *  <li>Creative Commons
+        *   <br />See http://wiki.creativecommons.org/Extend_Metadata.
+        *  </li>
+        *  <li>Dublin Core</li>
+        *  <li>Use hreflang to specify canonical and alternate links
+        *   <br />See http://www.google.com/support/webmasters/bin/answer.py?answer=189077
+        *  </li>
+        *  <li>Copyright</li>
+        * <ol>
+        * 
         * @param $out Object: instance of OutputPage
         */
        function addMetadataLinks( OutputPage $out ) {
                global $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
+               global $wgDisableLangConversion, $wgCanonicalLanguageLinks, $wgContLang;
                global $wgRightsPage, $wgRightsUrl;
 
                if ( $out->isArticleRelated() ) {
@@ -274,6 +298,29 @@ class Skin extends Linker {
                                );
                        }
                }
+
+               if ( !$wgDisableLangConversion && $wgCanonicalLanguageLinks
+                       && $wgContLang->hasVariants() ) {
+
+                       $urlvar = $wgContLang->getURLVariant();
+
+                       if ( !$urlvar ) {
+                               $variants = $wgContLang->getVariants();
+                               foreach ( $variants as $_v ) {
+                                       $out->addLink( array(
+                                               'rel' => 'alternate',
+                                               'hreflang' => $_v,
+                                               'href' => $this->mTitle->getLocalURL( '', $_v ) )
+                                       );
+                               }
+                       } else {
+                               $out->addLink( array(
+                                       'rel' => 'canonical',
+                                       'href' => $this->mTitle->getFullURL() )
+                               );
+                       }
+               }
+               
                $copyright = '';
                if ( $wgRightsPage ) {
                        $copy = Title::newFromText( $wgRightsPage );
@@ -357,10 +404,12 @@ class Skin extends Linker {
 
        static function makeVariablesScript( $data ) {
                if ( $data ) {
-                       return Html::inlineScript( 'mediaWiki.config.set(' . json_encode( $data ) . ');' );
+                       return Html::inlineScript(
+                               ResourceLoader::makeLoaderConditionalScript( ResourceLoader::makeConfigSetScript( $data ) )
+                       );
                } else {
                        return '';
-               }
+               } 
        }
 
        /**
@@ -368,50 +417,16 @@ class Skin extends Linker {
         * @param $skinName string Name of the skin
         * The odd calling convention is for backwards compatibility
         * @todo FIXME: Make this not depend on $wgTitle!
+        * 
+        * Do not add things here which can be evaluated in ResourceLoaderStartupScript - in other words, without state.
+        * You will only be adding bloat to the page and causing page caches to have to be purged on configuration changes.
         */
        static function makeGlobalVariablesScript( $skinName ) {
-               if ( is_array( $skinName ) ) {
-                       # Weird back-compat stuff.
-                       $skinName = $skinName['skinname'];
-               }
-
-               global $wgScript, $wgTitle, $wgStylePath, $wgUser, $wgScriptExtension;
-               global $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgLang;
-               global $wgOut, $wgArticle;
-               global $wgBreakFrames, $wgRequest, $wgVariantArticlePath, $wgActionPaths;
-               global $wgUseAjax, $wgAjaxWatch;
-               global $wgVersion, $wgEnableAPI, $wgEnableWriteAPI;
-               global $wgRestrictionTypes;
-               global $wgDBname, $wgEnableMWSuggest;
-               global $wgSitename;
-
+               global $wgTitle, $wgUser, $wgRequest, $wgArticle, $wgOut, $wgRestrictionTypes, $wgUseAjax, $wgEnableMWSuggest;
+               
                $ns = $wgTitle->getNamespace();
                $nsname = MWNamespace::exists( $ns ) ? MWNamespace::getCanonicalName( $ns ) : $wgTitle->getNsText();
-               $separatorTransTable = $wgContLang->separatorTransformTable();
-               $separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
-               $compactSeparatorTransTable = array(
-                       implode( "\t", array_keys( $separatorTransTable ) ),
-                       implode( "\t", $separatorTransTable ),
-               );
-               $digitTransTable = $wgContLang->digitTransformTable();
-               $digitTransTable = $digitTransTable ? $digitTransTable : array();
-               $compactDigitTransTable = array(
-                       implode( "\t", array_keys( $digitTransTable ) ),
-                       implode( "\t", $digitTransTable ),
-               );
-
-               $mainPage = Title::newMainPage();
                $vars = array(
-                       'skin' => $skinName,
-                       'stylepath' => $wgStylePath,
-                       'wgUrlProtocols' => wfUrlProtocols(),
-                       'wgArticlePath' => $wgArticlePath,
-                       'wgScriptPath' => $wgScriptPath,
-                       'wgScriptExtension' => $wgScriptExtension,
-                       'wgScript' => $wgScript,
-                       'wgVariantArticlePath' => $wgVariantArticlePath,
-                       'wgActionPaths' => (object)$wgActionPaths,
-                       'wgServer' => $wgServer,
                        'wgCanonicalNamespace' => $nsname,
                        'wgCanonicalSpecialPageName' => $ns == NS_SPECIAL ?
                                SpecialPage::resolveAlias( $wgTitle->getDBkey() ) : false, # bug 21115
@@ -423,56 +438,19 @@ class Skin extends Linker {
                        'wgIsArticle' => $wgOut->isArticle(),
                        'wgUserName' => $wgUser->isAnon() ? null : $wgUser->getName(),
                        'wgUserGroups' => $wgUser->getEffectiveGroups(),
-                       'wgUserLanguage' => $wgLang->getCode(),
-                       'wgContentLanguage' => $wgContLang->getCode(),
-                       'wgBreakFrames' => $wgBreakFrames,
                        'wgCurRevisionId' => isset( $wgArticle ) ? $wgArticle->getLatest() : 0,
-                       'wgVersion' => $wgVersion,
-                       'wgEnableAPI' => $wgEnableAPI,
-                       'wgEnableWriteAPI' => $wgEnableWriteAPI,
-                       'wgSeparatorTransformTable' => $compactSeparatorTransTable,
-                       'wgDigitTransformTable' => $compactDigitTransTable,
-                       'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
-                       'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
-                       'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
-                       'wgSiteName' => $wgSitename,
                        'wgCategories' => $wgOut->getCategories(),
                );
-
-               if ( $wgContLang->hasVariants() ) {
-                       $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
-               }
-
-               // if on upload page output the extension list & js_upload
-               if ( SpecialPage::resolveAlias( $wgTitle->getDBkey() ) == 'Upload' ) {
-                       global $wgFileExtensions;
-                       $vars['wgFileExtensions'] = $wgFileExtensions;
-               }
-
-               if ( $wgUseAjax && $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
-                       $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
-                       $vars['wgDBname'] = $wgDBname;
-                       $vars['wgSearchNamespaces'] = SearchEngine::userNamespaces( $wgUser );
-                       $vars['wgMWSuggestMessages'] = array( wfMsg( 'search-mwsuggest-enabled' ), wfMsg( 'search-mwsuggest-disabled' ) );
-               }
-
                foreach ( $wgRestrictionTypes as $type ) {
                        $vars['wgRestriction' . ucfirst( $type )] = $wgTitle->getRestrictions( $type );
                }
-
-               if ( $wgOut->isArticleRelated() && $wgUseAjax && $wgAjaxWatch && $wgUser->isLoggedIn() ) {
-                       $msgs = (object)array();
-
-                       foreach ( array( 'watch', 'unwatch', 'watching', 'unwatching',
-                               'tooltip-ca-watch', 'tooltip-ca-unwatch' ) as $msgName ) {
-                               $msgs-> { $msgName . 'Msg' } = wfMsg( $msgName );
-                       }
-                       $vars['wgAjaxWatch'] = $msgs;
+               if ( $wgUseAjax && $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
+                       $vars['wgSearchNamespaces'] = SearchEngine::userNamespaces( $wgUser );
                }
-
+               
                // Allow extensions to add their custom variables to the global JS variables
                wfRunHooks( 'MakeGlobalVariablesScript', array( &$vars ) );
-
+               
                return self::makeVariablesScript( $vars );
        }
 
@@ -521,51 +499,20 @@ class Skin extends Linker {
         * @return string
         */
        public function generateUserJs( $skinName = null ) {
-               global $wgStylePath;
-
-               wfProfileIn( __METHOD__ );
-
-               if ( !$skinName ) {
-                       $skinName = $this->getSkinName();
-               }
-
-               $s = "/* generated javascript */\n";
-               $s .= "var skin = '" . Xml::escapeJsString( $skinName ) . "';\n";
-               $s .= "var stylepath = '" . Xml::escapeJsString( $wgStylePath ) . "';";
-               $s .= "\n\n/* MediaWiki:Common.js */\n";
-
-               $commonJs = wfMsgExt( 'common.js', 'content' );
-
-               if ( !wfEmptyMsg( 'common.js', $commonJs ) ) {
-                       $s .= $commonJs;
-               }
-
-               $s .= "\n\n/* MediaWiki:" . ucfirst( $skinName ) . ".js */\n";
-
-               // avoid inclusion of non defined user JavaScript (with custom skins only)
-               // by checking for default message content
-               $msgKey = ucfirst( $skinName ) . '.js';
-               $userJS = wfMsgExt( $msgKey, 'content' );
-
-               if ( !wfEmptyMsg( $msgKey, $userJS ) ) {
-                       $s .= $userJS;
-               }
-
-               wfProfileOut( __METHOD__ );
-               return $s;
+               
+               // Stub - see ResourceLoaderSiteModule, CologneBlue, Simple and Standard skins override this
+               
+               return '';
        }
 
        /**
         * Generate user stylesheet for action=raw&gen=css
         */
        public function generateUserStylesheet() {
-               wfProfileIn( __METHOD__ );
-
-               $s = "/* generated user stylesheet */\n" .
-                       $this->reallyGenerateUserStylesheet();
-
-               wfProfileOut( __METHOD__ );
-               return $s;
+               
+               // Stub - see ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
+               
+               return '';
        }
 
        /**
@@ -573,121 +520,47 @@ class Skin extends Linker {
         * Anything in here won't be generated if $wgAllowUserCssPrefs is false.
         */
        protected function reallyGenerateUserStylesheet() {
-               global $wgUser;
-
-               $s = '';
-
-               if ( ( $undopt = $wgUser->getOption( 'underline' ) ) < 2 ) {
-                       $underline = $undopt ? 'underline' : 'none';
-                       $s .= "a { text-decoration: $underline; }\n";
-               }
-
-               if ( $wgUser->getOption( 'highlightbroken' ) ) {
-                       $s .= "a.new, #quickbar a.new { color: #CC2200; }\n";
-               } else {
-                       $s .= <<<CSS
-a.new, #quickbar a.new,
-a.stub, #quickbar a.stub {
-       color: inherit;
-}
-a.new:after, #quickbar a.new:after {
-       content: "?";
-       color: #CC2200;
-}
-a.stub:after, #quickbar a.stub:after {
-       content: "!";
-       color: #772233;
-}
-CSS;
-               }
-
-               if ( $wgUser->getOption( 'justify' ) ) {
-                       $s .= "#article, #bodyContent, #mw_content { text-align: justify; }\n";
-               }
-
-               if ( !$wgUser->getOption( 'showtoc' ) ) {
-                       $s .= "#toc { display: none; }\n";
-               }
-
-               if ( !$wgUser->getOption( 'editsection' ) ) {
-                       $s .= ".editsection { display: none; }\n";
-               }
-
-               $fontstyle = $wgUser->getOption( 'editfont' );
-
-               if ( $fontstyle !== 'default' ) {
-                       $s .= "textarea { font-family: $fontstyle; }\n";
-               }
-
-               return $s;
+               
+               // Stub - see  ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
+               
+               return '';
        }
 
        /**
         * @private
         */
        function setupUserCss( OutputPage $out ) {
-               global $wgRequest, $wgUser;
-               global $wgAllowUserCss, $wgUseSiteCss, $wgSquidMaxage;
+               global $wgRequest;
+               global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs;
 
                wfProfileIn( __METHOD__ );
 
                $this->setupSkinUserCss( $out );
-
-               $siteargs = array(
-                       'action' => 'raw',
-                       'maxage' => $wgSquidMaxage,
-               );
-
                // Add any extension CSS
                foreach ( $out->getExtStyle() as $url ) {
                        $out->addStyle( $url );
                }
 
-               // If we use the site's dynamic CSS, throw that in, too
                // Per-site custom styles
                if ( $wgUseSiteCss ) {
                        $out->addModuleStyles( 'site' );
                }
 
-               global $wgAllowUserCssPrefs;
-
-               if ( $wgAllowUserCssPrefs ) {
-                       if ( $wgUser->isLoggedIn() ) {
-                               // Ensure that logged-in users' generated CSS isn't clobbered
-                               // by anons' publicly cacheable generated CSS.
-                               $siteargs['smaxage'] = '0';
-                               $siteargs['ts'] = $wgUser->mTouched;
-                       }
-
-                       // Per-user styles based on preferences
-                       $siteargs['gen'] = 'css';
-
-                       if ( ( $us = $wgRequest->getVal( 'useskin', '' ) ) !== '' ) {
-                               $siteargs['useskin'] = $us;
-                       }
-
-                       $out->addStyle( self::makeUrl( '-', wfArrayToCGI( $siteargs ) ) );
-               }
-
-               // Per-user custom style pages
-               if ( $wgAllowUserCss && $wgUser->isLoggedIn() ) {
-                       $action = $wgRequest->getVal( 'action' );
-
-                       # If we're previewing the CSS page, use it
-                       if ( $this->mTitle->isCssSubpage() && $this->userCanPreview( $action ) ) {
+               // Per-user custom styles
+               if ( $wgAllowUserCss ) {
+                       if ( $this->mTitle->isCssSubpage() && $this->userCanPreview( $wgRequest->getVal( 'action' ) ) ) {
                                // @FIXME: properly escape the cdata!
                                $out->addInlineStyle( $wgRequest->getText( 'wpTextbox1' ) );
                        } else {
-                               $names = array( 'common', $this->getSkinName() );
-                               foreach ( $names as $name ) {
-                                       $out->addStyle( self::makeUrl(
-                                               $this->userpage . '/' . $name . '.css',
-                                               'action=raw&ctype=text/css' )
-                                       );
-                               }
+                               $out->addModuleStyles( 'user' );
                        }
                }
 
+               // Per-user preference styles
+               if ( $wgAllowUserCssPrefs ) {
+                       $out->addModuleStyles( 'user.options' );
+               }
+
                wfProfileOut( __METHOD__ );
        }
 
@@ -736,6 +609,15 @@ CSS;
                return "$numeric $type $name";
        }
 
+       /**
+        * This will be called by OutputPage::headElement when it is creating the
+        * <body> tag, skins can override it if they have a need to add in any
+        * body attributes or classes of their own.
+        */
+       function addToBodyAttributes( $out, &$bodyAttrs ) {
+               // does nothing by default
+       }
+
        /**
         * URL to the logo
         */
@@ -994,7 +876,6 @@ CSS;
                $ret = '<li>';
 
                foreach ( $lines as $line ) {
-                       $m = array();
                        $display = ltrim( $line );
                        $ident = strlen( $line ) - strlen( $display );
                        $diff = $ident - $curIdent;
@@ -1225,7 +1106,7 @@ CSS;
        function subPageSubtitle() {
                $subpages = '';
 
-               if ( !wfRunHooks( 'SkinSubPageSubtitle', array( &$subpages ) ) ) {
+               if ( !wfRunHooks( 'SkinSubPageSubtitle', array( &$subpages, $this ) ) ) {
                        return $subpages;
                }
 
@@ -1329,7 +1210,7 @@ CSS;
                                        SpecialPage::getTitleFor( 'Userlogout' ), wfMsg( 'logout' ),
                                        array(), array( 'returnto' => $returnTo )
                                ),
-                               $this->specialLink( 'preferences' ),
+                               $this->specialLink( 'Preferences' ),
                        ) );
                }
 
@@ -1383,7 +1264,7 @@ CSS;
 
                $s = array(
                        $this->mainPageLink(),
-                       $this->specialLink( 'recentchanges' )
+                       $this->specialLink( 'Recentchanges' )
                );
 
                if ( $wgOut->isArticleRelated() ) {
@@ -1669,9 +1550,9 @@ CSS;
                global $wgStylePath;
 
                $url = htmlspecialchars( "$wgStylePath/common/images/poweredby_mediawiki_88x31.png" );
-               $img = '<a href="http://www.mediawiki.org/"><img src="' . $url . '" height="31" width="88" alt="Powered by MediaWiki" /></a>';
-
-               return $img;
+               $text = '<a href="http://www.mediawiki.org/"><img src="' . $url . '" height="31" width="88" alt="Powered by MediaWiki" /></a>';
+               wfRunHooks( 'SkinGetPoweredBy', array( &$text, $this ) );       
+               return $text;
        }
 
        function lastModified() {
@@ -1749,6 +1630,29 @@ CSS;
                return $s;
        }
 
+       /**
+        * Renders a $wgFooterIcons icon acording to the method's arguments
+        * @param $icon Array: The icon to build the html for, see $wgFooterIcons for the format of this array
+        * @param $withImage Boolean: Whether to use the icon's image or output a text-only footericon
+        */
+       function makeFooterIcon( $icon, $withImage = 'withImage' ) {
+               if ( is_string( $icon ) ) {
+                       $html = $icon;
+               } else { // Assuming array
+                       $url = $icon["url"];
+                       unset( $icon["url"] );
+                       if ( isset( $icon["src"] ) && $withImage === 'withImage' ) {
+                               $html = Html::element( 'img', $icon ); // do this the lazy way, just pass icon data as an attribute array
+                       } else {
+                               $html = htmlspecialchars( $icon["alt"] );
+                       }
+                       if ( $url ) {
+                               $html = Html::rawElement( 'a', array( "href" => $url ), $html );
+                       }
+               }
+               return $html;
+       }
+
        /**
         * Gets the link to the wiki's main page.
         * @return string
@@ -1765,7 +1669,7 @@ CSS;
                return $s;
        }
 
-       private function footerLink( $desc, $page ) {
+       public function footerLink( $desc, $page ) {
                // if the link description has been set to "-" in the default language,
                if ( wfMsgForContent( $desc )  == '-' ) {
                        // then it is disabled, for all languages.
@@ -2155,6 +2059,30 @@ CSS;
                }
        }
 
+       /**
+        * Return a fully resolved style path url to images or styles stored in the common folder.
+        * This method returns a url resolved using the configured skin style path
+        * and includes the style version inside of the url.
+        * @param $name String: The name or path of a skin resource file
+        * @return String The fully resolved style path url including styleversion
+        */
+       function getCommonStylePath( $name ) {
+               global $wgStylePath, $wgStyleVersion;
+               return "$wgStylePath/common/$name?$wgStyleVersion";
+       }
+
+       /**
+        * Return a fully resolved style path url to images or styles stored in the curent skins's folder.
+        * This method returns a url resolved using the configured skin style path
+        * and includes the style version inside of the url.
+        * @param $name String: The name or path of a skin resource file
+        * @return String The fully resolved style path url including styleversion
+        */
+       function getSkinStylePath( $name ) {
+               global $wgStylePath, $wgStyleVersion;
+               return "$wgStylePath/{$this->stylename}/$name?$wgStyleVersion";
+       }
+
        /* these are used extensively in SkinTemplate, but also some other places */
        static function makeMainPageUrl( $urlaction = '' ) {
                $title = Title::newMainPage();
@@ -2213,7 +2141,7 @@ CSS;
 
                return array(
                        'href' => $title->getLocalURL( $urlaction ),
-                       'exists' => $title->getArticleID() != 0 ? true : false
+                       'exists' => $title->getArticleID() != 0,
                );
        }
 
@@ -2351,12 +2279,10 @@ CSS;
 
                                        $line = substr( $line, 2, strlen( $line ) - 4 );
 
-                                       if ( is_null( $wgParser->mOptions ) ) {
-                                               $wgParser->mOptions = new ParserOptions();
-                                       }
-
-                                       $wgParser->mOptions->setEditSection( false );
-                                       $wikiBar[$heading] = $wgParser->parse( wfMsgForContentNoTrans( $line ) , $wgTitle, $wgParser->mOptions )->getText();
+                                       $options = new ParserOptions();
+                                       $options->setEditSection( false );
+                                       $options->setInterfaceMessage( true );
+                                       $wikiBar[$heading] = $wgParser->parse( wfMsgForContentNoTrans( $line ) , $wgTitle, $options )->getText();
                                } else {
                                        continue;
                                }