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 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 */
46 * @param $context ResourceLoaderContext
48 abstract protected function getPages( ResourceLoaderContext
$context );
50 /* Protected Methods */
53 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
54 * but subclasses may want to override this to return a remote DB object, or to return
55 * null if getTitleMTimes() shouldn't access the DB at all.
57 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
58 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
59 * will use the local DB irrespective of the return value of this method.
61 * @return DatabaseBase|null
63 protected function getDB() {
64 return wfGetDB( DB_SLAVE
);
71 protected function getContent( $title ) {
72 if ( $title->getNamespace() === NS_MEDIAWIKI
) {
73 // The first "true" is to use the database, the second is to use the content langue
74 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
75 $text = MessageCache
::singleton()->get( $title->getDBkey(), true, true, true );
76 return $text === false ?
'' : $text;
78 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
81 $revision = Revision
::newFromTitle( $title );
86 $content = $revision->getContent( Revision
::RAW
);
87 $model = $content->getModel();
89 if ( $model !== CONTENT_MODEL_CSS
&& $model !== CONTENT_MODEL_JAVASCRIPT
) {
90 wfDebug( __METHOD__
. "bad content model #$model for JS/CSS page!\n" );
94 return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS
100 * @param $context ResourceLoaderContext
103 public function getScript( ResourceLoaderContext
$context ) {
105 foreach ( $this->getPages( $context ) as $titleText => $options ) {
106 if ( $options['type'] !== 'script' ) {
109 $title = Title
::newFromText( $titleText );
110 if ( !$title ||
$title->isRedirect() ) {
113 $script = $this->getContent( $title );
114 if ( strval( $script ) !== '' ) {
115 $script = $this->validateScriptFile( $titleText, $script );
116 if ( strpos( $titleText, '*/' ) === false ) {
117 $scripts .= "/* $titleText */\n";
119 $scripts .= $script . "\n";
126 * @param $context ResourceLoaderContext
129 public function getStyles( ResourceLoaderContext
$context ) {
130 global $wgScriptPath;
133 foreach ( $this->getPages( $context ) as $titleText => $options ) {
134 if ( $options['type'] !== 'style' ) {
137 $title = Title
::newFromText( $titleText );
138 if ( !$title ||
$title->isRedirect() ) {
141 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
142 $style = $this->getContent( $title );
143 if ( strval( $style ) === '' ) {
146 if ( $this->getFlip( $context ) ) {
147 $style = CSSJanus
::transform( $style, true, false );
149 $style = CSSMin
::remap( $style, false, $wgScriptPath, true );
150 if ( !isset( $styles[$media] ) ) {
151 $styles[$media] = '';
153 if ( strpos( $titleText, '*/' ) === false ) {
154 $styles[$media] .= "/* $titleText */\n";
156 $styles[$media] .= $style . "\n";
162 * @param $context ResourceLoaderContext
165 public function getModifiedTime( ResourceLoaderContext
$context ) {
166 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
167 $mtimes = $this->getTitleMtimes( $context );
168 if ( count( $mtimes ) ) {
169 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
171 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
172 return $modifiedTime;
176 * @param $context ResourceLoaderContext
179 public function isKnownEmpty( ResourceLoaderContext
$context ) {
180 return count( $this->getTitleMtimes( $context ) ) == 0;
184 * Get the modification times of all titles that would be loaded for
186 * @param $context ResourceLoaderContext: Context object
187 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
189 protected function getTitleMtimes( ResourceLoaderContext
$context ) {
190 $dbr = $this->getDB();
192 // We're dealing with a subclass that doesn't have a DB
196 $hash = $context->getHash();
197 if ( isset( $this->titleMtimes
[$hash] ) ) {
198 return $this->titleMtimes
[$hash];
201 $this->titleMtimes
[$hash] = array();
202 $batch = new LinkBatch
;
203 foreach ( $this->getPages( $context ) as $titleText => $options ) {
204 $batch->addObj( Title
::newFromText( $titleText ) );
207 if ( !$batch->isEmpty() ) {
208 $res = $dbr->select( 'page',
209 array( 'page_namespace', 'page_title', 'page_touched' ),
210 $batch->constructSet( 'page', $dbr ),
213 foreach ( $res as $row ) {
214 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
215 $this->titleMtimes
[$hash][$title->getPrefixedDBkey()] =
216 wfTimestamp( TS_UNIX
, $row->page_touched
);
219 return $this->titleMtimes
[$hash];