Removed Title::isValidCssJsSubpage(), deprecated since 1.17 (77309), and updated...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderWikiModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 defined( 'MEDIAWIKI' ) || die( 1 );
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 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
74 return $message->exists() ? $message->plain() : '';
75 }
76 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
77 return null;
78 }
79 $revision = Revision::newFromTitle( $title );
80 if ( !$revision ) {
81 return null;
82 }
83 return $revision->getRawText();
84 }
85
86 /* Methods */
87
88 /**
89 * @param $context ResourceLoaderContext
90 * @return string
91 */
92 public function getScript( ResourceLoaderContext $context ) {
93 $scripts = '';
94 foreach ( $this->getPages( $context ) as $titleText => $options ) {
95 if ( $options['type'] !== 'script' ) {
96 continue;
97 }
98 $title = Title::newFromText( $titleText );
99 if ( !$title || $title->isRedirect() ) {
100 continue;
101 }
102 $script = $this->getContent( $title );
103 if ( strval( $script ) !== '' ) {
104 $script = $this->validateScriptFile( $titleText, $script );
105 if ( strpos( $titleText, '*/' ) === false ) {
106 $scripts .= "/* $titleText */\n";
107 }
108 $scripts .= $script . "\n";
109 }
110 }
111 return $scripts;
112 }
113
114 /**
115 * @param $context ResourceLoaderContext
116 * @return array
117 */
118 public function getStyles( ResourceLoaderContext $context ) {
119 global $wgScriptPath;
120
121 $styles = array();
122 foreach ( $this->getPages( $context ) as $titleText => $options ) {
123 if ( $options['type'] !== 'style' ) {
124 continue;
125 }
126 $title = Title::newFromText( $titleText );
127 if ( !$title || $title->isRedirect() ) {
128 continue;
129 }
130 $media = isset( $options['media'] ) ? $options['media'] : 'all';
131 $style = $this->getContent( $title );
132 if ( strval( $style ) === '' ) {
133 continue;
134 }
135 if ( $this->getFlip( $context ) ) {
136 $style = CSSJanus::transform( $style, true, false );
137 }
138 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
139 if ( !isset( $styles[$media] ) ) {
140 $styles[$media] = '';
141 }
142 if ( strpos( $titleText, '*/' ) === false ) {
143 $styles[$media] .= "/* $titleText */\n";
144 }
145 $styles[$media] .= $style . "\n";
146 }
147 return $styles;
148 }
149
150 /**
151 * @param $context ResourceLoaderContext
152 * @return int|mixed
153 */
154 public function getModifiedTime( ResourceLoaderContext $context ) {
155 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
156 $mtimes = $this->getTitleMtimes( $context );
157 if ( count( $mtimes ) ) {
158 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
159 }
160 return $modifiedTime;
161 }
162
163 /**
164 * @param $context ResourceLoaderContext
165 * @return bool
166 */
167 public function isKnownEmpty( ResourceLoaderContext $context ) {
168 return count( $this->getTitleMtimes( $context ) ) == 0;
169 }
170
171 /**
172 * Get the modification times of all titles that would be loaded for
173 * a given context.
174 * @param $context ResourceLoaderContext: Context object
175 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
176 */
177 protected function getTitleMtimes( ResourceLoaderContext $context ) {
178 $dbr = $this->getDB();
179 if ( !$dbr ) {
180 // We're dealing with a subclass that doesn't have a DB
181 return array();
182 }
183
184 $hash = $context->getHash();
185 if ( isset( $this->titleMtimes[$hash] ) ) {
186 return $this->titleMtimes[$hash];
187 }
188
189 $this->titleMtimes[$hash] = array();
190 $batch = new LinkBatch;
191 foreach ( $this->getPages( $context ) as $titleText => $options ) {
192 $batch->addObj( Title::newFromText( $titleText ) );
193 }
194
195 if ( !$batch->isEmpty() ) {
196 $res = $dbr->select( 'page',
197 array( 'page_namespace', 'page_title', 'page_touched' ),
198 $batch->constructSet( 'page', $dbr ),
199 __METHOD__
200 );
201 foreach ( $res as $row ) {
202 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
203 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
204 wfTimestamp( TS_UNIX, $row->page_touched );
205 }
206 }
207 return $this->titleMtimes[$hash];
208 }
209 }