wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blob52eb2d81be4ff61111e7627c2238aa3a944e9acb
1 <?php
2 /**
3 * Abstraction for ResourceLoader modules that 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 ResourceLoader 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 Title::isCssJsSubpage.
31 * This module supports being used as a placeholder for a module on a remote wiki.
32 * To do so, getDB() must be overloaded to return a foreign database object that
33 * allows local wikis to query page metadata.
35 * Safe for calls on local wikis are:
36 * - Option getters:
37 * - getGroup()
38 * - getPosition()
39 * - getPages()
40 * - Basic methods that strictly involve the foreign database
41 * - getDB()
42 * - isKnownEmpty()
43 * - getTitleInfo()
45 class ResourceLoaderWikiModule extends ResourceLoaderModule {
46 /** @var string Position on the page to load this module at */
47 protected $position = 'bottom';
49 // Origin defaults to users with sitewide authority
50 protected $origin = self::ORIGIN_USER_SITEWIDE;
52 // In-process cache for title info
53 protected $titleInfo = array();
55 // List of page names that contain CSS
56 protected $styles = array();
58 // List of page names that contain JavaScript
59 protected $scripts = array();
61 // Group of module
62 protected $group;
64 /**
65 * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
67 public function __construct( array $options = null ) {
68 if ( is_null( $options ) ) {
69 return;
72 foreach ( $options as $member => $option ) {
73 switch ( $member ) {
74 case 'position':
75 case 'styles':
76 case 'scripts':
77 case 'group':
78 $this->{$member} = $option;
79 break;
84 /**
85 * Subclasses should return an associative array of resources in the module.
86 * Keys should be the title of a page in the MediaWiki or User namespace.
88 * Values should be a nested array of options. The supported keys are 'type' and
89 * (CSS only) 'media'.
91 * For scripts, 'type' should be 'script'.
93 * For stylesheets, 'type' should be 'style'.
94 * There is an optional media key, the value of which can be the
95 * medium ('screen', 'print', etc.) of the stylesheet.
97 * @param ResourceLoaderContext $context
98 * @return array
100 protected function getPages( ResourceLoaderContext $context ) {
101 $config = $this->getConfig();
102 $pages = array();
104 // Filter out pages from origins not allowed by the current wiki configuration.
105 if ( $config->get( 'UseSiteJs' ) ) {
106 foreach ( $this->scripts as $script ) {
107 $pages[$script] = array( 'type' => 'script' );
111 if ( $config->get( 'UseSiteCss' ) ) {
112 foreach ( $this->styles as $style ) {
113 $pages[$style] = array( 'type' => 'style' );
117 return $pages;
121 * Get group name
123 * @return string
125 public function getGroup() {
126 return $this->group;
130 * Get the Database object used in getTitleInfo().
132 * Defaults to the local slave DB. Subclasses may want to override this to return a foreign
133 * database object, or null if getTitleInfo() shouldn't access the database.
135 * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
136 * In particular, it doesn't work for getContent() or getScript() etc.
138 * @return IDatabase|null
140 protected function getDB() {
141 return wfGetDB( DB_SLAVE );
145 * @param string $title
146 * @return null|string
148 protected function getContent( $titleText ) {
149 $title = Title::newFromText( $titleText );
150 if ( !$title ) {
151 return null;
154 $handler = ContentHandler::getForTitle( $title );
155 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
156 $format = CONTENT_FORMAT_CSS;
157 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
158 $format = CONTENT_FORMAT_JAVASCRIPT;
159 } else {
160 return null;
163 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
164 if ( !$revision ) {
165 return null;
168 $content = $revision->getContent( Revision::RAW );
170 if ( !$content ) {
171 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
172 return null;
175 return $content->serialize( $format );
179 * @param ResourceLoaderContext $context
180 * @return string
182 public function getScript( ResourceLoaderContext $context ) {
183 $scripts = '';
184 foreach ( $this->getPages( $context ) as $titleText => $options ) {
185 if ( $options['type'] !== 'script' ) {
186 continue;
188 $script = $this->getContent( $titleText );
189 if ( strval( $script ) !== '' ) {
190 $script = $this->validateScriptFile( $titleText, $script );
191 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
194 return $scripts;
198 * @param ResourceLoaderContext $context
199 * @return array
201 public function getStyles( ResourceLoaderContext $context ) {
202 $styles = array();
203 foreach ( $this->getPages( $context ) as $titleText => $options ) {
204 if ( $options['type'] !== 'style' ) {
205 continue;
207 $media = isset( $options['media'] ) ? $options['media'] : 'all';
208 $style = $this->getContent( $titleText );
209 if ( strval( $style ) === '' ) {
210 continue;
212 if ( $this->getFlip( $context ) ) {
213 $style = CSSJanus::transform( $style, true, false );
215 $style = MemoizedCallable::call( 'CSSMin::remap',
216 array( $style, false, $this->getConfig()->get( 'ScriptPath' ), true ) );
217 if ( !isset( $styles[$media] ) ) {
218 $styles[$media] = array();
220 $style = ResourceLoader::makeComment( $titleText ) . $style;
221 $styles[$media][] = $style;
223 return $styles;
227 * Disable module content versioning.
229 * This class does not support generating content outside of a module
230 * request due to foreign database support.
232 * See getDefinitionSummary() for meta-data versioning.
234 * @return bool
236 public function enableModuleContentVersion() {
237 return false;
241 * @param ResourceLoaderContext $context
242 * @return array
244 public function getDefinitionSummary( ResourceLoaderContext $context ) {
245 $summary = parent::getDefinitionSummary( $context );
246 $summary[] = array(
247 'pages' => $this->getPages( $context ),
248 // Includes SHA1 of content
249 'titleInfo' => $this->getTitleInfo( $context ),
251 return $summary;
255 * @param ResourceLoaderContext $context
256 * @return bool
258 public function isKnownEmpty( ResourceLoaderContext $context ) {
259 $revisions = $this->getTitleInfo( $context );
261 // For user modules, don't needlessly load if there are no non-empty pages
262 if ( $this->getGroup() === 'user' ) {
263 foreach ( $revisions as $revision ) {
264 if ( $revision['rev_len'] > 0 ) {
265 // At least one non-empty page, module should be loaded
266 return false;
269 return true;
272 // Bug 68488: For other modules (i.e. ones that are called in cached html output) only check
273 // page existance. This ensures that, if some pages in a module are temporarily blanked,
274 // we don't end omit the module's script or link tag on some pages.
275 return count( $revisions ) === 0;
279 * Get the information about the wiki pages for a given context.
280 * @param ResourceLoaderContext $context
281 * @return array Keyed by page name. Contains arrays with 'rev_len' and 'rev_sha1' keys
283 protected function getTitleInfo( ResourceLoaderContext $context ) {
284 $dbr = $this->getDB();
285 if ( !$dbr ) {
286 // We're dealing with a subclass that doesn't have a DB
287 return array();
290 $pages = $this->getPages( $context );
291 $key = implode( '|', array_keys( $pages ) );
292 if ( !isset( $this->titleInfo[$key] ) ) {
293 $this->titleInfo[$key] = array();
294 $batch = new LinkBatch;
295 foreach ( $pages as $titleText => $options ) {
296 $batch->addObj( Title::newFromText( $titleText ) );
299 if ( !$batch->isEmpty() ) {
300 $res = $dbr->select( array( 'page', 'revision' ),
301 array( 'page_namespace', 'page_title', 'rev_len', 'rev_sha1' ),
302 $batch->constructSet( 'page', $dbr ),
303 __METHOD__,
304 array(),
305 array( 'revision' => array( 'INNER JOIN', array( 'page_latest=rev_id' ) ) )
307 foreach ( $res as $row ) {
308 // Avoid including ids or timestamps of revision/page tables so
309 // that versions are not wasted
310 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
311 $this->titleInfo[$key][$title->getPrefixedText()] = array(
312 'rev_len' => $row->rev_len,
313 'rev_sha1' => $row->rev_sha1,
318 return $this->titleInfo[$key];
321 public function getPosition() {
322 return $this->position;