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