]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/resourceloader/ResourceLoaderUserOptionsModule.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / resourceloader / ResourceLoaderUserOptionsModule.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 /**
24  * Module for user preference customizations
25  */
26 class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
27
28         /* Protected Members */
29
30         protected $modifiedTime = array();
31
32         /* Methods */
33
34         public function getModifiedTime( ResourceLoaderContext $context ) {
35                 $hash = $context->getHash();
36                 if ( isset( $this->modifiedTime[$hash] ) ) {
37                         return $this->modifiedTime[$hash];
38                 }
39
40                 global $wgUser;
41
42                 if ( $context->getUser() === $wgUser->getName() ) {
43                         return $this->modifiedTime[$hash] = $wgUser->getTouched();
44                 } else {
45                         return 1;
46                 }
47         }
48
49         /**
50          * Fetch the context's user options, or if it doesn't match current user,
51          * the default options.
52          * 
53          * @param $context ResourceLoaderContext: Context object
54          * @return Array: List of user options keyed by option name
55          */
56         protected function contextUserOptions( ResourceLoaderContext $context ) {
57                 global $wgUser;
58
59                 // Verify identity -- this is a private module
60                 if ( $context->getUser() === $wgUser->getName() ) {
61                         return $wgUser->getOptions();
62                 } else {
63                         return User::getDefaultOptions();
64                 }
65         }
66
67         public function getScript( ResourceLoaderContext $context ) {
68                 return Xml::encodeJsCall( 'mediaWiki.user.options.set', 
69                         array( $this->contextUserOptions( $context ) ) );
70         }
71
72         public function getStyles( ResourceLoaderContext $context ) {
73                 global $wgAllowUserCssPrefs;
74
75                 if ( $wgAllowUserCssPrefs ) {
76                         $options = $this->contextUserOptions( $context );
77
78                         // Build CSS rules
79                         $rules = array();
80                         if ( $options['underline'] < 2 ) {
81                                 $rules[] = "a { text-decoration: " . 
82                                         ( $options['underline'] ? 'underline' : 'none' ) . "; }";
83                         }
84                         if ( $options['highlightbroken'] ) {
85                                 $rules[] = "a.new, #quickbar a.new { color: #ba0000; }\n";
86                         } else {
87                                 $rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
88                                 $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #ba0000; }";
89                                 $rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
90                         }
91                         if ( $options['justify'] ) {
92                                 $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
93                         }
94                         if ( !$options['showtoc'] ) {
95                                 $rules[] = "#toc { display: none; }\n";
96                         }
97                         if ( !$options['editsection'] ) {
98                                 $rules[] = ".editsection { display: none; }\n";
99                         }
100                         if ( $options['editfont'] !== 'default' ) {
101                                 $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
102                         }
103                         $style = implode( "\n", $rules );
104                         if ( $this->getFlip( $context ) ) {
105                                 $style = CSSJanus::transform( $style, true, false );
106                         }
107                         return array( 'all' => $style );
108                 }
109                 return array();
110         }
111
112         public function getFlip( $context ) {
113                 global $wgContLang;
114
115                 return $wgContLang->getDir() !== $context->getDirection();
116         }
117
118         public function getGroup() {
119                 return 'private';
120         }
121 }