Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blob1a1a6d0008e5bff93161e57f9a19eedc61f82012
1 <?php
2 /**
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
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
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 info
40 protected $titleInfo = array();
42 /* Abstract Protected Methods */
44 /**
45 * Subclasses should return an associative array of resources in the module.
46 * Keys should be the title of a page in the MediaWiki or User namespace.
48 * Values should be a nested array of options. The supported keys are 'type' and
49 * (CSS only) 'media'.
51 * For scripts, 'type' should be 'script'.
53 * For stylesheets, 'type' should be 'style'.
54 * There is an optional media key, the value of which can be the
55 * medium ('screen', 'print', etc.) of the stylesheet.
57 * @param ResourceLoaderContext $context
58 * @return array
60 abstract protected function getPages( ResourceLoaderContext $context );
62 /* Protected Methods */
64 /**
65 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
66 * but subclasses may want to override this to return a remote DB object, or to return
67 * null if getTitleMTimes() shouldn't access the DB at all.
69 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
70 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
71 * will use the local DB irrespective of the return value of this method.
73 * @return IDatabase|null
75 protected function getDB() {
76 return wfGetDB( DB_SLAVE );
79 /**
80 * @param Title $title
81 * @return null|string
83 protected function getContent( $title ) {
84 $handler = ContentHandler::getForTitle( $title );
85 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
86 $format = CONTENT_FORMAT_CSS;
87 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
88 $format = CONTENT_FORMAT_JAVASCRIPT;
89 } else {
90 return null;
93 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
94 if ( !$revision ) {
95 return null;
98 $content = $revision->getContent( Revision::RAW );
100 if ( !$content ) {
101 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
102 return null;
105 return $content->serialize( $format );
108 /* Methods */
111 * @param ResourceLoaderContext $context
112 * @return string
114 public function getScript( ResourceLoaderContext $context ) {
115 $scripts = '';
116 foreach ( $this->getPages( $context ) as $titleText => $options ) {
117 if ( $options['type'] !== 'script' ) {
118 continue;
120 $title = Title::newFromText( $titleText );
121 if ( !$title || $title->isRedirect() ) {
122 continue;
124 $script = $this->getContent( $title );
125 if ( strval( $script ) !== '' ) {
126 $script = $this->validateScriptFile( $titleText, $script );
127 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
130 return $scripts;
134 * @param ResourceLoaderContext $context
135 * @return array
137 public function getStyles( ResourceLoaderContext $context ) {
138 $styles = array();
139 foreach ( $this->getPages( $context ) as $titleText => $options ) {
140 if ( $options['type'] !== 'style' ) {
141 continue;
143 $title = Title::newFromText( $titleText );
144 if ( !$title || $title->isRedirect() ) {
145 continue;
147 $media = isset( $options['media'] ) ? $options['media'] : 'all';
148 $style = $this->getContent( $title );
149 if ( strval( $style ) === '' ) {
150 continue;
152 if ( $this->getFlip( $context ) ) {
153 $style = CSSJanus::transform( $style, true, false );
155 $style = CSSMin::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true );
156 if ( !isset( $styles[$media] ) ) {
157 $styles[$media] = array();
159 $style = ResourceLoader::makeComment( $titleText ) . $style;
160 $styles[$media][] = $style;
162 return $styles;
166 * @param ResourceLoaderContext $context
167 * @return int
169 public function getModifiedTime( ResourceLoaderContext $context ) {
170 $modifiedTime = 1;
171 $titleInfo = $this->getTitleInfo( $context );
172 if ( count( $titleInfo ) ) {
173 $mtimes = array_map( function ( $value ) {
174 return $value['timestamp'];
175 }, $titleInfo );
176 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
178 $modifiedTime = max(
179 $modifiedTime,
180 $this->getMsgBlobMtime( $context->getLanguage() ),
181 $this->getDefinitionMtime( $context )
183 return $modifiedTime;
187 * Get the definition summary for this module.
189 * @param ResourceLoaderContext $context
190 * @return array
192 public function getDefinitionSummary( ResourceLoaderContext $context ) {
193 return array(
194 'class' => get_class( $this ),
195 'pages' => $this->getPages( $context ),
200 * @param ResourceLoaderContext $context
201 * @return bool
203 public function isKnownEmpty( ResourceLoaderContext $context ) {
204 $titleInfo = $this->getTitleInfo( $context );
205 // Bug 68488: For modules in the "user" group, we should actually
206 // check that the pages are empty (page_len == 0), but for other
207 // groups, just check the pages exist so that we don't end up
208 // caching temporarily-blank pages without the appropriate
209 // <script> or <link> tag.
210 if ( $this->getGroup() !== 'user' ) {
211 return count( $titleInfo ) === 0;
214 foreach ( $titleInfo as $info ) {
215 if ( $info['length'] !== 0 ) {
216 // At least one non-0-lenth page, not empty
217 return false;
221 // All pages are 0-length, so it's empty
222 return true;
226 * Get the modification times of all titles that would be loaded for
227 * a given context.
228 * @param ResourceLoaderContext $context Context object
229 * @return array Keyed by page dbkey. Value is an array with 'length' and 'timestamp'
230 * keys, where the timestamp is a UNIX timestamp
232 protected function getTitleInfo( ResourceLoaderContext $context ) {
233 $dbr = $this->getDB();
234 if ( !$dbr ) {
235 // We're dealing with a subclass that doesn't have a DB
236 return array();
239 $hash = $context->getHash();
240 if ( isset( $this->titleInfo[$hash] ) ) {
241 return $this->titleInfo[$hash];
244 $this->titleInfo[$hash] = array();
245 $batch = new LinkBatch;
246 foreach ( $this->getPages( $context ) as $titleText => $options ) {
247 $batch->addObj( Title::newFromText( $titleText ) );
250 if ( !$batch->isEmpty() ) {
251 $res = $dbr->select( 'page',
252 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
253 $batch->constructSet( 'page', $dbr ),
254 __METHOD__
256 foreach ( $res as $row ) {
257 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
258 $this->titleInfo[$hash][$title->getPrefixedDBkey()] = array(
259 'timestamp' => wfTimestamp( TS_UNIX, $row->page_touched ),
260 'length' => $row->page_len,
264 return $this->titleInfo[$hash];