Revert r106545 and pass a null variable by ref (also updated the documentation) so...
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blobb13753119f24cdb116072c33ff159e14d3da2a2e
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) || die( 1 );
25 /**
26 * Abstraction for resource loader modules which pull from wiki pages
28 * This can only be used for wiki pages in the MediaWiki and User namespaces,
29 * because of its dependence on the functionality of
30 * Title::isCssJsSubpage.
32 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
34 /* Protected Members */
36 # Origin is user-supplied code
37 protected $origin = self::ORIGIN_USER_SITEWIDE;
39 // In-object cache for title mtimes
40 protected $titleMtimes = array();
42 /* Abstract Protected Methods */
44 /**
45 * @abstract
46 * @param $context ResourceLoaderContext
48 abstract protected function getPages( ResourceLoaderContext $context );
50 /* Protected Methods */
52 /**
53 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
54 * but subclasses may want to override this to return a remote DB object, or to return
55 * null if getTitleMTimes() shouldn't access the DB at all.
57 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
58 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
59 * will use the local DB irrespective of the return value of this method.
61 * @return DatabaseBase|null
63 protected function getDB() {
64 return wfGetDB( DB_SLAVE );
67 /**
68 * @param $title Title
69 * @return null|string
71 protected function getContent( $title ) {
72 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
73 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
74 return $message->exists() ? $message->plain() : '';
76 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
77 return null;
79 $revision = Revision::newFromTitle( $title );
80 if ( !$revision ) {
81 return null;
83 return $revision->getRawText();
86 /* Methods */
88 /**
89 * @param $context ResourceLoaderContext
90 * @return string
92 public function getScript( ResourceLoaderContext $context ) {
93 $scripts = '';
94 foreach ( $this->getPages( $context ) as $titleText => $options ) {
95 if ( $options['type'] !== 'script' ) {
96 continue;
98 $title = Title::newFromText( $titleText );
99 if ( !$title || $title->isRedirect() ) {
100 continue;
102 $script = $this->getContent( $title );
103 if ( strval( $script ) !== '' ) {
104 $script = $this->validateScriptFile( $titleText, $script );
105 if ( strpos( $titleText, '*/' ) === false ) {
106 $scripts .= "/* $titleText */\n";
108 $scripts .= $script . "\n";
111 return $scripts;
115 * @param $context ResourceLoaderContext
116 * @return array
118 public function getStyles( ResourceLoaderContext $context ) {
119 global $wgScriptPath;
121 $styles = array();
122 foreach ( $this->getPages( $context ) as $titleText => $options ) {
123 if ( $options['type'] !== 'style' ) {
124 continue;
126 $title = Title::newFromText( $titleText );
127 if ( !$title || $title->isRedirect() ) {
128 continue;
130 $media = isset( $options['media'] ) ? $options['media'] : 'all';
131 $style = $this->getContent( $title );
132 if ( strval( $style ) === '' ) {
133 continue;
135 if ( $this->getFlip( $context ) ) {
136 $style = CSSJanus::transform( $style, true, false );
138 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
139 if ( !isset( $styles[$media] ) ) {
140 $styles[$media] = '';
142 if ( strpos( $titleText, '*/' ) === false ) {
143 $styles[$media] .= "/* $titleText */\n";
145 $styles[$media] .= $style . "\n";
147 return $styles;
151 * @param $context ResourceLoaderContext
152 * @return int|mixed
154 public function getModifiedTime( ResourceLoaderContext $context ) {
155 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
156 $mtimes = $this->getTitleMtimes( $context );
157 if ( count( $mtimes ) ) {
158 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
160 return $modifiedTime;
164 * @param $context ResourceLoaderContext
165 * @return bool
167 public function isKnownEmpty( ResourceLoaderContext $context ) {
168 return count( $this->getTitleMtimes( $context ) ) == 0;
172 * Get the modification times of all titles that would be loaded for
173 * a given context.
174 * @param $context ResourceLoaderContext: Context object
175 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
177 protected function getTitleMtimes( ResourceLoaderContext $context ) {
178 $dbr = $this->getDB();
179 if ( !$dbr ) {
180 // We're dealing with a subclass that doesn't have a DB
181 return array();
184 $hash = $context->getHash();
185 if ( isset( $this->titleMtimes[$hash] ) ) {
186 return $this->titleMtimes[$hash];
189 $this->titleMtimes[$hash] = array();
190 $batch = new LinkBatch;
191 foreach ( $this->getPages( $context ) as $titleText => $options ) {
192 $batch->addObj( Title::newFromText( $titleText ) );
195 if ( !$batch->isEmpty() ) {
196 $res = $dbr->select( 'page',
197 array( 'page_namespace', 'page_title', 'page_touched' ),
198 $batch->constructSet( 'page', $dbr ),
199 __METHOD__
201 foreach ( $res as $row ) {
202 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
203 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
204 wfTimestamp( TS_UNIX, $row->page_touched );
207 return $this->titleMtimes[$hash];