310101bb56bb01ac4a6a2c36b7ec346ec0cfdcbd
[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 /**
24 * Module for user preference customizations
25 */
26 class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
27
28 /* Protected Members */
29
30 protected $modifiedTime = array();
31
32 protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
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] = wfTimestamp( TS_UNIX, $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: Context object
56 * @return Array: List of user options keyed by option name
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 return Xml::encodeJsCall( 'mw.user.options.set',
71 array( $this->contextUserOptions( $context ) ) );
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: " .
84 ( $options['underline'] ? 'underline' : 'none' ) . "; }";
85 }
86 if ( $options['highlightbroken'] ) {
87 $rules[] = "a.new, #quickbar a.new { color: #ba0000; }\n";
88 } else {
89 $rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
90 $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #ba0000; }";
91 $rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
92 }
93 if ( $options['justify'] ) {
94 $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
95 }
96 if ( !$options['showtoc'] ) {
97 $rules[] = "#toc { display: none; }\n";
98 }
99 if ( !$options['editsection'] ) {
100 $rules[] = ".editsection { display: none; }\n";
101 }
102 if ( $options['editfont'] !== 'default' ) {
103 $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
104 }
105 $style = implode( "\n", $rules );
106 if ( $this->getFlip( $context ) ) {
107 $style = CSSJanus::transform( $style, true, false );
108 }
109 return array( 'all' => $style );
110 }
111 return array();
112 }
113
114 /**
115 * @param $context ResourceLoaderContext
116 * @return bool
117 */
118 public function getFlip( $context ) {
119 global $wgContLang;
120
121 return $wgContLang->getDir() !== $context->getDirection();
122 }
123
124 public function getGroup() {
125 return 'private';
126 }
127 }