(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blob91a51f896ce6a41f8392990a1131cd0a9bbbe13a
1 <?php
2 /**
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
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) || die( 1 );
25 /**
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 */
44 /**
45 * @abstract
46 * @param $context ResourceLoaderContext
48 abstract protected function getPages( ResourceLoaderContext $context );
50 /* Protected Methods */
52 /**
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 );
67 /**
68 * @param $title Title
69 * @return null|string
71 protected function getContent( $title ) {
72 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
73 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
74 return $message->exists() ? $message->plain() : '';
76 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
77 return null;
79 $revision = Revision::newFromTitle( $title );
80 if ( !$revision ) {
81 return null;
83 return $revision->getRawText();
86 /* Methods */
88 /**
89 * @param $context ResourceLoaderContext
90 * @return string
92 public function getScript( ResourceLoaderContext $context ) {
93 $scripts = '';
94 foreach ( $this->getPages( $context ) as $titleText => $options ) {
95 if ( $options['type'] !== 'script' ) {
96 continue;
98 $title = Title::newFromText( $titleText );
99 if ( !$title || $title->isRedirect() ) {
100 continue;
102 $script = $this->getContent( $title );
103 if ( strval( $script ) !== '' ) {
104 $script = $this->validateScriptFile( $titleText, $script );
105 if ( strpos( $titleText, '*/' ) === false ) {
106 $scripts .= "/* $titleText */\n";
108 $scripts .= $script . "\n";
111 return $scripts;
115 * @param $context ResourceLoaderContext
116 * @return array
118 public function getStyles( ResourceLoaderContext $context ) {
119 global $wgScriptPath;
121 $styles = array();
122 foreach ( $this->getPages( $context ) as $titleText => $options ) {
123 if ( $options['type'] !== 'style' ) {
124 continue;
126 $title = Title::newFromText( $titleText );
127 if ( !$title || $title->isRedirect() ) {
128 continue;
130 $media = isset( $options['media'] ) ? $options['media'] : 'all';
131 $style = $this->getContent( $title );
132 if ( strval( $style ) === '' ) {
133 continue;
135 if ( $this->getFlip( $context ) ) {
136 $style = CSSJanus::transform( $style, true, false );
138 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
139 if ( !isset( $styles[$media] ) ) {
140 $styles[$media] = '';
142 if ( strpos( $titleText, '*/' ) === false ) {
143 $styles[$media] .= "/* $titleText */\n";
145 $styles[$media] .= $style . "\n";
147 return $styles;
151 * @param $context ResourceLoaderContext
152 * @return int|mixed
154 public function getModifiedTime( ResourceLoaderContext $context ) {
155 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
156 $mtimes = $this->getTitleMtimes( $context );
157 if ( count( $mtimes ) ) {
158 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
160 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
161 return $modifiedTime;
165 * @param $context ResourceLoaderContext
166 * @return bool
168 public function isKnownEmpty( ResourceLoaderContext $context ) {
169 return count( $this->getTitleMtimes( $context ) ) == 0;
173 * Get the modification times of all titles that would be loaded for
174 * a given context.
175 * @param $context ResourceLoaderContext: Context object
176 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
178 protected function getTitleMtimes( ResourceLoaderContext $context ) {
179 $dbr = $this->getDB();
180 if ( !$dbr ) {
181 // We're dealing with a subclass that doesn't have a DB
182 return array();
185 $hash = $context->getHash();
186 if ( isset( $this->titleMtimes[$hash] ) ) {
187 return $this->titleMtimes[$hash];
190 $this->titleMtimes[$hash] = array();
191 $batch = new LinkBatch;
192 foreach ( $this->getPages( $context ) as $titleText => $options ) {
193 $batch->addObj( Title::newFromText( $titleText ) );
196 if ( !$batch->isEmpty() ) {
197 $res = $dbr->select( 'page',
198 array( 'page_namespace', 'page_title', 'page_touched' ),
199 $batch->constructSet( 'page', $dbr ),
200 __METHOD__
202 foreach ( $res as $row ) {
203 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
204 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
205 wfTimestamp( TS_UNIX, $row->page_touched );
208 return $this->titleMtimes[$hash];