Fixes for ResourceLoaderWikiModule r72776. No serious bugs found, do not merge before...
[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 modified time
40 protected $modifiedTime = array();
41
42 /* Abstract Protected Methods */
43
44 abstract protected function getPages( ResourceLoaderContext $context );
45
46 /* Protected Methods */
47
48 protected function getContent( $title ) {
49 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
50 $dbkey = $title->getDBkey();
51 return wfEmptyMsg( $dbkey ) ? '' : wfMsgExt( $dbkey, 'content' );
52 }
53 if ( !$title->isCssJsSubpage() ) {
54 return null;
55 }
56 $revision = Revision::newFromTitle( $title );
57 if ( !$revision ) {
58 return null;
59 }
60 return $revision->getRawText();
61 }
62
63 /* Methods */
64
65 public function getScript( ResourceLoaderContext $context ) {
66 $scripts = '';
67 foreach ( $this->getPages( $context ) as $titleText => $options ) {
68 if ( $options['type'] !== 'script' ) {
69 continue;
70 }
71 $title = Title::newFromText( $titleText );
72 if ( !$title ) {
73 continue;
74 }
75 $script = $this->getContent( $title );
76 if ( strval( $script ) !== '' ) {
77 if ( strpos( $titleText, '*/' ) === false ) {
78 $scripts .= "/* $titleText */\n";
79 }
80 $scripts .= $script . "\n";
81 }
82 }
83 return $scripts;
84 }
85
86 public function getStyles( ResourceLoaderContext $context ) {
87
88 $styles = array();
89 foreach ( $this->getPages( $context ) as $titleText => $options ) {
90 if ( $options['type'] !== 'style' ) {
91 continue;
92 }
93 $title = Title::newFromText( $titleText );
94 if ( !$title ) {
95 continue;
96 }
97 $media = isset( $options['media'] ) ? $options['media'] : 'all';
98 $style = $this->getContent( $title );
99 if ( strval( $style ) === '' ) {
100 continue;
101 }
102 if ( $this->getFlip( $context ) ) {
103 $style = CSSJanus::transform( $style, true, false );
104 }
105 if ( !isset( $styles[$media] ) ) {
106 $styles[$media] = '';
107 }
108 if ( strpos( $titleText, '*/' ) === false ) {
109 $styles[$media] .= "/* $titleText */\n";
110 }
111 $styles[$media] .= $style . "\n";
112 }
113 return $styles;
114 }
115
116 public function getModifiedTime( ResourceLoaderContext $context ) {
117 $hash = $context->getHash();
118 if ( isset( $this->modifiedTime[$hash] ) ) {
119 return $this->modifiedTime[$hash];
120 }
121
122 $batch = new LinkBatch;
123 foreach ( $this->getPages( $context ) as $titleText => $options ) {
124 $batch->addObj( Title::newFromText( $titleText ) );
125 }
126
127 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
128 if ( !$batch->isEmpty() ) {
129 $dbr = wfGetDB( DB_SLAVE );
130 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
131 $batch->constructSet( 'page', $dbr ),
132 __METHOD__ );
133
134 if ( $latest ) {
135 $modifiedTime = wfTimestamp( TS_UNIX, $latest );
136 }
137 }
138
139 $this->modifiedTime[$hash] = $modifiedTime;
140 return $modifiedTime;
141 }
142 }