Made email option actually work, fixed bug 18580 (new preferences page does not displ...
[lhc/web/wiklou.git] / includes / Preferences.php
1 <?php
2
3 class Preferences {
4 static $defaultPreferences = null;
5 static $saveFilters =
6 array(
7 'timecorrection' => array( 'Preferences', 'filterTimezoneInput' ),
8 );
9
10 static function getPreferences( $user ) {
11 if (self::$defaultPreferences)
12 return self::$defaultPreferences;
13
14 global $wgLang, $wgRCMaxAge;
15
16 $defaultPreferences = array();
17
18 ## User info #####################################
19 // Information panel
20 $defaultPreferences['username'] =
21 array(
22 'type' => 'info',
23 'label-message' => 'username',
24 'default' => $user->getName(),
25 'section' => 'personal',
26 );
27
28 $defaultPreferences['userid'] =
29 array(
30 'type' => 'info',
31 'label-message' => 'uid',
32 'default' => $user->getId(),
33 'section' => 'personal',
34 );
35
36 # Get groups to which the user belongs
37 $userEffectiveGroups = $user->getEffectiveGroups();
38 $userEffectiveGroupsArray = array();
39 foreach( $userEffectiveGroups as $ueg ) {
40 if( $ueg == '*' ) {
41 // Skip the default * group, seems useless here
42 continue;
43 }
44 $userEffectiveGroupsArray[] = User::makeGroupLinkHTML( $ueg );
45 }
46 asort( $userEffectiveGroupsArray );
47
48 $defaultPreferences['usergroups'] =
49 array(
50 'type' => 'info',
51 'label' => wfMsgExt( 'prefs-memberingroups', 'parseinline',
52 count($userEffectiveGroupsArray) ),
53 'default' => $wgLang->commaList( $userEffectiveGroupsArray ),
54 'raw' => true,
55 'section' => 'personal',
56 );
57
58 $defaultPreferences['editcount'] =
59 array(
60 'type' => 'info',
61 'label-message' => 'prefs-edits',
62 'default' => $user->getEditCount(),
63 'section' => 'personal',
64 );
65
66 if ($user->getRegistration()) {
67 $defaultPreferences['registrationdate'] =
68 array(
69 'type' => 'info',
70 'label-message' => 'prefs-registration',
71 'default' => $wgLang->timeanddate( $user->getRegistration() ),
72 'section' => 'personal',
73 );
74 }
75
76 // Actually changeable stuff
77 global $wgAllowRealName;
78 if ($wgAllowRealName) {
79 $defaultPreferences['realname'] =
80 array(
81 'type' => 'text',
82 'default' => $user->getRealName(),
83 'section' => 'personal',
84 'label-message' => 'yourrealname',
85 'help-message' => 'prefs-help-realname',
86 );
87 }
88
89 global $wgEmailConfirmToEdit;
90
91 $defaultPreferences['emailaddress'] =
92 array(
93 'type' => 'text',
94 'default' => $user->getEmail(),
95 'section' => 'personal',
96 'label-message' => 'youremail',
97 'help-message' => $wgEmailConfirmToEdit
98 ? 'prefs-help-email-required'
99 : 'prefs-help-email',
100 'validation-callback' => array( 'Preferences', 'validateEmail' ),
101 );
102
103 global $wgAuth;
104 if ($wgAuth->allowPasswordChange()) {
105 global $wgUser; // For skin.
106 $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'ResetPass' ),
107 wfMsgHtml( 'prefs-resetpass' ), array() ,
108 array('returnto' => SpecialPage::getTitleFor( 'Preferences') ) );
109
110 $defaultPreferences['password'] =
111 array(
112 'type' => 'info',
113 'raw' => true,
114 'default' => $link,
115 'label-message' => 'yourpassword',
116 'section' => 'personal',
117 );
118 }
119
120 $defaultPreferences['gender'] =
121 array(
122 'type' => 'select',
123 'section' => 'personal',
124 'options' => array(
125 wfMsg('gender-male') => 'male',
126 wfMsg('gender-female') => 'female',
127 wfMsg('gender-unknown') => 'unknown',
128 ),
129 'label-message' => 'yourgender',
130 'help-message' => 'prefs-help-gender',
131 );
132
133 // Language
134 global $wgContLanguageCode;
135 $languages = array_reverse( Language::getLanguageNames( false ) );
136 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
137 $languages[$wgContLanguageCode] = $wgContLanguageCode;
138 }
139 ksort( $languages );
140
141 $options = array();
142 foreach( $languages as $code => $name ) {
143 $display = "$code - $name";
144 $options[$display] = $code;
145 }
146 $defaultPreferences['language'] =
147 array(
148 'type' => 'select',
149 'section' => 'personal',
150 'options' => $options,
151 'label-message' => 'yourlanguage',
152 );
153
154 global $wgContLang, $wgDisableLangConversion;
155 /* see if there are multiple language variants to choose from*/
156 $variantArray = array();
157 if(!$wgDisableLangConversion) {
158 $variants = $wgContLang->getVariants();
159
160 $languages = Language::getLanguageNames( true );
161 foreach($variants as $v) {
162 $v = str_replace( '_', '-', strtolower($v));
163 if( array_key_exists( $v, $languages ) ) {
164 // If it doesn't have a name, we'll pretend it doesn't exist
165 $variantArray[$v] = $languages[$v];
166 }
167 }
168
169 $options = array();
170 foreach( $variantArray as $code => $name ) {
171 $display = "$code - $name";
172 $options[$display] = $code;
173 }
174
175 if(count($variantArray) > 1) {
176 $defaultPreferences['variant'] =
177 array(
178 'label-message' => 'yourvariant',
179 'type' => 'select',
180 'options' => $options,
181 'section' => 'personal',
182 );
183 }
184 }
185
186 if( count($variantArray) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
187 $defaultPreferences['noconvertlink'] =
188 array(
189 'type' => 'toggle',
190 'section' => 'misc',
191 'label-message' => 'tog-noconvertlink',
192 );
193 }
194
195 global $wgMaxSigChars;
196 $defaultPreferences['nickname'] =
197 array(
198 'type' => 'text',
199 'maxlength' => $wgMaxSigChars,
200 'label-message' => 'yournick',
201 'validation-callback' =>
202 array( 'Preferences', 'validateSignature' ),
203 'section' => 'personal',
204 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
205 );
206 $defaultPreferences['fancysig'] =
207 array(
208 'type' => 'toggle',
209 'label-message' => 'tog-fancysig',
210 'section' => 'personal'
211 );
212
213 $defaultPreferences['rememberpassword'] =
214 array(
215 'type' => 'toggle',
216 'label-message' => 'tog-rememberpassword',
217 'section' => 'personal',
218 );
219
220
221 ## Email #######################################
222 ## Email stuff
223 global $wgEnableEmail, $wgEnableUserEmail, $wgEmailAuthentication;
224
225 if ( $wgEmailAuthentication ) {
226 if ( $user->getEmail() ) {
227 if( $user->getEmailAuthenticationTimestamp() ) {
228 // date and time are separate parameters to facilitate localisation.
229 // $time is kept for backward compat reasons.
230 // 'emailauthenticated' is also used in SpecialConfirmemail.php
231 $time = $wgLang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true );
232 $d = $wgLang->date( $user->getEmailAuthenticationTimestamp(), true );
233 $t = $wgLang->time( $user->getEmailAuthenticationTimestamp(), true );
234 $emailauthenticated = wfMsg('emailauthenticated', $time, $d, $t ).'<br />';
235 $disableEmailPrefs = false;
236 } else {
237 $disableEmailPrefs = true;
238 $skin = $wgUser->getSkin();
239 $emailauthenticated = wfMsg('emailnotauthenticated').'<br />' .
240 $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Confirmemail' ),
241 wfMsg( 'emailconfirmlink' ) ) . '<br />';
242 }
243 } else {
244 $emailauthenticated = wfMsg( 'noemailprefs' );
245 }
246
247 $defaultPreferences['emailauthentication'] =
248 array(
249 'type' => 'info',
250 'raw' => true,
251 'section' => 'email',
252 'label-message' => 'prefs-emailconfirm-label',
253 'default' => $emailauthenticated,
254 );
255
256 }
257
258 if ($wgEnableEmail) {
259 if ($wgEnableUserEmail) {
260 $defaultPreferences['disablemail'] =
261 array(
262 'type' => 'toggle',
263 'invert' => true,
264 'section' => 'email',
265 'label-message' => 'allowemail',
266 );
267 $defaultPreferences['ccmeonemails'] =
268 array(
269 'type' => 'toggle',
270 'section' => 'email',
271 'label-message' => 'tog-ccmeonemails',
272 );
273 }
274
275 $defaultPreferences['enotifwatchlistpages'] =
276 array(
277 'type' => 'toggle',
278 'section' => 'email',
279 'label-message' => 'tog-enotifwatchlistpages',
280 );
281 $defaultPreferences['enotifusertalkpages'] =
282 array(
283 'type' => 'toggle',
284 'section' => 'email',
285 'label-message' => 'tog-enotifusertalkpages',
286 );
287 $defaultPreferences['enotifminoredits'] =
288 array(
289 'type' => 'toggle',
290 'section' => 'email',
291 'label-message' => 'tog-enotifminoredits',
292 );
293 $defaultPreferences['enotifrevealaddr'] =
294 array(
295 'type' => 'toggle',
296 'section' => 'email',
297 'label-message' => 'tog-enotifrevealaddr'
298 );
299 }
300
301 ## Skin #####################################
302 global $wgAllowUserSkin;
303
304 if ($wgAllowUserSkin) {
305 $defaultPreferences['skin'] =
306 array(
307 'type' => 'radio',
308 'options' => self::generateSkinOptions( $user ),
309 'label' => '&nbsp;',
310 'section' => 'skin',
311 );
312 }
313
314 ## TODO QUICKBAR
315
316 ## Math #####################################
317 global $wgUseTeX;
318 if ($wgUseTeX) {
319 $defaultPreferences['math'] =
320 array(
321 'type' => 'radio',
322 'options' =>
323 array_flip( array_map( 'wfMsg', $wgLang->getMathNames() ) ),
324 'label' => '&nbsp;',
325 'section' => 'math',
326 );
327 }
328
329 ## Files #####################################
330 $defaultPreferences['imagesize'] =
331 array(
332 'type' => 'select',
333 'options' => self::getImageSizes(),
334 'label-message' => 'imagemaxsize',
335 'section' => 'files',
336 );
337 $defaultPreferences['thumbsize'] =
338 array(
339 'type' => 'select',
340 'options' => self::getThumbSizes(),
341 'label-message' => 'thumbsize',
342 'section' => 'files',
343 );
344
345 ## Date and time #####################################
346 $dateOptions = self::getDateOptions();
347 if ($dateOptions) {
348 $defaultPreferences['date'] =
349 array(
350 'type' => 'radio',
351 'options' => $dateOptions,
352 'label-message' => 'dateformat',
353 'section' => 'datetime',
354 );
355 }
356
357 // Info
358 $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
359 $wgLang->time( $now = wfTimestampNow(), true ) );
360 $nowserver = $wgLang->time( $now, false ) .
361 Xml::hidden( 'wpServerTime', substr( $now, 8, 2 ) * 60 + substr( $now, 10, 2 ) );
362
363 $defaultPreferences['nowserver'] =
364 array(
365 'type' => 'info',
366 'raw' => 1,
367 'label-message' => 'servertime',
368 'default' => $nowserver,
369 'section' => 'datetime',
370 );
371
372 $defaultPreferences['nowlocal'] =
373 array(
374 'type' => 'info',
375 'raw' => 1,
376 'label-message' => 'localtime',
377 'default' => $nowlocal,
378 'section' => 'datetime',
379 );
380
381 // Grab existing pref.
382 $tzOffset = $user->getOption( 'timecorrection' );
383 $tz = explode( '|', $tzOffset, 2 );
384
385 $tzSetting = $tzOffset;
386 if (count($tz) > 1 && $tz[0] == 'Offset') {
387 $minDiff = $tz[1];
388 $tzSetting = sprintf( '%+03d:%02d', floor($minDiff/60), abs($minDiff)%60 );;
389 }
390
391 $defaultPreferences['timecorrection'] =
392 array(
393 'class' => 'HTMLSelectOrOtherField',
394 'label-message' => 'timezonelegend',
395 'options' => self::getTimezoneOptions(),
396 'default' => $tzSetting,
397 'section' => 'datetime',
398 );
399
400 ## Page Rendering ##############################
401 $defaultPreferences['underline'] =
402 array(
403 'type' => 'select',
404 'options' => array(
405 wfMsg( 'underline-never' ) => 0,
406 wfMsg( 'underline-always' ) => 1,
407 wfMsg( 'underline-default' ) => 2,
408 ),
409 'label-message' => 'tog-underline',
410 'section' => 'rendering',
411 );
412
413 $stubThresholdValues = array( 0, 50, 100, 500, 1000, 2000, 5000, 10000 );
414 $stubThresholdOptions = array();
415 foreach( $stubThresholdValues as $value ) {
416 $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value;
417 }
418
419 $defaultPreferences['stubthreshold'] =
420 array(
421 'type' => 'selectorother',
422 'section' => 'rendering',
423 'options' => $stubThresholdOptions,
424 'label' => wfMsg('stub-threshold'), // Raw HTML message. Yay?
425 );
426 $defaultPreferences['highlightbroken'] =
427 array(
428 'type' => 'toggle',
429 'section' => 'rendering',
430 'label' => wfMsg('tog-highlightbroken'), // Raw HTML
431 );
432 $defaultPreferences['showtoc'] =
433 array(
434 'type' => 'toggle',
435 'section' => 'rendering',
436 'label-message' => 'tog-showtoc',
437 );
438 $defaultPreferences['nocache'] =
439 array(
440 'type' => 'toggle',
441 'label-message' => 'tog-nocache',
442 'section' => 'rendering',
443 );
444 $defaultPreferences['showhiddencats'] =
445 array(
446 'type' => 'toggle',
447 'section' => 'rendering',
448 'label-message' => 'tog-showhiddencats'
449 );
450 $defaultPreferences['showjumplinks'] =
451 array(
452 'type' => 'toggle',
453 'section' => 'rendering',
454 'label-message' => 'tog-showjumplinks',
455 );
456 $defaultPreferences['justify'] =
457 array(
458 'type' => 'toggle',
459 'section' => 'rendering',
460 'label-message' => 'tog-justify',
461 );
462 $defaultPreferences['numberheadings'] =
463 array(
464 'type' => 'toggle',
465 'section' => 'rendering',
466 'label-message' => 'tog-numberheadings',
467 );
468
469 ## Editing #####################################
470 $defaultPreferences['cols'] =
471 array(
472 'type' => 'int',
473 'label-message' => 'columns',
474 'section' => 'editing',
475 'min' => 4,
476 'max' => 1000,
477 );
478 $defaultPreferences['rows'] =
479 array(
480 'type' => 'int',
481 'label-message' => 'rows',
482 'section' => 'editing',
483 'min' => 4,
484 'max' => 1000,
485 );
486 $defaultPreferences['previewontop'] =
487 array(
488 'type' => 'toggle',
489 'section' => 'editing',
490 'label-message' => 'tog-previewontop',
491 );
492 $defaultPreferences['previewonfirst'] =
493 array(
494 'type' => 'toggle',
495 'section' => 'editing',
496 'label-message' => 'tog-previewonfirst',
497 );
498 $defaultPreferences['editsection'] =
499 array(
500 'type' => 'toggle',
501 'section' => 'editing',
502 'label-message' => 'tog-editsection',
503 );
504 $defaultPreferences['editsectiononrightclick'] =
505 array(
506 'type' => 'toggle',
507 'section' => 'editing',
508 'label-message' => 'tog-editsectiononrightclick',
509 );
510 $defaultPreferences['editondblclick'] =
511 array(
512 'type' => 'toggle',
513 'section' => 'editing',
514 'label-message' => 'tog-editondblclick',
515 );
516 $defaultPreferences['editwidth'] =
517 array(
518 'type' => 'toggle',
519 'section' => 'editing',
520 'label-message' => 'tog-editwidth',
521 );
522 $defaultPreferences['showtoolbar'] =
523 array(
524 'type' => 'toggle',
525 'section' => 'editing',
526 'label-message' => 'tog-showtoolbar',
527 );
528 $defaultPreferences['minordefault'] =
529 array(
530 'type' => 'toggle',
531 'section' => 'editing',
532 'label-message' => 'tog-minordefault',
533 );
534 $defaultPreferences['externaleditor'] =
535 array(
536 'type' => 'toggle',
537 'section' => 'editing',
538 'label-message' => 'tog-externaleditor',
539 );
540 $defaultPreferences['externaldiff'] =
541 array(
542 'type' => 'toggle',
543 'section' => 'editing',
544 'label-message' => 'tog-externaldiff',
545 );
546 $defaultPreferences['forceeditsummary'] =
547 array(
548 'type' => 'toggle',
549 'section' => 'editing',
550 'label-message' => 'tog-forceeditsummary',
551 );
552 $defaultPreferences['uselivepreview'] =
553 array(
554 'type' => 'toggle',
555 'section' => 'editing',
556 'label-message' => 'tog-uselivepreview',
557 );
558
559 ## RecentChanges #####################################
560 $defaultPreferences['rcdays'] =
561 array(
562 'type' => 'int',
563 'label-message' => 'recentchangesdays',
564 'section' => 'rc',
565 'min' => 1,
566 'max' => ceil($wgRCMaxAge / (3600*24)),
567 );
568 $defaultPreferences['rclimit'] =
569 array(
570 'type' => 'int',
571 'label-message' => 'recentchangescount',
572 'section' => 'rc',
573 );
574 $defaultPreferences['usenewrc'] =
575 array(
576 'type' => 'toggle',
577 'label-message' => 'tog-usenewrc',
578 'section' => 'rc',
579 );
580 $defaultPreferences['hideminor'] =
581 array(
582 'type' => 'toggle',
583 'label-message' => 'tog-hideminor',
584 'section' => 'rc',
585 );
586
587 global $wgUseRCPatrol;
588 if ($wgUseRCPatrol) {
589 $defaultPreferences['hidepatrolled'] =
590 array(
591 'type' => 'toggle',
592 'section' => 'rc',
593 'label-message' => 'tog-hidepatrolled',
594 );
595 $defaultPreferences['newpageshidepatrolled'] =
596 array(
597 'type' => 'toggle',
598 'section' => 'rc',
599 'label-message' => 'tog-newpageshidepatrolled',
600 );
601 }
602
603 global $wgRCShowWatchingUsers;
604 if ($wgRCShowWatchingUsers) {
605 $defaultPreferences['shownumberswatching'] =
606 array(
607 'type' => 'toggle',
608 'section' => 'rc',
609 'label-message' => 'tog-shownumberswatching',
610 );
611 }
612
613 ## Watchlist #####################################
614 $defaultPreferences['wllimit'] =
615 array(
616 'type' => 'int',
617 'min' => 0,
618 'max' => 1000,
619 'label-message' => 'prefs-watchlist-edits',
620 'section' => 'watchlist'
621 );
622 $defaultPreferences['watchlistdays'] =
623 array(
624 'type' => 'int',
625 'min' => 0,
626 'max' => 7,
627 'section' => 'watchlist',
628 'label-message' => 'prefs-watchlist-days',
629 );
630 $defaultPreferences['extendwatchlist'] =
631 array(
632 'type' => 'toggle',
633 'section' => 'watchlist',
634 'label-message' => 'tog-extendwatchlist',
635 );
636 $defaultPreferences['watchlisthideminor'] =
637 array(
638 'type' => 'toggle',
639 'section' => 'watchlist',
640 'label-message' => 'tog-watchlisthideminor',
641 );
642 $defaultPreferences['watchlisthidebots'] =
643 array(
644 'type' => 'toggle',
645 'section' => 'watchlist',
646 'label-message' => 'tog-watchlisthidebots',
647 );
648 $defaultPreferences['watchlisthideown'] =
649 array(
650 'type' => 'toggle',
651 'section' => 'watchlist',
652 'label-message' => 'tog-watchlisthideown',
653 );
654 $defaultPreferences['watchlisthideanons'] =
655 array(
656 'type' => 'toggle',
657 'section' => 'watchlist',
658 'label-message' => 'tog-watchlisthideanons',
659 );
660 $defaultPreferences['watchlisthideliu'] =
661 array(
662 'type' => 'toggle',
663 'section' => 'watchlist',
664 'label-message' => 'tog-watchlisthideliu',
665 );
666
667 if ( $wgUseRCPatrol ) {
668 $defaultPreferences['watchlisthidepatrolled'] =
669 array(
670 'type' => 'toggle',
671 'section' => 'watchlist',
672 'label-message' => 'tog-watchlisthidepatrolled',
673 );
674 }
675
676 $watchTypes = array( 'edit' => 'watchdefault',
677 'move' => 'watchmoves',
678 'delete' => 'watchdeletion' );
679
680 // Kinda hacky
681 if( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
682 $watchTypes['read'] = 'watchcreations';
683 }
684
685 foreach( $watchTypes as $action => $pref ) {
686 if ( $user->isAllowed( $action ) ) {
687 $defaultPreferences[$pref] = array(
688 'type' => 'toggle',
689 'section' => 'watchlist',
690 'label-message' => "tog-$pref",
691 );
692 }
693 }
694
695 ## Search #####################################
696 $defaultPreferences['searchlimit'] =
697 array(
698 'type' => 'int',
699 'label-message' => 'resultsperpage',
700 'section' => 'searchoptions',
701 'min' => 0,
702 );
703 $defaultPreferences['contextlines'] =
704 array(
705 'type' => 'int',
706 'label-message' => 'contextlines',
707 'section' => 'searchoptions',
708 'min' => 0,
709 );
710 $defaultPreferences['contextchars'] =
711 array(
712 'type' => 'int',
713 'label-message' => 'contextchars',
714 'section' => 'searchoptions',
715 'min' => 0,
716 );
717
718 // Searchable namespaces back-compat with old format
719 $searchableNamespaces = SearchEngine::searchableNamespaces();
720
721 $nsOptions = array();
722 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
723 if ($ns < 0) continue;
724 $displayNs = str_replace( '_', ' ', $name );
725
726 if (!$displayNs) $displayNs = wfMsg( 'blanknamespace' );
727
728 $nsOptions[$displayNs] = $ns;
729 }
730
731 $defaultPreferences['searchnamespaces'] =
732 array(
733 'type' => 'multiselect',
734 'label-message' => 'defaultns',
735 'options' => $nsOptions,
736 'section' => 'searchoptions',
737 'prefix' => 'searchNs',
738 );
739
740 global $wgEnableMWSuggest;
741 if ($wgEnableMWSuggest) {
742 $defaultPreferences['disablesuggest'] =
743 array(
744 'type' => 'toggle',
745 'label-message' => 'mwsuggest-disable',
746 'section' => 'searchoptions',
747 );
748 }
749
750 ## Misc #####################################
751 $defaultPreferences['diffonly'] =
752 array(
753 'type' => 'toggle',
754 'section' => 'misc',
755 'label-message' => 'tog-diffonly',
756 );
757 $defaultPreferences['norollbackdiff'] =
758 array(
759 'type' => 'toggle',
760 'section' => 'misc',
761 'label-message' => 'tog-norollbackdiff',
762 );
763
764 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
765
766 ## Prod in defaults from the user
767 global $wgDefaultUserOptions;
768 foreach( $defaultPreferences as $name => &$info ) {
769 $prefFromUser = self::getOptionFromUser( $name, $info, $user );
770 $field = HTMLForm::loadInputFromParameters( $info ); // For validation
771 $globalDefault = isset($wgDefaultUserOptions[$name])
772 ? $wgDefaultUserOptions[$name]
773 : null;
774
775 // If it validates, set it as the default
776 if ( isset($info['default']) ) {
777 // Already set, no problem
778 continue;
779 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
780 $field->validate( $prefFromUser, $user->mOptions ) ) {
781 $info['default'] = $prefFromUser;
782 } elseif( $field->validate( $globalDefault, $user->mOptions ) ) {
783 $info['default'] = $globalDefault;
784 }
785 }
786
787 self::$defaultPreferences = $defaultPreferences;
788
789 return $defaultPreferences;
790 }
791
792 // Pull option from a user account. Handles stuff like array-type preferences.
793 static function getOptionFromUser( $name, $info, $user ) {
794 $val = $user->getOption( $name );
795
796 // Handling for array-type preferences
797 if ( ( isset($info['type']) && $info['type'] == 'multiselect' ) ||
798 ( isset($info['class']) && $info['class'] == 'HTMLMultiSelectField' ) ) {
799
800 $options = HTMLFormField::flattenOptions($info['options']);
801 $prefix = isset($info['prefix']) ? $info['prefix'] : $name;
802 $val = array();
803
804 foreach( $options as $label => $value ) {
805 if ($user->getOption( "$prefix$value" ) ) {
806 $val[] = $value;
807 }
808 }
809 }
810
811 return $val;
812 }
813
814 static function generateSkinOptions( $user ) {
815 global $wgDefaultSkin;
816 $ret = array();
817
818 $mptitle = Title::newMainPage();
819 $previewtext = wfMsg( 'skin-preview' );
820 # Only show members of Skin::getSkinNames() rather than
821 # $skinNames (skins is all skin names from Language.php)
822 $validSkinNames = Skin::getUsableSkins();
823 # Sort by UI skin name. First though need to update validSkinNames as sometimes
824 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
825 foreach ( $validSkinNames as $skinkey => &$skinname ) {
826 $msgName = "skinname-{$skinkey}";
827 $localisedSkinName = wfMsg( $msgName );
828 if ( !wfEmptyMsg( $msgName, $localisedSkinName ) ) {
829 $skinname = $localisedSkinName;
830 }
831 }
832 asort($validSkinNames);
833 $sk = $user->getSkin();
834
835 foreach( $validSkinNames as $skinkey => $sn ) {
836 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
837 $previewlink = "(<a target='_blank' href=\"$mplink\">$previewtext</a>)";
838 $extraLinks = '';
839 global $wgAllowUserCss, $wgAllowUserJs;
840 if( $wgAllowUserCss ) {
841 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName().'/'.$skinkey.'.css' );
842 $customCSS = $sk->makeLinkObj( $cssPage, wfMsgExt('prefs-custom-css', array() ) );
843 $extraLinks .= " ($customCSS)";
844 }
845 if( $wgAllowUserJs ) {
846 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName().'/'.$skinkey.'.js' );
847 $customJS = $sk->makeLinkObj( $jsPage, wfMsgHtml('prefs-custom-js') );
848 $extraLinks .= " ($customJS)";
849 }
850 if( $skinkey == $wgDefaultSkin )
851 $sn .= ' (' . wfMsg( 'default' ) . ')';
852 $display = "$sn $previewlink{$extraLinks}";
853 $ret[$display] = $skinkey;
854 }
855
856 return $ret;
857 }
858
859 static function getDateOptions() {
860 global $wgLang;
861 $dateopts = $wgLang->getDatePreferences();
862
863 $ret = array();
864
865 if ($dateopts) {
866 $idCnt = 0;
867 $epoch = '20010115161234'; # Wikipedia day
868 foreach( $dateopts as $key ) {
869 if( $key == 'default' ) {
870 $formatted = wfMsg( 'datedefault' );
871 } else {
872 $formatted = $wgLang->timeanddate( $epoch, false, $key );
873 }
874 $ret[$formatted] = $key;
875 }
876 }
877 return $ret;
878 }
879
880 static function getImageSizes() {
881 global $wgImageLimits;
882
883 $ret = array();
884
885 foreach ( $wgImageLimits as $index => $limits ) {
886 $display = "{$limits[0]}×{$limits[1]}" . wfMsg('unit-pixel');
887 $ret[$display] = $index;
888 }
889
890 return $ret;
891 }
892
893 static function getThumbSizes() {
894 global $wgThumbLimits;
895
896 $ret = array();
897
898 foreach ( $wgThumbLimits as $index => $size ) {
899 $display = $size . wfMsg('unit-pixel');
900 $ret[$display] = $index;
901 }
902
903 return $ret;
904 }
905
906 static function validateSignature( $signature, $alldata ) {
907 global $wgParser, $wgMaxSigChars, $wgLang;
908 if( mb_strlen( $signature ) > $wgMaxSigChars ) {
909 return
910 Xml::element( 'span', array( 'class' => 'error' ),
911 wfMsgExt( 'badsiglength', 'parsemag',
912 $wgLang->formatNum( $wgMaxSigChars )
913 )
914 );
915 } elseif( !empty( $alldata['fancysig'] ) &&
916 false === $wgParser->validateSig( $signature ) ) {
917 return Xml::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) );
918 } else {
919 return true;
920 }
921 }
922
923 static function cleanSignature( $signature, $alldata ) {
924 global $wgParser;
925 if( $alldata['fancysig'] ) {
926 $signature = $wgParser->cleanSig( $signature );
927 } else {
928 // When no fancy sig used, make sure ~{3,5} get removed.
929 $signature = $wgParser->cleanSigInSig( $signature );
930 }
931
932 return $signature;
933 }
934
935 static function validateEmail( $email, $alldata ) {
936 global $wgUser; // To check
937
938 if ( $email && !$wgUser->isValidEmailAddr( $email ) ) {
939 return wfMsgExt( 'invalidemailaddress', 'parseinline' );
940 }
941
942 global $wgEmailConfirmToEdit;
943 if( $wgEmailConfirmToEdit && !$email ) {
944 return wfMsgExt( 'noemailtitle', 'parseinline' );
945 }
946 return true;
947 }
948
949 static function getFormObject( $user ) {
950 $formDescriptor = Preferences::getPreferences( $user );
951 $htmlForm = new PreferencesForm( $formDescriptor, 'prefs' );
952
953 $htmlForm->setSubmitText( wfMsg('saveprefs') );
954 $htmlForm->setTitle( SpecialPage::getTitleFor( 'Preferences' ) );
955 $htmlForm->setSubmitID( 'prefsubmit' );
956 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
957
958 return $htmlForm;
959 }
960
961 static function getTimezoneOptions() {
962 $opt = array();
963
964 global $wgLocalTZoffset;
965
966 $opt[wfMsg( 'timezoneuseserverdefault' )] = "System|$wgLocalTZoffset";
967 $opt[wfMsg( 'timezoneuseoffset' )] = 'other';
968 $opt[wfMsg( 'guesstimezone' )] = 'guess';
969
970 if ( function_exists( 'timezone_identifiers_list' ) ) {
971 # Read timezone list
972 $tzs = timezone_identifiers_list();
973 sort( $tzs );
974
975 $tzRegions = array();
976 $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' );
977 $tzRegions['America'] = wfMsg( 'timezoneregion-america' );
978 $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' );
979 $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' );
980 $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' );
981 $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' );
982 $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' );
983 $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' );
984 $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' );
985 $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' );
986 asort( $tzRegions );
987
988 $prefill = array_fill_keys( array_values($tzRegions), array() );
989 $opt = array_merge( $opt, $prefill );
990
991 $now = date_create( 'now' );
992
993 foreach ( $tzs as $tz ) {
994 $z = explode( '/', $tz, 2 );
995
996 # timezone_identifiers_list() returns a number of
997 # backwards-compatibility entries. This filters them out of the
998 # list presented to the user.
999 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) )
1000 continue;
1001
1002 # Localize region
1003 $z[0] = $tzRegions[$z[0]];
1004
1005 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1006
1007 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1008 $value = "ZoneInfo|$minDiff|$tz";
1009
1010 $opt[$z[0]][$display] = $value;
1011 }
1012 }
1013 return $opt;
1014 }
1015
1016 static function filterTimezoneInput( $tz, $alldata ) {
1017 $data = explode( '|', $tz, 3 );
1018 switch ( $data[0] ) {
1019 case 'ZoneInfo':
1020 case 'System':
1021 return $tz;
1022 default:
1023 $data = explode( ':', $tz, 2 );
1024 $minDiff = 0;
1025 if( count( $data ) == 2 ) {
1026 $data[0] = intval( $data[0] );
1027 $data[1] = intval( $data[1] );
1028 $minDiff = abs( $data[0] ) * 60 + $data[1];
1029 if ( $data[0] < 0 ) $minDiff = -$minDiff;
1030 } else {
1031 $minDiff = intval( $data[0] ) * 60;
1032 }
1033
1034 # Max is +14:00 and min is -12:00, see:
1035 # http://en.wikipedia.org/wiki/Timezone
1036 $minDiff = min( $minDiff, 840 ); # 14:00
1037 $minDiff = max( $minDiff, -720 ); # -12:00
1038 return 'Offset|'.$minDiff;
1039 }
1040 }
1041
1042 static function tryFormSubmit( $formData ) {
1043 global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
1044
1045 // Filter input
1046 foreach( array_keys($formData) as $name ) {
1047 if ( isset(self::$saveFilters[$name]) ) {
1048 $formData[$name] =
1049 call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1050 }
1051 }
1052
1053 // Stuff that shouldn't be saved as a preference.
1054 $saveBlacklist = array(
1055 'realname',
1056 'emailaddress',
1057 );
1058
1059 if( $wgEnableEmail ) {
1060 $newadr = $formData['emailaddress'];
1061 $oldadr = $wgUser->getEmail();
1062 if( ($newadr != '') && ($newadr != $oldadr) ) {
1063 # the user has supplied a new email address on the login page
1064 # new behaviour: set this new emailaddr from login-page into user database record
1065 $wgUser->setEmail( $newadr );
1066 # but flag as "dirty" = unauthenticated
1067 $wgUser->invalidateEmail();
1068 if ($wgEmailAuthentication) {
1069 # Mail a temporary password to the dirty address.
1070 # User can come back through the confirmation URL to re-enable email.
1071 $result = $wgUser->sendConfirmationMail();
1072 if( WikiError::isError( $result ) ) {
1073 return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
1074 } else {
1075 // TODO return this somehow
1076 # wfMsg( 'eauthentsent', $wgUser->getName() );
1077 }
1078 }
1079 } else {
1080 $wgUser->setEmail( $newadr );
1081 }
1082 if( $oldadr != $newadr ) {
1083 wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldadr, $newadr ) );
1084 }
1085 }
1086
1087 // Fortunately, the realname field is MUCH simpler
1088 global $wgAllowRealName;
1089 if ($wgAllowRealName) {
1090 $realName = $formData['realname'];
1091 $wgUser->setRealName( $realName );
1092 }
1093
1094 foreach( $saveBlacklist as $b )
1095 unset( $formData[$b] );
1096
1097 // Keeps old preferences from interfering due to back-compat
1098 // code, etc.
1099 $wgUser->resetOptions();
1100
1101 foreach( $formData as $key => $value ) {
1102 $wgUser->setOption( $key, $value );
1103 }
1104
1105 $wgUser->saveSettings();
1106
1107 // Done
1108 global $wgOut;
1109 $wgOut->redirect( SpecialPage::getTitleFor( 'Preferences' )->getFullURL( 'success' ) );
1110
1111 return true;
1112 }
1113
1114 public static function loadOldSearchNs( $user ) {
1115 $searchableNamespaces = SearchEngine::searchableNamespaces();
1116 // Back compat with old format
1117 $arr = array();
1118
1119 foreach( $searchableNamespaces as $ns => $name ) {
1120 if( $user->getOption( 'searchNs' . $ns ) ) {
1121 $arr[] = $ns;
1122 }
1123 }
1124
1125 return $arr;
1126 }
1127 }
1128
1129 /** Some tweaks to allow js prefs to work */
1130 class PreferencesForm extends HTMLForm {
1131
1132 function wrapForm( $html ) {
1133 $html = Xml::tags( 'div', array( 'id' => 'preferences' ), $html );
1134
1135 return parent::wrapForm( $html );
1136 }
1137
1138 function getButtons() {
1139 $html = parent::getButtons();
1140
1141 global $wgUser;
1142
1143 $sk = $wgUser->getSkin();
1144 $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1145
1146 $html .= "\n" . $sk->link( $t, wfMsg( 'restoreprefs' ) );
1147
1148 return $html;
1149 }
1150
1151 function filterDataForSubmit( $data ) {
1152 // Support for separating MultiSelect preferences into multiple preferences
1153 // Due to lack of array support.
1154 foreach( $this->mFlatFields as $fieldname => $field ) {
1155 $info = $field->mParams;
1156 if ($field instanceof HTMLMultiSelectField) {
1157 $options = HTMLFormField::flattenOptions( $info['options'] );
1158 $prefix = isset($info['prefix']) ? $info['prefix'] : $fieldname;
1159
1160 foreach( $options as $opt ) {
1161 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1162 }
1163
1164 unset( $data[$fieldname] );
1165 }
1166 }
1167
1168 return $data;
1169 }
1170 }