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, because of it's dependence on the
29 * functionality of Title::isValidCssJsSubpage.
31 abstract class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
33 /* Protected Members */
35 // In-object cache for modified time
36 protected $modifiedTime = array();
38 /* Abstract Protected Methods */
40 abstract protected function getPages( ResourceLoaderContext
$context );
42 /* Protected Methods */
44 protected function getContent( $page, $ns ) {
45 if ( $ns === NS_MEDIAWIKI
) {
46 return wfEmptyMsg( $page ) ?
'' : wfMsgExt( $page, 'content' );
48 if ( $title = Title
::newFromText( $page, $ns ) ) {
49 if ( $title->isValidCssJsSubpage() && $revision = Revision
::newFromTitle( $title ) ) {
50 return $revision->getRawText();
58 public function getScript( ResourceLoaderContext
$context ) {
60 foreach ( $this->getPages( $context ) as $page => $options ) {
61 if ( $options['type'] === 'script' ) {
62 if ( $script = $this->getContent( $page, $options['ns'] ) ) {
63 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
64 $scripts .= "/*$ns:$page */\n$script\n";
71 public function getStyles( ResourceLoaderContext
$context ) {
74 foreach ( $this->getPages( $context ) as $page => $options ) {
75 if ( $options['type'] === 'style' ) {
76 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
77 if ( $style = $this->getContent( $page, $options['ns'] ) ) {
78 if ( !isset( $styles[$media] ) ) {
81 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
82 $styles[$media] .= "/* $ns:$page */\n$style\n";
89 public function getModifiedTime( ResourceLoaderContext
$context ) {
90 $hash = $context->getHash();
91 if ( isset( $this->modifiedTime
[$hash] ) ) {
92 return $this->modifiedTime
[$hash];
96 foreach ( $this->getPages( $context ) as $page => $options ) {
97 $titles[$options['ns']][$page] = true;
100 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
103 $dbr = wfGetDB( DB_SLAVE
);
104 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
105 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
109 $modifiedTime = wfTimestamp( TS_UNIX
, $latest );
113 return $this->modifiedTime
[$hash] = $modifiedTime;