* (bug 28626) Validate JavaScript files and pages loaded via ResourceLoader before...
[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::isValidCssJsSubpage.
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 abstract protected function getPages( ResourceLoaderContext $context );
45
46 /* Protected Methods */
47
48 /**
49 * @param $title Title
50 * @return null|string
51 */
52 protected function getContent( $title ) {
53 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
54 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
55 return $message->exists() ? $message->plain() : '';
56 }
57 if ( !$title->isCssJsSubpage() ) {
58 return null;
59 }
60 $revision = Revision::newFromTitle( $title );
61 if ( !$revision ) {
62 return null;
63 }
64 return $revision->getRawText();
65 }
66
67 /* Methods */
68
69 /**
70 * @param $context ResourceLoaderContext
71 * @return string
72 */
73 public function getScript( ResourceLoaderContext $context ) {
74 $scripts = '';
75 foreach ( $this->getPages( $context ) as $titleText => $options ) {
76 if ( $options['type'] !== 'script' ) {
77 continue;
78 }
79 $title = Title::newFromText( $titleText );
80 if ( !$title ) {
81 continue;
82 }
83 $script = $this->getContent( $title );
84 if ( strval( $script ) !== '' ) {
85 $script = $this->validateScriptFile( $titleText, $script );
86 if ( strpos( $titleText, '*/' ) === false ) {
87 $scripts .= "/* $titleText */\n";
88 }
89 $scripts .= $script . "\n";
90 }
91 }
92 return $scripts;
93 }
94
95 /**
96 * @param $context ResourceLoaderContext
97 * @return array
98 */
99 public function getStyles( ResourceLoaderContext $context ) {
100 global $wgScriptPath;
101
102 $styles = array();
103 foreach ( $this->getPages( $context ) as $titleText => $options ) {
104 if ( $options['type'] !== 'style' ) {
105 continue;
106 }
107 $title = Title::newFromText( $titleText );
108 if ( !$title ) {
109 continue;
110 }
111 $media = isset( $options['media'] ) ? $options['media'] : 'all';
112 $style = $this->getContent( $title );
113 if ( strval( $style ) === '' ) {
114 continue;
115 }
116 if ( $this->getFlip( $context ) ) {
117 $style = CSSJanus::transform( $style, true, false );
118 }
119 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
120 if ( !isset( $styles[$media] ) ) {
121 $styles[$media] = '';
122 }
123 if ( strpos( $titleText, '*/' ) === false ) {
124 $styles[$media] .= "/* $titleText */\n";
125 }
126 $styles[$media] .= $style . "\n";
127 }
128 return $styles;
129 }
130
131 /**
132 * @param $context ResourceLoaderContext
133 * @return int|mixed
134 */
135 public function getModifiedTime( ResourceLoaderContext $context ) {
136 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
137 $mtimes = $this->getTitleMtimes( $context );
138 if ( count( $mtimes ) ) {
139 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
140 }
141 return $modifiedTime;
142 }
143
144 /**
145 * @param $context ResourceLoaderContext
146 * @return bool
147 */
148 public function isKnownEmpty( ResourceLoaderContext $context ) {
149 return count( $this->getTitleMtimes( $context ) ) == 0;
150 }
151
152 /**
153 * Get the modification times of all titles that would be loaded for
154 * a given context.
155 * @param $context ResourceLoaderContext: Context object
156 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
157 */
158 protected function getTitleMtimes( ResourceLoaderContext $context ) {
159 $hash = $context->getHash();
160 if ( isset( $this->titleMtimes[$hash] ) ) {
161 return $this->titleMtimes[$hash];
162 }
163
164 $this->titleMtimes[$hash] = array();
165 $batch = new LinkBatch;
166 foreach ( $this->getPages( $context ) as $titleText => $options ) {
167 $batch->addObj( Title::newFromText( $titleText ) );
168 }
169
170 if ( !$batch->isEmpty() ) {
171 $dbr = wfGetDB( DB_SLAVE );
172 $res = $dbr->select( 'page',
173 array( 'page_namespace', 'page_title', 'page_touched' ),
174 $batch->constructSet( 'page', $dbr ),
175 __METHOD__
176 );
177 foreach ( $res as $row ) {
178 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
179 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
180 wfTimestamp( TS_UNIX, $row->page_touched );
181 }
182 }
183 return $this->titleMtimes[$hash];
184 }
185 }