Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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 * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
50 * but subclasses may want to override this to return a remote DB object.
51 *
52 * NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
53 * In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
54 * will use the local DB irrespective of the return value of this method.
55 *
56 * @return DatabaseBase
57 */
58 protected function getDB() {
59 return wfGetDB( DB_SLAVE );
60 }
61
62 /**
63 * @param $title Title
64 * @return null|string
65 */
66 protected function getContent( $title ) {
67 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
68 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
69 return $message->exists() ? $message->plain() : '';
70 }
71 if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
72 return null;
73 }
74 $revision = Revision::newFromTitle( $title );
75 if ( !$revision ) {
76 return null;
77 }
78 return $revision->getRawText();
79 }
80
81 /* Methods */
82
83 /**
84 * @param $context ResourceLoaderContext
85 * @return string
86 */
87 public function getScript( ResourceLoaderContext $context ) {
88 $scripts = '';
89 foreach ( $this->getPages( $context ) as $titleText => $options ) {
90 if ( $options['type'] !== 'script' ) {
91 continue;
92 }
93 $title = Title::newFromText( $titleText );
94 if ( !$title || $title->isRedirect() ) {
95 continue;
96 }
97 $script = $this->getContent( $title );
98 if ( strval( $script ) !== '' ) {
99 $script = $this->validateScriptFile( $titleText, $script );
100 if ( strpos( $titleText, '*/' ) === false ) {
101 $scripts .= "/* $titleText */\n";
102 }
103 $scripts .= $script . "\n";
104 }
105 }
106 return $scripts;
107 }
108
109 /**
110 * @param $context ResourceLoaderContext
111 * @return array
112 */
113 public function getStyles( ResourceLoaderContext $context ) {
114 global $wgScriptPath;
115
116 $styles = array();
117 foreach ( $this->getPages( $context ) as $titleText => $options ) {
118 if ( $options['type'] !== 'style' ) {
119 continue;
120 }
121 $title = Title::newFromText( $titleText );
122 if ( !$title || $title->isRedirect() ) {
123 continue;
124 }
125 $media = isset( $options['media'] ) ? $options['media'] : 'all';
126 $style = $this->getContent( $title );
127 if ( strval( $style ) === '' ) {
128 continue;
129 }
130 if ( $this->getFlip( $context ) ) {
131 $style = CSSJanus::transform( $style, true, false );
132 }
133 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
134 if ( !isset( $styles[$media] ) ) {
135 $styles[$media] = '';
136 }
137 if ( strpos( $titleText, '*/' ) === false ) {
138 $styles[$media] .= "/* $titleText */\n";
139 }
140 $styles[$media] .= $style . "\n";
141 }
142 return $styles;
143 }
144
145 /**
146 * @param $context ResourceLoaderContext
147 * @return int|mixed
148 */
149 public function getModifiedTime( ResourceLoaderContext $context ) {
150 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
151 $mtimes = $this->getTitleMtimes( $context );
152 if ( count( $mtimes ) ) {
153 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
154 }
155 return $modifiedTime;
156 }
157
158 /**
159 * @param $context ResourceLoaderContext
160 * @return bool
161 */
162 public function isKnownEmpty( ResourceLoaderContext $context ) {
163 return count( $this->getTitleMtimes( $context ) ) == 0;
164 }
165
166 /**
167 * Get the modification times of all titles that would be loaded for
168 * a given context.
169 * @param $context ResourceLoaderContext: Context object
170 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
171 */
172 protected function getTitleMtimes( ResourceLoaderContext $context ) {
173 $hash = $context->getHash();
174 if ( isset( $this->titleMtimes[$hash] ) ) {
175 return $this->titleMtimes[$hash];
176 }
177
178 $this->titleMtimes[$hash] = array();
179 $batch = new LinkBatch;
180 foreach ( $this->getPages( $context ) as $titleText => $options ) {
181 $batch->addObj( Title::newFromText( $titleText ) );
182 }
183
184 if ( !$batch->isEmpty() ) {
185 $dbr = $this->getDB();
186 $res = $dbr->select( 'page',
187 array( 'page_namespace', 'page_title', 'page_touched' ),
188 $batch->constructSet( 'page', $dbr ),
189 __METHOD__
190 );
191 foreach ( $res as $row ) {
192 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
193 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
194 wfTimestamp( TS_UNIX, $row->page_touched );
195 }
196 }
197 return $this->titleMtimes[$hash];
198 }
199 }