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 title mtimes
40 protected $titleMtimes = array();
42 /* Abstract Protected Methods */
44 abstract protected function getPages( ResourceLoaderContext
$context );
46 /* Protected Methods */
52 protected function getContent( $title ) {
53 if ( $title->getNamespace() === NS_MEDIAWIKI
) {
54 $dbkey = $title->getDBkey();
55 return wfEmptyMsg( $dbkey ) ?
'' : wfMsgExt( $dbkey, 'content' );
57 if ( !$title->isCssJsSubpage() ) {
60 $revision = Revision
::newFromTitle( $title );
64 return $revision->getRawText();
69 public function getScript( ResourceLoaderContext
$context ) {
71 foreach ( $this->getPages( $context ) as $titleText => $options ) {
72 if ( $options['type'] !== 'script' ) {
75 $title = Title
::newFromText( $titleText );
79 $script = $this->getContent( $title );
80 if ( strval( $script ) !== '' ) {
81 if ( strpos( $titleText, '*/' ) === false ) {
82 $scripts .= "/* $titleText */\n";
84 $scripts .= $script . "\n";
90 public function getStyles( ResourceLoaderContext
$context ) {
94 foreach ( $this->getPages( $context ) as $titleText => $options ) {
95 if ( $options['type'] !== 'style' ) {
98 $title = Title
::newFromText( $titleText );
102 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
103 $style = $this->getContent( $title );
104 if ( strval( $style ) === '' ) {
107 if ( $this->getFlip( $context ) ) {
108 $style = CSSJanus
::transform( $style, true, false );
110 $style = CSSMin
::remap( $style, false, $wgScriptPath, true );
111 if ( !isset( $styles[$media] ) ) {
112 $styles[$media] = '';
114 if ( strpos( $titleText, '*/' ) === false ) {
115 $styles[$media] .= "/* $titleText */\n";
117 $styles[$media] .= $style . "\n";
122 public function getModifiedTime( ResourceLoaderContext
$context ) {
123 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
124 $mtimes = $this->getTitleMtimes( $context );
125 if ( count( $mtimes ) ) {
126 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
128 return $modifiedTime;
131 public function isKnownEmpty( ResourceLoaderContext
$context ) {
132 return count( $this->getTitleMtimes( $context ) ) == 0;
136 * Get the modification times of all titles that would be loaded for
138 * @param $context ResourceLoaderContext: Context object
139 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
141 protected function getTitleMtimes( ResourceLoaderContext
$context ) {
142 $hash = $context->getHash();
143 if ( isset( $this->titleMtimes
[$hash] ) ) {
144 return $this->titleMtimes
[$hash];
147 $this->titleMtimes
[$hash] = array();
148 $batch = new LinkBatch
;
149 foreach ( $this->getPages( $context ) as $titleText => $options ) {
150 $batch->addObj( Title
::newFromText( $titleText ) );
153 if ( !$batch->isEmpty() ) {
154 $dbr = wfGetDB( DB_SLAVE
);
155 $res = $dbr->select( 'page',
156 array( 'page_namespace', 'page_title', 'page_touched' ),
157 $batch->constructSet( 'page', $dbr ),
160 foreach ( $res as $row ) {
161 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
162 $this->titleMtimes
[$hash][$title->getPrefixedDBkey()] =
163 wfTimestamp( TS_UNIX
, $row->page_touched
);
166 return $this->titleMtimes
[$hash];