Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[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 * @abstract
46 * @param $context ResourceLoaderContext
47 */
48 abstract protected function getPages( ResourceLoaderContext $context );
49
50 /* Protected Methods */
51
52 /**
53 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
54 * but subclasses may want to override this to return a remote DB object, or to return
55 * null if getTitleMTimes() shouldn't access the DB at all.
56 *
57 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
58 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
59 * will use the local DB irrespective of the return value of this method.
60 *
61 * @return DatabaseBase|null
62 */
63 protected function getDB() {
64 return wfGetDB( DB_SLAVE );
65 }
66
67 /**
68 * @param $title Title
69 * @return null|string
70 */
71 protected function getContent( $title ) {
72 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
73 // The first "true" is to use the database, the second is to use the content langue
74 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
75 $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
76 return $text === false ? '' : $text;
77 }
78 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
79 return null;
80 }
81 $revision = Revision::newFromTitle( $title );
82 if ( !$revision ) {
83 return null;
84 }
85
86 $content = $revision->getContent( Revision::RAW );
87 $model = $content->getModel();
88
89 if ( $model !== CONTENT_MODEL_CSS && $model !== CONTENT_MODEL_JAVASCRIPT ) {
90 wfDebug( __METHOD__ . "bad content model #$model for JS/CSS page!\n" );
91 return null;
92 }
93
94 return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS
95 }
96
97 /* Methods */
98
99 /**
100 * @param $context ResourceLoaderContext
101 * @return string
102 */
103 public function getScript( ResourceLoaderContext $context ) {
104 $scripts = '';
105 foreach ( $this->getPages( $context ) as $titleText => $options ) {
106 if ( $options['type'] !== 'script' ) {
107 continue;
108 }
109 $title = Title::newFromText( $titleText );
110 if ( !$title || $title->isRedirect() ) {
111 continue;
112 }
113 $script = $this->getContent( $title );
114 if ( strval( $script ) !== '' ) {
115 $script = $this->validateScriptFile( $titleText, $script );
116 if ( strpos( $titleText, '*/' ) === false ) {
117 $scripts .= "/* $titleText */\n";
118 }
119 $scripts .= $script . "\n";
120 }
121 }
122 return $scripts;
123 }
124
125 /**
126 * @param $context ResourceLoaderContext
127 * @return array
128 */
129 public function getStyles( ResourceLoaderContext $context ) {
130 global $wgScriptPath;
131
132 $styles = array();
133 foreach ( $this->getPages( $context ) as $titleText => $options ) {
134 if ( $options['type'] !== 'style' ) {
135 continue;
136 }
137 $title = Title::newFromText( $titleText );
138 if ( !$title || $title->isRedirect() ) {
139 continue;
140 }
141 $media = isset( $options['media'] ) ? $options['media'] : 'all';
142 $style = $this->getContent( $title );
143 if ( strval( $style ) === '' ) {
144 continue;
145 }
146 if ( $this->getFlip( $context ) ) {
147 $style = CSSJanus::transform( $style, true, false );
148 }
149 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
150 if ( !isset( $styles[$media] ) ) {
151 $styles[$media] = '';
152 }
153 if ( strpos( $titleText, '*/' ) === false ) {
154 $styles[$media] .= "/* $titleText */\n";
155 }
156 $styles[$media] .= $style . "\n";
157 }
158 return $styles;
159 }
160
161 /**
162 * @param $context ResourceLoaderContext
163 * @return int|mixed
164 */
165 public function getModifiedTime( ResourceLoaderContext $context ) {
166 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
167 $mtimes = $this->getTitleMtimes( $context );
168 if ( count( $mtimes ) ) {
169 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
170 }
171 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
172 return $modifiedTime;
173 }
174
175 /**
176 * @param $context ResourceLoaderContext
177 * @return bool
178 */
179 public function isKnownEmpty( ResourceLoaderContext $context ) {
180 return count( $this->getTitleMtimes( $context ) ) == 0;
181 }
182
183 /**
184 * Get the modification times of all titles that would be loaded for
185 * a given context.
186 * @param $context ResourceLoaderContext: Context object
187 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
188 */
189 protected function getTitleMtimes( ResourceLoaderContext $context ) {
190 $dbr = $this->getDB();
191 if ( !$dbr ) {
192 // We're dealing with a subclass that doesn't have a DB
193 return array();
194 }
195
196 $hash = $context->getHash();
197 if ( isset( $this->titleMtimes[$hash] ) ) {
198 return $this->titleMtimes[$hash];
199 }
200
201 $this->titleMtimes[$hash] = array();
202 $batch = new LinkBatch;
203 foreach ( $this->getPages( $context ) as $titleText => $options ) {
204 $batch->addObj( Title::newFromText( $titleText ) );
205 }
206
207 if ( !$batch->isEmpty() ) {
208 $res = $dbr->select( 'page',
209 array( 'page_namespace', 'page_title', 'page_touched' ),
210 $batch->constructSet( 'page', $dbr ),
211 __METHOD__
212 );
213 foreach ( $res as $row ) {
214 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
215 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
216 wfTimestamp( TS_UNIX, $row->page_touched );
217 }
218 }
219 return $this->titleMtimes[$hash];
220 }
221 }