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