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