(bug 25573) Send Cache-Control: must-revalidate in debug mode
[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, because of it's dependence on the
29 * functionality of Title::isValidCssJsSubpage.
30 */
31 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
32
33 /* Protected Members */
34
35 // In-object cache for modified time
36 protected $modifiedTime = array();
37
38 /* Abstract Protected Methods */
39
40 abstract protected function getPages( ResourceLoaderContext $context );
41
42 /* Protected Methods */
43
44 protected function getContent( $page, $ns ) {
45 if ( $ns === NS_MEDIAWIKI ) {
46 return wfEmptyMsg( $page ) ? '' : wfMsgExt( $page, 'content' );
47 }
48 $title = Title::newFromText( $page, $ns );
49 if ( $title ) {
50 if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) {
51 return $revision->getRawText();
52 }
53 }
54 return null;
55 }
56
57 /* Methods */
58
59 public function getScript( ResourceLoaderContext $context ) {
60 $scripts = '';
61 foreach ( $this->getPages( $context ) as $page => $options ) {
62 if ( $options['type'] === 'script' ) {
63 $script = $this->getContent( $page, $options['ns'] );
64 if ( $script ) {
65 $ns = MWNamespace::getCanonicalName( $options['ns'] );
66 $scripts .= "/*$ns:$page */\n$script\n";
67 }
68 }
69 }
70 return $scripts;
71 }
72
73 public function getStyles( ResourceLoaderContext $context ) {
74
75 $styles = array();
76 foreach ( $this->getPages( $context ) as $page => $options ) {
77 if ( $options['type'] === 'style' ) {
78 $media = isset( $options['media'] ) ? $options['media'] : 'all';
79 $style = $this->getContent( $page, $options['ns'] );
80 if ( $style ) {
81 if ( !isset( $styles[$media] ) ) {
82 $styles[$media] = '';
83 }
84 $ns = MWNamespace::getCanonicalName( $options['ns'] );
85 $styles[$media] .= "/* $ns:$page */\n$style\n";
86 }
87 }
88 }
89 return $styles;
90 }
91
92 public function getModifiedTime( ResourceLoaderContext $context ) {
93 $hash = $context->getHash();
94 if ( isset( $this->modifiedTime[$hash] ) ) {
95 return $this->modifiedTime[$hash];
96 }
97
98 $titles = array();
99 foreach ( $this->getPages( $context ) as $page => $options ) {
100 $titles[$options['ns']][$page] = true;
101 }
102
103 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
104
105 if ( $titles ) {
106 $dbr = wfGetDB( DB_SLAVE );
107 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
108 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
109 __METHOD__ );
110
111 if ( $latest ) {
112 $modifiedTime = wfTimestamp( TS_UNIX, $latest );
113 }
114 }
115
116 return $this->modifiedTime[$hash] = $modifiedTime;
117 }
118 }