MediaWiki  REL1_20
ResourceLoaderWikiModule.php
Go to the documentation of this file.
00001 <?php
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 
00047         abstract protected function getPages( ResourceLoaderContext $context );
00048 
00049         /* Protected Methods */
00050 
00062         protected function getDB() {
00063                 return wfGetDB( DB_SLAVE );
00064         }
00065 
00070         protected function getContent( $title ) {
00071                 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
00072                         return null;
00073                 }
00074                 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
00075                 if ( !$revision ) {
00076                         return null;
00077                 }
00078                 return $revision->getRawText();
00079         }
00080 
00081         /* Methods */
00082 
00087         public function getScript( ResourceLoaderContext $context ) {
00088                 $scripts = '';
00089                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00090                         if ( $options['type'] !== 'script' ) {
00091                                 continue;
00092                         }
00093                         $title = Title::newFromText( $titleText );
00094                         if ( !$title || $title->isRedirect() ) {
00095                                 continue;
00096                         }
00097                         $script = $this->getContent( $title );
00098                         if ( strval( $script ) !== '' ) {
00099                                 $script = $this->validateScriptFile( $titleText, $script );
00100                                 if ( strpos( $titleText, '*/' ) === false ) {
00101                                         $scripts .=  "/* $titleText */\n";
00102                                 }
00103                                 $scripts .= $script . "\n";
00104                         }
00105                 }
00106                 return $scripts;
00107         }
00108 
00113         public function getStyles( ResourceLoaderContext $context ) {
00114                 global $wgScriptPath;
00115 
00116                 $styles = array();
00117                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00118                         if ( $options['type'] !== 'style' ) {
00119                                 continue;
00120                         }
00121                         $title = Title::newFromText( $titleText );
00122                         if ( !$title || $title->isRedirect()  ) {
00123                                 continue;
00124                         }
00125                         $media = isset( $options['media'] ) ? $options['media'] : 'all';
00126                         $style = $this->getContent( $title );
00127                         if ( strval( $style ) === '' ) {
00128                                 continue;
00129                         }
00130                         if ( $this->getFlip( $context ) ) {
00131                                 $style = CSSJanus::transform( $style, true, false );
00132                         }
00133                         $style = CSSMin::remap( $style, false, $wgScriptPath, true );
00134                         if ( !isset( $styles[$media] ) ) {
00135                                 $styles[$media] = array();
00136                         }
00137                         if ( strpos( $titleText, '*/' ) === false ) {
00138                                 $style =  "/* $titleText */\n" . $style;
00139                         }
00140                         $styles[$media][] = $style;
00141                 }
00142                 return $styles;
00143         }
00144 
00149         public function getModifiedTime( ResourceLoaderContext $context ) {
00150                 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
00151                 $mtimes = $this->getTitleMtimes( $context );
00152                 if ( count( $mtimes ) ) {
00153                         $modifiedTime = max( $modifiedTime, max( $mtimes ) );
00154                 }
00155                 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
00156                 return $modifiedTime;
00157         }
00158 
00163         public function isKnownEmpty( ResourceLoaderContext $context ) {
00164                 return count( $this->getTitleMtimes( $context ) ) == 0;
00165         }
00166 
00173         protected function getTitleMtimes( ResourceLoaderContext $context ) {
00174                 $dbr = $this->getDB();
00175                 if ( !$dbr ) {
00176                         // We're dealing with a subclass that doesn't have a DB
00177                         return array();
00178                 }
00179 
00180                 $hash = $context->getHash();
00181                 if ( isset( $this->titleMtimes[$hash] ) ) {
00182                         return $this->titleMtimes[$hash];
00183                 }
00184 
00185                 $this->titleMtimes[$hash] = array();
00186                 $batch = new LinkBatch;
00187                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
00188                         $batch->addObj( Title::newFromText( $titleText ) );
00189                 }
00190 
00191                 if ( !$batch->isEmpty() ) {
00192                         $res = $dbr->select( 'page',
00193                                 array( 'page_namespace', 'page_title', 'page_touched' ),
00194                                 $batch->constructSet( 'page', $dbr ),
00195                                 __METHOD__
00196                         );
00197                         foreach ( $res as $row ) {
00198                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00199                                 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
00200                                         wfTimestamp( TS_UNIX, $row->page_touched );
00201                         }
00202                 }
00203                 return $this->titleMtimes[$hash];
00204         }
00205 }