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