From ed054b1ace36a7fefeb8e1ad8413d274ce149c06 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Sat, 11 Sep 2010 07:33:16 +0000 Subject: [PATCH] Fixed user scripts/styles and site scripts/styles - they were totally broken in r72772 - but now the refactoring is paying off. --- includes/OutputPage.php | 10 +---- includes/ResourceLoaderContext.php | 2 +- includes/ResourceLoaderModule.php | 65 +++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c22882bf9b..b86cb6ca7a 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2360,7 +2360,6 @@ class OutputPage { ); } - // TODO: User Scripts should be included using the resource loader // Add user JS if enabled if( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) { $action = $wgRequest->getVal( 'action', 'view' ); @@ -2368,14 +2367,7 @@ class OutputPage { # XXX: additional security check/prompt? $this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) ); } else { - $userpage = $wgUser->getUserPage(); - foreach( array( 'common', $sk->getSkinName() ) 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, 'user', 'scripts' ); } } $scripts .= "\n" . $this->mScripts; diff --git a/includes/ResourceLoaderContext.php b/includes/ResourceLoaderContext.php index 82ed6fd836..ff0e810c1b 100644 --- a/includes/ResourceLoaderContext.php +++ b/includes/ResourceLoaderContext.php @@ -86,7 +86,7 @@ class ResourceLoaderContext { } public function getUser() { - return $this->skin; + return $this->user; } public function getDebug() { diff --git a/includes/ResourceLoaderModule.php b/includes/ResourceLoaderModule.php index 2020e57e3d..8184dbbfda 100644 --- a/includes/ResourceLoaderModule.php +++ b/includes/ResourceLoaderModule.php @@ -703,29 +703,51 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { abstract protected function getPages( ResourceLoaderContext $context ); + /* Protected Methods */ + + protected function getContent( $page, $ns ) { + if ( $ns === NS_MEDIAWIKI ) { + return wfMsgExt( $page, 'content' ); + } + if ( $title = Title::newFromText( $page, $ns ) ) { + if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) { + return $revision->getRawText(); + } + } + return null; + } + /* Methods */ public function getScript( ResourceLoaderContext $context ) { + global $wgCanonicalNamespaceNames; + $scripts = ''; foreach ( $this->getPages( $context ) as $page => $options ) { if ( $options['type'] === 'script' ) { - $script = wfMsgExt( $page, 'content' ); - $scripts .= "/* MediaWiki:$page */\n" . ( !wfEmptyMsg( $page, $script ) ? $script : '' ) . "\n"; + if ( $script = $this->getContent( $page, $options['ns'] ) ) { + $ns = $wgCanonicalNamespaceNames[$options['ns']]; + $scripts .= "/*$ns:$page */\n$script\n"; + } } } return $scripts; } public function getStyles( ResourceLoaderContext $context ) { + global $wgCanonicalNamespaceNames; + $styles = array(); foreach ( $this->getPages( $context ) as $page => $options ) { if ( $options['type'] === 'style' ) { $media = isset( $options['media'] ) ? $options['media'] : 'all'; - $style = wfMsgExt( $page, 'content' ); - if ( !isset( $styles[$media] ) ) { - $styles[$media] = ''; + if ( $style = $this->getContent( $page, $options['ns'] ) ) { + if ( !isset( $styles[$media] ) ) { + $styles[$media] = ''; + } + $ns = $wgCanonicalNamespaceNames[$options['ns']]; + $styles[$media] .= "/* $ns:$page */\n$style\n"; } - $styles[$media] .= "/* MediaWiki:$page */\n" . ( !wfEmptyMsg( $page, $style ) ? $style : '' ) . "\n"; } } return $styles; @@ -738,7 +760,7 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { } $titles = array(); foreach ( $this->getPages( $context ) as $page => $options ) { - $titles[] = Title::makeTitle( NS_MEDIAWIKI, $page ); + $titles[] = Title::makeTitle( $options['ns'], $page ); } // Do batch existence check // TODO: This would work better if page_touched were loaded by this as well @@ -765,14 +787,14 @@ class ResourceLoaderSiteModule extends ResourceLoaderWikiModule { global $wgHandheldStyle; $pages = array( - 'Common.js' => array( 'type' => 'script' ), - 'Common.css' => array( 'type' => 'style' ), - ucfirst( $context->getSkin() ) . '.js' => array( 'type' => 'script' ), - ucfirst( $context->getSkin() ) . '.css' => array( 'type' => 'style' ), - 'Print.css' => array( 'type' => 'style', 'media' => 'print' ), + 'Common.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ), + 'Common.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ), + ucfirst( $context->getSkin() ) . '.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ), + ucfirst( $context->getSkin() ) . '.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ), + 'Print.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'print' ), ); if ( $wgHandheldStyle ) { - $pages['Handheld.css'] = array( 'type' => 'style', 'media' => 'handheld' ); + $pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'handheld' ); } return $pages; } @@ -789,11 +811,12 @@ class ResourceLoaderUserModule extends ResourceLoaderWikiModule { global $wgAllowUserCss; if ( $context->getUser() && $wgAllowUserCss ) { - $user = User::newFromName( $context->getUser() ); - $userPage = $user->getUserPage()->getPrefixedText(); + $username = $context->getUser(); return array( - "$userPage/common.css" => array( 'type' => 'style' ), - "$userPage/" . $context->getSkin() . '.css' => array( 'type' => 'style' ), + "$username/common.js" => array( 'ns' => NS_USER, 'type' => 'script' ), + "$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER, 'type' => 'script' ), + "$username/common.css" => array( 'ns' => NS_USER, 'type' => 'style' ), + "$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER, 'type' => 'style' ), ); } return array(); @@ -816,8 +839,12 @@ class ResourceLoaderUserPreferencesModule extends ResourceLoaderModule { if ( isset( $this->modifiedTime[$hash] ) ) { return $this->modifiedTime[$hash]; } - $user = User::newFromName( $context->getUser() ); - return $this->modifiedTime[$hash] = $user->getTouched(); + if ( $context->getUser() ) { + $user = User::newFromName( $context->getUser() ); + return $this->modifiedTime[$hash] = $user->getTouched(); + } else { + return 0; + } } public function getStyles( ResourceLoaderContext $context ) { -- 2.20.1