Merge "[FileRepo] Fixed file move data-loss race condition."
[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 return $revision->getRawText();
86 }
87
88 /* Methods */
89
90 /**
91 * @param $context ResourceLoaderContext
92 * @return string
93 */
94 public function getScript( ResourceLoaderContext $context ) {
95 $scripts = '';
96 foreach ( $this->getPages( $context ) as $titleText => $options ) {
97 if ( $options['type'] !== 'script' ) {
98 continue;
99 }
100 $title = Title::newFromText( $titleText );
101 if ( !$title || $title->isRedirect() ) {
102 continue;
103 }
104 $script = $this->getContent( $title );
105 if ( strval( $script ) !== '' ) {
106 $script = $this->validateScriptFile( $titleText, $script );
107 if ( strpos( $titleText, '*/' ) === false ) {
108 $scripts .= "/* $titleText */\n";
109 }
110 $scripts .= $script . "\n";
111 }
112 }
113 return $scripts;
114 }
115
116 /**
117 * @param $context ResourceLoaderContext
118 * @return array
119 */
120 public function getStyles( ResourceLoaderContext $context ) {
121 global $wgScriptPath;
122
123 $styles = array();
124 foreach ( $this->getPages( $context ) as $titleText => $options ) {
125 if ( $options['type'] !== 'style' ) {
126 continue;
127 }
128 $title = Title::newFromText( $titleText );
129 if ( !$title || $title->isRedirect() ) {
130 continue;
131 }
132 $media = isset( $options['media'] ) ? $options['media'] : 'all';
133 $style = $this->getContent( $title );
134 if ( strval( $style ) === '' ) {
135 continue;
136 }
137 if ( $this->getFlip( $context ) ) {
138 $style = CSSJanus::transform( $style, true, false );
139 }
140 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
141 if ( !isset( $styles[$media] ) ) {
142 $styles[$media] = '';
143 }
144 if ( strpos( $titleText, '*/' ) === false ) {
145 $styles[$media] .= "/* $titleText */\n";
146 }
147 $styles[$media] .= $style . "\n";
148 }
149 return $styles;
150 }
151
152 /**
153 * @param $context ResourceLoaderContext
154 * @return int|mixed
155 */
156 public function getModifiedTime( ResourceLoaderContext $context ) {
157 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
158 $mtimes = $this->getTitleMtimes( $context );
159 if ( count( $mtimes ) ) {
160 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
161 }
162 $modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
163 return $modifiedTime;
164 }
165
166 /**
167 * @param $context ResourceLoaderContext
168 * @return bool
169 */
170 public function isKnownEmpty( ResourceLoaderContext $context ) {
171 return count( $this->getTitleMtimes( $context ) ) == 0;
172 }
173
174 /**
175 * Get the modification times of all titles that would be loaded for
176 * a given context.
177 * @param $context ResourceLoaderContext: Context object
178 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
179 */
180 protected function getTitleMtimes( ResourceLoaderContext $context ) {
181 $dbr = $this->getDB();
182 if ( !$dbr ) {
183 // We're dealing with a subclass that doesn't have a DB
184 return array();
185 }
186
187 $hash = $context->getHash();
188 if ( isset( $this->titleMtimes[$hash] ) ) {
189 return $this->titleMtimes[$hash];
190 }
191
192 $this->titleMtimes[$hash] = array();
193 $batch = new LinkBatch;
194 foreach ( $this->getPages( $context ) as $titleText => $options ) {
195 $batch->addObj( Title::newFromText( $titleText ) );
196 }
197
198 if ( !$batch->isEmpty() ) {
199 $res = $dbr->select( 'page',
200 array( 'page_namespace', 'page_title', 'page_touched' ),
201 $batch->constructSet( 'page', $dbr ),
202 __METHOD__
203 );
204 foreach ( $res as $row ) {
205 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
206 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
207 wfTimestamp( TS_UNIX, $row->page_touched );
208 }
209 }
210 return $this->titleMtimes[$hash];
211 }
212 }