var $mCategoryLinks = array(), $mCategories = array(), $mLanguageLinks = array();
var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array();
+ var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array();
var $mInlineMsg = array();
var $mTemplateIds = array();
*/
public function addScriptFile( $file, $version = null ) {
global $wgStylePath, $wgStyleVersion;
+ // 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 {
return $this->mScripts . $this->getHeadItems();
}
+ /**
+ * Get the list of modules to include on this page
+ *
+ * @return Array of module names
+ */
+ public function getModules() {
+ return $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 $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 $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 $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
*
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;
}
$popts, true, true, $this->mRevisionId
);
$popts->setTidy( false );
- if ( $cache && $article && !$parserOutput->isCacheable() ) {
+ if ( $cache && $article && $parserOutput->isCacheable() ) {
$parserCache = ParserCache::singleton();
$parserCache->save( $parserOutput, $article, $popts );
}
}
$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] ) ) {
*/
public function output() {
global $wgUser, $wgOutputEncoding, $wgRequest;
- global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType;
+ global $wgLanguageCode, $wgDebugRedirects, $wgMimeType;
global $wgUseAjax, $wgAjaxWatch;
global $wgEnableMWSuggest, $wgUniversalEditButton;
- global $wgArticle, $wgJQueryOnEveryPage;
+ global $wgArticle;
if( $this->mDoNothing ) {
return;
}
$sk = $wgUser->getSkin();
-
+
+ // Add base resources
+ $this->addModules( array( '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->includeJQuery();
- $this->addScriptFile( 'ajaxwatch.js' );
+ $this->addModules( 'mediawiki.legacy.ajaxwatch' );
}
if ( $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
- $this->addScriptFile( 'mwsuggest.js' );
+ $this->addModules( 'mediawiki.legacy.mwsuggest' );
}
}
if( $wgUser->getBoolOption( 'editsectiononrightclick' ) ) {
- $this->addScriptFile( 'rightclickedit.js' );
+ $this->addModules( 'mediawiki.legacy.rightclickedit' );
}
if( $wgUniversalEditButton ) {
}
}
- if ( $wgJQueryOnEveryPage ) {
- $this->includeJQuery();
- }
# 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 );
if ( $this->mArticleBodyOnly ) {
$this->out( $this->mBodytext );
if ( $action == null ) {
$text = wfMsgNoTrans( 'permissionserrorstext', count( $errors ) ) . "\n\n";
} else {
- global $wgLang;
$action_desc = wfMsgNoTrans( "action-$action" );
$text = wfMsgNoTrans(
'permissionserrorstext-withaction',
if ( !empty( $reasons ) ) {
// Permissions error
if( $source ) {
- $this->setPageTitle( wfMsg( 'viewsource' ) );
- $this->setSubtitle(
- wfMsg( 'viewsourcefor', $skin->linkKnown( $this->getTitle() ) )
- );
+ $title = $this->getTitle();
+ $link = $skin->linkKnown( $title );
+ $this->mPagetitle = wfMessage( 'viewsourceheader' )->rawParams( $link )->escaped();
+ $this->mHTMLtitle = wfMessage( 'viewsourcetitle', $title->getPrefixedText() )->escaped();
} else {
$this->setPageTitle( wfMsg( 'badaccess' ) );
}
}
}
+ /**
+ * Adds JS-based password security checker
+ * @param $passwordId String ID of input box containing password
+ * @param $retypeId String ID of input box containing retyped password
+ * @return none
+ */
+ public function addPasswordSecurity( $passwordId, $retypeId ) {
+ $this->includeJQuery();
+ $data = array(
+ 'password' => '#' . $passwordId,
+ 'retype' => '#' . $retypeId,
+ 'messages' => array(),
+ );
+ foreach ( array( 'password-strength', 'password-strength-bad', 'password-strength-mediocre',
+ 'password-strength-acceptable', 'password-strength-good', 'password-retype', 'password-retype-mismatch'
+ ) as $message ) {
+ $data['messages'][$message] = wfMsg( $message );
+ }
+ $this->addScript( Html::inlineScript( 'var passwordSecurity=' . FormatJson::encode( $data ) ) );
+ $this->addModules( 'mediawiki.legacy.password' );
+ }
+
/** @deprecated */
public function errorpage( $title, $msg ) {
wfDeprecated( __METHOD__ );
global $wgUser, $wgRequest, $wgLang;
if ( $sk->commonPrintStylesheet() ) {
- $this->addStyle( 'common/wikiprintable.css', 'print' );
+ $this->addModuleStyles( 'mediawiki.legacy.wikiprintable' );
}
$sk->setupUserCss( $this );
$ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
$ret .= implode( "\n", array(
- $this->getHeadLinks(),
+ $this->getHeadLinks( $sk ),
$this->buildCssLinks(),
- $this->getHeadScripts( $sk ) . $this->getHeadItems(),
+ $this->getHeadItems(),
) );
if ( $sk->usercss ) {
$ret .= Html::inlineStyle( $sk->usercss );
return $ret;
}
-
+
+ // TODO: Document
+ static function makeResourceLoaderLink( $skin, $modules, $only, $useESI = false ) {
+ global $wgUser, $wgLang, $wgRequest, $wgLoadScript, $wgResourceLoaderDebug, $wgResourceLoaderUseESI,
+ $wgResourceLoaderInlinePrivateModules;
+ // TODO: Should this be a static function of ResourceLoader instead?
+ // TODO: Divide off modules starting with "user", and add the user parameter to them
+ $query = array(
+ 'lang' => $wgLang->getCode(),
+ 'debug' => $wgRequest->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ? 'true' : 'false',
+ 'skin' => $wgUser->getSkin()->getSkinName(),
+ 'only' => $only,
+ );
+ // Remove duplicate module requests
+ $modules = array_unique( (array) $modules );
+ // Sort module names so requests are more uniform
+ sort( $modules );
+ // Create keyed-by-group list of module objects from modules list
+ $groups = array();
+ 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['modules'] = implode( '|', array_keys( $modules ) );
+ // Special handling for user-specific groups
+ if ( ( $group === 'user' || $group === 'private' ) && $wgUser->isLoggedIn() ) {
+ $query['user'] = $wgUser->getName();
+ }
+ // Support inlining of private modules if configured as such
+ if ( $group === 'private' && $wgResourceLoaderInlinePrivateModules ) {
+ $context = new ResourceLoaderContext( new FauxRequest( $query ) );
+ if ( $only == 'styles' ) {
+ $links .= Html::inlineStyle(
+ ResourceLoader::makeLoaderConditionalScript(
+ ResourceLoader::makeModuleResponse( $context, $modules )
+ )
+ );
+ } else {
+ $links .= Html::inlineScript(
+ ResourceLoader::makeLoaderConditionalScript(
+ ResourceLoader::makeModuleResponse( $context, $modules )
+ )
+ );
+ }
+ continue;
+ }
+ // Special handling for user and site groups; because users might change their stuff on-wiki like site or
+ // 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
+ if ( $group === 'user' || $group === 'site' ) {
+ // Create a fake request based on the one we are about to make so modules return correct times
+ $context = new ResourceLoaderContext( new FauxRequest( $query ) );
+ // Get the maximum timestamp
+ $timestamp = 0;
+ 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, round( $timestamp, -2 ) );
+ }
+ // Make queries uniform in order
+ ksort( $query );
+
+ $url = wfAppendQuery( $wgLoadScript, $query );
+ 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( wfAppendQuery( $wgLoadScript, $query ) ) . "\n";
+ } else {
+ $links .= Html::linkedScript( wfAppendQuery( $wgLoadScript, $query ) ) . "\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;
-
- $scripts = Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n";
- $scripts .= Html::linkedScript( "{$wgStylePath}/common/wikibits.js?$wgStyleVersion" );
-
- // 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() )
- )
- );
+ global $wgUser, $wgRequest, $wgUseSiteJs, $wgResourceLoaderDebug;
+
+ // Startup - this will immediately load jquery and mediawiki modules
+ $scripts = self::makeResourceLoaderLink( $sk, 'startup', 'scripts', true );
+
+ // 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"
+ if ( $wgRequest->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ) {
+ // Scripts
+ foreach ( $this->getModuleScripts() as $name ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, $name, 'scripts' );
+ }
+ // Messages
+ foreach ( $this->getModuleMessages() as $name ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, $name, 'messages' );
+ }
+ } else {
+ // Scripts
+ if ( count( $this->getModuleScripts() ) ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' );
+ }
+ // Messages
+ if ( count( $this->getModuleMessages() ) ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' );
+ }
}
-
- // add user JS if enabled
- if( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) {
+
+ // Modules - let the client calculate dependencies and batch requests as it likes
+ if ( $this->getModules() ) {
+ $modules = FormatJson::encode( $this->getModules() );
+ $scripts .= Html::inlineScript(
+ "if ( window.mediaWiki ) { mediaWiki.loader.load( {$modules} ); mediaWiki.loader.go(); }"
+ ) . "\n";
+ }
+
+ // 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() && $sk->userCanPreview( $action ) ) {
# XXX: additional security check/prompt?
$this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) );
} else {
- $userpage = $wgUser->getUserPage();
- $names = array( 'common', $sk->getSkinName() );
- foreach( $names as $name ) {
- $scriptpage = Title::makeTitleSafe(
- NS_USER,
- $userpage->getDBkey() . '/' . $name . '.js'
- );
- if ( $scriptpage && $scriptpage->exists() && ( $scriptpage->getLength() > 0 ) ) {
- $userjs = $scriptpage->getLocalURL( 'action=raw&ctype=' . $wgJsMimeType );
- $this->addScriptFile( $userjs, $scriptpage->getLatestRevID() );
- }
- }
+ $scripts .= self::makeResourceLoaderLink( $sk, array( 'user', 'user.options' ), 'scripts' );
+ $userOptionsAdded = true;
}
}
-
+ if ( !$userOptionsAdded ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, 'user.options', 'scripts' );
+ }
$scripts .= "\n" . $this->mScripts;
+
+ // Add site JS if enabled
+ if ( $wgUseSiteJs ) {
+ $scripts .= self::makeResourceLoaderLink( $sk, 'site', 'scripts' );
+ }
+
return $scripts;
}
/**
* @return string HTML tag links to be put in the header.
*/
- public function getHeadLinks() {
- global $wgFeed;
+ public function getHeadLinks( $sk ) {
+ global $wgFeed, $wgRequest, $wgResourceLoaderDebug;
// Ideally this should happen earlier, somewhere. :P
$this->addDefaultMeta();
}
}
+ // Support individual script requests in debug mode
+ if ( $wgRequest->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ) {
+ foreach ( $this->getModuleStyles() as $name ) {
+ $tags[] = self::makeResourceLoaderLink( $sk, $name, 'styles' );
+ }
+ } else {
+ if ( count( $this->getModuleStyles() ) ) {
+ $tags[] = self::makeResourceLoaderLink( $sk, $this->getModuleStyles(), 'styles' );
+ }
+ }
+
return implode( "\n", $tags );
}
* @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, $wgJQueryVersion, $wgJQueryMinified;
-
- $supportedModules = array( /** TODO: add things here */ );
- $unsupported = array_diff( $modules, $supportedModules );
-
- $min = $wgJQueryMinified ? '.min' : '';
- $url = "$wgStylePath/common/jquery-$wgJQueryVersion$min.js?$wgStyleVersion";
- if ( !$this->mJQueryDone ) {
- $this->mJQueryDone = true;
- $this->mScripts = Html::linkedScript( $url ) . "\n" . $this->mScripts;
- }
- return $unsupported;
+ return array();
}
}