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