Extension to r89419, removing a bugous CSS code
[mediawiki.git] / includes / resourceloader / ResourceLoaderWikiModule.php
blobcab97e19fcb65e091b33e646cfa80606b547e209
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.
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.
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
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) || die( 1 );
25 /**
26 * Abstraction for resource loader modules which pull from wiki pages
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.
32 abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
34 /* Protected Members */
36 # Origin is user-supplied code
37 protected $origin = self::ORIGIN_USER_SITEWIDE;
39 // In-object cache for title mtimes
40 protected $titleMtimes = array();
42 /* Abstract Protected Methods */
44 abstract protected function getPages( ResourceLoaderContext $context );
46 /* Protected Methods */
48 /**
49 * @param $title Title
50 * @return null|string
52 protected function getContent( $title ) {
53 if ( $title->getNamespace() === NS_MEDIAWIKI ) {
54 $message = wfMessage( $title->getDBkey() )->inContentLanguage();
55 return $message->exists() ? $message->plain() : '';
57 if ( !$title->isCssJsSubpage() ) {
58 return null;
60 $revision = Revision::newFromTitle( $title );
61 if ( !$revision ) {
62 return null;
64 return $revision->getRawText();
67 /* Methods */
69 /**
70 * @param $context ResourceLoaderContext
71 * @return string
73 public function getScript( ResourceLoaderContext $context ) {
74 $scripts = '';
75 foreach ( $this->getPages( $context ) as $titleText => $options ) {
76 if ( $options['type'] !== 'script' ) {
77 continue;
79 $title = Title::newFromText( $titleText );
80 if ( !$title ) {
81 continue;
83 $script = $this->getContent( $title );
84 if ( strval( $script ) !== '' ) {
85 if ( strpos( $titleText, '*/' ) === false ) {
86 $scripts .= "/* $titleText */\n";
88 $scripts .= $script . "\n";
91 return $scripts;
94 /**
95 * @param $context ResourceLoaderContext
96 * @return array
98 public function getStyles( ResourceLoaderContext $context ) {
99 global $wgScriptPath;
101 $styles = array();
102 foreach ( $this->getPages( $context ) as $titleText => $options ) {
103 if ( $options['type'] !== 'style' ) {
104 continue;
106 $title = Title::newFromText( $titleText );
107 if ( !$title ) {
108 continue;
110 $media = isset( $options['media'] ) ? $options['media'] : 'all';
111 $style = $this->getContent( $title );
112 if ( strval( $style ) === '' ) {
113 continue;
115 if ( $this->getFlip( $context ) ) {
116 $style = CSSJanus::transform( $style, true, false );
118 $style = CSSMin::remap( $style, false, $wgScriptPath, true );
119 if ( !isset( $styles[$media] ) ) {
120 $styles[$media] = '';
122 if ( strpos( $titleText, '*/' ) === false ) {
123 $styles[$media] .= "/* $titleText */\n";
125 $styles[$media] .= $style . "\n";
127 return $styles;
131 * @param $context ResourceLoaderContext
132 * @return int|mixed
134 public function getModifiedTime( ResourceLoaderContext $context ) {
135 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
136 $mtimes = $this->getTitleMtimes( $context );
137 if ( count( $mtimes ) ) {
138 $modifiedTime = max( $modifiedTime, max( $mtimes ) );
140 return $modifiedTime;
144 * @param $context ResourceLoaderContext
145 * @return bool
147 public function isKnownEmpty( ResourceLoaderContext $context ) {
148 return count( $this->getTitleMtimes( $context ) ) == 0;
152 * Get the modification times of all titles that would be loaded for
153 * a given context.
154 * @param $context ResourceLoaderContext: Context object
155 * @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
157 protected function getTitleMtimes( ResourceLoaderContext $context ) {
158 $hash = $context->getHash();
159 if ( isset( $this->titleMtimes[$hash] ) ) {
160 return $this->titleMtimes[$hash];
163 $this->titleMtimes[$hash] = array();
164 $batch = new LinkBatch;
165 foreach ( $this->getPages( $context ) as $titleText => $options ) {
166 $batch->addObj( Title::newFromText( $titleText ) );
169 if ( !$batch->isEmpty() ) {
170 $dbr = wfGetDB( DB_SLAVE );
171 $res = $dbr->select( 'page',
172 array( 'page_namespace', 'page_title', 'page_touched' ),
173 $batch->constructSet( 'page', $dbr ),
174 __METHOD__
176 foreach ( $res as $row ) {
177 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
178 $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
179 wfTimestamp( TS_UNIX, $row->page_touched );
182 return $this->titleMtimes[$hash];