]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - includes/OutputPage.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / OutputPage.php
index 97e26110d9efb7fd2c4d6e4b9446aa8182b5a855..6ecc2754636b7b85303f0796f25e43b15b561dff 100644 (file)
@@ -1,6 +1,7 @@
 <?php
-if ( ! defined( 'MEDIAWIKI' ) )
+if ( !defined( 'MEDIAWIKI' ) ) {
        die( 1 );
+}
 
 /**
  * @todo document
@@ -8,13 +9,23 @@ if ( ! defined( 'MEDIAWIKI' ) )
 class OutputPage {
        var $mMetatags = array(), $mKeywords = array(), $mLinktags = array();
        var $mExtStyles = array();
-       var $mPagetitle = '', $mBodytext = '', $mDebugtext = '';
-       var $mHTMLtitle = '', $mHTMLtitleFromPagetitle = true, $mIsarticle = true, $mPrintable = false;
+       var $mPagetitle = '', $mBodytext = '';
+
+       /**
+        * Holds the debug lines that will be outputted as comments in page source if
+        * $wgDebugComments is enabled. See also $wgShowDebug.
+        * TODO: make a getter method for this
+        */
+       public $mDebugtext = '';
+
+       var $mHTMLtitle = '', $mIsarticle = true, $mPrintable = false;
        var $mSubtitle = '', $mRedirect = '', $mStatusCode;
        var $mLastModified = '', $mETag = false;
        var $mCategoryLinks = array(), $mCategories = array(), $mLanguageLinks = array();
 
-       var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array();
+       var $mScripts = '', $mInlineStyles = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array();
+       var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array();
+       var $mResourceLoader;
        var $mInlineMsg = array();
 
        var $mTemplateIds = array();
@@ -55,9 +66,10 @@ class OutputPage {
 
        private $mIndexPolicy = 'index';
        private $mFollowPolicy = 'follow';
-       private $mVaryHeader = array( 'Accept-Encoding' => array('list-contains=gzip'),
-                                                                 'Cookie' => null );
-
+       private $mVaryHeader = array(
+               'Accept-Encoding' => array( 'list-contains=gzip' ),
+               'Cookie' => null
+       );
 
        /**
         * Constructor
@@ -99,7 +111,6 @@ class OutputPage {
                $this->mStatusCode = $statusCode;
        }
 
-
        /**
         * Add a new <meta> tag
         * To add an http-equiv meta tag, precede the name with "http:"
@@ -148,7 +159,6 @@ class OutputPage {
                $haveMeta = true;
        }
 
-
        /**
         * Add raw HTML to the list of scripts (including \<script\> tag, etc.)
         *
@@ -184,15 +194,19 @@ class OutputPage {
         *
         * @param $file String: filename in skins/common or complete on-server path
         *              (/foo/bar.js)
+        * @param $version String: style version of the file. Defaults to $wgStyleVersion
         */
-       public function addScriptFile( $file ) {
+       public function addScriptFile( $file, $version = null ) {
                global $wgStylePath, $wgStyleVersion;
-               if( substr( $file, 0, 1 ) == '/' || substr( $file, 0, 7 ) == 'http://' ) {
+               // See if $file parameter is an absolute URL or begins with a slash
+               if( substr( $file, 0, 1 ) == '/' || preg_match( '#^[a-z]*://#i', $file ) ) {
                        $path = $file;
                } else {
-                       $path =  "{$wgStylePath}/common/{$file}";
+                       $path = "{$wgStylePath}/common/{$file}";
                }
-               $this->addScript( Html::linkedScript( wfAppendQuery( $path, $wgStyleVersion ) ) );
+               if ( is_null( $version ) )
+                       $version = $wgStyleVersion;
+               $this->addScript( Html::linkedScript( wfAppendQuery( $path, $version ) ) );
        }
 
        /**
@@ -213,6 +227,85 @@ class OutputPage {
                return $this->mScripts . $this->getHeadItems();
        }
 
+       /**
+        * Get the list of modules to include on this page
+        *
+        * @return Array of module names
+        */
+       public function getModules() {
+               return array_values( array_unique( $this->mModules ) );
+       }
+
+       /**
+        * Add one or more modules recognized by the resource loader. Modules added
+        * through this function will be loaded by the resource loader when the
+        * page loads.
+        *
+        * @param $modules Mixed: module name (string) or array of module names
+        */
+       public function addModules( $modules ) {
+               $this->mModules = array_merge( $this->mModules, (array)$modules );
+       }
+
+       /**
+        * Get the list of module JS to include on this page
+        * @return array of module names
+        */
+       public function getModuleScripts() {
+               return array_values( array_unique( $this->mModuleScripts ) );
+       }
+
+       /**
+        * Add only JS of one or more modules recognized by the resource loader. Module
+        * scripts added through this function will be loaded by the resource loader when
+        * the page loads.
+        *
+        * @param $modules Mixed: module name (string) or array of module names
+        */
+       public function addModuleScripts( $modules ) {
+               $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
+       }
+
+       /**
+        * Get the list of module CSS to include on this page
+        *
+        * @return Array of module names
+        */
+       public function getModuleStyles() {
+               return array_values( array_unique( $this->mModuleStyles ) );
+       }
+
+       /**
+        * Add only CSS of one or more modules recognized by the resource loader. Module
+        * styles added through this function will be loaded by the resource loader when
+        * the page loads.
+        *
+        * @param $modules Mixed: module name (string) or array of module names
+        */
+       public function addModuleStyles( $modules ) {
+               $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
+       }
+
+       /**
+        * Get the list of module messages to include on this page
+        *
+        * @return Array of module names
+        */
+       public function getModuleMessages() {
+               return array_values( array_unique( $this->mModuleMessages ) );
+       }
+
+       /**
+        * Add only messages of one or more modules recognized by the resource loader.
+        * Module messages added through this function will be loaded by the resource
+        * loader when the page loads.
+        *
+        * @param $modules Mixed: module name (string) or array of module names
+        */
+       public function addModuleMessages( $modules ) {
+               $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
+       }
+
        /**
         * Get all header items in a string
         *
@@ -275,7 +368,6 @@ class OutputPage {
                return $this->mArticleBodyOnly;
        }
 
-
        /**
         * checkLastModified tells the client to use the client-cached page if
         * possible. If sucessful, the OutputPage is disabled so that
@@ -352,7 +444,7 @@ class OutputPage {
                # Not modified
                # Give a 304 response code and disable body output
                wfDebug( __METHOD__ . ": NOT MODIFIED, $info\n", false );
-               ini_set('zlib.output_compression', 0);
+               ini_set( 'zlib.output_compression', 0 );
                $wgRequest->response()->header( "HTTP/1.1 304 Not Modified" );
                $this->sendCacheControl();
                $this->disable();
@@ -365,6 +457,15 @@ class OutputPage {
                return true;
        }
 
+       /**
+        * Override the last modified timestamp
+        *
+        * @param $timestamp String: new timestamp, in a format readable by
+        *        wfTimestamp()
+        */
+       public function setLastModified( $timestamp ) {
+               $this->mLastModified = wfTimestamp( TS_RFC2822, $timestamp );
+       }
 
        /**
         * Set the robot policy for the page: <http://www.robotstxt.org/meta.html>
@@ -377,10 +478,10 @@ class OutputPage {
        public function setRobotPolicy( $policy ) {
                $policy = Article::formatRobotPolicy( $policy );
 
-               if( isset( $policy['index'] ) ){
+               if( isset( $policy['index'] ) ) {
                        $this->setIndexPolicy( $policy['index'] );
                }
-               if( isset( $policy['follow'] ) ){
+               if( isset( $policy['follow'] ) ) {
                        $this->setFollowPolicy( $policy['follow'] );
                }
        }
@@ -413,7 +514,6 @@ class OutputPage {
                }
        }
 
-
        /**
         * Set the new value of the "action text", this will be added to the
         * "HTML title", separated from it with " - ".
@@ -438,17 +538,9 @@ class OutputPage {
        /**
         * "HTML title" means the contents of <title>.
         * It is stored as plain, unescaped text and will be run through htmlspecialchars in the skin file.
-        * If $name is from page title, it can only override names which are also from page title,
-        * but if it is not from page title, it can override all other names.
         */
-       public function setHTMLTitle( $name, $frompagetitle = false ) {
-               if ( $frompagetitle && $this->mHTMLtitleFromPagetitle ) {
-                       $this->mHTMLtitle = $name;
-               }
-               elseif ( $this->mHTMLtitleFromPagetitle ) {
-                       $this->mHTMLtitle = $name;
-                       $this->mHTMLtitleFromPagetitle = false;
-               }
+       public function setHTMLTitle( $name ) {
+               $this->mHTMLtitle = $name;
        }
 
        /**
@@ -472,11 +564,6 @@ class OutputPage {
                $nameWithTags = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $name ) );
                $this->mPagetitle = $nameWithTags;
 
-               $taction =  $this->getPageTitleActionText();
-               if( !empty( $taction ) ) {
-                       $name .= ' - '.$taction;
-               }
-
                # change "<i>foo&amp;bar</i>" to "foo&bar"
                $this->setHTMLTitle( wfMsg( 'pagetitle', Sanitizer::stripAllTags( $nameWithTags ) ) );
        }
@@ -508,7 +595,7 @@ class OutputPage {
                if ( $this->mTitle instanceof Title ) {
                        return $this->mTitle;
                } else {
-                       wfDebug( __METHOD__ . ' called and $mTitle is null. Return $wgTitle for sanity' );
+                       wfDebug( __METHOD__ . " called and \$mTitle is null. Return \$wgTitle for sanity\n" );
                        global $wgTitle;
                        return $wgTitle;
                }
@@ -541,7 +628,6 @@ class OutputPage {
                return $this->mSubtitle;
        }
 
-
        /**
         * Set the page as printable, i.e. it'll be displayed with with all
         * print styles included
@@ -559,7 +645,6 @@ class OutputPage {
                return $this->mPrintable;
        }
 
-
        /**
         * Disable output completely, i.e. calling output() will have no effect
         */
@@ -576,7 +661,6 @@ class OutputPage {
                return $this->mDoNothing;
        }
 
-
        /**
         * Show an "add new section" link?
         *
@@ -595,7 +679,6 @@ class OutputPage {
                return $this->mHideNewSectionLink;
        }
 
-
        /**
         * Add or remove feed links in the page header
         * This is mainly kept for backward compatibility, see OutputPage::addFeedLink()
@@ -642,7 +725,11 @@ class OutputPage {
         * @param $href String: URL
         */
        public function addFeedLink( $format, $href ) {
-               $this->mFeedLinks[$format] = $href;
+               global $wgAdvertisedFeedTypes;
+
+               if ( in_array( $format, $wgAdvertisedFeedTypes ) ) {
+                       $this->mFeedLinks[$format] = $href;
+               }
        }
 
        /**
@@ -716,7 +803,6 @@ class OutputPage {
                return $this->mIsArticleRelated;
        }
 
-
        /**
         * Add new language links
         *
@@ -746,7 +832,6 @@ class OutputPage {
                return $this->mLanguageLinks;
        }
 
-
        /**
         * Add an array of categories, with names in the keys
         *
@@ -769,7 +854,7 @@ class OutputPage {
                $pageTable = $dbr->tableName( 'page' );
                $where = $lb->constructSet( 'page', $dbr );
                $propsTable = $dbr->tableName( 'page_props' );
-               $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect, pp_value
+               $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect, page_latest, pp_value
                        FROM $pageTable LEFT JOIN $propsTable ON pp_propname='hiddencat' AND pp_page=page_id WHERE $where";
                $res = $dbr->query( $sql, __METHOD__ );
 
@@ -777,8 +862,10 @@ class OutputPage {
                $lb->addResultToCache( LinkCache::singleton(), $res );
 
                # Set all the values to 'normal'. This can be done with array_fill_keys in PHP 5.2.0+
-               $categories = array_combine( array_keys( $categories ),
-                       array_fill( 0, count( $categories ), 'normal' ) );
+               $categories = array_combine(
+                       array_keys( $categories ),
+                       array_fill( 0, count( $categories ), 'normal' )
+               );
 
                # Mark hidden categories
                foreach ( $res as $row ) {
@@ -794,9 +881,11 @@ class OutputPage {
                                $origcategory = $category;
                                $title = Title::makeTitleSafe( NS_CATEGORY, $category );
                                $wgContLang->findVariantLink( $category, $title, true );
-                               if ( $category != $origcategory )
-                                       if ( array_key_exists( $category, $categories ) )
+                               if ( $category != $origcategory ) {
+                                       if ( array_key_exists( $category, $categories ) ) {
                                                continue;
+                                       }
+                               }
                                $text = $wgContLang->convertHtml( $title->getText() );
                                $this->mCategories[] = $title->getText();
                                $this->mCategoryLinks[$type][] = $sk->link( $title, $text );
@@ -835,7 +924,6 @@ class OutputPage {
                return $this->mCategories;
        }
 
-
        /**
         * Suppress the quickbar from the output, only for skin supporting
         * the quickbar
@@ -853,7 +941,6 @@ class OutputPage {
                return $this->mSuppressQuickbar;
        }
 
-
        /**
         * Remove user JavaScript from scripts to load
         */
@@ -870,7 +957,6 @@ class OutputPage {
                return $this->mAllowUserJs;
        }
 
-
        /**
         * Prepend $text to the body HTML
         *
@@ -905,7 +991,6 @@ class OutputPage {
                return $this->mBodytext;
        }
 
-
        /**
         * Add $text to the debug output
         *
@@ -915,7 +1000,6 @@ class OutputPage {
                $this->mDebugtext .= $text;
        }
 
-
        /**
         * @deprecated use parserOptions() instead
         */
@@ -983,7 +1067,7 @@ class OutputPage {
        }
 
        /**
-        * Add wikitext with a custom Title object and 
+        * Add wikitext with a custom Title object and
         *
         * @param $text String: wikitext
         * @param $title Title object
@@ -1001,7 +1085,7 @@ class OutputPage {
         */
        public function addWikiTextTidy( $text, $linestart = true ) {
                $title = $this->getTitle();
-               $this->addWikiTextTitleTidy($text, $title, $linestart);
+               $this->addWikiTextTitleTidy( $text, $title, $linestart );
        }
 
        /**
@@ -1022,8 +1106,10 @@ class OutputPage {
                $popts = $this->parserOptions();
                $oldTidy = $popts->setTidy( $tidy );
 
-               $parserOutput = $wgParser->parse( $text, $title, $popts,
-                       $linestart, true, $this->mRevisionId );
+               $parserOutput = $wgParser->parse(
+                       $text, $title, $popts,
+                       $linestart, true, $this->mRevisionId
+               );
 
                $popts->setTidy( $oldTidy );
 
@@ -1047,13 +1133,15 @@ class OutputPage {
                wfDeprecated( __METHOD__ );
 
                $popts = $this->parserOptions();
-               $popts->setTidy(true);
-               $parserOutput = $wgParser->parse( $text, $article->mTitle,
-                       $popts, true, true, $this->mRevisionId );
-               $popts->setTidy(false);
-               if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
+               $popts->setTidy( true );
+               $parserOutput = $wgParser->parse(
+                       $text, $article->mTitle,
+                       $popts, true, true, $this->mRevisionId
+               );
+               $popts->setTidy( false );
+               if ( $cache && $article && $parserOutput->isCacheable() ) {
                        $parserCache = ParserCache::singleton();
-                       $parserCache->save( $parserOutput, $article, $popts);
+                       $parserCache->save( $parserOutput, $article, $popts );
                }
 
                $this->addParserOutput( $parserOutput );
@@ -1064,29 +1152,27 @@ class OutputPage {
         */
        public function addSecondaryWikiText( $text, $linestart = true ) {
                wfDeprecated( __METHOD__ );
-               $this->addWikiTextTitleTidy($text, $this->getTitle(), $linestart);
+               $this->addWikiTextTitleTidy( $text, $this->getTitle(), $linestart );
        }
 
-
        /**
         * Add a ParserOutput object, but without Html
         *
         * @param $parserOutput ParserOutput object
         */
        public function addParserOutputNoText( &$parserOutput ) {
-               global $wgExemptFromUserRobotsControl, $wgContentNamespaces;
-
                $this->mLanguageLinks += $parserOutput->getLanguageLinks();
                $this->addCategoryLinks( $parserOutput->getCategories() );
                $this->mNewSectionLink = $parserOutput->getNewSection();
                $this->mHideNewSectionLink = $parserOutput->getHideNewSection();
 
                $this->mParseWarnings = $parserOutput->getWarnings();
-               if ( $parserOutput->getCacheTime() == -1 ) {
+               if ( !$parserOutput->isCacheable() ) {
                        $this->enableClientCache( false );
                }
                $this->mNoGallery = $parserOutput->getNoGallery();
                $this->mHeadItems = array_merge( $this->mHeadItems, $parserOutput->getHeadItems() );
+               $this->addModules( $parserOutput->getModules() );
                // Versioning...
                foreach ( (array)$parserOutput->mTemplateIds as $ns => $dbks ) {
                        if ( isset( $this->mTemplateIds[$ns] ) ) {
@@ -1095,11 +1181,6 @@ class OutputPage {
                                $this->mTemplateIds[$ns] = $dbks;
                        }
                }
-               // Page title
-               $title = $parserOutput->getTitleText();
-               if ( $title != '' ) {
-                       $this->setPageTitle( $title );
-               }
 
                // Hooks registered in the object
                global $wgParserOutputHooks;
@@ -1121,7 +1202,7 @@ class OutputPage {
        function addParserOutput( &$parserOutput ) {
                $this->addParserOutputNoText( $parserOutput );
                $text = $parserOutput->getText();
-               wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
+               wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
                $this->addHTML( $text );
        }
 
@@ -1154,10 +1235,16 @@ class OutputPage {
                        throw new MWException( 'Empty $mTitle in ' . __METHOD__ );
                }
                $popts = $this->parserOptions();
-               if ( $interface) { $popts->setInterfaceMessage(true); }
-               $parserOutput = $wgParser->parse( $text, $this->getTitle(), $popts,
-                       $linestart, true, $this->mRevisionId );
-               if ( $interface) { $popts->setInterfaceMessage(false); }
+               if ( $interface ) {
+                       $popts->setInterfaceMessage( true );
+               }
+               $parserOutput = $wgParser->parse(
+                       $text, $this->getTitle(), $popts,
+                       $linestart, true, $this->mRevisionId
+               );
+               if ( $interface ) {
+                       $popts->setInterfaceMessage( false );
+               }
                return $parserOutput->getText();
        }
 
@@ -1192,7 +1279,7 @@ class OutputPage {
                wfDeprecated( __METHOD__ );
                $parserOutput = ParserCache::singleton()->get( $article, $article->getParserOptions() );
 
-               if ($parserOutput !== false) {
+               if ( $parserOutput !== false ) {
                        $this->addParserOutput( $parserOutput );
                        return true;
                } else {
@@ -1235,21 +1322,21 @@ class OutputPage {
                                ),
                                $wgCacheVaryCookies
                        );
-                       wfRunHooks('GetCacheVaryCookies', array( $this, &$cookies ) );
+                       wfRunHooks( 'GetCacheVaryCookies', array( $this, &$cookies ) );
                }
                return $cookies;
        }
 
        /**
         * Return whether this page is not cacheable because "useskin" or "uselang"
-        * url parameters were passed
+        * URL parameters were passed.
         *
         * @return Boolean
         */
        function uncacheableBecauseRequestVars() {
                global $wgRequest;
-               return $wgRequest->getText('useskin', false) === false
-                       && $wgRequest->getText('uselang', false) === false;
+               return $wgRequest->getText( 'useskin', false ) === false
+                       && $wgRequest->getText( 'uselang', false ) === false;
        }
 
        /**
@@ -1268,11 +1355,11 @@ class OutputPage {
                foreach ( $cvCookies as $cookieName ) {
                        # Check for a simple string match, like the way squid does it
                        if ( strpos( $cookieHeader, $cookieName ) !== false ) {
-                               wfDebug( __METHOD__.": found $cookieName\n" );
+                               wfDebug( __METHOD__ . ": found $cookieName\n" );
                                return true;
                        }
                }
-               wfDebug( __METHOD__.": no cache-varying cookies found\n" );
+               wfDebug( __METHOD__ . ": no cache-varying cookies found\n" );
                return false;
        }
 
@@ -1281,16 +1368,16 @@ class OutputPage {
         *
         * @param $header String: header name
         * @param $option either an Array or null
+        * @fixme Document the $option parameter; it appears to be for
+        *        X-Vary-Options but what format is acceptable?
         */
        public function addVaryHeader( $header, $option = null ) {
                if ( !array_key_exists( $header, $this->mVaryHeader ) ) {
-                       $this->mVaryHeader[$header] = $option;
-               }
-               elseif( is_array( $option ) ) {
+                       $this->mVaryHeader[$header] = (array)$option;
+               } elseif( is_array( $option ) ) {
                        if( is_array( $this->mVaryHeader[$header] ) ) {
                                $this->mVaryHeader[$header] = array_merge( $this->mVaryHeader[$header], $option );
-                       }
-                       else {
+                       } else {
                                $this->mVaryHeader[$header] = $option;
                        }
                }
@@ -1304,22 +1391,23 @@ class OutputPage {
         */
        public function getXVO() {
                $cvCookies = $this->getCacheVaryCookies();
-               
+
                $cookiesOption = array();
                foreach ( $cvCookies as $cookieName ) {
                        $cookiesOption[] = 'string-contains=' . $cookieName;
                }
                $this->addVaryHeader( 'Cookie', $cookiesOption );
-               
+
                $headers = array();
                foreach( $this->mVaryHeader as $header => $option ) {
                        $newheader = $header;
-                       if( is_array( $option ) )
+                       if( is_array( $option ) ) {
                                $newheader .= ';' . implode( ';', $option );
+                       }
                        $headers[] = $newheader;
                }
                $xvo = 'X-Vary-Options: ' . implode( ',', $headers );
-               
+
                return $xvo;
        }
 
@@ -1330,19 +1418,28 @@ class OutputPage {
         * For example:
         *   /w/index.php?title=Main_page should always be served; but
         *   /w/index.php?title=Main_page&variant=zh-cn should never be served.
-        *
-        * patched by Liangent and Philip
         */
        function addAcceptLanguage() {
                global $wgRequest, $wgContLang;
-               if( !$wgRequest->getCheck('variant') && $wgContLang->hasVariants() ) {
+               if( !$wgRequest->getCheck( 'variant' ) && $wgContLang->hasVariants() ) {
                        $variants = $wgContLang->getVariants();
                        $aloption = array();
                        foreach ( $variants as $variant ) {
-                               if( $variant === $wgContLang->getCode() )
+                               if( $variant === $wgContLang->getCode() ) {
                                        continue;
-                               else
-                                       $aloption[] = "string-contains=$variant";
+                               } else {
+                                       $aloption[] = 'string-contains=' . $variant;
+                                       
+                                       // IE and some other browsers use another form of language code
+                                       // in their Accept-Language header, like "zh-CN" or "zh-TW".
+                                       // We should handle these too.
+                                       $ievariant = explode( '-', $variant );
+                                       if ( count( $ievariant ) == 2 ) {
+                                               $ievariant[1] = strtoupper( $ievariant[1] );
+                                               $ievariant = implode( '-', $ievariant );
+                                               $aloption[] = 'string-contains=' . $ievariant;
+                                       }
+                               }
                        }
                        $this->addVaryHeader( 'Accept-Language', $aloption );
                }
@@ -1390,8 +1487,9 @@ class OutputPage {
                global $wgUseSquid, $wgUseESI, $wgUseETag, $wgSquidMaxage, $wgRequest, $wgUseXVO;
 
                $response = $wgRequest->response();
-               if ($wgUseETag && $this->mETag)
-                       $response->header("ETag: $this->mETag");
+               if ( $wgUseETag && $this->mETag ) {
+                       $response->header( "ETag: $this->mETag" );
+               }
 
                $this->addAcceptLanguage();
 
@@ -1405,8 +1503,10 @@ class OutputPage {
                }
 
                if( !$this->uncacheableBecauseRequestVars() && $this->mEnableClientCache ) {
-                       if( $wgUseSquid && session_id() == '' &&
-                         ! $this->isPrintable() && $this->mSquidMaxage != 0 && !$this->haveCacheVaryCookies() )
+                       if(
+                               $wgUseSquid && session_id() == '' && !$this->isPrintable() &&
+                               $this->mSquidMaxage != 0 && !$this->haveCacheVaryCookies()
+                       )
                        {
                                if ( $wgUseESI ) {
                                        # We'll purge the proxy cache explicitly, but require end user agents
@@ -1513,12 +1613,12 @@ class OutputPage {
         */
        public function output() {
                global $wgUser, $wgOutputEncoding, $wgRequest;
-               global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType;
+               global $wgLanguageCode, $wgDebugRedirects, $wgMimeType;
                global $wgUseAjax, $wgAjaxWatch;
                global $wgEnableMWSuggest, $wgUniversalEditButton;
                global $wgArticle;
 
-               if( $this->mDoNothing ){
+               if( $this->mDoNothing ) {
                        return;
                }
                wfProfileIn( __METHOD__ );
@@ -1547,35 +1647,44 @@ class OutputPage {
                        return;
                } elseif ( $this->mStatusCode ) {
                        $message = self::getStatusMessage( $this->mStatusCode );
-                       if ( $message )
+                       if ( $message ) {
                                $wgRequest->response()->header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $message );
+                       }
                }
 
                $sk = $wgUser->getSkin();
 
+               // Add base resources
+               $this->addModules( 'mediawiki.util' );
+               global $wgIncludeLegacyJavaScript;
+               if( $wgIncludeLegacyJavaScript ){
+                       $this->addModules( 'mediawiki.legacy.wikibits' );
+               }
+
+               // Add various resources if required
                if ( $wgUseAjax ) {
-                       $this->addScriptFile( 'ajax.js' );
+                       $this->addModules( 'mediawiki.legacy.ajax' );
 
                        wfRunHooks( 'AjaxAddScript', array( &$this ) );
 
                        if( $wgAjaxWatch && $wgUser->isLoggedIn() ) {
-                               $this->addScriptFile( 'ajaxwatch.js' );
+                               $this->addModules( 'mediawiki.legacy.ajaxwatch' );
                        }
 
-                       if ( $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ){
-                               $this->addScriptFile( 'mwsuggest.js' );
+                       if ( $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
+                               $this->addModules( 'mediawiki.legacy.mwsuggest' );
                        }
                }
 
                if( $wgUser->getBoolOption( 'editsectiononrightclick' ) ) {
-                       $this->addScriptFile( 'rightclickedit.js' );
+                       $this->addModules( 'mediawiki.action.view.rightClickEdit' );
                }
 
                if( $wgUniversalEditButton ) {
                        if( isset( $wgArticle ) && $this->getTitle() && $this->getTitle()->quickUserCan( 'edit' )
                                && ( $this->getTitle()->exists() || $this->getTitle()->quickUserCan( 'create' ) ) ) {
                                // Original UniversalEditButton
-                               $msg = wfMsg('edit');
+                               $msg = wfMsg( 'edit' );
                                $this->addLink( array(
                                        'rel' => 'alternate',
                                        'type' => 'application/x-wiki',
@@ -1591,11 +1700,12 @@ class OutputPage {
                        }
                }
 
+
                # Buffer output; final headers may depend on later processing
                ob_start();
 
                $wgRequest->response()->header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
-               $wgRequest->response()->header( 'Content-language: '.$wgContLanguageCode );
+               $wgRequest->response()->header( 'Content-language: ' . $wgLanguageCode );
 
                // Prevent framing, if requested
                $frameOptions = $this->getFrameOptions();
@@ -1603,9 +1713,8 @@ class OutputPage {
                        $wgRequest->response()->header( "X-Frame-Options: $frameOptions" );
                }
 
-
-               if ($this->mArticleBodyOnly) {
-                       $this->out($this->mBodytext);
+               if ( $this->mArticleBodyOnly ) {
+                       $this->out( $this->mBodytext );
                } else {
                        // Hook that allows last minute changes to the output page, e.g.
                        // adding of CSS or Javascript by extensions.
@@ -1633,7 +1742,9 @@ class OutputPage {
                        $outs = $ins;
                } else {
                        $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
-                       if ( false === $outs ) { $outs = $ins; }
+                       if ( false === $outs ) {
+                               $outs = $ins;
+                       }
                }
                print $outs;
        }
@@ -1643,7 +1754,6 @@ class OutputPage {
         */
        public static function setEncodings() {
                global $wgInputEncoding, $wgOutputEncoding;
-               global $wgContLang;
 
                $wgInputEncoding = strtolower( $wgInputEncoding );
 
@@ -1683,7 +1793,9 @@ class OutputPage {
                if( $reason == '' ) {
                        $reason = wfMsg( 'blockednoreason' );
                }
-               $blockTimestamp = $wgLang->timeanddate( wfTimestamp( TS_MW, $wgUser->mBlock->mTimestamp ), true );
+               $blockTimestamp = $wgLang->timeanddate(
+                       wfTimestamp( TS_MW, $wgUser->mBlock->mTimestamp ), true
+               );
                $ip = wfGetIP();
 
                $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
@@ -1696,16 +1808,20 @@ class OutputPage {
                        // Search for localization in 'ipboptions'
                        $scBlockExpiryOptions = wfMsg( 'ipboptions' );
                        foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) {
-                               if ( strpos( $option, ":" ) === false )
+                               if ( strpos( $option, ':' ) === false ) {
                                        continue;
-                               list( $show, $value ) = explode( ":", $option );
+                               }
+                               list( $show, $value ) = explode( ':', $option );
                                if ( $value == 'infinite' || $value == 'indefinite' ) {
                                        $blockExpiry = $show;
                                        break;
                                }
                        }
                } else {
-                       $blockExpiry = $wgLang->timeanddate( wfTimestamp( TS_MW, $blockExpiry ), true );
+                       $blockExpiry = $wgLang->timeanddate(
+                               wfTimestamp( TS_MW, $blockExpiry ),
+                               true
+                       );
                }
 
                if ( $wgUser->mBlock->mAuto ) {
@@ -1715,10 +1831,13 @@ class OutputPage {
                }
 
                /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
-                * This could be a username, an ip range, or a single ip. */
+                * This could be a username, an IP range, or a single IP. */
                $intended = $wgUser->mBlock->mAddress;
 
-               $this->addWikiMsg( $msg, $link, $reason, $ip, $name, $blockid, $blockExpiry, $intended, $blockTimestamp );
+               $this->addWikiMsg(
+                       $msg, $link, $reason, $ip, $name, $blockid, $blockExpiry,
+                       $intended, $blockTimestamp
+               );
 
                # Don't auto-return to special pages
                if( $return ) {
@@ -1806,34 +1925,22 @@ class OutputPage {
                $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
                        User::getGroupsWithPermission( $permission ) );
                if( $groups ) {
-                       $this->addWikiMsg( 'badaccess-groups',
+                       $this->addWikiMsg(
+                               'badaccess-groups',
                                $wgLang->commaList( $groups ),
-                               count( $groups) );
+                               count( $groups )
+                       );
                } else {
                        $this->addWikiMsg( 'badaccess-group0' );
                }
                $this->returnToMain();
        }
 
-       /**
-        * @deprecated use permissionRequired()
-        */
-       public function sysopRequired() {
-               throw new MWException( "Call to deprecated OutputPage::sysopRequired() method\n" );
-       }
-
-       /**
-        * @deprecated use permissionRequired()
-        */
-       public function developerRequired() {
-               throw new MWException( "Call to deprecated OutputPage::developerRequired() method\n" );
-       }
-
        /**
         * Produce the stock "please login to use the wiki" page
         */
        public function loginToUse() {
-               global $wgUser, $wgContLang;
+               global $wgUser;
 
                if( $wgUser->isLoggedIn() ) {
                        $this->permissionRequired( 'read' );
@@ -1856,13 +1963,14 @@ class OutputPage {
                        array( 'known', 'noclasses' )
                );
                $this->addHTML( wfMsgWikiHtml( 'loginreqpagetext', $loginLink ) );
-               $this->addHTML( "\n<!--" . $this->getTitle()->getPrefixedUrl() . "-->" );
+               $this->addHTML( "\n<!--" . $this->getTitle()->getPrefixedUrl() . '-->' );
 
                # Don't return to the main page if the user can't read it
                # otherwise we'll end up in a pointless loop
                $mainPage = Title::newMainPage();
-               if( $mainPage->userCanRead() )
+               if( $mainPage->userCanRead() ) {
                        $this->returnToMain( null, $mainPage );
+               }
        }
 
        /**
@@ -1873,26 +1981,30 @@ class OutputPage {
         * @return String: the wikitext error-messages, formatted into a list.
         */
        public function formatPermissionsErrorMessage( $errors, $action = null ) {
-               if ($action == null) {
-                       $text = wfMsgNoTrans( 'permissionserrorstext', count($errors)). "\n\n";
+               if ( $action == null ) {
+                       $text = wfMsgNoTrans( 'permissionserrorstext', count( $errors ) ) . "\n\n";
                } else {
-                       global $wgLang;
                        $action_desc = wfMsgNoTrans( "action-$action" );
-                       $text = wfMsgNoTrans( 'permissionserrorstext-withaction', count($errors), $action_desc ) . "\n\n";
+                       $text = wfMsgNoTrans(
+                               'permissionserrorstext-withaction',
+                               count( $errors ),
+                               $action_desc
+                       ) . "\n\n";
                }
 
-               if (count( $errors ) > 1) {
+               if ( count( $errors ) > 1 ) {
                        $text .= '<ul class="permissions-errors">' . "\n";
 
-                       foreach( $errors as $error )
-                       {
+                       foreach( $errors as $error ) {
                                $text .= '<li>';
                                $text .= call_user_func_array( 'wfMsgNoTrans', $error );
                                $text .= "</li>\n";
                        }
                        $text .= '</ul>';
                } else {
-                       $text .= "<div class=\"permissions-errors\">\n" . call_user_func_array( 'wfMsgNoTrans', reset( $errors ) ) . "\n</div>";
+                       $text .= "<div class=\"permissions-errors\">\n" .
+                                       call_user_func_array( 'wfMsgNoTrans', reset( $errors ) ) .
+                                       "\n</div>";
                }
 
                return $text;
@@ -1927,25 +2039,16 @@ class OutputPage {
 
                // If no reason is given, just supply a default "I can't let you do
                // that, Dave" message.  Should only occur if called by legacy code.
-               if ( $protected && empty($reasons) ) {
+               if ( $protected && empty( $reasons ) ) {
                        $reasons[] = array( 'badaccess-group0' );
                }
 
-               if ( !empty($reasons) ) {
+               if ( !empty( $reasons ) ) {
                        // Permissions error
                        if( $source ) {
                                $this->setPageTitle( wfMsg( 'viewsource' ) );
                                $this->setSubtitle(
-                                       wfMsg(
-                                               'viewsourcefor',
-                                               $skin->link(
-                                                       $this->getTitle(),
-                                                       null,
-                                                       array(),
-                                                       array(),
-                                                       array( 'known', 'noclasses' )
-                                               )
-                                       )
+                                       wfMsg( 'viewsourcefor', $skin->linkKnown( $this->getTitle() ) )
                                );
                        } else {
                                $this->setPageTitle( wfMsg( 'badaccess' ) );
@@ -1955,7 +2058,7 @@ class OutputPage {
                        // Wiki is read only
                        $this->setPageTitle( wfMsg( 'readonly' ) );
                        $reason = wfReadOnlyReason();
-                       $this->wrapWikiMsg( '<div class="mw-readonly-error">\n$1</div>', array( 'readonlytext', $reason ) );
+                       $this->wrapWikiMsg( "<div class='mw-readonly-error'>\n$1\n</div>", array( 'readonlytext', $reason ) );
                }
 
                // Show source, if supplied
@@ -2036,8 +2139,8 @@ class OutputPage {
        }
 
        public function showFatalError( $message ) {
-               $this->setPageTitle( wfMsg( "internalerror" ) );
-               $this->setRobotPolicy( "noindex,nofollow" );
+               $this->setPageTitle( wfMsg( 'internalerror' ) );
+               $this->setRobotPolicy( 'noindex,nofollow' );
                $this->setArticleRelated( false );
                $this->enableClientCache( false );
                $this->mRedirect = '';
@@ -2069,12 +2172,15 @@ class OutputPage {
         *
         * @param $title Title to link
         * @param $query String: query string
+        * @param $text String text of the link (input is not escaped)
         */
-       public function addReturnTo( $title, $query = array() ) {
+       public function addReturnTo( $title, $query = array(), $text = null ) {
                global $wgUser;
-               $this->addLink( array( 'rel' => 'next', 'href' => $title->getFullUrl() ) );
-               $link = wfMsgHtml( 'returnto', $wgUser->getSkin()->link(
-                       $title, null, array(), $query ) );
+               $this->addLink( array( 'rel' => 'next', 'href' => $title->getFullURL() ) );
+               $link = wfMsgHtml(
+                       'returnto',
+                       $wgUser->getSkin()->link( $title, $text, array(), $query )
+               );
                $this->addHTML( "<p id=\"mw-returnto\">{$link}</p>\n" );
        }
 
@@ -2115,88 +2221,75 @@ class OutputPage {
 
        /**
         * @param $sk Skin The given Skin
-        * @param $includeStyle Unused (?)
+        * @param $includeStyle Boolean: unused
         * @return String: The doctype, opening <html>, and head element.
         */
        public function headElement( Skin $sk, $includeStyle = true ) {
-               global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
-               global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces, $wgHtml5Version;
-               global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgHtml5, $wgWellFormedXml;
+               global $wgOutputEncoding, $wgMimeType;
+               global $wgUseTrackbacks, $wgHtml5;
                global $wgUser, $wgRequest, $wgLang;
 
-               $this->addMeta( "http:Content-Type", "$wgMimeType; charset={$wgOutputEncoding}" );
                if ( $sk->commonPrintStylesheet() ) {
-                       $this->addStyle( 'common/wikiprintable.css', 'print' );
+                       $this->addModuleStyles( 'mediawiki.legacy.wikiprintable' );
                }
                $sk->setupUserCss( $this );
 
-               $ret = '';
-
-               if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
-                       $ret .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
-               }
+               $lang = wfUILang();
+               $ret = Html::htmlHeader( array( 'lang' => $lang->getCode(), 'dir' => $lang->getDir() ) );
 
                if ( $this->getHTMLTitle() == '' ) {
-                       $this->setHTMLTitle(  wfMsg( 'pagetitle', $this->getPageTitle() ));
+                       $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ) );
                }
 
-               $dir = $wgContLang->getDir();
+               $openHead = Html::openElement( 'head' );
+               if ( $openHead ) {
+                       # Don't bother with the newline if $head == ''
+                       $ret .= "$openHead\n";
+               }
 
                if ( $wgHtml5 ) {
-                       if ( $wgWellFormedXml ) {
-                               # Unknown elements and attributes are okay in XML, but unknown
-                               # named entities are well-formedness errors and will break XML
-                               # parsers.  Thus we need a doctype that gives us appropriate
-                               # entity definitions.  The HTML5 spec permits four legacy
-                               # doctypes as obsolete but conforming, so let's pick one of
-                               # those, although it makes our pages look like XHTML1 Strict.
-                               # Isn't compatibility great?
-                               $ret .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
-                       } else {
-                               # Much saner.
-                               $ret .= "<!doctype html>\n";
-                       }
-                       $ret .= "<html lang=\"$wgContLanguageCode\" dir=\"$dir\"";
-                       if ( $wgHtml5Version ) $ret .= " version=\"$wgHtml5Version\"";
-                       $ret .= ">\n";
+                       # More succinct than <meta http-equiv=Content-Type>, has the
+                       # same effect
+                       $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n";
                } else {
-                       $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n";
-                       $ret .= "<html xmlns=\"{$wgXhtmlDefaultNamespace}\" ";
-                       foreach($wgXhtmlNamespaces as $tag => $ns) {
-                               $ret .= "xmlns:{$tag}=\"{$ns}\" ";
-                       }
-                       $ret .= "lang=\"$wgContLanguageCode\" dir=\"$dir\">\n";
+                       $this->addMeta( 'http:Content-Type', "$wgMimeType; charset=$wgOutputEncoding" );
                }
 
-               $ret .= "<head>\n";
-               $ret .= "<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
+               $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
+
                $ret .= implode( "\n", array(
-                       $this->getHeadLinks(),
-                       $this->buildCssLinks(),
-                       $this->getHeadScripts( $sk ),
-                       $this->getHeadItems(),
-               ));
-               if( $sk->usercss ){
-                       $ret .= Html::inlineStyle( $sk->usercss );
-               }
+                       $this->getHeadLinks( $sk ),
+                       $this->buildCssLinks( $sk ),
+                       $this->getHeadItems()
+               ) );
 
-               if ($wgUseTrackbacks && $this->isArticleRelated())
+               if ( $wgUseTrackbacks && $this->isArticleRelated() ) {
                        $ret .= $this->getTitle()->trackbackRDF();
+               }
 
-               $ret .= "</head>\n";
+               $closeHead = Html::closeElement( 'head' );
+               if ( $closeHead ) {
+                       $ret .= "$closeHead\n";
+               }
 
                $bodyAttrs = array();
 
                # Crazy edit-on-double-click stuff
                $action = $wgRequest->getVal( 'action', 'view' );
 
-               if ( $this->getTitle()->getNamespace() != NS_SPECIAL
-               && !in_array( $action, array( 'edit', 'submit' ) )
-               && $wgUser->getOption( 'editondblclick' ) ) {
-                       $bodyAttrs['ondblclick'] = "document.location = '" . Xml::escapeJsString( $this->getTitle()->getEditURL() ) . "'";
+               if (
+                       $this->getTitle()->getNamespace() != NS_SPECIAL &&
+                       !in_array( $action, array( 'edit', 'submit' ) ) &&
+                       $wgUser->getOption( 'editondblclick' )
+               )
+               {
+                       $editUrl = $this->getTitle()->getLocalUrl( $sk->editUrlOptions() );
+                       $bodyAttrs['ondblclick'] = "document.location = '" .
+                               Xml::escapeJsString( $editUrl ) . "'";
                }
 
                # Class bloat
+               $dir = wfUILang()->getDir();
                $bodyAttrs['class'] = "mediawiki $dir";
 
                if ( $wgLang->capitalizeAllNouns() ) {
@@ -2214,55 +2307,220 @@ class OutputPage {
                $bodyAttrs['class'] .= ' ' . Sanitizer::escapeClass( 'page-' . $this->getTitle()->getPrefixedText() );
                $bodyAttrs['class'] .= ' skin-' . Sanitizer::escapeClass( $wgUser->getSkin()->getSkinName() );
 
+               $sk->addToBodyAttributes( $this, $bodyAttrs ); // Allow skins to add body attributes they need
+               wfRunHooks( 'OutputPageBodyAttributes', array( $this, $sk, &$bodyAttrs ) );
+
                $ret .= Html::openElement( 'body', $bodyAttrs ) . "\n";
 
                return $ret;
        }
 
+       /**
+        * Get a ResourceLoader object associated with this OutputPage
+        */
+       public function getResourceLoader() {
+               if ( is_null( $this->mResourceLoader ) ) {
+                       $this->mResourceLoader = new ResourceLoader();
+               }
+               return $this->mResourceLoader;
+       }               
+
+       /**
+        * TODO: Document
+        * @param $skin Skin
+        * @param $modules Array/string with the module name
+        * @param $only string May be styles, messages or scripts
+        * @param $useESI boolean
+        * @return string html <script> and <style> tags
+        */
+       protected function makeResourceLoaderLink( Skin $skin, $modules, $only, $useESI = false ) {
+               global $wgUser, $wgLang, $wgLoadScript, $wgResourceLoaderUseESI,
+                       $wgResourceLoaderInlinePrivateModules, $wgRequest;
+               // Lazy-load ResourceLoader
+               // TODO: Should this be a static function of ResourceLoader instead?
+               $baseQuery = array(
+                       'lang' => $wgLang->getCode(),
+                       'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
+                       'skin' => $skin->getSkinName(),
+                       'only' => $only,
+               );
+               // Propagate printable and handheld parameters if present
+               if ( $wgRequest->getBool( 'printable' ) ) {
+                       $baseQuery['printable'] = 1;
+               }
+               if ( $wgRequest->getBool( 'handheld' ) ) {
+                       $baseQuery['handheld'] = 1;
+               }
+               
+               if ( !count( $modules ) ) {
+                       return '';
+               }
+               
+               if ( count( $modules ) > 1 ) {
+                       // Remove duplicate module requests
+                       $modules = array_unique( (array) $modules );
+                       // Sort module names so requests are more uniform
+                       sort( $modules );
+               
+                       if ( ResourceLoader::inDebugMode() ) {
+                               // Recursively call us for every item
+                               $links = '';
+                               foreach ( $modules as $name ) {
+                                       $links .= $this->makeResourceLoaderLink( $skin, $name, $only, $useESI );
+                               }
+                               return $links;
+                       }
+               }
+               
+               // Create keyed-by-group list of module objects from modules list
+               $groups = array();
+               $resourceLoader = $this->getResourceLoader();
+               foreach ( (array) $modules as $name ) {
+                       $module = $resourceLoader->getModule( $name );
+
+                       $group = $module->getGroup();
+                       if ( !isset( $groups[$group] ) ) {
+                               $groups[$group] = array();
+                       }
+                       $groups[$group][$name] = $module;
+               }
+               $links = '';
+               foreach ( $groups as $group => $modules ) {
+                       $query = $baseQuery;
+                       // Special handling for user-specific groups
+                       if ( ( $group === 'user' || $group === 'private' ) && $wgUser->isLoggedIn() ) {
+                               $query['user'] = $wgUser->getName();
+                       }
+                       
+                       // Create a fake request based on the one we are about to make so modules return
+                       // correct timestamp and emptiness data
+                       $context = new ResourceLoaderContext( $resourceLoader, new FauxRequest( $query ) );
+                       // Drop modules that know they're empty
+                       foreach ( $modules as $key => $module ) {
+                               if ( $module->isKnownEmpty( $context ) ) {
+                                       unset( $modules[$key] );
+                               }
+                       }
+                       // If there are no modules left, skip this group
+                       if ( $modules === array() ) {
+                               continue;
+                       }
+                       
+                       $query['modules'] = ResourceLoader::makePackedModulesString( array_keys( $modules ) );
+                       
+                       // Support inlining of private modules if configured as such
+                       if ( $group === 'private' && $wgResourceLoaderInlinePrivateModules ) {
+                               if ( $only == 'styles' ) {
+                                       $links .= Html::inlineStyle(
+                                               $resourceLoader->makeModuleResponse( $context, $modules )
+                                       );
+                               } else {
+                                       $links .= Html::inlineScript(
+                                               ResourceLoader::makeLoaderConditionalScript(
+                                                       $resourceLoader->makeModuleResponse( $context, $modules )
+                                               )
+                                       );
+                               }
+                               continue;
+                       }
+                       // Special handling for the user group; because users might change their stuff
+                       // on-wiki like user pages, or user preferences; we need to find the highest
+                       // timestamp of these user-changable modules so we can ensure cache misses on change
+                       // This should NOT be done for the site group (bug 27564) because anons get that too
+                       // and we shouldn't be putting timestamps in Squid-cached HTML
+                       if ( $group === 'user' ) {
+                               // Get the maximum timestamp
+                               $timestamp = 1;
+                               foreach ( $modules as $module ) {
+                                       $timestamp = max( $timestamp, $module->getModifiedTime( $context ) );
+                               }
+                               // Add a version parameter so cache will break when things change
+                               $query['version'] = wfTimestamp( TS_ISO_8601_BASIC, $timestamp );
+                       }
+                       // Make queries uniform in order
+                       ksort( $query );
+
+                       $url = wfAppendQuery( $wgLoadScript, $query );
+                       // Prevent the IE6 extension check from being triggered (bug 28840)
+                       // by appending a character that's invalid in Windows extensions ('*')
+                       $url .= '&*';
+                       if ( $useESI && $wgResourceLoaderUseESI ) {
+                               $esi = Xml::element( 'esi:include', array( 'src' => $url ) );
+                               if ( $only == 'styles' ) {
+                                       $links .= Html::inlineStyle( $esi );
+                               } else {
+                                       $links .= Html::inlineScript( $esi );
+                               }
+                       } else {
+                               // Automatically select style/script elements
+                               if ( $only === 'styles' ) {
+                                       $links .= Html::linkedStyle( $url ) . "\n";
+                               } else {
+                                       $links .= Html::linkedScript( $url ) . "\n";
+                               }
+                       }
+               }
+               return $links;
+       }
+
        /**
         * Gets the global variables and mScripts; also adds userjs to the end if
-        * enabled
+        * enabled. Despite the name, these scripts are no longer put in the
+        * <head> but at the bottom of the <body>
         *
         * @param $sk Skin object to use
         * @return String: HTML fragment
         */
        function getHeadScripts( Skin $sk ) {
-               global $wgUser, $wgRequest, $wgJsMimeType, $wgUseSiteJs;
-               global $wgStylePath, $wgStyleVersion;
+               global $wgUser, $wgRequest, $wgUseSiteJs;
 
-               $scripts = Skin::makeGlobalVariablesScript( $sk->getSkinName() );
-               $scripts .= Html::linkedScript( "{$wgStylePath}/common/wikibits.js?$wgStyleVersion" );
+               // Startup - this will immediately load jquery and mediawiki modules
+               $scripts = $this->makeResourceLoaderLink( $sk, 'startup', 'scripts', true );
 
-               //add site JS if enabled:
-               if( $wgUseSiteJs ) {
-                       $jsCache = $wgUser->isLoggedIn() ? '&smaxage=0' : '';
-                       $this->addScriptFile(  Skin::makeUrl( '-',
-                                       "action=raw$jsCache&gen=js&useskin=" .
-                                       urlencode( $sk->getSkinName() )
-                                       )
-                               );
+               // Configuration -- This could be merged together with the load and go, but
+               // makeGlobalVariablesScript returns a whole script tag -- grumble grumble...
+               $scripts .= Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n";
+
+               // Script and Messages "only" requests
+               $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' );
+               $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' );
+
+               // Modules requests - let the client calculate dependencies and batch requests as it likes
+               if ( $this->getModules() ) {
+                       $scripts .= Html::inlineScript(
+                               ResourceLoader::makeLoaderConditionalScript(
+                                       Xml::encodeJsCall( 'mediaWiki.loader.load', array( $this->getModules() ) ) .
+                                       Xml::encodeJsCall( 'mediaWiki.loader.go', array() )
+                               )
+                       ) . "\n";
                }
 
-               //add user js if enabled:
-               if( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) {
+               // Legacy Scripts
+               $scripts .= "\n" . $this->mScripts;
+
+               // Add site JS if enabled
+               if ( $wgUseSiteJs ) {
+                       $scripts .= $this->makeResourceLoaderLink( $sk, 'site', 'scripts' );
+               }
+
+               // Add user JS if enabled - trying to load user.options as a bundle if possible
+               $userOptionsAdded = false;
+               if ( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) {
                        $action = $wgRequest->getVal( 'action', 'view' );
-                       if( $this->mTitle && $this->mTitle->isJsSubpage() and $sk->userCanPreview( $action ) ) {
+                       if( $this->mTitle && $this->mTitle->isJsSubpage() && $sk->userCanPreview( $action ) ) {
                                # XXX: additional security check/prompt?
-                               $this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) );
+                               $scripts .= Html::inlineScript( "\n" . $wgRequest->getText( 'wpTextbox1' ) . "\n" ) . "\n";
                        } else {
-                               $userpage = $wgUser->getUserPage();
-                               $scriptpage = Title::makeTitleSafe(
-                                       NS_USER,
-                                       $userpage->getDBkey() . '/' . $sk->getSkinName() . '.js'
+                               $scripts .= $this->makeResourceLoaderLink(
+                                       $sk, array( 'user', 'user.options' ), 'scripts'
                                );
-                               if ( $scriptpage && $scriptpage->exists() ) {
-                                       $userjs = Skin::makeUrl( $scriptpage->getPrefixedText(), 'action=raw&ctype=' . $wgJsMimeType );
-                                       $this->addScriptFile( $userjs );
-                               }
+                               $userOptionsAdded = true;
                        }
                }
-
-               $scripts .= "\n" . $this->mScripts;
+               if ( !$userOptionsAdded ) {
+                       $scripts .= $this->makeResourceLoaderLink( $sk, 'user.options', 'scripts' );
+               }
+               
                return $scripts;
        }
 
@@ -2280,7 +2538,7 @@ class OutputPage {
                $called = true;
 
                if ( !$wgHtml5 ) {
-                       $this->addMeta( 'http:Content-Style-Type', 'text/css' ); //bug 15835
+                       $this->addMeta( 'http:Content-Style-Type', 'text/css' ); // bug 15835
                }
                $this->addMeta( 'generator', "MediaWiki $wgVersion" );
 
@@ -2296,15 +2554,22 @@ class OutputPage {
                                "/<.*?" . ">/" => '',
                                "/_/" => ' '
                        );
-                       $this->addMeta( 'keywords', preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ) ) );
+                       $this->addMeta(
+                               'keywords',
+                               preg_replace(
+                                       array_keys( $strip ),
+                                       array_values( $strip ),
+                                       implode( ',', $this->mKeywords )
+                               )
+                       );
                }
        }
 
        /**
         * @return string HTML tag links to be put in the header.
         */
-       public function getHeadLinks() {
-               global $wgRequest, $wgFeed;
+       public function getHeadLinks( Skin $sk ) {
+               global $wgFeed;
 
                // Ideally this should happen earlier, somewhere. :P
                $this->addDefaultMeta();
@@ -2321,7 +2586,9 @@ class OutputPage {
                        $tags[] = Html::element( 'meta',
                                array(
                                        $a => $tag[0],
-                                       'content' => $tag[1] ) );
+                                       'content' => $tag[1]
+                               )
+                       );
                }
                foreach ( $this->mLinktags as $tag ) {
                        $tags[] = Html::element( 'link', $tag );
@@ -2338,7 +2605,8 @@ class OutputPage {
                                        $format,
                                        $link,
                                        # Used messages: 'page-rss-feed' and 'page-atom-feed' (for an easier grep)
-                                       wfMsg( "page-{$format}-feed", $this->getTitle()->getPrefixedText() ) );
+                                       wfMsg( "page-{$format}-feed", $this->getTitle()->getPrefixedText() )
+                               );
                        }
 
                        # Recent changes feed should appear on every page (except recentchanges,
@@ -2354,17 +2622,19 @@ class OutputPage {
 
                        if ( $wgOverrideSiteFeed ) {
                                foreach ( $wgOverrideSiteFeed as $type => $feedUrl ) {
-                                       $tags[] = $this->feedLink (
+                                       $tags[] = $this->feedLink(
                                                $type,
                                                htmlspecialchars( $feedUrl ),
-                                               wfMsg( "site-{$type}-feed", $wgSitename ) );
+                                               wfMsg( "site-{$type}-feed", $wgSitename )
+                                       );
                                }
                        } elseif ( $this->getTitle()->getPrefixedText() != $rctitle->getPrefixedText() ) {
                                foreach ( $wgAdvertisedFeedTypes as $format ) {
                                        $tags[] = $this->feedLink(
                                                $format,
                                                $rctitle->getLocalURL( "feed={$format}" ),
-                                               wfMsg( "site-{$format}-feed", $wgSitename ) ); # For grep: 'site-rss-feed', 'site-atom-feed'.
+                                               wfMsg( "site-{$format}-feed", $wgSitename ) # For grep: 'site-rss-feed', 'site-atom-feed'.
+                                       );
                                }
                        }
                }
@@ -2385,7 +2655,8 @@ class OutputPage {
                        'rel' => 'alternate',
                        'type' => "application/$type+xml",
                        'title' => $text,
-                       'href' => $url ) );
+                       'href' => $url )
+               );
        }
 
        /**
@@ -2397,16 +2668,19 @@ class OutputPage {
         * @param $condition String: for IE conditional comments, specifying an IE version
         * @param $dir String: set to 'rtl' or 'ltr' for direction-specific sheets
         */
-       public function addStyle( $style, $media='', $condition='', $dir='' ) {
+       public function addStyle( $style, $media = '', $condition = '', $dir = '' ) {
                $options = array();
                // Even though we expect the media type to be lowercase, but here we
                // force it to lowercase to be safe.
-               if( $media )
+               if( $media ) {
                        $options['media'] = $media;
-               if( $condition )
+               }
+               if( $condition ) {
                        $options['condition'] = $condition;
-               if( $dir )
+               }
+               if( $dir ) {
                        $options['dir'] = $dir;
+               }
                $this->styles[$style] = $options;
        }
 
@@ -2415,22 +2689,58 @@ class OutputPage {
         * @param $style_css Mixed: inline CSS
         */
        public function addInlineStyle( $style_css ){
-               $this->mScripts .= Html::inlineStyle( $style_css );
+               $this->mInlineStyles .= Html::inlineStyle( $style_css );
        }
 
        /**
         * Build a set of <link>s for the stylesheets specified in the $this->styles array.
         * These will be applied to various media & IE conditionals.
+        * @param $sk Skin object
         */
-       public function buildCssLinks() {
+       public function buildCssLinks( $sk ) {
+               $ret = '';
+               // Add ResourceLoader styles
+               // Split the styles into four groups
+               $styles = array( 'other' => array(), 'user' => array(), 'site' => array(), 'private' => array() );
+               $resourceLoader = $this->getResourceLoader();
+               foreach ( $this->getModuleStyles() as $name ) {
+                       $group = $resourceLoader->getModule( $name )->getGroup();
+                       // Modules in groups named "other" or anything different than "user", "site" or "private"
+                       // will be placed in the "other" group
+                       $styles[isset( $styles[$group] ) ? $group : 'other'][] = $name;
+               }
+
+               // We want site, private and user styles to override dynamically added styles from modules, but we want
+               // dynamically added styles to override statically added styles from other modules. So the order
+               // has to be other, dynamic, site, private, user
+               // Add statically added styles for other modules
+               $ret .= $this->makeResourceLoaderLink( $sk, $styles['other'], 'styles' );
+               // Add normal styles added through addStyle()/addInlineStyle() here
+               $ret .= implode( "\n", $this->buildCssLinksArray() ) . $this->mInlineStyles;
+               // Add marker tag to mark the place where the client-side loader should inject dynamic styles
+               // We use a <meta> tag with a made-up name for this because that's valid HTML
+               $ret .= Html::element( 'meta', array( 'name' => 'ResourceLoaderDynamicStyles', 'content' => '' ) );
+               
+               // Add site, private and user styles
+               // 'private' at present only contains user.options, so put that before 'user'
+               // Any future private modules will likely have a similar user-specific character
+               foreach ( array( 'site', 'private', 'user' ) as $group ) {
+                       $ret .= $this->makeResourceLoaderLink(
+                               $sk, array_merge( $styles['site'], $styles['user'] ), 'styles'
+                       );
+               }
+               return $ret;
+       }
+
+       public function buildCssLinksArray() {
                $links = array();
                foreach( $this->styles as $file => $options ) {
                        $link = $this->styleLink( $file, $options );
-                       if( $link )
-                               $links[] = $link;
+                       if( $link ) {
+                               $links[$file] = $link;
+                       }
                }
-
-               return implode( "\n", $links );
+               return $links;
        }
 
        /**
@@ -2442,17 +2752,15 @@ class OutputPage {
         * @return String: HTML fragment
         */
        protected function styleLink( $style, $options ) {
-               global $wgRequest;
-
                if( isset( $options['dir'] ) ) {
-                       global $wgContLang;
-                       $siteDir = $wgContLang->getDir();
-                       if( $siteDir != $options['dir'] )
+                       $siteDir = wfUILang()->getDir();
+                       if( $siteDir != $options['dir'] ) {
                                return '';
+                       }
                }
 
                if( isset( $options['media'] ) ) {
-                       $media = $this->transformCssMedia( $options['media'] );
+                       $media = self::transformCssMedia( $options['media'] );
                        if( is_null( $media ) ) {
                                return '';
                        }
@@ -2484,7 +2792,7 @@ class OutputPage {
         * @param $media String: current value of the "media" attribute
         * @return String: modified value of the "media" attribute
         */
-       function transformCssMedia( $media ) {
+       public static function transformCssMedia( $media ) {
                global $wgRequest, $wgHandheldForIPhone;
 
                // Switch in on-screen display for media testing
@@ -2522,13 +2830,13 @@ class OutputPage {
         * for when rate limiting has triggered.
         */
        public function rateLimited() {
-               $this->setPageTitle(wfMsg('actionthrottled'));
+               $this->setPageTitle( wfMsg( 'actionthrottled' ) );
                $this->setRobotPolicy( 'noindex,follow' );
                $this->setArticleRelated( false );
                $this->enableClientCache( false );
                $this->mRedirect = '';
                $this->clearHTML();
-               $this->setStatusCode(503);
+               $this->setStatusCode( 503 );
                $this->addWikiMsg( 'actionthrottledtext' );
 
                $this->returnToMain( null, $this->getTitle() );
@@ -2595,11 +2903,11 @@ class OutputPage {
         *
         * For example:
         *
-        *    $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>", 'some-error' );
+        *    $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>", 'some-error' );
         *
         * Is equivalent to:
         *
-        *    $wgOut->addWikiText( "<div class='error'>\n" . wfMsgNoTrans( 'some-error' ) . "</div>" );
+        *    $wgOut->addWikiText( "<div class='error'>\n" . wfMsgNoTrans( 'some-error' ) . "\n</div>" );
         *
         * The newline after opening div is needed in some wikitext. See bug 19226.
         */
@@ -2628,26 +2936,15 @@ class OutputPage {
 
        /**
         * Include jQuery core. Use this to avoid loading it multiple times
-        * before we get a usable script loader. 
+        * before we get a usable script loader.
         *
         * @param $modules Array: list of jQuery modules which should be loaded
         * @return Array: the list of modules which were not loaded.
+        * @since 1.16
+        * @deprecated No longer needed as of 1.17
         */
        public function includeJQuery( $modules = array() ) {
-               global $wgStylePath, $wgStyleVersion, $wgJsMimeType;
-
-               $supportedModules = array( /** TODO: add things here */ );
-               $unsupported = array_diff( $modules, $supportedModules );
-
-               $params = array(
-                       'type' => $wgJsMimeType,
-                       'src' => "$wgStylePath/common/jquery.min.js?$wgStyleVersion",
-               );
-               if ( !$this->mJQueryDone ) {
-                       $this->mJQueryDone = true;
-                       $this->mScripts = Html::element( 'script', $params ) . "\n" . $this->mScripts;
-               }
-               return $unsupported;
+               return array();
        }
 
 }