64e71bcaf1c118c2607b2b0fb2eac8dadd6e25dc
[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 $selectedSkin = $user->getOption( 'skin' );
315 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
316 $settings = array_flip($wgLang->getQuickbarSettings());
317
318 $defaultPreferences['quickbar'] =
319 array(
320 'type' => 'radio',
321 'options' => $settings,
322 'section' => 'skin',
323 'label-message' => 'qbsettings',
324 );
325 }
326
327 ## Math #####################################
328 global $wgUseTeX;
329 if ($wgUseTeX) {
330 $defaultPreferences['math'] =
331 array(
332 'type' => 'radio',
333 'options' =>
334 array_flip( array_map( 'wfMsg', $wgLang->getMathNames() ) ),
335 'label' => '&nbsp;',
336 'section' => 'math',
337 );
338 }
339
340 ## Files #####################################
341 $defaultPreferences['imagesize'] =
342 array(
343 'type' => 'select',
344 'options' => self::getImageSizes(),
345 'label-message' => 'imagemaxsize',
346 'section' => 'files',
347 );
348 $defaultPreferences['thumbsize'] =
349 array(
350 'type' => 'select',
351 'options' => self::getThumbSizes(),
352 'label-message' => 'thumbsize',
353 'section' => 'files',
354 );
355
356 ## Date and time #####################################
357 $dateOptions = self::getDateOptions();
358 if ($dateOptions) {
359 $defaultPreferences['date'] =
360 array(
361 'type' => 'radio',
362 'options' => $dateOptions,
363 'label-message' => 'dateformat',
364 'section' => 'datetime',
365 );
366 }
367
368 // Info
369 $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
370 $wgLang->time( $now = wfTimestampNow(), true ) );
371 $nowserver = $wgLang->time( $now, false ) .
372 Xml::hidden( 'wpServerTime', substr( $now, 8, 2 ) * 60 + substr( $now, 10, 2 ) );
373
374 $defaultPreferences['nowserver'] =
375 array(
376 'type' => 'info',
377 'raw' => 1,
378 'label-message' => 'servertime',
379 'default' => $nowserver,
380 'section' => 'datetime',
381 );
382
383 $defaultPreferences['nowlocal'] =
384 array(
385 'type' => 'info',
386 'raw' => 1,
387 'label-message' => 'localtime',
388 'default' => $nowlocal,
389 'section' => 'datetime',
390 );
391
392 // Grab existing pref.
393 $tzOffset = $user->getOption( 'timecorrection' );
394 $tz = explode( '|', $tzOffset, 2 );
395
396 $tzSetting = $tzOffset;
397 if (count($tz) > 1 && $tz[0] == 'Offset') {
398 $minDiff = $tz[1];
399 $tzSetting = sprintf( '%+03d:%02d', floor($minDiff/60), abs($minDiff)%60 );;
400 }
401
402 $defaultPreferences['timecorrection'] =
403 array(
404 'class' => 'HTMLSelectOrOtherField',
405 'label-message' => 'timezonelegend',
406 'options' => self::getTimezoneOptions(),
407 'default' => $tzSetting,
408 'section' => 'datetime',
409 );
410
411 ## Page Rendering ##############################
412 $defaultPreferences['underline'] =
413 array(
414 'type' => 'select',
415 'options' => array(
416 wfMsg( 'underline-never' ) => 0,
417 wfMsg( 'underline-always' ) => 1,
418 wfMsg( 'underline-default' ) => 2,
419 ),
420 'label-message' => 'tog-underline',
421 'section' => 'rendering',
422 );
423
424 $stubThresholdValues = array( 0, 50, 100, 500, 1000, 2000, 5000, 10000 );
425 $stubThresholdOptions = array();
426 foreach( $stubThresholdValues as $value ) {
427 $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value;
428 }
429
430 $defaultPreferences['stubthreshold'] =
431 array(
432 'type' => 'selectorother',
433 'section' => 'rendering',
434 'options' => $stubThresholdOptions,
435 'label' => wfMsg('stub-threshold'), // Raw HTML message. Yay?
436 );
437 $defaultPreferences['highlightbroken'] =
438 array(
439 'type' => 'toggle',
440 'section' => 'rendering',
441 'label' => wfMsg('tog-highlightbroken'), // Raw HTML
442 );
443 $defaultPreferences['showtoc'] =
444 array(
445 'type' => 'toggle',
446 'section' => 'rendering',
447 'label-message' => 'tog-showtoc',
448 );
449 $defaultPreferences['nocache'] =
450 array(
451 'type' => 'toggle',
452 'label-message' => 'tog-nocache',
453 'section' => 'rendering',
454 );
455 $defaultPreferences['showhiddencats'] =
456 array(
457 'type' => 'toggle',
458 'section' => 'rendering',
459 'label-message' => 'tog-showhiddencats'
460 );
461 $defaultPreferences['showjumplinks'] =
462 array(
463 'type' => 'toggle',
464 'section' => 'rendering',
465 'label-message' => 'tog-showjumplinks',
466 );
467 $defaultPreferences['justify'] =
468 array(
469 'type' => 'toggle',
470 'section' => 'rendering',
471 'label-message' => 'tog-justify',
472 );
473 $defaultPreferences['numberheadings'] =
474 array(
475 'type' => 'toggle',
476 'section' => 'rendering',
477 'label-message' => 'tog-numberheadings',
478 );
479
480 ## Editing #####################################
481 $defaultPreferences['cols'] =
482 array(
483 'type' => 'int',
484 'label-message' => 'columns',
485 'section' => 'editing',
486 'min' => 4,
487 'max' => 1000,
488 );
489 $defaultPreferences['rows'] =
490 array(
491 'type' => 'int',
492 'label-message' => 'rows',
493 'section' => 'editing',
494 'min' => 4,
495 'max' => 1000,
496 );
497 $defaultPreferences['previewontop'] =
498 array(
499 'type' => 'toggle',
500 'section' => 'editing',
501 'label-message' => 'tog-previewontop',
502 );
503 $defaultPreferences['previewonfirst'] =
504 array(
505 'type' => 'toggle',
506 'section' => 'editing',
507 'label-message' => 'tog-previewonfirst',
508 );
509 $defaultPreferences['editsection'] =
510 array(
511 'type' => 'toggle',
512 'section' => 'editing',
513 'label-message' => 'tog-editsection',
514 );
515 $defaultPreferences['editsectiononrightclick'] =
516 array(
517 'type' => 'toggle',
518 'section' => 'editing',
519 'label-message' => 'tog-editsectiononrightclick',
520 );
521 $defaultPreferences['editondblclick'] =
522 array(
523 'type' => 'toggle',
524 'section' => 'editing',
525 'label-message' => 'tog-editondblclick',
526 );
527 $defaultPreferences['editwidth'] =
528 array(
529 'type' => 'toggle',
530 'section' => 'editing',
531 'label-message' => 'tog-editwidth',
532 );
533 $defaultPreferences['showtoolbar'] =
534 array(
535 'type' => 'toggle',
536 'section' => 'editing',
537 'label-message' => 'tog-showtoolbar',
538 );
539 $defaultPreferences['minordefault'] =
540 array(
541 'type' => 'toggle',
542 'section' => 'editing',
543 'label-message' => 'tog-minordefault',
544 );
545 $defaultPreferences['externaleditor'] =
546 array(
547 'type' => 'toggle',
548 'section' => 'editing',
549 'label-message' => 'tog-externaleditor',
550 );
551 $defaultPreferences['externaldiff'] =
552 array(
553 'type' => 'toggle',
554 'section' => 'editing',
555 'label-message' => 'tog-externaldiff',
556 );
557 $defaultPreferences['forceeditsummary'] =
558 array(
559 'type' => 'toggle',
560 'section' => 'editing',
561 'label-message' => 'tog-forceeditsummary',
562 );
563 $defaultPreferences['uselivepreview'] =
564 array(
565 'type' => 'toggle',
566 'section' => 'editing',
567 'label-message' => 'tog-uselivepreview',
568 );
569
570 ## RecentChanges #####################################
571 $defaultPreferences['rcdays'] =
572 array(
573 'type' => 'int',
574 'label-message' => 'recentchangesdays',
575 'section' => 'rc',
576 'min' => 1,
577 'max' => ceil($wgRCMaxAge / (3600*24)),
578 );
579 $defaultPreferences['rclimit'] =
580 array(
581 'type' => 'int',
582 'label-message' => 'recentchangescount',
583 'section' => 'rc',
584 );
585 $defaultPreferences['usenewrc'] =
586 array(
587 'type' => 'toggle',
588 'label-message' => 'tog-usenewrc',
589 'section' => 'rc',
590 );
591 $defaultPreferences['hideminor'] =
592 array(
593 'type' => 'toggle',
594 'label-message' => 'tog-hideminor',
595 'section' => 'rc',
596 );
597
598 global $wgUseRCPatrol;
599 if ($wgUseRCPatrol) {
600 $defaultPreferences['hidepatrolled'] =
601 array(
602 'type' => 'toggle',
603 'section' => 'rc',
604 'label-message' => 'tog-hidepatrolled',
605 );
606 $defaultPreferences['newpageshidepatrolled'] =
607 array(
608 'type' => 'toggle',
609 'section' => 'rc',
610 'label-message' => 'tog-newpageshidepatrolled',
611 );
612 }
613
614 global $wgRCShowWatchingUsers;
615 if ($wgRCShowWatchingUsers) {
616 $defaultPreferences['shownumberswatching'] =
617 array(
618 'type' => 'toggle',
619 'section' => 'rc',
620 'label-message' => 'tog-shownumberswatching',
621 );
622 }
623
624 ## Watchlist #####################################
625 $defaultPreferences['wllimit'] =
626 array(
627 'type' => 'int',
628 'min' => 0,
629 'max' => 1000,
630 'label-message' => 'prefs-watchlist-edits',
631 'section' => 'watchlist'
632 );
633 $defaultPreferences['watchlistdays'] =
634 array(
635 'type' => 'int',
636 'min' => 0,
637 'max' => 7,
638 'section' => 'watchlist',
639 'label-message' => 'prefs-watchlist-days',
640 );
641 $defaultPreferences['extendwatchlist'] =
642 array(
643 'type' => 'toggle',
644 'section' => 'watchlist',
645 'label-message' => 'tog-extendwatchlist',
646 );
647 $defaultPreferences['watchlisthideminor'] =
648 array(
649 'type' => 'toggle',
650 'section' => 'watchlist',
651 'label-message' => 'tog-watchlisthideminor',
652 );
653 $defaultPreferences['watchlisthidebots'] =
654 array(
655 'type' => 'toggle',
656 'section' => 'watchlist',
657 'label-message' => 'tog-watchlisthidebots',
658 );
659 $defaultPreferences['watchlisthideown'] =
660 array(
661 'type' => 'toggle',
662 'section' => 'watchlist',
663 'label-message' => 'tog-watchlisthideown',
664 );
665 $defaultPreferences['watchlisthideanons'] =
666 array(
667 'type' => 'toggle',
668 'section' => 'watchlist',
669 'label-message' => 'tog-watchlisthideanons',
670 );
671 $defaultPreferences['watchlisthideliu'] =
672 array(
673 'type' => 'toggle',
674 'section' => 'watchlist',
675 'label-message' => 'tog-watchlisthideliu',
676 );
677
678 if ( $wgUseRCPatrol ) {
679 $defaultPreferences['watchlisthidepatrolled'] =
680 array(
681 'type' => 'toggle',
682 'section' => 'watchlist',
683 'label-message' => 'tog-watchlisthidepatrolled',
684 );
685 }
686
687 $watchTypes = array( 'edit' => 'watchdefault',
688 'move' => 'watchmoves',
689 'delete' => 'watchdeletion' );
690
691 // Kinda hacky
692 if( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
693 $watchTypes['read'] = 'watchcreations';
694 }
695
696 foreach( $watchTypes as $action => $pref ) {
697 if ( $user->isAllowed( $action ) ) {
698 $defaultPreferences[$pref] = array(
699 'type' => 'toggle',
700 'section' => 'watchlist',
701 'label-message' => "tog-$pref",
702 );
703 }
704 }
705
706 ## Search #####################################
707 $defaultPreferences['searchlimit'] =
708 array(
709 'type' => 'int',
710 'label-message' => 'resultsperpage',
711 'section' => 'searchoptions',
712 'min' => 0,
713 );
714 $defaultPreferences['contextlines'] =
715 array(
716 'type' => 'int',
717 'label-message' => 'contextlines',
718 'section' => 'searchoptions',
719 'min' => 0,
720 );
721 $defaultPreferences['contextchars'] =
722 array(
723 'type' => 'int',
724 'label-message' => 'contextchars',
725 'section' => 'searchoptions',
726 'min' => 0,
727 );
728
729 // Searchable namespaces back-compat with old format
730 $searchableNamespaces = SearchEngine::searchableNamespaces();
731
732 $nsOptions = array();
733 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
734 if ($ns < 0) continue;
735 $displayNs = str_replace( '_', ' ', $name );
736
737 if (!$displayNs) $displayNs = wfMsg( 'blanknamespace' );
738
739 $nsOptions[$displayNs] = $ns;
740 }
741
742 $defaultPreferences['searchnamespaces'] =
743 array(
744 'type' => 'multiselect',
745 'label-message' => 'defaultns',
746 'options' => $nsOptions,
747 'section' => 'searchoptions',
748 'prefix' => 'searchNs',
749 );
750
751 global $wgEnableMWSuggest;
752 if ($wgEnableMWSuggest) {
753 $defaultPreferences['disablesuggest'] =
754 array(
755 'type' => 'toggle',
756 'label-message' => 'mwsuggest-disable',
757 'section' => 'searchoptions',
758 );
759 }
760
761 ## Misc #####################################
762 $defaultPreferences['diffonly'] =
763 array(
764 'type' => 'toggle',
765 'section' => 'misc',
766 'label-message' => 'tog-diffonly',
767 );
768 $defaultPreferences['norollbackdiff'] =
769 array(
770 'type' => 'toggle',
771 'section' => 'misc',
772 'label-message' => 'tog-norollbackdiff',
773 );
774
775 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
776
777 ## Prod in defaults from the user
778 global $wgDefaultUserOptions;
779 foreach( $defaultPreferences as $name => &$info ) {
780 $prefFromUser = self::getOptionFromUser( $name, $info, $user );
781 $field = HTMLForm::loadInputFromParameters( $info ); // For validation
782 $globalDefault = isset($wgDefaultUserOptions[$name])
783 ? $wgDefaultUserOptions[$name]
784 : null;
785
786 // If it validates, set it as the default
787 if ( isset($info['default']) ) {
788 // Already set, no problem
789 continue;
790 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
791 $field->validate( $prefFromUser, $user->mOptions ) ) {
792 $info['default'] = $prefFromUser;
793 } elseif( $field->validate( $globalDefault, $user->mOptions ) ) {
794 $info['default'] = $globalDefault;
795 }
796 }
797
798 self::$defaultPreferences = $defaultPreferences;
799
800 return $defaultPreferences;
801 }
802
803 // Pull option from a user account. Handles stuff like array-type preferences.
804 static function getOptionFromUser( $name, $info, $user ) {
805 $val = $user->getOption( $name );
806
807 // Handling for array-type preferences
808 if ( ( isset($info['type']) && $info['type'] == 'multiselect' ) ||
809 ( isset($info['class']) && $info['class'] == 'HTMLMultiSelectField' ) ) {
810
811 $options = HTMLFormField::flattenOptions($info['options']);
812 $prefix = isset($info['prefix']) ? $info['prefix'] : $name;
813 $val = array();
814
815 foreach( $options as $label => $value ) {
816 if ($user->getOption( "$prefix$value" ) ) {
817 $val[] = $value;
818 }
819 }
820 }
821
822 return $val;
823 }
824
825 static function generateSkinOptions( $user ) {
826 global $wgDefaultSkin;
827 $ret = array();
828
829 $mptitle = Title::newMainPage();
830 $previewtext = wfMsg( 'skin-preview' );
831 # Only show members of Skin::getSkinNames() rather than
832 # $skinNames (skins is all skin names from Language.php)
833 $validSkinNames = Skin::getUsableSkins();
834 # Sort by UI skin name. First though need to update validSkinNames as sometimes
835 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
836 foreach ( $validSkinNames as $skinkey => &$skinname ) {
837 $msgName = "skinname-{$skinkey}";
838 $localisedSkinName = wfMsg( $msgName );
839 if ( !wfEmptyMsg( $msgName, $localisedSkinName ) ) {
840 $skinname = $localisedSkinName;
841 }
842 }
843 asort($validSkinNames);
844 $sk = $user->getSkin();
845
846 foreach( $validSkinNames as $skinkey => $sn ) {
847 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
848 $previewlink = "(<a target='_blank' href=\"$mplink\">$previewtext</a>)";
849 $extraLinks = '';
850 global $wgAllowUserCss, $wgAllowUserJs;
851 if( $wgAllowUserCss ) {
852 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName().'/'.$skinkey.'.css' );
853 $customCSS = $sk->makeLinkObj( $cssPage, wfMsgExt('prefs-custom-css', array() ) );
854 $extraLinks .= " ($customCSS)";
855 }
856 if( $wgAllowUserJs ) {
857 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName().'/'.$skinkey.'.js' );
858 $customJS = $sk->makeLinkObj( $jsPage, wfMsgHtml('prefs-custom-js') );
859 $extraLinks .= " ($customJS)";
860 }
861 if( $skinkey == $wgDefaultSkin )
862 $sn .= ' (' . wfMsg( 'default' ) . ')';
863 $display = "$sn $previewlink{$extraLinks}";
864 $ret[$display] = $skinkey;
865 }
866
867 return $ret;
868 }
869
870 static function getDateOptions() {
871 global $wgLang;
872 $dateopts = $wgLang->getDatePreferences();
873
874 $ret = array();
875
876 if ($dateopts) {
877 $idCnt = 0;
878 $epoch = '20010115161234'; # Wikipedia day
879 foreach( $dateopts as $key ) {
880 if( $key == 'default' ) {
881 $formatted = wfMsg( 'datedefault' );
882 } else {
883 $formatted = $wgLang->timeanddate( $epoch, false, $key );
884 }
885 $ret[$formatted] = $key;
886 }
887 }
888 return $ret;
889 }
890
891 static function getImageSizes() {
892 global $wgImageLimits;
893
894 $ret = array();
895
896 foreach ( $wgImageLimits as $index => $limits ) {
897 $display = "{$limits[0]}×{$limits[1]}" . wfMsg('unit-pixel');
898 $ret[$display] = $index;
899 }
900
901 return $ret;
902 }
903
904 static function getThumbSizes() {
905 global $wgThumbLimits;
906
907 $ret = array();
908
909 foreach ( $wgThumbLimits as $index => $size ) {
910 $display = $size . wfMsg('unit-pixel');
911 $ret[$display] = $index;
912 }
913
914 return $ret;
915 }
916
917 static function validateSignature( $signature, $alldata ) {
918 global $wgParser, $wgMaxSigChars, $wgLang;
919 if( mb_strlen( $signature ) > $wgMaxSigChars ) {
920 return
921 Xml::element( 'span', array( 'class' => 'error' ),
922 wfMsgExt( 'badsiglength', 'parsemag',
923 $wgLang->formatNum( $wgMaxSigChars )
924 )
925 );
926 } elseif( !empty( $alldata['fancysig'] ) &&
927 false === $wgParser->validateSig( $signature ) ) {
928 return Xml::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) );
929 } else {
930 return true;
931 }
932 }
933
934 static function cleanSignature( $signature, $alldata ) {
935 global $wgParser;
936 if( $alldata['fancysig'] ) {
937 $signature = $wgParser->cleanSig( $signature );
938 } else {
939 // When no fancy sig used, make sure ~{3,5} get removed.
940 $signature = $wgParser->cleanSigInSig( $signature );
941 }
942
943 return $signature;
944 }
945
946 static function validateEmail( $email, $alldata ) {
947 global $wgUser; // To check
948
949 if ( $email && !$wgUser->isValidEmailAddr( $email ) ) {
950 return wfMsgExt( 'invalidemailaddress', 'parseinline' );
951 }
952
953 global $wgEmailConfirmToEdit;
954 if( $wgEmailConfirmToEdit && !$email ) {
955 return wfMsgExt( 'noemailtitle', 'parseinline' );
956 }
957 return true;
958 }
959
960 static function getFormObject( $user ) {
961 $formDescriptor = Preferences::getPreferences( $user );
962 $htmlForm = new PreferencesForm( $formDescriptor, 'prefs' );
963
964 $htmlForm->setSubmitText( wfMsg('saveprefs') );
965 $htmlForm->setTitle( SpecialPage::getTitleFor( 'Preferences' ) );
966 $htmlForm->setSubmitID( 'prefsubmit' );
967 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
968
969 return $htmlForm;
970 }
971
972 static function getTimezoneOptions() {
973 $opt = array();
974
975 global $wgLocalTZoffset;
976
977 $opt[wfMsg( 'timezoneuseserverdefault' )] = "System|$wgLocalTZoffset";
978 $opt[wfMsg( 'timezoneuseoffset' )] = 'other';
979 $opt[wfMsg( 'guesstimezone' )] = 'guess';
980
981 if ( function_exists( 'timezone_identifiers_list' ) ) {
982 # Read timezone list
983 $tzs = timezone_identifiers_list();
984 sort( $tzs );
985
986 $tzRegions = array();
987 $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' );
988 $tzRegions['America'] = wfMsg( 'timezoneregion-america' );
989 $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' );
990 $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' );
991 $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' );
992 $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' );
993 $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' );
994 $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' );
995 $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' );
996 $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' );
997 asort( $tzRegions );
998
999 $prefill = array_fill_keys( array_values($tzRegions), array() );
1000 $opt = array_merge( $opt, $prefill );
1001
1002 $now = date_create( 'now' );
1003
1004 foreach ( $tzs as $tz ) {
1005 $z = explode( '/', $tz, 2 );
1006
1007 # timezone_identifiers_list() returns a number of
1008 # backwards-compatibility entries. This filters them out of the
1009 # list presented to the user.
1010 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) )
1011 continue;
1012
1013 # Localize region
1014 $z[0] = $tzRegions[$z[0]];
1015
1016 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1017
1018 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1019 $value = "ZoneInfo|$minDiff|$tz";
1020
1021 $opt[$z[0]][$display] = $value;
1022 }
1023 }
1024 return $opt;
1025 }
1026
1027 static function filterTimezoneInput( $tz, $alldata ) {
1028 $data = explode( '|', $tz, 3 );
1029 switch ( $data[0] ) {
1030 case 'ZoneInfo':
1031 case 'System':
1032 return $tz;
1033 default:
1034 $data = explode( ':', $tz, 2 );
1035 $minDiff = 0;
1036 if( count( $data ) == 2 ) {
1037 $data[0] = intval( $data[0] );
1038 $data[1] = intval( $data[1] );
1039 $minDiff = abs( $data[0] ) * 60 + $data[1];
1040 if ( $data[0] < 0 ) $minDiff = -$minDiff;
1041 } else {
1042 $minDiff = intval( $data[0] ) * 60;
1043 }
1044
1045 # Max is +14:00 and min is -12:00, see:
1046 # http://en.wikipedia.org/wiki/Timezone
1047 $minDiff = min( $minDiff, 840 ); # 14:00
1048 $minDiff = max( $minDiff, -720 ); # -12:00
1049 return 'Offset|'.$minDiff;
1050 }
1051 }
1052
1053 static function tryFormSubmit( $formData ) {
1054 global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
1055
1056 // Filter input
1057 foreach( array_keys($formData) as $name ) {
1058 if ( isset(self::$saveFilters[$name]) ) {
1059 $formData[$name] =
1060 call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1061 }
1062 }
1063
1064 // Stuff that shouldn't be saved as a preference.
1065 $saveBlacklist = array(
1066 'realname',
1067 'emailaddress',
1068 );
1069
1070 if( $wgEnableEmail ) {
1071 $newadr = $formData['emailaddress'];
1072 $oldadr = $wgUser->getEmail();
1073 if( ($newadr != '') && ($newadr != $oldadr) ) {
1074 # the user has supplied a new email address on the login page
1075 # new behaviour: set this new emailaddr from login-page into user database record
1076 $wgUser->setEmail( $newadr );
1077 # but flag as "dirty" = unauthenticated
1078 $wgUser->invalidateEmail();
1079 if ($wgEmailAuthentication) {
1080 # Mail a temporary password to the dirty address.
1081 # User can come back through the confirmation URL to re-enable email.
1082 $result = $wgUser->sendConfirmationMail();
1083 if( WikiError::isError( $result ) ) {
1084 return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
1085 } else {
1086 // TODO return this somehow
1087 # wfMsg( 'eauthentsent', $wgUser->getName() );
1088 }
1089 }
1090 } else {
1091 $wgUser->setEmail( $newadr );
1092 }
1093 if( $oldadr != $newadr ) {
1094 wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldadr, $newadr ) );
1095 }
1096 }
1097
1098 // Fortunately, the realname field is MUCH simpler
1099 global $wgAllowRealName;
1100 if ($wgAllowRealName) {
1101 $realName = $formData['realname'];
1102 $wgUser->setRealName( $realName );
1103 }
1104
1105 foreach( $saveBlacklist as $b )
1106 unset( $formData[$b] );
1107
1108 // Keeps old preferences from interfering due to back-compat
1109 // code, etc.
1110 $wgUser->resetOptions();
1111
1112 foreach( $formData as $key => $value ) {
1113 $wgUser->setOption( $key, $value );
1114 }
1115
1116 $wgUser->saveSettings();
1117
1118 // Done
1119 global $wgOut;
1120 $wgOut->redirect( SpecialPage::getTitleFor( 'Preferences' )->getFullURL( 'success' ) );
1121
1122 return true;
1123 }
1124
1125 public static function loadOldSearchNs( $user ) {
1126 $searchableNamespaces = SearchEngine::searchableNamespaces();
1127 // Back compat with old format
1128 $arr = array();
1129
1130 foreach( $searchableNamespaces as $ns => $name ) {
1131 if( $user->getOption( 'searchNs' . $ns ) ) {
1132 $arr[] = $ns;
1133 }
1134 }
1135
1136 return $arr;
1137 }
1138 }
1139
1140 /** Some tweaks to allow js prefs to work */
1141 class PreferencesForm extends HTMLForm {
1142
1143 function wrapForm( $html ) {
1144 $html = Xml::tags( 'div', array( 'id' => 'preferences' ), $html );
1145
1146 return parent::wrapForm( $html );
1147 }
1148
1149 function getButtons() {
1150 $html = parent::getButtons();
1151
1152 global $wgUser;
1153
1154 $sk = $wgUser->getSkin();
1155 $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1156
1157 $html .= "\n" . $sk->link( $t, wfMsg( 'restoreprefs' ) );
1158
1159 return $html;
1160 }
1161
1162 function filterDataForSubmit( $data ) {
1163 // Support for separating MultiSelect preferences into multiple preferences
1164 // Due to lack of array support.
1165 foreach( $this->mFlatFields as $fieldname => $field ) {
1166 $info = $field->mParams;
1167 if ($field instanceof HTMLMultiSelectField) {
1168 $options = HTMLFormField::flattenOptions( $info['options'] );
1169 $prefix = isset($info['prefix']) ? $info['prefix'] : $fieldname;
1170
1171 foreach( $options as $opt ) {
1172 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1173 }
1174
1175 unset( $data[$fieldname] );
1176 }
1177 }
1178
1179 return $data;
1180 }
1181 }