Merge "(bug 34876) improve $.makeCollapsible performance on the initial collapsing"
[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 mtimes
40 protected $titleMtimes = 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 $context ResourceLoaderContext
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 wfDebug( __METHOD__ . "failed to load content of JS/CSS page!\n" );
96 return null;
97 }
98
99 $model = $content->getModel();
100
101 if ( $model !== CONTENT_MODEL_CSS && $model !== CONTENT_MODEL_JAVASCRIPT ) {
102 wfDebug( __METHOD__ . "bad content model $model for JS/CSS page!\n" );
103 return null;
104 }
105
106 return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS
107 }
108
109 /* Methods */
110
111 /**
112 * @param $context ResourceLoaderContext
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 if ( strpos( $titleText, '*/' ) === false ) {
129 $scripts .= "/* $titleText */\n";
130 }
131 $scripts .= $script . "\n";
132 }
133 }
134 return $scripts;
135 }
136
137 /**
138 * @param $context ResourceLoaderContext
139 * @return array
140 */
141 public function getStyles( ResourceLoaderContext $context ) {
142 global $wgScriptPath;
143
144 $styles = array();
145 foreach ( $this->getPages( $context ) as $titleText => $options ) {
146 if ( $options['type'] !== 'style' ) {
147 continue;
148 }
149 $title = Title::newFromText( $titleText );
150 if ( !$title || $title->isRedirect() ) {
151 continue;
152 }
153 $media = isset( $options['media'] ) ? $options['media'] : 'all';
154 $style = $this->getContent( $title );
155 if ( strval( $style ) === '' ) {
156 continue;
157 }
158 if ( $this->getFlip( $context ) ) {
159 $style = CSSJanus::transform( $style, true, false );
160 }
161 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
162 if ( !isset( $styles[$media] ) ) {
163 $styles[$media] = array();
164 }
165 if ( strpos( $titleText, '*/' ) === false ) {
166 $style = "/* $titleText */\n" . $style;
167 }
168 $styles[$media][] = $style;
169 }
170 return $styles;
171 }
172
173 /**
174 * @param $context ResourceLoaderContext
175 * @return int|mixed
176 */
177 public function getModifiedTime( ResourceLoaderContext $context ) {
178 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
179 $mtimes = $this->getTitleMtimes( $context );
180 if ( count( $mtimes ) ) {
181 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
182 }
183 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
184 return $modifiedTime;
185 }
186
187 /**
188 * @param $context ResourceLoaderContext
189 * @return bool
190 */
191 public function isKnownEmpty( ResourceLoaderContext $context ) {
192 return count( $this->getTitleMtimes( $context ) ) == 0;
193 }
194
195 /**
196 * Get the modification times of all titles that would be loaded for
197 * a given context.
198 * @param $context ResourceLoaderContext: Context object
199 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
200 */
201 protected function getTitleMtimes( ResourceLoaderContext $context ) {
202 $dbr = $this->getDB();
203 if ( !$dbr ) {
204 // We're dealing with a subclass that doesn't have a DB
205 return array();
206 }
207
208 $hash = $context->getHash();
209 if ( isset( $this->titleMtimes[$hash] ) ) {
210 return $this->titleMtimes[$hash];
211 }
212
213 $this->titleMtimes[$hash] = array();
214 $batch = new LinkBatch;
215 foreach ( $this->getPages( $context ) as $titleText => $options ) {
216 $batch->addObj( Title::newFromText( $titleText ) );
217 }
218
219 if ( !$batch->isEmpty() ) {
220 $res = $dbr->select( 'page',
221 array( 'page_namespace', 'page_title', 'page_touched' ),
222 $batch->constructSet( 'page', $dbr ),
223 __METHOD__
224 );
225 foreach ( $res as $row ) {
226 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
227 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
228 wfTimestamp( TS_UNIX, $row->page_touched );
229 }
230 }
231 return $this->titleMtimes[$hash];
232 }
233 }