Merge "update.php: Remove max seconds of lag from wfWaitForSlaves() call"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderWikiModule.php
1 <?php
2 /**
3 * Abstraction for resource loader modules which pull from wiki pages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 /**
26 * Abstraction for resource loader modules which pull from wiki pages
27 *
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.
31 */
32 class ResourceLoaderWikiModule extends ResourceLoaderModule {
33
34 // Origin defaults to users with sitewide authority
35 protected $origin = self::ORIGIN_USER_SITEWIDE;
36
37 // In-object cache for title info
38 protected $titleInfo = array();
39
40 // List of page names that contain CSS
41 protected $styles = array();
42
43 // List of page names that contain JavaScript
44 protected $scripts = array();
45
46 // Group of module
47 protected $group;
48
49 /**
50 * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
51 */
52 public function __construct( array $options = null ) {
53 if ( isset( $options['styles'] ) ) {
54 $this->styles = $options['styles'];
55 }
56 if ( isset( $options['scripts'] ) ) {
57 $this->scripts = $options['scripts'];
58 }
59 if ( isset( $options['group'] ) ) {
60 $this->group = $options['group'];
61 }
62 }
63
64 /**
65 * Subclasses should return an associative array of resources in the module.
66 * Keys should be the title of a page in the MediaWiki or User namespace.
67 *
68 * Values should be a nested array of options. The supported keys are 'type' and
69 * (CSS only) 'media'.
70 *
71 * For scripts, 'type' should be 'script'.
72 *
73 * For stylesheets, 'type' should be 'style'.
74 * There is an optional media key, the value of which can be the
75 * medium ('screen', 'print', etc.) of the stylesheet.
76 *
77 * @param ResourceLoaderContext $context
78 * @return array
79 */
80 protected function getPages( ResourceLoaderContext $context ) {
81 $config = $this->getConfig();
82 $pages = array();
83
84 // Filter out pages from origins not allowed by the current wiki configuration.
85 if ( $config->get( 'UseSiteJs' ) ) {
86 foreach ( $this->scripts as $script ) {
87 $pages[$script] = array( 'type' => 'script' );
88 }
89 }
90
91 if ( $config->get( 'UseSiteCss' ) ) {
92 foreach ( $this->styles as $style ) {
93 $pages[$style] = array( 'type' => 'style' );
94 }
95 }
96
97 return $pages;
98 }
99
100 /**
101 * Get group name
102 *
103 * @return string
104 */
105 public function getGroup() {
106 return $this->group;
107 }
108
109 /**
110 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
111 * but subclasses may want to override this to return a remote DB object, or to return
112 * null if getTitleMTimes() shouldn't access the DB at all.
113 *
114 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
115 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
116 * will use the local DB irrespective of the return value of this method.
117 *
118 * @return IDatabase|null
119 */
120 protected function getDB() {
121 return wfGetDB( DB_SLAVE );
122 }
123
124 /**
125 * @param Title $title
126 * @return null|string
127 */
128 protected function getContent( $title ) {
129 $handler = ContentHandler::getForTitle( $title );
130 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
131 $format = CONTENT_FORMAT_CSS;
132 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
133 $format = CONTENT_FORMAT_JAVASCRIPT;
134 } else {
135 return null;
136 }
137
138 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
139 if ( !$revision ) {
140 return null;
141 }
142
143 $content = $revision->getContent( Revision::RAW );
144
145 if ( !$content ) {
146 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
147 return null;
148 }
149
150 return $content->serialize( $format );
151 }
152
153 /**
154 * @param ResourceLoaderContext $context
155 * @return string
156 */
157 public function getScript( ResourceLoaderContext $context ) {
158 $scripts = '';
159 foreach ( $this->getPages( $context ) as $titleText => $options ) {
160 if ( $options['type'] !== 'script' ) {
161 continue;
162 }
163 $title = Title::newFromText( $titleText );
164 if ( !$title || $title->isRedirect() ) {
165 continue;
166 }
167 $script = $this->getContent( $title );
168 if ( strval( $script ) !== '' ) {
169 $script = $this->validateScriptFile( $titleText, $script );
170 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
171 }
172 }
173 return $scripts;
174 }
175
176 /**
177 * @param ResourceLoaderContext $context
178 * @return array
179 */
180 public function getStyles( ResourceLoaderContext $context ) {
181 $styles = array();
182 foreach ( $this->getPages( $context ) as $titleText => $options ) {
183 if ( $options['type'] !== 'style' ) {
184 continue;
185 }
186 $title = Title::newFromText( $titleText );
187 if ( !$title || $title->isRedirect() ) {
188 continue;
189 }
190 $media = isset( $options['media'] ) ? $options['media'] : 'all';
191 $style = $this->getContent( $title );
192 if ( strval( $style ) === '' ) {
193 continue;
194 }
195 if ( $this->getFlip( $context ) ) {
196 $style = CSSJanus::transform( $style, true, false );
197 }
198 $style = CSSMin::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true );
199 if ( !isset( $styles[$media] ) ) {
200 $styles[$media] = array();
201 }
202 $style = ResourceLoader::makeComment( $titleText ) . $style;
203 $styles[$media][] = $style;
204 }
205 return $styles;
206 }
207
208 /**
209 * @param ResourceLoaderContext $context
210 * @return int
211 */
212 public function getModifiedTime( ResourceLoaderContext $context ) {
213 $modifiedTime = 1;
214 $titleInfo = $this->getTitleInfo( $context );
215 if ( count( $titleInfo ) ) {
216 $mtimes = array_map( function ( $value ) {
217 return $value['timestamp'];
218 }, $titleInfo );
219 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
220 }
221 $modifiedTime = max(
222 $modifiedTime,
223 $this->getMsgBlobMtime( $context->getLanguage() ),
224 $this->getDefinitionMtime( $context )
225 );
226 return $modifiedTime;
227 }
228
229 /**
230 * Get the definition summary for this module.
231 *
232 * @param ResourceLoaderContext $context
233 * @return array
234 */
235 public function getDefinitionSummary( ResourceLoaderContext $context ) {
236 return array(
237 'class' => get_class( $this ),
238 'pages' => $this->getPages( $context ),
239 );
240 }
241
242 /**
243 * @param ResourceLoaderContext $context
244 * @return bool
245 */
246 public function isKnownEmpty( ResourceLoaderContext $context ) {
247 $titleInfo = $this->getTitleInfo( $context );
248 // Bug 68488: For modules in the "user" group, we should actually
249 // check that the pages are empty (page_len == 0), but for other
250 // groups, just check the pages exist so that we don't end up
251 // caching temporarily-blank pages without the appropriate
252 // <script> or <link> tag.
253 if ( $this->getGroup() !== 'user' ) {
254 return count( $titleInfo ) === 0;
255 }
256
257 foreach ( $titleInfo as $info ) {
258 if ( $info['length'] !== 0 ) {
259 // At least one non-0-lenth page, not empty
260 return false;
261 }
262 }
263
264 // All pages are 0-length, so it's empty
265 return true;
266 }
267
268 /**
269 * Get the modification times of all titles that would be loaded for
270 * a given context.
271 * @param ResourceLoaderContext $context Context object
272 * @return array Keyed by page dbkey. Value is an array with 'length' and 'timestamp'
273 * keys, where the timestamp is a UNIX timestamp
274 */
275 protected function getTitleInfo( ResourceLoaderContext $context ) {
276 $dbr = $this->getDB();
277 if ( !$dbr ) {
278 // We're dealing with a subclass that doesn't have a DB
279 return array();
280 }
281
282 $hash = $context->getHash();
283 if ( isset( $this->titleInfo[$hash] ) ) {
284 return $this->titleInfo[$hash];
285 }
286
287 $this->titleInfo[$hash] = array();
288 $batch = new LinkBatch;
289 foreach ( $this->getPages( $context ) as $titleText => $options ) {
290 $batch->addObj( Title::newFromText( $titleText ) );
291 }
292
293 if ( !$batch->isEmpty() ) {
294 $res = $dbr->select( 'page',
295 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
296 $batch->constructSet( 'page', $dbr ),
297 __METHOD__
298 );
299 foreach ( $res as $row ) {
300 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
301 $this->titleInfo[$hash][$title->getPrefixedDBkey()] = array(
302 'timestamp' => wfTimestamp( TS_UNIX, $row->page_touched ),
303 'length' => $row->page_len,
304 );
305 }
306 }
307 return $this->titleInfo[$hash];
308 }
309 }