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 */
45 * @param $context ResourceLoaderContext
47 abstract protected function getPages( ResourceLoaderContext
$context );
49 /* Protected Methods */
52 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
53 * but subclasses may want to override this to return a remote DB object, or to return
54 * null if getTitleMTimes() shouldn't access the DB at all.
56 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
57 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
58 * will use the local DB irrespective of the return value of this method.
60 * @return DatabaseBase|null
62 protected function getDB() {
63 return wfGetDB( DB_SLAVE
);
70 protected function getContent( $title ) {
71 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
74 $revision = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
78 return $revision->getRawText();
84 * @param $context ResourceLoaderContext
87 public function getScript( ResourceLoaderContext
$context ) {
89 foreach ( $this->getPages( $context ) as $titleText => $options ) {
90 if ( $options['type'] !== 'script' ) {
93 $title = Title
::newFromText( $titleText );
94 if ( !$title ||
$title->isRedirect() ) {
97 $script = $this->getContent( $title );
98 if ( strval( $script ) !== '' ) {
99 $script = $this->validateScriptFile( $titleText, $script );
100 if ( strpos( $titleText, '*/' ) === false ) {
101 $scripts .= "/* $titleText */\n";
103 $scripts .= $script . "\n";
110 * @param $context ResourceLoaderContext
113 public function getStyles( ResourceLoaderContext
$context ) {
114 global $wgScriptPath;
117 foreach ( $this->getPages( $context ) as $titleText => $options ) {
118 if ( $options['type'] !== 'style' ) {
121 $title = Title
::newFromText( $titleText );
122 if ( !$title ||
$title->isRedirect() ) {
125 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
126 $style = $this->getContent( $title );
127 if ( strval( $style ) === '' ) {
130 if ( $this->getFlip( $context ) ) {
131 $style = CSSJanus
::transform( $style, true, false );
133 $style = CSSMin
::remap( $style, false, $wgScriptPath, true );
134 if ( !isset( $styles[$media] ) ) {
135 $styles[$media] = array();
137 if ( strpos( $titleText, '*/' ) === false ) {
138 $style = "/* $titleText */\n" . $style;
140 $styles[$media][] = $style;
146 * @param $context ResourceLoaderContext
149 public function getModifiedTime( ResourceLoaderContext
$context ) {
150 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
151 $mtimes = $this->getTitleMtimes( $context );
152 if ( count( $mtimes ) ) {
153 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
155 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
156 return $modifiedTime;
160 * @param $context ResourceLoaderContext
163 public function isKnownEmpty( ResourceLoaderContext
$context ) {
164 return count( $this->getTitleMtimes( $context ) ) == 0;
168 * Get the modification times of all titles that would be loaded for
170 * @param $context ResourceLoaderContext: Context object
171 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
173 protected function getTitleMtimes( ResourceLoaderContext
$context ) {
174 $dbr = $this->getDB();
176 // We're dealing with a subclass that doesn't have a DB
180 $hash = $context->getHash();
181 if ( isset( $this->titleMtimes
[$hash] ) ) {
182 return $this->titleMtimes
[$hash];
185 $this->titleMtimes
[$hash] = array();
186 $batch = new LinkBatch
;
187 foreach ( $this->getPages( $context ) as $titleText => $options ) {
188 $batch->addObj( Title
::newFromText( $titleText ) );
191 if ( !$batch->isEmpty() ) {
192 $res = $dbr->select( 'page',
193 array( 'page_namespace', 'page_title', 'page_touched' ),
194 $batch->constructSet( 'page', $dbr ),
197 foreach ( $res as $row ) {
198 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
199 $this->titleMtimes
[$hash][$title->getPrefixedDBkey()] =
200 wfTimestamp( TS_UNIX
, $row->page_touched
);
203 return $this->titleMtimes
[$hash];