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