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 );
85 return $revision->getRawText();
91 * @param $context ResourceLoaderContext
94 public function getScript( ResourceLoaderContext
$context ) {
96 foreach ( $this->getPages( $context ) as $titleText => $options ) {
97 if ( $options['type'] !== 'script' ) {
100 $title = Title
::newFromText( $titleText );
101 if ( !$title ||
$title->isRedirect() ) {
104 $script = $this->getContent( $title );
105 if ( strval( $script ) !== '' ) {
106 $script = $this->validateScriptFile( $titleText, $script );
107 if ( strpos( $titleText, '*/' ) === false ) {
108 $scripts .= "/* $titleText */\n";
110 $scripts .= $script . "\n";
117 * @param $context ResourceLoaderContext
120 public function getStyles( ResourceLoaderContext
$context ) {
121 global $wgScriptPath;
124 foreach ( $this->getPages( $context ) as $titleText => $options ) {
125 if ( $options['type'] !== 'style' ) {
128 $title = Title
::newFromText( $titleText );
129 if ( !$title ||
$title->isRedirect() ) {
132 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
133 $style = $this->getContent( $title );
134 if ( strval( $style ) === '' ) {
137 if ( $this->getFlip( $context ) ) {
138 $style = CSSJanus
::transform( $style, true, false );
140 $style = CSSMin
::remap( $style, false, $wgScriptPath, true );
141 if ( !isset( $styles[$media] ) ) {
142 $styles[$media] = '';
144 if ( strpos( $titleText, '*/' ) === false ) {
145 $styles[$media] .= "/* $titleText */\n";
147 $styles[$media] .= $style . "\n";
153 * @param $context ResourceLoaderContext
156 public function getModifiedTime( ResourceLoaderContext
$context ) {
157 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
158 $mtimes = $this->getTitleMtimes( $context );
159 if ( count( $mtimes ) ) {
160 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
162 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
163 return $modifiedTime;
167 * @param $context ResourceLoaderContext
170 public function isKnownEmpty( ResourceLoaderContext
$context ) {
171 return count( $this->getTitleMtimes( $context ) ) == 0;
175 * Get the modification times of all titles that would be loaded for
177 * @param $context ResourceLoaderContext: Context object
178 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
180 protected function getTitleMtimes( ResourceLoaderContext
$context ) {
181 $dbr = $this->getDB();
183 // We're dealing with a subclass that doesn't have a DB
187 $hash = $context->getHash();
188 if ( isset( $this->titleMtimes
[$hash] ) ) {
189 return $this->titleMtimes
[$hash];
192 $this->titleMtimes
[$hash] = array();
193 $batch = new LinkBatch
;
194 foreach ( $this->getPages( $context ) as $titleText => $options ) {
195 $batch->addObj( Title
::newFromText( $titleText ) );
198 if ( !$batch->isEmpty() ) {
199 $res = $dbr->select( 'page',
200 array( 'page_namespace', 'page_title', 'page_touched' ),
201 $batch->constructSet( 'page', $dbr ),
204 foreach ( $res as $row ) {
205 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
206 $this->titleMtimes
[$hash][$title->getPrefixedDBkey()] =
207 wfTimestamp( TS_UNIX
, $row->page_touched
);
210 return $this->titleMtimes
[$hash];