Add experimental MathML output option for math (only works with some
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?php
2
3 function wfSpecialPreferences()
4 {
5 global $wgRequest;
6
7 $form = new PreferencesForm( $wgRequest );
8 $form->execute();
9 }
10
11 class PreferencesForm {
12 var $mQuickbar, $mOldpass, $mNewpass, $mRetypePass, $mStubs;
13 var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick;
14 var $mSearch, $mRecent, $mHourDiff, $mSearchLines, $mSearchChars, $mAction;
15 var $mReset, $mPosted, $mToggles, $mSearchNs, $mRealName;
16
17 function PreferencesForm( &$request ) {
18 global $wgLang;
19
20 $this->mQuickbar = $request->getVal( 'wpQuickbar' );
21 $this->mOldpass = $request->getVal( 'wpOldpass' );
22 $this->mNewpass = $request->getVal( 'wpNewpass' );
23 $this->mRetypePass =$request->getVal( 'wpRetypePass' );
24 $this->mStubs = $request->getVal( 'wpStubs' );
25 $this->mRows = $request->getVal( 'wpRows' );
26 $this->mCols = $request->getVal( 'wpCols' );
27 $this->mSkin = $request->getVal( 'wpSkin' );
28 $this->mMath = $request->getVal( 'wpMath' );
29 $this->mDate = $request->getVal( 'wpDate' );
30 $this->mUserEmail = $request->getVal( 'wpUserEmail' );
31 $this->mRealName = $request->getVal( 'wpRealName' );
32 $this->mEmailFlag = $request->getCheck( 'wpEmailFlag' ) ? 1 : 0;
33 $this->mNick = $request->getVal( 'wpNick' );
34 $this->mSearch = $request->getVal( 'wpSearch' );
35 $this->mRecent = $request->getVal( 'wpRecent' );
36 $this->mHourDiff = $request->getVal( 'wpHourDiff' );
37 $this->mSearchLines = $request->getVal( 'wpSearchLines' );
38 $this->mSearchChars = $request->getVal( 'wpSearchChars' );
39 $this->mAction = $request->getVal( 'action' );
40 $this->mReset = $request->getCheck( 'wpReset' );
41 $this->mPosted = $request->wasPosted();
42 $this->mSaveprefs = $request->getCheck( 'wpSaveprefs' ) && $this->mPosted;
43
44 # User toggles (the big ugly unsorted list of checkboxes)
45 $this->mToggles = array();
46 if ( $this->mPosted ) {
47 $togs = $wgLang->getUserToggles();
48 foreach ( $togs as $tname => $ttext ) {
49 $this->mToggles[$tname] = $request->getCheck( "wpOp$tname" ) ? 1 : 0;
50 }
51 }
52
53 $this->mUsedToggles = array();
54
55 # Search namespace options
56 # Note: namespaces don't necessarily have consecutive keys
57 $this->mSearchNs = array();
58 if ( $this->mPosted ) {
59 $namespaces = $wgLang->getNamespaces();
60 foreach ( $namespaces as $i => $namespace ) {
61 if ( $i >= 0 ) {
62 $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0;
63 }
64 }
65 }
66 }
67
68 function execute() {
69 global $wgUser, $wgOut, $wgUseDynamicDates;
70
71 if ( 0 == $wgUser->getID() ) {
72 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
73 return;
74 }
75 if ( wfReadOnly() ) {
76 $wgOut->readOnlyPage();
77 return;
78 }
79 if ( $this->mReset ) {
80 $this->resetPrefs();
81 $this->mainPrefsForm( wfMsg( "prefsreset" ) );
82 } else if ( $this->mSaveprefs ) {
83 $this->savePreferences();
84 } else {
85 $this->resetPrefs();
86 $this->mainPrefsForm( "" );
87 }
88 }
89
90 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
91 $val = intval($val);
92 $val = min($val, $max);
93 $val = max($val, $min);
94 return $val;
95 }
96
97 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
98 $val = trim($val);
99 if($val === "") {
100 return $val;
101 } else {
102 return $this->validateInt( $val, $min, $max );
103 }
104 }
105
106 /* private */ function validateTimeZone( $s )
107 {
108
109 if ( $s !== "" ) {
110 if ( strpos( $s, ":" ) ) {
111 # HH:MM
112 $array = explode( ":" , $s );
113 $hour = intval( $array[0] );
114 $minute = intval( $array[1] );
115 } else {
116 $minute = intval( $s * 60 );
117 $hour = intval( $minute / 60 );
118 $minute = abs( $minute ) % 60;
119 }
120 $hour = min( $hour, 15 );
121 $hour = max( $hour, -15 );
122 $minute = min( $minute, 59 );
123 $minute = max( $minute, 0 );
124 $s = sprintf( "%02d:%02d", $hour, $minute );
125 }
126 return $s;
127 }
128
129 /* private */ function savePreferences()
130 {
131 global $wgUser, $wgLang, $wgDeferredUpdateList, $wgOut;
132
133 if ( "" != $this->mNewpass ) {
134 if ( $this->mNewpass != $this->mRetypePass ) {
135 $this->mainPrefsForm( wfMsg( "badretype" ) );
136 return;
137 }
138 $ep = $wgUser->encryptPassword( $this->mOldpass );
139 if ( $ep != $wgUser->getPassword() ) {
140 if ( $ep != $wgUser->getNewpassword() ) {
141 $this->mainPrefsForm( wfMsg( "wrongpassword" ) );
142 return;
143 }
144 }
145 $wgUser->setPassword( $this->mNewpass );
146 }
147 $wgUser->setEmail( $this->mUserEmail );
148 $wgUser->setRealName( $this->mRealName );
149 $wgUser->setOption( "nickname", $this->mNick );
150 $wgUser->setOption( "quickbar", $this->mQuickbar );
151 $wgUser->setOption( "skin", $this->mSkin );
152 $wgUser->setOption( "math", $this->mMath );
153 $wgUser->setOption( "date", $this->mDate );
154 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) );
155 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) );
156 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) );
157 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) );
158 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) );
159 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) );
160 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) );
161 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
162
163 # Set search namespace options
164 foreach( $this->mSearchNs as $i => $value ) {
165 $wgUser->setOption( "searchNs{$i}", $value );
166 }
167
168 $wgUser->setOption( "disablemail", $this->mEmailFlag );
169
170 # Set user toggles
171 foreach ( $this->mToggles as $tname => $tvalue ) {
172 $wgUser->setOption( $tname, $tvalue );
173 }
174 $wgUser->setCookies();
175 $up = new UserUpdate();
176 array_push( $wgDeferredUpdateList, $up );
177 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
178 $po = ParserOptions::newFromUser( $wgUser );
179 $this->mainPrefsForm( wfMsg( "savedprefs" ) );
180 }
181
182 /* private */ function resetPrefs()
183 {
184 global $wgUser, $wgLang;
185
186 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
187 $this->mUserEmail = $wgUser->getEmail();
188 $this->mRealName = $wgUser->getRealName();
189 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
190 else { $this->mEmailFlag = 0; }
191 $this->mNick = $wgUser->getOption( "nickname" );
192
193 $this->mQuickbar = $wgUser->getOption( "quickbar" );
194 $this->mSkin = $wgUser->getOption( "skin" );
195 $this->mMath = $wgUser->getOption( "math" );
196 $this->mDate = $wgUser->getOption( "date" );
197 $this->mRows = $wgUser->getOption( "rows" );
198 $this->mCols = $wgUser->getOption( "cols" );
199 $this->mStubs = $wgUser->getOption( "stubthreshold" );
200 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
201 $this->mSearch = $wgUser->getOption( "searchlimit" );
202 $this->mSearchLines = $wgUser->getOption( "contextlines" );
203 $this->mSearchChars = $wgUser->getOption( "contextchars" );
204 $this->mRecent = $wgUser->getOption( "rclimit" );
205
206 $togs = $wgLang->getUserToggles();
207 foreach ( $togs as $tname => $ttext ) {
208 $this->mToggles[$tname] = $wgUser->getOption( $tname );
209 }
210
211 $namespaces = $wgLang->getNamespaces();
212 foreach ( $namespaces as $i => $namespace ) {
213 if ( $i >= 0 ) {
214 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
215 }
216 }
217 }
218
219 /* private */ function namespacesCheckboxes()
220 {
221 global $wgLang, $wgUser;
222
223 # Determine namespace checkboxes
224 $namespaces = $wgLang->getNamespaces();
225 $r1 = "";
226
227 foreach ( $namespaces as $i => $name ) {
228 # Skip special or anything similar
229 if ( $i >= 0 ) {
230 $checked = "";
231 if ( $this->mSearchNs[$i] ) {
232 $checked = ' checked="checked"';
233 }
234 $name = str_replace( "_", " ", $namespaces[$i] );
235 if ( "" == $name ) {
236 $name = wfMsg( "blanknamespace" );
237 }
238
239 if ( 0 != $i ) {
240 $r1 .= " ";
241 }
242 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" .
243 "wpNs$i\"{$checked} />{$name}</label>\n";
244 }
245 }
246
247 return $r1;
248 }
249
250
251 function getToggle( $tname ) {
252 global $wgUser, $wgLang;
253
254 $this->mUsedToggles[$tname] = true;
255 $ttext = $wgLang->getUserToggle( $tname );
256
257 if ( 1 == $wgUser->getOption( $tname ) ) {
258 $checked = ' checked="checked"';
259 } else {
260 $checked = "";
261 }
262 return "<div><input type='checkbox' value=\"1\" "
263 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n";
264 }
265
266 /* private */ function mainPrefsForm( $err )
267 {
268 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
269
270 $wgOut->setPageTitle( wfMsg( "preferences" ) );
271 $wgOut->setArticleRelated( false );
272 $wgOut->setRobotpolicy( "noindex,nofollow" );
273
274 if ( "" != $err ) {
275 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" );
276 }
277 $uname = $wgUser->getName();
278 $uid = $wgUser->getID();
279
280 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
281
282 $qbs = $wgLang->getQuickbarSettings();
283 $skinNames = $wgLang->getSkinNames();
284 $mathopts = $wgLang->getMathNames();
285 $dateopts = $wgLang->getDateFormats();
286 $togs = $wgLang->getUserToggles();
287
288 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
289 $action = $titleObj->escapeLocalURL();
290
291 $qb = wfMsg( "qbsettings" );
292 $cp = wfMsg( "changepassword" );
293 $sk = wfMsg( "skin" );
294 $math = wfMsg( "math" );
295 $dateFormat = wfMsg("dateformat");
296 $opw = wfMsg( "oldpassword" );
297 $npw = wfMsg( "newpassword" );
298 $rpw = wfMsg( "retypenew" );
299 $svp = wfMsg( "saveprefs" );
300 $rsp = wfMsg( "resetprefs" );
301 $tbs = wfMsg( "textboxsize" );
302 $tbr = wfMsg( "rows" );
303 $tbc = wfMsg( "columns" );
304 $ltz = wfMsg( "localtime" );
305 $tzt = wfMsg( "timezonetext" );
306 $tzo = wfMsg( "timezoneoffset" );
307 $tzGuess = wfMsg( "guesstimezone" );
308 $tzServerTime = wfMsg( "servertime" );
309 $yem = wfMsg( "youremail" );
310 $yrn = wfMsg( "yourrealname" );
311 $emf = wfMsg( "emailflag" );
312 $ynn = wfMsg( "yournick" );
313 $stt = wfMsg ( "stubthreshold" ) ;
314 $srh = wfMsg( "searchresultshead" );
315 $rpp = wfMsg( "resultsperpage" );
316 $scl = wfMsg( "contextlines" );
317 $scc = wfMsg( "contextchars" );
318 $rcc = wfMsg( "recentchangescount" );
319 $dsn = wfMsg( "defaultns" );
320
321 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
322 method=\"post\">" );
323
324 # First section: identity
325 # Email, etc.
326 #
327 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
328 $this->mRealName = wfEscapeHTML( $this->mRealName );
329 $this->mNick = wfEscapeHTML( $this->mNick );
330 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
331 else { $emfc = ""; }
332
333 $ps = $this->namespacesCheckboxes();
334
335 $wgOut->addHTML( "<fieldset>
336 <legend>Idento</legend>
337 <div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>
338 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
339 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
340 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" );
341
342 # Fields for changing password
343 #
344 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
345 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
346 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
347
348 $wgOut->addHTML( "<fieldset>
349 <legend>$cp:</legend>
350 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
351 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
352 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
353 " . $this->getToggle( "rememberpassword" ) . "
354 </fieldset>\n</fieldset>\n" );
355
356
357 # Quickbar setting
358 #
359 $wgOut->addHtml( "<fieldset>\n<legend>$qb:</legend>\n" );
360 for ( $i = 0; $i < count( $qbs ); ++$i ) {
361 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
362 else { $checked = ""; }
363 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
364 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
365 }
366 $wgOut->addHtml( "</fieldset>\n\n" );
367
368 # Skin setting
369 #
370 $wgOut->addHTML( "<fieldset>\n<legend>$sk:</legend>\n" );
371 # Only show members of $wgValidSkinNames rather than
372 # $skinNames (skins is all skin names from Language.php)
373 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
374 if ( $skinkey == $this->mSkin ) {
375 $checked = ' checked="checked"';
376 } else {
377 $checked = "";
378 }
379 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
380 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" );
381 }
382 $wgOut->addHTML( "</fieldset>\n\n" );
383
384 # Math setting
385 #
386 $wgOut->addHTML( "<fieldset>\n<legend>$math:</legend>\n" );
387 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
388 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
389 else { $checked = ""; }
390 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
391 value=\"$i\"$checked /> {$mathopts[$i]}</label></div>\n" );
392 }
393 $wgOut->addHTML( "</fieldset>\n\n" );
394
395 # Date format
396 #
397 if ( $wgUseDynamicDates ) {
398 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat:</legend>\n" );
399 for ( $i = 0; $i < count( $dateopts ); ++$i) {
400 if ( $i == $this->mDate ) {
401 $checked = ' checked="checked"';
402 } else {
403 $checked = "";
404 }
405 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
406 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
407 }
408 $wgOut->addHTML( "</fieldset>\n\n");
409 }
410
411 # Textbox rows, cols
412 #
413 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
414 $nowserver = $wgLang->time( $now, false );
415 $wgOut->addHTML( "<fieldset>
416 <legend>$tbs:</legend>\n
417 <div>
418 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
419 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
420 </div> " .
421 $this->getToggle( "editwidth" ) .
422 $this->getToggle( "showtoolbar" ) .
423 $this->getToggle( "previewontop" ) .
424 $this->getToggle( "watchdefault" ) .
425 $this->getToggle( "minordefault" ) . "
426 </fieldset>
427
428 <fieldset>
429 <legend>$dateFormat:</legend>
430 <div><b>$tzServerTime:</b> $nowserver</div>
431 <div><b>$ltz:</b> $nowlocal</div>
432 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
433 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" /></div>
434 </fieldset>\n\n" );
435
436 $wgOut->addHTML( "
437 <fieldset>
438 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
439 " . $this->getToggle( "hideminor" ) .
440 $this->getToggle( "usenewrc" ) . "
441 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
442 </fieldset>
443
444 <fieldset>
445 <legend>$srh</legend>
446 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
447 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
448 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
449
450 <fieldset>
451 <legend>$dsn</legend>
452 $ps
453 </fieldset>
454 </fieldset>
455 " );
456
457 # Various checkbox options
458 #
459 $wgOut->addHTML("<fieldset>");
460 foreach ( $togs as $tname => $ttext ) {
461 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
462 $wgOut->addHTML( $this->getToggle( $tname ) );
463 }
464 }
465 $wgOut->addHTML( "</fieldset>\n\n" );
466
467 $wgOut->addHTML( "
468 <div>
469 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" />
470 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
471 </div>
472
473 <div>* {$tzt}</div>
474
475 </form>\n" );
476 }
477 }
478 ?>