Changed the way that ResourceLoaderFileModule and mediaWiki.loader work in debug...
[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 if ( $title = Title::newFromText( $page, $ns ) ) {
49 if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) {
50 return $revision->getRawText();
51 }
52 }
53 return null;
54 }
55
56 /* Methods */
57
58 public function getScript( ResourceLoaderContext $context ) {
59 $scripts = '';
60 foreach ( $this->getPages( $context ) as $page => $options ) {
61 if ( $options['type'] === 'script' ) {
62 if ( $script = $this->getContent( $page, $options['ns'] ) ) {
63 $ns = MWNamespace::getCanonicalName( $options['ns'] );
64 $scripts .= "/*$ns:$page */\n$script\n";
65 }
66 }
67 }
68 return $scripts;
69 }
70
71 public function getStyles( ResourceLoaderContext $context ) {
72
73 $styles = array();
74 foreach ( $this->getPages( $context ) as $page => $options ) {
75 if ( $options['type'] === 'style' ) {
76 $media = isset( $options['media'] ) ? $options['media'] : 'all';
77 if ( $style = $this->getContent( $page, $options['ns'] ) ) {
78 if ( !isset( $styles[$media] ) ) {
79 $styles[$media] = '';
80 }
81 $ns = MWNamespace::getCanonicalName( $options['ns'] );
82 $styles[$media] .= "/* $ns:$page */\n$style\n";
83 }
84 }
85 }
86 return $styles;
87 }
88
89 public function getModifiedTime( ResourceLoaderContext $context ) {
90 $hash = $context->getHash();
91 if ( isset( $this->modifiedTime[$hash] ) ) {
92 return $this->modifiedTime[$hash];
93 }
94
95 $titles = array();
96 foreach ( $this->getPages( $context ) as $page => $options ) {
97 $titles[$options['ns']][$page] = true;
98 }
99
100 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
101
102 if ( $titles ) {
103 $dbr = wfGetDB( DB_SLAVE );
104 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
105 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
106 __METHOD__ );
107
108 if ( $latest ) {
109 $modifiedTime = wfTimestamp( TS_UNIX, $latest );
110 }
111 }
112
113 return $this->modifiedTime[$hash] = $modifiedTime;
114 }
115 }