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
21 * @author Trevor Parscal
22 * @author Roan Kattouw
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 class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
34 // Origin defaults to users with sitewide authority
35 protected $origin = self
::ORIGIN_USER_SITEWIDE
;
37 // In-object cache for title info
38 protected $titleInfo = array();
40 // List of page names that contain CSS
41 protected $styles = array();
43 // List of page names that contain JavaScript
44 protected $scripts = array();
50 * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
52 public function __construct( array $options = null ) {
53 if ( isset( $options['styles'] ) ) {
54 $this->styles
= $options['styles'];
56 if ( isset( $options['scripts'] ) ) {
57 $this->scripts
= $options['scripts'];
59 if ( isset( $options['group'] ) ) {
60 $this->group
= $options['group'];
65 * Subclasses should return an associative array of resources in the module.
66 * Keys should be the title of a page in the MediaWiki or User namespace.
68 * Values should be a nested array of options. The supported keys are 'type' and
71 * For scripts, 'type' should be 'script'.
73 * For stylesheets, 'type' should be 'style'.
74 * There is an optional media key, the value of which can be the
75 * medium ('screen', 'print', etc.) of the stylesheet.
77 * @param ResourceLoaderContext $context
80 protected function getPages( ResourceLoaderContext
$context ) {
81 $config = $this->getConfig();
84 // Filter out pages from origins not allowed by the current wiki configuration.
85 if ( $config->get( 'UseSiteJs' ) ) {
86 foreach ( $this->scripts
as $script ) {
87 $pages[$script] = array( 'type' => 'script' );
91 if ( $config->get( 'UseSiteCss' ) ) {
92 foreach ( $this->styles
as $style ) {
93 $pages[$style] = array( 'type' => 'style' );
105 public function getGroup() {
110 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
111 * but subclasses may want to override this to return a remote DB object, or to return
112 * null if getTitleMTimes() shouldn't access the DB at all.
114 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
115 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
116 * will use the local DB irrespective of the return value of this method.
118 * @return IDatabase|null
120 protected function getDB() {
121 return wfGetDB( DB_SLAVE
);
125 * @param Title $title
126 * @return null|string
128 protected function getContent( $title ) {
129 $handler = ContentHandler
::getForTitle( $title );
130 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
131 $format = CONTENT_FORMAT_CSS
;
132 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
133 $format = CONTENT_FORMAT_JAVASCRIPT
;
138 $revision = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
143 $content = $revision->getContent( Revision
::RAW
);
146 wfDebugLog( 'resourceloader', __METHOD__
. ': failed to load content of JS/CSS page!' );
150 return $content->serialize( $format );
154 * @param ResourceLoaderContext $context
157 public function getScript( ResourceLoaderContext
$context ) {
159 foreach ( $this->getPages( $context ) as $titleText => $options ) {
160 if ( $options['type'] !== 'script' ) {
163 $title = Title
::newFromText( $titleText );
164 if ( !$title ||
$title->isRedirect() ) {
167 $script = $this->getContent( $title );
168 if ( strval( $script ) !== '' ) {
169 $script = $this->validateScriptFile( $titleText, $script );
170 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
177 * @param ResourceLoaderContext $context
180 public function getStyles( ResourceLoaderContext
$context ) {
182 foreach ( $this->getPages( $context ) as $titleText => $options ) {
183 if ( $options['type'] !== 'style' ) {
186 $title = Title
::newFromText( $titleText );
187 if ( !$title ||
$title->isRedirect() ) {
190 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
191 $style = $this->getContent( $title );
192 if ( strval( $style ) === '' ) {
195 if ( $this->getFlip( $context ) ) {
196 $style = CSSJanus
::transform( $style, true, false );
198 $style = CSSMin
::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true );
199 if ( !isset( $styles[$media] ) ) {
200 $styles[$media] = array();
202 $style = ResourceLoader
::makeComment( $titleText ) . $style;
203 $styles[$media][] = $style;
209 * @param ResourceLoaderContext $context
212 public function getModifiedTime( ResourceLoaderContext
$context ) {
214 $titleInfo = $this->getTitleInfo( $context );
215 if ( count( $titleInfo ) ) {
216 $mtimes = array_map( function ( $value ) {
217 return $value['timestamp'];
219 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
223 $this->getMsgBlobMtime( $context->getLanguage() ),
224 $this->getDefinitionMtime( $context )
226 return $modifiedTime;
230 * @param ResourceLoaderContext $context
233 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
234 $summary = parent
::getDefinitionSummary( $context );
236 'pages' => $this->getPages( $context ),
242 * @param ResourceLoaderContext $context
245 public function isKnownEmpty( ResourceLoaderContext
$context ) {
246 $titleInfo = $this->getTitleInfo( $context );
247 // Bug 68488: For modules in the "user" group, we should actually
248 // check that the pages are empty (page_len == 0), but for other
249 // groups, just check the pages exist so that we don't end up
250 // caching temporarily-blank pages without the appropriate
251 // <script> or <link> tag.
252 if ( $this->getGroup() !== 'user' ) {
253 return count( $titleInfo ) === 0;
256 foreach ( $titleInfo as $info ) {
257 if ( $info['length'] !== 0 ) {
258 // At least one non-0-lenth page, not empty
263 // All pages are 0-length, so it's empty
268 * Get the modification times of all titles that would be loaded for
270 * @param ResourceLoaderContext $context Context object
271 * @return array Keyed by page dbkey. Value is an array with 'length' and 'timestamp'
272 * keys, where the timestamp is a UNIX timestamp
274 protected function getTitleInfo( ResourceLoaderContext
$context ) {
275 $dbr = $this->getDB();
277 // We're dealing with a subclass that doesn't have a DB
281 $hash = $context->getHash();
282 if ( isset( $this->titleInfo
[$hash] ) ) {
283 return $this->titleInfo
[$hash];
286 $this->titleInfo
[$hash] = array();
287 $batch = new LinkBatch
;
288 foreach ( $this->getPages( $context ) as $titleText => $options ) {
289 $batch->addObj( Title
::newFromText( $titleText ) );
292 if ( !$batch->isEmpty() ) {
293 $res = $dbr->select( 'page',
294 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
295 $batch->constructSet( 'page', $dbr ),
298 foreach ( $res as $row ) {
299 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
300 $this->titleInfo
[$hash][$title->getPrefixedDBkey()] = array(
301 'timestamp' => wfTimestamp( TS_UNIX
, $row->page_touched
),
302 'length' => $row->page_len
,
306 return $this->titleInfo
[$hash];