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