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 it's dependence on the functionality of
30 * Title::isValidCssJsSubpage.
32 abstract class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
34 /* Protected Members */
36 // In-object cache for modified time
37 protected $modifiedTime = array();
39 /* Abstract Protected Methods */
41 abstract protected function getPages( ResourceLoaderContext
$context );
43 /* Protected Methods */
45 protected function getContent( $page, $ns ) {
46 if ( $ns === NS_MEDIAWIKI
) {
47 return wfEmptyMsg( $page ) ?
'' : wfMsgExt( $page, 'content' );
49 $title = Title
::newFromText( $page, $ns );
50 if ( !$title ||
!$title->isValidCssJsSubpage() ) {
53 $revision = Revision
::newFromTitle( $title );
57 return $revision->getRawText();
62 public function getScript( ResourceLoaderContext
$context ) {
64 foreach ( $this->getPages( $context ) as $page => $options ) {
65 if ( $options['type'] !== 'script' ) {
68 $script = $this->getContent( $page, $options['ns'] );
70 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
71 $scripts .= "/* $ns:$page */\n$script\n";
77 public function getStyles( ResourceLoaderContext
$context ) {
80 foreach ( $this->getPages( $context ) as $page => $options ) {
81 if ( $options['type'] !== 'style' ) {
84 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
85 $style = $this->getContent( $page, $options['ns'] );
89 if ( !isset( $styles[$media] ) ) {
92 $ns = MWNamespace
::getCanonicalName( $options['ns'] );
93 $styles[$media] .= "/* $ns:$page */\n$style\n";
98 public function getModifiedTime( ResourceLoaderContext
$context ) {
99 $hash = $context->getHash();
100 if ( isset( $this->modifiedTime
[$hash] ) ) {
101 return $this->modifiedTime
[$hash];
105 foreach ( $this->getPages( $context ) as $page => $options ) {
106 $titles[$options['ns']][$page] = true;
109 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
112 $dbr = wfGetDB( DB_SLAVE
);
113 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
114 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
118 $modifiedTime = wfTimestamp( TS_UNIX
, $latest );
122 return $this->modifiedTime
[$hash] = $modifiedTime;