diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
commit | c9aa36da061816dee256a979c2ff8d2ee41824d9 (patch) | |
tree | 29f7002b80ee984b488bd047dbbd80b36bf892e9 /includes/resourceloader/ResourceLoaderWikiModule.php | |
parent | b4274e0e33eafb5e9ead9d949ebf031a9fb8363b (diff) | |
parent | d1ba966140d7a60cd5ae4e8667ceb27c1a138592 (diff) |
Merge branch 'archwiki'
# Conflicts:
# skins/ArchLinux.php
# skins/ArchLinux/archlogo.gif
Diffstat (limited to 'includes/resourceloader/ResourceLoaderWikiModule.php')
-rw-r--r-- | includes/resourceloader/ResourceLoaderWikiModule.php | 110 |
1 files changed, 72 insertions, 38 deletions
diff --git a/includes/resourceloader/ResourceLoaderWikiModule.php b/includes/resourceloader/ResourceLoaderWikiModule.php index 3f10ae53..de61fc55 100644 --- a/includes/resourceloader/ResourceLoaderWikiModule.php +++ b/includes/resourceloader/ResourceLoaderWikiModule.php @@ -36,8 +36,8 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { # Origin is user-supplied code protected $origin = self::ORIGIN_USER_SITEWIDE; - // In-object cache for title mtimes - protected $titleMtimes = array(); + // In-object cache for title info + protected $titleInfo = array(); /* Abstract Protected Methods */ @@ -54,7 +54,7 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { * There is an optional media key, the value of which can be the * medium ('screen', 'print', etc.) of the stylesheet. * - * @param $context ResourceLoaderContext + * @param ResourceLoaderContext $context * @return array */ abstract protected function getPages( ResourceLoaderContext $context ); @@ -77,7 +77,7 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { } /** - * @param $title Title + * @param Title $title * @return null|string */ protected function getContent( $title ) { @@ -96,20 +96,20 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { return null; } - $model = $content->getModel(); - - if ( $model !== CONTENT_MODEL_CSS && $model !== CONTENT_MODEL_JAVASCRIPT ) { - wfDebugLog( 'resourceloader', __METHOD__ . ': bad content model $model for JS/CSS page!' ); + if ( $content->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) { + return $content->serialize( CONTENT_FORMAT_JAVASCRIPT ); + } elseif ( $content->isSupportedFormat( CONTENT_FORMAT_CSS ) ) { + return $content->serialize( CONTENT_FORMAT_CSS ); + } else { + wfDebugLog( 'resourceloader', __METHOD__ . ": bad content model {$content->getModel()} for JS/CSS page!" ); return null; } - - return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS } /* Methods */ /** - * @param $context ResourceLoaderContext + * @param ResourceLoaderContext $context * @return string */ public function getScript( ResourceLoaderContext $context ) { @@ -125,22 +125,17 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { $script = $this->getContent( $title ); if ( strval( $script ) !== '' ) { $script = $this->validateScriptFile( $titleText, $script ); - if ( strpos( $titleText, '*/' ) === false ) { - $scripts .= "/* $titleText */\n"; - } - $scripts .= $script . "\n"; + $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n"; } } return $scripts; } /** - * @param $context ResourceLoaderContext + * @param ResourceLoaderContext $context * @return array */ public function getStyles( ResourceLoaderContext $context ) { - global $wgScriptPath; - $styles = array(); foreach ( $this->getPages( $context ) as $titleText => $options ) { if ( $options['type'] !== 'style' ) { @@ -158,47 +153,84 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { if ( $this->getFlip( $context ) ) { $style = CSSJanus::transform( $style, true, false ); } - $style = CSSMin::remap( $style, false, $wgScriptPath, true ); + $style = CSSMin::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true ); if ( !isset( $styles[$media] ) ) { $styles[$media] = array(); } - if ( strpos( $titleText, '*/' ) === false ) { - $style = "/* $titleText */\n" . $style; - } + $style = ResourceLoader::makeComment( $titleText ) . $style; $styles[$media][] = $style; } return $styles; } /** - * @param $context ResourceLoaderContext + * @param ResourceLoaderContext $context * @return int|mixed */ public function getModifiedTime( ResourceLoaderContext $context ) { $modifiedTime = 1; // wfTimestamp() interprets 0 as "now" - $mtimes = $this->getTitleMtimes( $context ); - if ( count( $mtimes ) ) { + $titleInfo = $this->getTitleInfo( $context ); + if ( count( $titleInfo ) ) { + $mtimes = array_map( function( $value ) { + return $value['timestamp']; + }, $titleInfo ); $modifiedTime = max( $modifiedTime, max( $mtimes ) ); } - $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) ); + $modifiedTime = max( + $modifiedTime, + $this->getMsgBlobMtime( $context->getLanguage() ), + $this->getDefinitionMtime( $context ) + ); return $modifiedTime; } /** - * @param $context ResourceLoaderContext + * Get the definition summary for this module. + * + * @param ResourceLoaderContext $context + * @return array + */ + public function getDefinitionSummary( ResourceLoaderContext $context ) { + return array( + 'class' => get_class( $this ), + 'pages' => $this->getPages( $context ), + ); + } + + /** + * @param ResourceLoaderContext $context * @return bool */ public function isKnownEmpty( ResourceLoaderContext $context ) { - return count( $this->getTitleMtimes( $context ) ) == 0; + $titleInfo = $this->getTitleInfo( $context ); + // Bug 68488: For modules in the "user" group, we should actually + // check that the pages are empty (page_len == 0), but for other + // groups, just check the pages exist so that we don't end up + // caching temporarily-blank pages without the appropriate + // <script> or <link> tag. + if ( $this->getGroup() !== 'user' ) { + return count( $titleInfo ) === 0; + } + + foreach ( $titleInfo as $info ) { + if ( $info['length'] !== 0 ) { + // At least one non-0-lenth page, not empty + return false; + } + } + + // All pages are 0-length, so it's empty + return true; } /** * Get the modification times of all titles that would be loaded for * a given context. - * @param $context ResourceLoaderContext: Context object - * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped + * @param ResourceLoaderContext $context Context object + * @return array keyed by page dbkey, with value is an array with 'length' and 'timestamp' + * keys, where the timestamp is a unix one */ - protected function getTitleMtimes( ResourceLoaderContext $context ) { + protected function getTitleInfo( ResourceLoaderContext $context ) { $dbr = $this->getDB(); if ( !$dbr ) { // We're dealing with a subclass that doesn't have a DB @@ -206,11 +238,11 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { } $hash = $context->getHash(); - if ( isset( $this->titleMtimes[$hash] ) ) { - return $this->titleMtimes[$hash]; + if ( isset( $this->titleInfo[$hash] ) ) { + return $this->titleInfo[$hash]; } - $this->titleMtimes[$hash] = array(); + $this->titleInfo[$hash] = array(); $batch = new LinkBatch; foreach ( $this->getPages( $context ) as $titleText => $options ) { $batch->addObj( Title::newFromText( $titleText ) ); @@ -218,16 +250,18 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule { if ( !$batch->isEmpty() ) { $res = $dbr->select( 'page', - array( 'page_namespace', 'page_title', 'page_touched' ), + array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ), $batch->constructSet( 'page', $dbr ), __METHOD__ ); foreach ( $res as $row ) { $title = Title::makeTitle( $row->page_namespace, $row->page_title ); - $this->titleMtimes[$hash][$title->getPrefixedDBkey()] = - wfTimestamp( TS_UNIX, $row->page_touched ); + $this->titleInfo[$hash][$title->getPrefixedDBkey()] = array( + 'timestamp' => wfTimestamp( TS_UNIX, $row->page_touched ), + 'length' => $row->page_len, + ); } } - return $this->titleMtimes[$hash]; + return $this->titleInfo[$hash]; } } |