MWException: Don't send headers multiple times
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blobbfb3c422a2f7eb48b5f8f59720e7b919c4e3bbf6
1 <?php
2 /**
3 * Abstraction for resource loader modules which pull from wiki pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
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 * Subclasses should return an associative array of resources in the module.
46 * Keys should be the title of a page in the MediaWiki or User namespace.
48 * Values should be a nested array of options. The supported keys are 'type' and
49 * (CSS only) 'media'.
51 * For scripts, 'type' should be 'script'.
53 * For stylesheets, 'type' should be 'style'.
54 * There is an optional media key, the value of which can be the
55 * medium ('screen', 'print', etc.) of the stylesheet.
57 * @param ResourceLoaderContext $context
58 * @return array
60 abstract protected function getPages( ResourceLoaderContext $context );
62 /* Protected Methods */
64 /**
65 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
66 * but subclasses may want to override this to return a remote DB object, or to return
67 * null if getTitleMTimes() shouldn't access the DB at all.
69 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
70 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
71 * will use the local DB irrespective of the return value of this method.
73 * @return DatabaseBase|null
75 protected function getDB() {
76 return wfGetDB( DB_SLAVE );
79 /**
80 * @param Title $title
81 * @return null|string
83 protected function getContent( $title ) {
84 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
85 return null;
87 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
88 if ( !$revision ) {
89 return null;
92 $content = $revision->getContent( Revision::RAW );
94 if ( !$content ) {
95 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
96 return null;
99 $model = $content->getModel();
101 if ( $model !== CONTENT_MODEL_CSS && $model !== CONTENT_MODEL_JAVASCRIPT ) {
102 wfDebugLog( 'resourceloader', __METHOD__ . ': bad content model $model for JS/CSS page!' );
103 return null;
106 return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS
109 /* Methods */
112 * @param ResourceLoaderContext $context
113 * @return string
115 public function getScript( ResourceLoaderContext $context ) {
116 $scripts = '';
117 foreach ( $this->getPages( $context ) as $titleText => $options ) {
118 if ( $options['type'] !== 'script' ) {
119 continue;
121 $title = Title::newFromText( $titleText );
122 if ( !$title || $title->isRedirect() ) {
123 continue;
125 $script = $this->getContent( $title );
126 if ( strval( $script ) !== '' ) {
127 $script = $this->validateScriptFile( $titleText, $script );
128 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
131 return $scripts;
135 * @param ResourceLoaderContext $context
136 * @return array
138 public function getStyles( ResourceLoaderContext $context ) {
139 global $wgScriptPath;
141 $styles = array();
142 foreach ( $this->getPages( $context ) as $titleText => $options ) {
143 if ( $options['type'] !== 'style' ) {
144 continue;
146 $title = Title::newFromText( $titleText );
147 if ( !$title || $title->isRedirect() ) {
148 continue;
150 $media = isset( $options['media'] ) ? $options['media'] : 'all';
151 $style = $this->getContent( $title );
152 if ( strval( $style ) === '' ) {
153 continue;
155 if ( $this->getFlip( $context ) ) {
156 $style = CSSJanus::transform( $style, true, false );
158 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
159 if ( !isset( $styles[$media] ) ) {
160 $styles[$media] = array();
162 $style = ResourceLoader::makeComment( $titleText ) . $style;
163 $styles[$media][] = $style;
165 return $styles;
169 * @param ResourceLoaderContext $context
170 * @return int|mixed
172 public function getModifiedTime( ResourceLoaderContext $context ) {
173 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
174 $mtimes = $this->getTitleMtimes( $context );
175 if ( count( $mtimes ) ) {
176 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
178 $modifiedTime = max(
179 $modifiedTime,
180 $this->getMsgBlobMtime( $context->getLanguage() ),
181 $this->getDefinitionMtime( $context )
183 return $modifiedTime;
187 * Get the definition summary for this module.
189 * @return array
191 public function getDefinitionSummary( ResourceLoaderContext $context ) {
192 return array(
193 'class' => get_class( $this ),
194 'pages' => $this->getPages( $context ),
199 * @param ResourceLoaderContext $context
200 * @return bool
202 public function isKnownEmpty( ResourceLoaderContext $context ) {
203 return count( $this->getTitleMtimes( $context ) ) == 0;
207 * Get the modification times of all titles that would be loaded for
208 * a given context.
209 * @param ResourceLoaderContext $context Context object
210 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
212 protected function getTitleMtimes( ResourceLoaderContext $context ) {
213 $dbr = $this->getDB();
214 if ( !$dbr ) {
215 // We're dealing with a subclass that doesn't have a DB
216 return array();
219 $hash = $context->getHash();
220 if ( isset( $this->titleMtimes[$hash] ) ) {
221 return $this->titleMtimes[$hash];
224 $this->titleMtimes[$hash] = array();
225 $batch = new LinkBatch;
226 foreach ( $this->getPages( $context ) as $titleText => $options ) {
227 $batch->addObj( Title::newFromText( $titleText ) );
230 if ( !$batch->isEmpty() ) {
231 $res = $dbr->select( 'page',
232 array( 'page_namespace', 'page_title', 'page_touched' ),
233 $batch->constructSet( 'page', $dbr ),
234 __METHOD__
236 foreach ( $res as $row ) {
237 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
238 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
239 wfTimestamp( TS_UNIX, $row->page_touched );
242 return $this->titleMtimes[$hash];