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
{
33 /** @var string Position on the page to load this module at */
34 protected $position = 'bottom';
36 // Origin defaults to users with sitewide authority
37 protected $origin = self
::ORIGIN_USER_SITEWIDE
;
39 // In-object cache for title info
40 protected $titleInfo = array();
42 // List of page names that contain CSS
43 protected $styles = array();
45 // List of page names that contain JavaScript
46 protected $scripts = array();
52 * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
54 public function __construct( array $options = null ) {
55 if ( is_null( $options ) ) {
59 foreach ( $options as $member => $option ) {
62 $this->isPositionDefined
= true;
63 // Don't break since we need the member set as well
67 $this->{$member} = $option;
74 * Subclasses should return an associative array of resources in the module.
75 * Keys should be the title of a page in the MediaWiki or User namespace.
77 * Values should be a nested array of options. The supported keys are 'type' and
80 * For scripts, 'type' should be 'script'.
82 * For stylesheets, 'type' should be 'style'.
83 * There is an optional media key, the value of which can be the
84 * medium ('screen', 'print', etc.) of the stylesheet.
86 * @param ResourceLoaderContext $context
89 protected function getPages( ResourceLoaderContext
$context ) {
90 $config = $this->getConfig();
93 // Filter out pages from origins not allowed by the current wiki configuration.
94 if ( $config->get( 'UseSiteJs' ) ) {
95 foreach ( $this->scripts
as $script ) {
96 $pages[$script] = array( 'type' => 'script' );
100 if ( $config->get( 'UseSiteCss' ) ) {
101 foreach ( $this->styles
as $style ) {
102 $pages[$style] = array( 'type' => 'style' );
114 public function getGroup() {
119 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
120 * but subclasses may want to override this to return a remote DB object, or to return
121 * null if getTitleMTimes() shouldn't access the DB at all.
123 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
124 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
125 * will use the local DB irrespective of the return value of this method.
127 * @return IDatabase|null
129 protected function getDB() {
130 return wfGetDB( DB_SLAVE
);
134 * @param Title $title
135 * @return null|string
137 protected function getContent( $title ) {
138 $handler = ContentHandler
::getForTitle( $title );
139 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
140 $format = CONTENT_FORMAT_CSS
;
141 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
142 $format = CONTENT_FORMAT_JAVASCRIPT
;
147 $revision = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
152 $content = $revision->getContent( Revision
::RAW
);
155 wfDebugLog( 'resourceloader', __METHOD__
. ': failed to load content of JS/CSS page!' );
159 return $content->serialize( $format );
163 * @param ResourceLoaderContext $context
166 public function getScript( ResourceLoaderContext
$context ) {
168 foreach ( $this->getPages( $context ) as $titleText => $options ) {
169 if ( $options['type'] !== 'script' ) {
172 $title = Title
::newFromText( $titleText );
173 if ( !$title ||
$title->isRedirect() ) {
176 $script = $this->getContent( $title );
177 if ( strval( $script ) !== '' ) {
178 $script = $this->validateScriptFile( $titleText, $script );
179 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
186 * @param ResourceLoaderContext $context
189 public function getStyles( ResourceLoaderContext
$context ) {
191 foreach ( $this->getPages( $context ) as $titleText => $options ) {
192 if ( $options['type'] !== 'style' ) {
195 $title = Title
::newFromText( $titleText );
196 if ( !$title ||
$title->isRedirect() ) {
199 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
200 $style = $this->getContent( $title );
201 if ( strval( $style ) === '' ) {
204 if ( $this->getFlip( $context ) ) {
205 $style = CSSJanus
::transform( $style, true, false );
207 $style = CSSMin
::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true );
208 if ( !isset( $styles[$media] ) ) {
209 $styles[$media] = array();
211 $style = ResourceLoader
::makeComment( $titleText ) . $style;
212 $styles[$media][] = $style;
218 * @param ResourceLoaderContext $context
221 public function getModifiedTime( ResourceLoaderContext
$context ) {
223 $titleInfo = $this->getTitleInfo( $context );
224 if ( count( $titleInfo ) ) {
225 $mtimes = array_map( function ( $value ) {
226 return $value['timestamp'];
228 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
232 $this->getMsgBlobMtime( $context->getLanguage() ),
233 $this->getDefinitionMtime( $context )
235 return $modifiedTime;
239 * @param ResourceLoaderContext $context
242 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
243 $summary = parent
::getDefinitionSummary( $context );
245 'pages' => $this->getPages( $context ),
251 * @param ResourceLoaderContext $context
254 public function isKnownEmpty( ResourceLoaderContext
$context ) {
255 $titleInfo = $this->getTitleInfo( $context );
256 // Bug 68488: For modules in the "user" group, we should actually
257 // check that the pages are empty (page_len == 0), but for other
258 // groups, just check the pages exist so that we don't end up
259 // caching temporarily-blank pages without the appropriate
260 // <script> or <link> tag.
261 if ( $this->getGroup() !== 'user' ) {
262 return count( $titleInfo ) === 0;
265 foreach ( $titleInfo as $info ) {
266 if ( $info['length'] !== 0 ) {
267 // At least one non-0-lenth page, not empty
272 // All pages are 0-length, so it's empty
277 * Get the modification times of all titles that would be loaded for
279 * @param ResourceLoaderContext $context Context object
280 * @return array Keyed by page dbkey. Value is an array with 'length' and 'timestamp'
281 * keys, where the timestamp is a UNIX timestamp
283 protected function getTitleInfo( ResourceLoaderContext
$context ) {
284 $dbr = $this->getDB();
286 // We're dealing with a subclass that doesn't have a DB
290 $hash = $context->getHash();
291 if ( isset( $this->titleInfo
[$hash] ) ) {
292 return $this->titleInfo
[$hash];
295 $this->titleInfo
[$hash] = array();
296 $batch = new LinkBatch
;
297 foreach ( $this->getPages( $context ) as $titleText => $options ) {
298 $batch->addObj( Title
::newFromText( $titleText ) );
301 if ( !$batch->isEmpty() ) {
302 $res = $dbr->select( 'page',
303 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
304 $batch->constructSet( 'page', $dbr ),
307 foreach ( $res as $row ) {
308 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
309 $this->titleInfo
[$hash][$title->getPrefixedDBkey()] = array(
310 'timestamp' => wfTimestamp( TS_UNIX
, $row->page_touched
),
311 'length' => $row->page_len
,
315 return $this->titleInfo
[$hash];
318 public function getPosition() {
319 return $this->position
;