Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[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 * @param ResourceLoaderContext $context
231 * @return array
232 */
233 public function getDefinitionSummary( ResourceLoaderContext $context ) {
234 $summary = parent::getDefinitionSummary( $context );
235 $summary[] = array(
236 'pages' => $this->getPages( $context ),
237 );
238 return $summary;
239 }
240
241 /**
242 * @param ResourceLoaderContext $context
243 * @return bool
244 */
245 public function isKnownEmpty( ResourceLoaderContext $context ) {
246 $titleInfo = $this->getTitleInfo( $context );
247 // Bug 68488: For modules in the "user" group, we should actually
248 // check that the pages are empty (page_len == 0), but for other
249 // groups, just check the pages exist so that we don't end up
250 // caching temporarily-blank pages without the appropriate
251 // <script> or <link> tag.
252 if ( $this->getGroup() !== 'user' ) {
253 return count( $titleInfo ) === 0;
254 }
255
256 foreach ( $titleInfo as $info ) {
257 if ( $info['length'] !== 0 ) {
258 // At least one non-0-lenth page, not empty
259 return false;
260 }
261 }
262
263 // All pages are 0-length, so it's empty
264 return true;
265 }
266
267 /**
268 * Get the modification times of all titles that would be loaded for
269 * a given context.
270 * @param ResourceLoaderContext $context Context object
271 * @return array Keyed by page dbkey. Value is an array with 'length' and 'timestamp'
272 * keys, where the timestamp is a UNIX timestamp
273 */
274 protected function getTitleInfo( ResourceLoaderContext $context ) {
275 $dbr = $this->getDB();
276 if ( !$dbr ) {
277 // We're dealing with a subclass that doesn't have a DB
278 return array();
279 }
280
281 $hash = $context->getHash();
282 if ( isset( $this->titleInfo[$hash] ) ) {
283 return $this->titleInfo[$hash];
284 }
285
286 $this->titleInfo[$hash] = array();
287 $batch = new LinkBatch;
288 foreach ( $this->getPages( $context ) as $titleText => $options ) {
289 $batch->addObj( Title::newFromText( $titleText ) );
290 }
291
292 if ( !$batch->isEmpty() ) {
293 $res = $dbr->select( 'page',
294 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
295 $batch->constructSet( 'page', $dbr ),
296 __METHOD__
297 );
298 foreach ( $res as $row ) {
299 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
300 $this->titleInfo[$hash][$title->getPrefixedDBkey()] = array(
301 'timestamp' => wfTimestamp( TS_UNIX, $row->page_touched ),
302 'length' => $row->page_len,
303 );
304 }
305 }
306 return $this->titleInfo[$hash];
307 }
308 }