ResourceLoaderWikiModule: Check content format instead of content model
[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 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
33
34 /* Protected Members */
35
36 # Origin is user-supplied code
37 protected $origin = self::ORIGIN_USER_SITEWIDE;
38
39 // In-object cache for title info
40 protected $titleInfo = array();
41
42 /* Abstract Protected Methods */
43
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.
47 *
48 * Values should be a nested array of options. The supported keys are 'type' and
49 * (CSS only) 'media'.
50 *
51 * For scripts, 'type' should be 'script'.
52 *
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.
56 *
57 * @param ResourceLoaderContext $context
58 * @return array
59 */
60 abstract protected function getPages( ResourceLoaderContext $context );
61
62 /* Protected Methods */
63
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.
68 *
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.
72 *
73 * @return DatabaseBase|null
74 */
75 protected function getDB() {
76 return wfGetDB( DB_SLAVE );
77 }
78
79 /**
80 * @param Title $title
81 * @return null|string
82 */
83 protected function getContent( $title ) {
84 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
85 return null;
86 }
87 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
88 if ( !$revision ) {
89 return null;
90 }
91
92 $content = $revision->getContent( Revision::RAW );
93
94 if ( !$content ) {
95 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
96 return null;
97 }
98
99 if ( $content->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
100 return $content->serialize( CONTENT_FORMAT_JAVASCRIPT );
101 } elseif ( $content->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
102 return $content->serialize( CONTENT_FORMAT_CSS );
103 } else {
104 wfDebugLog( 'resourceloader', __METHOD__ . ": bad content model {$content->getModel()} for JS/CSS page!" );
105 return null;
106 }
107 }
108
109 /* Methods */
110
111 /**
112 * @param ResourceLoaderContext $context
113 * @return string
114 */
115 public function getScript( ResourceLoaderContext $context ) {
116 $scripts = '';
117 foreach ( $this->getPages( $context ) as $titleText => $options ) {
118 if ( $options['type'] !== 'script' ) {
119 continue;
120 }
121 $title = Title::newFromText( $titleText );
122 if ( !$title || $title->isRedirect() ) {
123 continue;
124 }
125 $script = $this->getContent( $title );
126 if ( strval( $script ) !== '' ) {
127 $script = $this->validateScriptFile( $titleText, $script );
128 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
129 }
130 }
131 return $scripts;
132 }
133
134 /**
135 * @param ResourceLoaderContext $context
136 * @return array
137 */
138 public function getStyles( ResourceLoaderContext $context ) {
139 $styles = array();
140 foreach ( $this->getPages( $context ) as $titleText => $options ) {
141 if ( $options['type'] !== 'style' ) {
142 continue;
143 }
144 $title = Title::newFromText( $titleText );
145 if ( !$title || $title->isRedirect() ) {
146 continue;
147 }
148 $media = isset( $options['media'] ) ? $options['media'] : 'all';
149 $style = $this->getContent( $title );
150 if ( strval( $style ) === '' ) {
151 continue;
152 }
153 if ( $this->getFlip( $context ) ) {
154 $style = CSSJanus::transform( $style, true, false );
155 } else {
156 $style = CSSJanus::nullTransform( $style );
157 }
158 $style = CSSMin::remap( $style, false, $this->getConfig()->get( 'ScriptPath' ), true );
159 if ( !isset( $styles[$media] ) ) {
160 $styles[$media] = array();
161 }
162 $style = ResourceLoader::makeComment( $titleText ) . $style;
163 $styles[$media][] = $style;
164 }
165 return $styles;
166 }
167
168 /**
169 * @param ResourceLoaderContext $context
170 * @return int|mixed
171 */
172 public function getModifiedTime( ResourceLoaderContext $context ) {
173 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
174 $titleInfo = $this->getTitleInfo( $context );
175 if ( count( $titleInfo ) ) {
176 $mtimes = array_map( function( $value ) {
177 return $value['timestamp'];
178 }, $titleInfo );
179 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
180 }
181 $modifiedTime = max(
182 $modifiedTime,
183 $this->getMsgBlobMtime( $context->getLanguage() ),
184 $this->getDefinitionMtime( $context )
185 );
186 return $modifiedTime;
187 }
188
189 /**
190 * Get the definition summary for this module.
191 *
192 * @param ResourceLoaderContext $context
193 * @return array
194 */
195 public function getDefinitionSummary( ResourceLoaderContext $context ) {
196 return array(
197 'class' => get_class( $this ),
198 'pages' => $this->getPages( $context ),
199 );
200 }
201
202 /**
203 * @param ResourceLoaderContext $context
204 * @return bool
205 */
206 public function isKnownEmpty( ResourceLoaderContext $context ) {
207 $titleInfo = $this->getTitleInfo( $context );
208 // Bug 68488: For modules in the "user" group, we should actually
209 // check that the pages are empty (page_len == 0), but for other
210 // groups, just check the pages exist so that we don't end up
211 // caching temporarily-blank pages without the appropriate
212 // <script> or <link> tag.
213 if ( $this->getGroup() !== 'user' ) {
214 return count( $titleInfo ) === 0;
215 }
216
217 foreach ( $titleInfo as $info ) {
218 if ( $info['length'] !== 0 ) {
219 // At least one non-0-lenth page, not empty
220 return false;
221 }
222 }
223
224 // All pages are 0-length, so it's empty
225 return true;
226 }
227
228 /**
229 * Get the modification times of all titles that would be loaded for
230 * a given context.
231 * @param ResourceLoaderContext $context Context object
232 * @return array keyed by page dbkey, with value is an array with 'length' and 'timestamp'
233 * keys, where the timestamp is a unix one
234 */
235 protected function getTitleInfo( ResourceLoaderContext $context ) {
236 $dbr = $this->getDB();
237 if ( !$dbr ) {
238 // We're dealing with a subclass that doesn't have a DB
239 return array();
240 }
241
242 $hash = $context->getHash();
243 if ( isset( $this->titleInfo[$hash] ) ) {
244 return $this->titleInfo[$hash];
245 }
246
247 $this->titleInfo[$hash] = array();
248 $batch = new LinkBatch;
249 foreach ( $this->getPages( $context ) as $titleText => $options ) {
250 $batch->addObj( Title::newFromText( $titleText ) );
251 }
252
253 if ( !$batch->isEmpty() ) {
254 $res = $dbr->select( 'page',
255 array( 'page_namespace', 'page_title', 'page_touched', 'page_len' ),
256 $batch->constructSet( 'page', $dbr ),
257 __METHOD__
258 );
259 foreach ( $res as $row ) {
260 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
261 $this->titleInfo[$hash][$title->getPrefixedDBkey()] = array(
262 'timestamp' => wfTimestamp( TS_UNIX, $row->page_touched ),
263 'length' => $row->page_len,
264 );
265 }
266 }
267 return $this->titleInfo[$hash];
268 }
269 }