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