MediaWiki  REL1_19
ResourceLoaderWikiModule.php
Go to the documentation of this file.
00001 <?php
00023 defined( 'MEDIAWIKI' ) || die( 1 );
00024 
00032 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
00033 
00034         /* Protected Members */
00035 
00036         # Origin is user-supplied code
00037         protected $origin = self::ORIGIN_USER_SITEWIDE;
00038 
00039         // In-object cache for title mtimes
00040         protected $titleMtimes = array();
00041 
00042         /* Abstract Protected Methods */
00043 
00048         abstract protected function getPages( ResourceLoaderContext $context );
00049 
00050         /* Protected Methods */
00051 
00063         protected function getDB() {
00064                 return wfGetDB( DB_SLAVE );
00065         }
00066 
00071         protected function getContent( $title ) {
00072                 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
00073                         $message = wfMessage( $title->getDBkey() )->inContentLanguage();
00074                         return $message->exists() ? $message->plain() : '';
00075                 }
00076                 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
00077                         return null;
00078                 }
00079                 $revision = Revision::newFromTitle( $title );
00080                 if ( !$revision ) {
00081                         return null;
00082                 }
00083                 return $revision->getRawText();
00084         }
00085 
00086         /* Methods */
00087 
00092         public function getScript( ResourceLoaderContext $context ) {
00093                 $scripts = '';
00094                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00095                         if ( $options['type'] !== 'script' ) {
00096                                 continue;
00097                         }
00098                         $title = Title::newFromText( $titleText );
00099                         if ( !$title || $title->isRedirect() ) {
00100                                 continue;
00101                         }
00102                         $script = $this->getContent( $title );
00103                         if ( strval( $script ) !== '' ) {
00104                                 $script = $this->validateScriptFile( $titleText, $script );
00105                                 if ( strpos( $titleText, '*/' ) === false ) {
00106                                         $scripts .=  "/* $titleText */\n";
00107                                 }
00108                                 $scripts .= $script . "\n";
00109                         }
00110                 }
00111                 return $scripts;
00112         }
00113 
00118         public function getStyles( ResourceLoaderContext $context ) {
00119                 global $wgScriptPath;
00120 
00121                 $styles = array();
00122                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00123                         if ( $options['type'] !== 'style' ) {
00124                                 continue;
00125                         }
00126                         $title = Title::newFromText( $titleText );
00127                         if ( !$title || $title->isRedirect()  ) {
00128                                 continue;
00129                         }
00130                         $media = isset( $options['media'] ) ? $options['media'] : 'all';
00131                         $style = $this->getContent( $title );
00132                         if ( strval( $style ) === '' ) {
00133                                 continue;
00134                         }
00135                         if ( $this->getFlip( $context ) ) {
00136                                 $style = CSSJanus::transform( $style, true, false );
00137                         }
00138                         $style = CSSMin::remap( $style, false, $wgScriptPath, true );
00139                         if ( !isset( $styles[$media] ) ) {
00140                                 $styles[$media] = '';
00141                         }
00142                         if ( strpos( $titleText, '*/' ) === false ) {
00143                                 $styles[$media] .=  "/* $titleText */\n";
00144                         }
00145                         $styles[$media] .= $style . "\n";
00146                 }
00147                 return $styles;
00148         }
00149 
00154         public function getModifiedTime( ResourceLoaderContext $context ) {
00155                 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
00156                 $mtimes = $this->getTitleMtimes( $context );
00157                 if ( count( $mtimes ) ) {
00158                         $modifiedTime = max( $modifiedTime, max( $mtimes ) );
00159                 }
00160                 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
00161                 return $modifiedTime;
00162         }
00163 
00168         public function isKnownEmpty( ResourceLoaderContext $context ) {
00169                 return count( $this->getTitleMtimes( $context ) ) == 0;
00170         }
00171 
00178         protected function getTitleMtimes( ResourceLoaderContext $context ) {
00179                 $dbr = $this->getDB();
00180                 if ( !$dbr ) {
00181                         // We're dealing with a subclass that doesn't have a DB
00182                         return array();
00183                 }
00184                 
00185                 $hash = $context->getHash();
00186                 if ( isset( $this->titleMtimes[$hash] ) ) {
00187                         return $this->titleMtimes[$hash];
00188                 }
00189 
00190                 $this->titleMtimes[$hash] = array();
00191                 $batch = new LinkBatch;
00192                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00193                         $batch->addObj( Title::newFromText( $titleText ) );
00194                 }
00195 
00196                 if ( !$batch->isEmpty() ) {
00197                         $res = $dbr->select( 'page',
00198                                 array( 'page_namespace', 'page_title', 'page_touched' ),
00199                                 $batch->constructSet( 'page', $dbr ),
00200                                 __METHOD__
00201                         );
00202                         foreach ( $res as $row ) {
00203                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00204                                 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
00205                                         wfTimestamp( TS_UNIX, $row->page_touched );
00206                         }
00207                 }
00208                 return $this->titleMtimes[$hash];
00209         }
00210 }