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
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) ||
die( 1 );
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::isValidCssJsSubpage.
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 modified time
40 protected $modifiedTime = array();
42 /* Abstract Protected Methods */
44 abstract protected function getPages( ResourceLoaderContext
$context );
46 /* Protected Methods */
48 protected function getContent( $page, $ns ) {
49 if ( $ns === NS_MEDIAWIKI
) {
50 return wfEmptyMsg( $page ) ?
'' : wfMsgExt( $page, 'content' );
52 $title = Title
::newFromText( $page, $ns );
53 if ( !$title ||
!$title->isCssJsSubpage() ) {
56 $revision = Revision
::newFromTitle( $title );
60 return $revision->getRawText();
65 public function getScript( ResourceLoaderContext
$context ) {
67 foreach ( $this->getPages( $context ) as $page => $options ) {
68 if ( $options['type'] !== 'script' ) {
71 $script = $this->getContent( $page, $options['ns'] );
73 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
74 $scripts .= "/* $ns:$page */\n$script\n";
80 public function getStyles( ResourceLoaderContext
$context ) {
83 foreach ( $this->getPages( $context ) as $page => $options ) {
84 if ( $options['type'] !== 'style' ) {
87 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
88 $style = $this->getContent( $page, $options['ns'] );
92 if ( $this->getFlip( $context ) ) {
93 $style = CSSJanus
::transform( $style, true, false );
95 if ( !isset( $styles[$media] ) ) {
98 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
99 $styles[$media] .= "/* $ns:$page */\n$style\n";
104 public function getModifiedTime( ResourceLoaderContext
$context ) {
105 $hash = $context->getHash();
106 if ( isset( $this->modifiedTime
[$hash] ) ) {
107 return $this->modifiedTime
[$hash];
111 foreach ( $this->getPages( $context ) as $page => $options ) {
112 $titles[$options['ns']][$page] = true;
115 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
118 $dbr = wfGetDB( DB_SLAVE
);
119 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
120 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
124 $modifiedTime = wfTimestamp( TS_UNIX
, $latest );
128 return $this->modifiedTime
[$hash] = $modifiedTime;