re r106025 — apply follow up from Luca Fulchir
[lhc/web/wiklou.git] / includes / Preferences.php
1 <?php
2 /**
3 * We're now using the HTMLForm object with some customisation to generate the
4 * Preferences form. This object handles generic submission, CSRF protection,
5 * layout and other logic in a reusable manner. We subclass it as a PreferencesForm
6 * to make some minor customisations.
7 *
8 * In order to generate the form, the HTMLForm object needs an array structure
9 * detailing the form fields available, and that's what this class is for. Each
10 * element of the array is a basic property-list, including the type of field,
11 * the label it is to be given in the form, callbacks for validation and
12 * 'filtering', and other pertinent information. Note that the 'default' field
13 * is named for generic forms, and does not represent the preference's default
14 * (which is stored in $wgDefaultUserOptions), but the default for the form
15 * field, which should be whatever the user has set for that preference. There
16 * is no need to override it unless you have some special storage logic (for
17 * instance, those not presently stored as options, but which are best set from
18 * the user preferences view).
19 *
20 * Field types are implemented as subclasses of the generic HTMLFormField
21 * object, and typically implement at least getInputHTML, which generates the
22 * HTML for the input field to be placed in the table.
23 *
24 * Once fields have been retrieved and validated, submission logic is handed
25 * over to the tryUISubmit static method of this class.
26 */
27
28 class Preferences {
29 static $defaultPreferences = null;
30 static $saveFilters = array(
31 'timecorrection' => array( 'Preferences', 'filterTimezoneInput' ),
32 'rows' => array( 'Preferences', 'filterIntval' ),
33 'rclimit' => array( 'Preferences', 'filterIntval' ),
34 'wllimit' => array( 'Preferences', 'filterIntval' ),
35 'searchlimit' => array( 'Preferences', 'filterIntval' ),
36 );
37
38 /**
39 * @throws MWException
40 * @param $user User
41 * @param $context IContextSource
42 * @return array|null
43 */
44 static function getPreferences( $user, IContextSource $context ) {
45 if ( self::$defaultPreferences ) {
46 return self::$defaultPreferences;
47 }
48
49 $defaultPreferences = array();
50
51 self::profilePreferences( $user, $context, $defaultPreferences );
52 self::skinPreferences( $user, $context, $defaultPreferences );
53 self::filesPreferences( $user, $context, $defaultPreferences );
54 self::datetimePreferences( $user, $context, $defaultPreferences );
55 self::renderingPreferences( $user, $context, $defaultPreferences );
56 self::editingPreferences( $user, $context, $defaultPreferences );
57 self::rcPreferences( $user, $context, $defaultPreferences );
58 self::watchlistPreferences( $user, $context, $defaultPreferences );
59 self::searchPreferences( $user, $context, $defaultPreferences );
60 self::miscPreferences( $user, $context, $defaultPreferences );
61
62 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
63
64 ## Remove preferences that wikis don't want to use
65 global $wgHiddenPrefs;
66 foreach ( $wgHiddenPrefs as $pref ) {
67 if ( isset( $defaultPreferences[$pref] ) ) {
68 unset( $defaultPreferences[$pref] );
69 }
70 }
71
72 ## Prod in defaults from the user
73 foreach ( $defaultPreferences as $name => &$info ) {
74 $prefFromUser = self::getOptionFromUser( $name, $info, $user );
75 $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation
76 $defaultOptions = User::getDefaultOptions();
77 $globalDefault = isset( $defaultOptions[$name] )
78 ? $defaultOptions[$name]
79 : null;
80
81 // If it validates, set it as the default
82 if ( isset( $info['default'] ) ) {
83 // Already set, no problem
84 continue;
85 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
86 $field->validate( $prefFromUser, $user->mOptions ) === true ) {
87 $info['default'] = $prefFromUser;
88 } elseif ( $field->validate( $globalDefault, $user->mOptions ) === true ) {
89 $info['default'] = $globalDefault;
90 } else {
91 throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
92 }
93 }
94
95 self::$defaultPreferences = $defaultPreferences;
96
97 return $defaultPreferences;
98 }
99
100 /**
101 * Pull option from a user account. Handles stuff like array-type preferences.
102 *
103 * @param $name
104 * @param $info
105 * @param $user User
106 * @return array|String
107 */
108 static function getOptionFromUser( $name, $info, $user ) {
109 $val = $user->getOption( $name );
110
111 // Handling for array-type preferences
112 if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
113 ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
114 $options = HTMLFormField::flattenOptions( $info['options'] );
115 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
116 $val = array();
117
118 foreach ( $options as $value ) {
119 if ( $user->getOption( "$prefix$value" ) ) {
120 $val[] = $value;
121 }
122 }
123 }
124
125 return $val;
126 }
127
128 /**
129 * @param $user User
130 * @param $context IContextSource
131 * @param $defaultPreferences
132 * @return void
133 */
134 static function profilePreferences( $user, IContextSource $context, &$defaultPreferences ) {
135 global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
136 $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
137 $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
138 $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress;
139
140 ## User info #####################################
141 // Information panel
142 $defaultPreferences['username'] = array(
143 'type' => 'info',
144 'label-message' => 'username',
145 'default' => $user->getName(),
146 'section' => 'personal/info',
147 );
148
149 $defaultPreferences['userid'] = array(
150 'type' => 'info',
151 'label-message' => 'uid',
152 'default' => $user->getId(),
153 'section' => 'personal/info',
154 );
155
156 # Get groups to which the user belongs
157 $userEffectiveGroups = $user->getEffectiveGroups();
158 $userGroups = $userMembers = array();
159 foreach ( $userEffectiveGroups as $ueg ) {
160 if ( $ueg == '*' ) {
161 // Skip the default * group, seems useless here
162 continue;
163 }
164 $groupName = User::getGroupName( $ueg );
165 $userGroups[] = User::makeGroupLinkHTML( $ueg, $groupName );
166
167 $memberName = User::getGroupMember( $ueg, $user->getName() );
168 $userMembers[] = User::makeGroupLinkHTML( $ueg, $memberName );
169 }
170 asort( $userGroups );
171 asort( $userMembers );
172
173 $lang = $context->getLanguage();
174
175 $defaultPreferences['usergroups'] = array(
176 'type' => 'info',
177 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
178 count( $userGroups ) )->parse(),
179 'default' => $context->msg( 'prefs-memberingroups-type',
180 $lang->commaList( $userGroups ),
181 $lang->commaList( $userMembers )
182 )->plain(),
183 'raw' => true,
184 'section' => 'personal/info',
185 );
186
187 $defaultPreferences['editcount'] = array(
188 'type' => 'info',
189 'label-message' => 'prefs-edits',
190 'default' => $lang->formatNum( $user->getEditCount() ),
191 'section' => 'personal/info',
192 );
193
194 if ( $user->getRegistration() ) {
195 $displayUser = $context->getUser();
196 $userRegistration = $user->getRegistration();
197 $defaultPreferences['registrationdate'] = array(
198 'type' => 'info',
199 'label-message' => 'prefs-registration',
200 'default' => $context->msg(
201 'prefs-registration-date-time',
202 $lang->userTimeAndDate( $userRegistration, $displayUser ),
203 $lang->userDate( $userRegistration, $displayUser ),
204 $lang->userTime( $userRegistration, $displayUser )
205 )->parse(),
206 'section' => 'personal/info',
207 );
208 }
209
210 // Actually changeable stuff
211 $defaultPreferences['realname'] = array(
212 'type' => $wgAuth->allowPropChange( 'realname' ) ? 'text' : 'info',
213 'default' => $user->getRealName(),
214 'section' => 'personal/info',
215 'label-message' => 'yourrealname',
216 'help-message' => 'prefs-help-realname',
217 );
218
219 $defaultPreferences['gender'] = array(
220 'type' => 'select',
221 'section' => 'personal/info',
222 'options' => array(
223 $context->msg( 'gender-male' )->text() => 'male',
224 $context->msg( 'gender-female' )->text() => 'female',
225 $context->msg( 'gender-unknown' )->text() => 'unknown',
226 ),
227 'label-message' => 'yourgender',
228 'help-message' => 'prefs-help-gender',
229 );
230
231 if ( $wgAuth->allowPasswordChange() ) {
232 $link = Linker::link( SpecialPage::getTitleFor( 'ChangePassword' ),
233 $context->msg( 'prefs-resetpass' )->escaped(), array(),
234 array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
235
236 $defaultPreferences['password'] = array(
237 'type' => 'info',
238 'raw' => true,
239 'default' => $link,
240 'label-message' => 'yourpassword',
241 'section' => 'personal/info',
242 );
243 }
244 if ( $wgCookieExpiration > 0 ) {
245 $defaultPreferences['rememberpassword'] = array(
246 'type' => 'toggle',
247 'label' => $context->msg( 'tog-rememberpassword' )->numParams(
248 ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
249 'section' => 'personal/info',
250 );
251 }
252
253 // Language
254 $languages = Language::getLanguageNames( false );
255 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
256 $languages[$wgLanguageCode] = $wgLanguageCode;
257 }
258 ksort( $languages );
259
260 $options = array();
261 foreach ( $languages as $code => $name ) {
262 $display = wfBCP47( $code ) . ' - ' . $name;
263 $options[$display] = $code;
264 }
265 $defaultPreferences['language'] = array(
266 'type' => 'select',
267 'section' => 'personal/i18n',
268 'options' => $options,
269 'label-message' => 'yourlanguage',
270 );
271
272 /* see if there are multiple language variants to choose from*/
273 $variantArray = array();
274 if ( !$wgDisableLangConversion ) {
275 $variants = $wgContLang->getVariants();
276
277 foreach ( $variants as $v ) {
278 $v = str_replace( '_', '-', strtolower( $v ) );
279 $variantArray[$v] = $wgContLang->getVariantname( $v, false );
280 }
281
282 $options = array();
283 foreach ( $variantArray as $code => $name ) {
284 $display = wfBCP47( $code ) . ' - ' . $name;
285 $options[$display] = $code;
286 }
287
288 if ( count( $variantArray ) > 1 ) {
289 $defaultPreferences['variant'] = array(
290 'label-message' => 'yourvariant',
291 'type' => 'select',
292 'options' => $options,
293 'section' => 'personal/i18n',
294 'help-message' => 'prefs-help-variant',
295 );
296 }
297 }
298
299 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
300 $defaultPreferences['noconvertlink'] =
301 array(
302 'type' => 'toggle',
303 'section' => 'personal/i18n',
304 'label-message' => 'tog-noconvertlink',
305 );
306 }
307
308 // show a preview of the old signature first
309 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions::newFromContext( $context ) );
310 $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
311 $defaultPreferences['oldsig'] = array(
312 'type' => 'info',
313 'raw' => true,
314 'label-message' => 'tog-oldsig',
315 'default' => $oldsigHTML,
316 'section' => 'personal/signature',
317 );
318 $defaultPreferences['nickname'] = array(
319 'type' => $wgAuth->allowPropChange( 'nickname' ) ? 'text' : 'info',
320 'maxlength' => $wgMaxSigChars,
321 'label-message' => 'yournick',
322 'validation-callback' => array( 'Preferences', 'validateSignature' ),
323 'section' => 'personal/signature',
324 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
325 );
326 $defaultPreferences['fancysig'] = array(
327 'type' => 'toggle',
328 'label-message' => 'tog-fancysig',
329 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
330 'section' => 'personal/signature'
331 );
332
333 ## Email stuff
334
335 if ( $wgEnableEmail ) {
336 $helpMessages[] = $wgEmailConfirmToEdit
337 ? 'prefs-help-email-required'
338 : 'prefs-help-email' ;
339
340 if( $wgEnableUserEmail ) {
341 // additional messages when users can send email to each other
342 $helpMessages[] = 'prefs-help-email-others';
343 }
344
345 $link = Linker::link(
346 SpecialPage::getTitleFor( 'ChangeEmail' ),
347 $context->msg( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' )->escaped(),
348 array(),
349 array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
350
351 $emailAddress = $user->getEmail() ? htmlspecialchars( $user->getEmail() ) : '';
352 if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
353 $emailAddress .= $emailAddress == '' ? $link : " ($link)";
354 }
355
356 $defaultPreferences['emailaddress'] = array(
357 'type' => 'info',
358 'raw' => true,
359 'default' => $emailAddress,
360 'label-message' => 'youremail',
361 'section' => 'personal/email',
362 );
363
364 $disableEmailPrefs = false;
365
366 if ( $wgEmailAuthentication ) {
367 if ( $user->getEmail() ) {
368 if ( $user->getEmailAuthenticationTimestamp() ) {
369 // date and time are separate parameters to facilitate localisation.
370 // $time is kept for backward compat reasons.
371 // 'emailauthenticated' is also used in SpecialConfirmemail.php
372 $displayUser = $context->getUser();
373 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
374 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
375 $d = $lang->userDate( $emailTimestamp, $displayUser );
376 $t = $lang->userTime( $emailTimestamp, $displayUser );
377 $emailauthenticated = $context->msg( 'emailauthenticated',
378 $time, $d, $t )->parse() . '<br />';
379 $disableEmailPrefs = false;
380 } else {
381 $disableEmailPrefs = true;
382 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
383 Linker::linkKnown(
384 SpecialPage::getTitleFor( 'Confirmemail' ),
385 $context->msg( 'emailconfirmlink' )->escaped()
386 ) . '<br />';
387 }
388 } else {
389 $disableEmailPrefs = true;
390 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
391 }
392
393 $defaultPreferences['emailauthentication'] = array(
394 'type' => 'info',
395 'raw' => true,
396 'section' => 'personal/email',
397 'label-message' => 'prefs-emailconfirm-label',
398 'default' => $emailauthenticated,
399 );
400
401 }
402
403 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
404 $defaultPreferences['disablemail'] = array(
405 'type' => 'toggle',
406 'invert' => true,
407 'section' => 'personal/email',
408 'label-message' => 'allowemail',
409 'disabled' => $disableEmailPrefs,
410 );
411 $defaultPreferences['ccmeonemails'] = array(
412 'type' => 'toggle',
413 'section' => 'personal/email',
414 'label-message' => 'tog-ccmeonemails',
415 'disabled' => $disableEmailPrefs,
416 );
417 }
418
419 if ( $wgEnotifWatchlist ) {
420 $defaultPreferences['enotifwatchlistpages'] = array(
421 'type' => 'toggle',
422 'section' => 'personal/email',
423 'label-message' => 'tog-enotifwatchlistpages',
424 'disabled' => $disableEmailPrefs,
425 );
426 }
427 if ( $wgEnotifUserTalk ) {
428 $defaultPreferences['enotifusertalkpages'] = array(
429 'type' => 'toggle',
430 'section' => 'personal/email',
431 'label-message' => 'tog-enotifusertalkpages',
432 'disabled' => $disableEmailPrefs,
433 );
434 }
435 if ( $wgEnotifUserTalk || $wgEnotifWatchlist ) {
436 $defaultPreferences['enotifminoredits'] = array(
437 'type' => 'toggle',
438 'section' => 'personal/email',
439 'label-message' => 'tog-enotifminoredits',
440 'disabled' => $disableEmailPrefs,
441 );
442
443 if ( $wgEnotifRevealEditorAddress ) {
444 $defaultPreferences['enotifrevealaddr'] = array(
445 'type' => 'toggle',
446 'section' => 'personal/email',
447 'label-message' => 'tog-enotifrevealaddr',
448 'disabled' => $disableEmailPrefs,
449 );
450 }
451 }
452 }
453 }
454
455 /**
456 * @param $user User
457 * @param $context IContextSource
458 * @param $defaultPreferences
459 * @return void
460 */
461 static function skinPreferences( $user, IContextSource $context, &$defaultPreferences ) {
462 ## Skin #####################################
463 global $wgAllowUserCss, $wgAllowUserJs;
464
465 $defaultPreferences['skin'] = array(
466 'type' => 'radio',
467 'options' => self::generateSkinOptions( $user, $context ),
468 'label' => '&#160;',
469 'section' => 'rendering/skin',
470 );
471
472 # Create links to user CSS/JS pages for all skins
473 # This code is basically copied from generateSkinOptions(). It'd
474 # be nice to somehow merge this back in there to avoid redundancy.
475 if ( $wgAllowUserCss || $wgAllowUserJs ) {
476 $linkTools = array();
477
478 if ( $wgAllowUserCss ) {
479 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.css' );
480 $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
481 }
482
483 if ( $wgAllowUserJs ) {
484 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.js' );
485 $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
486 }
487
488 $defaultPreferences['commoncssjs'] = array(
489 'type' => 'info',
490 'raw' => true,
491 'default' => $context->getLanguage()->pipeList( $linkTools ),
492 'label-message' => 'prefs-common-css-js',
493 'section' => 'rendering/skin',
494 );
495 }
496
497 $selectedSkin = $user->getOption( 'skin' );
498 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
499 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
500
501 $defaultPreferences['quickbar'] = array(
502 'type' => 'radio',
503 'options' => $settings,
504 'section' => 'rendering/skin',
505 'label-message' => 'qbsettings',
506 );
507 }
508 }
509
510 /**
511 * @param $user User
512 * @param $context IContextSource
513 * @param $defaultPreferences Array
514 */
515 static function filesPreferences( $user, IContextSource $context, &$defaultPreferences ) {
516 ## Files #####################################
517 $defaultPreferences['imagesize'] = array(
518 'type' => 'select',
519 'options' => self::getImageSizes( $context ),
520 'label-message' => 'imagemaxsize',
521 'section' => 'rendering/files',
522 );
523 $defaultPreferences['thumbsize'] = array(
524 'type' => 'select',
525 'options' => self::getThumbSizes( $context ),
526 'label-message' => 'thumbsize',
527 'section' => 'rendering/files',
528 );
529 }
530
531 /**
532 * @param $user User
533 * @param $context IContextSource
534 * @param $defaultPreferences
535 * @return void
536 */
537 static function datetimePreferences( $user, IContextSource $context, &$defaultPreferences ) {
538 ## Date and time #####################################
539 $dateOptions = self::getDateOptions( $context );
540 if ( $dateOptions ) {
541 $defaultPreferences['date'] = array(
542 'type' => 'radio',
543 'options' => $dateOptions,
544 'label' => '&#160;',
545 'section' => 'datetime/dateformat',
546 );
547 }
548
549 // Info
550 $now = wfTimestampNow();
551 $lang = $context->getLanguage();
552 $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
553 $lang->time( $now, true ) );
554 $nowserver = $lang->time( $now, false ) .
555 Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
556
557 $defaultPreferences['nowserver'] = array(
558 'type' => 'info',
559 'raw' => 1,
560 'label-message' => 'servertime',
561 'default' => $nowserver,
562 'section' => 'datetime/timeoffset',
563 );
564
565 $defaultPreferences['nowlocal'] = array(
566 'type' => 'info',
567 'raw' => 1,
568 'label-message' => 'localtime',
569 'default' => $nowlocal,
570 'section' => 'datetime/timeoffset',
571 );
572
573 // Grab existing pref.
574 $tzOffset = $user->getOption( 'timecorrection' );
575 $tz = explode( '|', $tzOffset, 3 );
576
577 $tzOptions = self::getTimezoneOptions( $context );
578
579 $tzSetting = $tzOffset;
580 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
581 $minDiff = $tz[1];
582 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 );
583 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
584 !in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) ) )
585 {
586 # Timezone offset can vary with DST
587 $userTZ = timezone_open( $tz[2] );
588 if ( $userTZ !== false ) {
589 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
590 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
591 }
592 }
593
594 $defaultPreferences['timecorrection'] = array(
595 'class' => 'HTMLSelectOrOtherField',
596 'label-message' => 'timezonelegend',
597 'options' => $tzOptions,
598 'default' => $tzSetting,
599 'size' => 20,
600 'section' => 'datetime/timeoffset',
601 );
602 }
603
604 /**
605 * @param $user User
606 * @param $context IContextSource
607 * @param $defaultPreferences Array
608 */
609 static function renderingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
610 ## Page Rendering ##############################
611 global $wgAllowUserCssPrefs;
612 if ( $wgAllowUserCssPrefs ) {
613 $defaultPreferences['underline'] = array(
614 'type' => 'select',
615 'options' => array(
616 $context->msg( 'underline-never' )->text() => 0,
617 $context->msg( 'underline-always' )->text() => 1,
618 $context->msg( 'underline-default' )->text() => 2,
619 ),
620 'label-message' => 'tog-underline',
621 'section' => 'rendering/advancedrendering',
622 );
623 }
624
625 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
626 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
627 foreach ( $stubThresholdValues as $value ) {
628 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
629 }
630
631 $defaultPreferences['stubthreshold'] = array(
632 'type' => 'selectorother',
633 'section' => 'rendering/advancedrendering',
634 'options' => $stubThresholdOptions,
635 'size' => 20,
636 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
637 );
638
639 if ( $wgAllowUserCssPrefs ) {
640 $defaultPreferences['highlightbroken'] = array(
641 'type' => 'toggle',
642 'section' => 'rendering/advancedrendering',
643 'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML
644 );
645 $defaultPreferences['showtoc'] = array(
646 'type' => 'toggle',
647 'section' => 'rendering/advancedrendering',
648 'label-message' => 'tog-showtoc',
649 );
650 }
651 $defaultPreferences['nocache'] = array(
652 'type' => 'toggle',
653 'label-message' => 'tog-nocache',
654 'section' => 'rendering/advancedrendering',
655 );
656 $defaultPreferences['showhiddencats'] = array(
657 'type' => 'toggle',
658 'section' => 'rendering/advancedrendering',
659 'label-message' => 'tog-showhiddencats'
660 );
661 $defaultPreferences['showjumplinks'] = array(
662 'type' => 'toggle',
663 'section' => 'rendering/advancedrendering',
664 'label-message' => 'tog-showjumplinks',
665 );
666
667 if ( $wgAllowUserCssPrefs ) {
668 $defaultPreferences['justify'] = array(
669 'type' => 'toggle',
670 'section' => 'rendering/advancedrendering',
671 'label-message' => 'tog-justify',
672 );
673 }
674
675 $defaultPreferences['numberheadings'] = array(
676 'type' => 'toggle',
677 'section' => 'rendering/advancedrendering',
678 'label-message' => 'tog-numberheadings',
679 );
680 }
681
682 /**
683 * @param $user User
684 * @param $context IContextSource
685 * @param $defaultPreferences Array
686 */
687 static function editingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
688 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
689
690 ## Editing #####################################
691
692 $defaultPreferences['rows'] = array(
693 'type' => 'int',
694 'label-message' => 'rows',
695 'section' => 'editing/textboxsize',
696 'min' => 4,
697 'max' => 1000,
698 );
699
700 if ( $wgAllowUserCssPrefs ) {
701 $defaultPreferences['editfont'] = array(
702 'type' => 'select',
703 'section' => 'editing/advancedediting',
704 'label-message' => 'editfont-style',
705 'options' => array(
706 $context->msg( 'editfont-default' )->text() => 'default',
707 $context->msg( 'editfont-monospace' )->text() => 'monospace',
708 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
709 $context->msg( 'editfont-serif' )->text() => 'serif',
710 )
711 );
712 }
713 $defaultPreferences['previewontop'] = array(
714 'type' => 'toggle',
715 'section' => 'editing/advancedediting',
716 'label-message' => 'tog-previewontop',
717 );
718 $defaultPreferences['previewonfirst'] = array(
719 'type' => 'toggle',
720 'section' => 'editing/advancedediting',
721 'label-message' => 'tog-previewonfirst',
722 );
723
724 if ( $wgAllowUserCssPrefs ) {
725 $defaultPreferences['editsection'] = array(
726 'type' => 'toggle',
727 'section' => 'editing/advancedediting',
728 'label-message' => 'tog-editsection',
729 );
730 }
731 $defaultPreferences['editsectiononrightclick'] = array(
732 'type' => 'toggle',
733 'section' => 'editing/advancedediting',
734 'label-message' => 'tog-editsectiononrightclick',
735 );
736 $defaultPreferences['editondblclick'] = array(
737 'type' => 'toggle',
738 'section' => 'editing/advancedediting',
739 'label-message' => 'tog-editondblclick',
740 );
741 $defaultPreferences['showtoolbar'] = array(
742 'type' => 'toggle',
743 'section' => 'editing/advancedediting',
744 'label-message' => 'tog-showtoolbar',
745 );
746
747 if ( $user->isAllowed( 'minoredit' ) ) {
748 $defaultPreferences['minordefault'] = array(
749 'type' => 'toggle',
750 'section' => 'editing/advancedediting',
751 'label-message' => 'tog-minordefault',
752 );
753 }
754
755 if ( $wgUseExternalEditor ) {
756 $defaultPreferences['externaleditor'] = array(
757 'type' => 'toggle',
758 'section' => 'editing/advancedediting',
759 'label-message' => 'tog-externaleditor',
760 );
761 $defaultPreferences['externaldiff'] = array(
762 'type' => 'toggle',
763 'section' => 'editing/advancedediting',
764 'label-message' => 'tog-externaldiff',
765 );
766 }
767
768 $defaultPreferences['forceeditsummary'] = array(
769 'type' => 'toggle',
770 'section' => 'editing/advancedediting',
771 'label-message' => 'tog-forceeditsummary',
772 );
773
774
775 $defaultPreferences['uselivepreview'] = array(
776 'type' => 'toggle',
777 'section' => 'editing/advancedediting',
778 'label-message' => 'tog-uselivepreview',
779 );
780 }
781
782 /**
783 * @param $user User
784 * @param $context IContextSource
785 * @param $defaultPreferences Array
786 */
787 static function rcPreferences( $user, IContextSource $context, &$defaultPreferences ) {
788 global $wgRCMaxAge, $wgRCShowWatchingUsers;
789
790 ## RecentChanges #####################################
791 $defaultPreferences['rcdays'] = array(
792 'type' => 'float',
793 'label-message' => 'recentchangesdays',
794 'section' => 'rc/displayrc',
795 'min' => 1,
796 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
797 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
798 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
799 );
800 $defaultPreferences['rclimit'] = array(
801 'type' => 'int',
802 'label-message' => 'recentchangescount',
803 'help-message' => 'prefs-help-recentchangescount',
804 'section' => 'rc/displayrc',
805 );
806 $defaultPreferences['usenewrc'] = array(
807 'type' => 'toggle',
808 'label-message' => 'tog-usenewrc',
809 'section' => 'rc/advancedrc',
810 );
811 $defaultPreferences['hideminor'] = array(
812 'type' => 'toggle',
813 'label-message' => 'tog-hideminor',
814 'section' => 'rc/advancedrc',
815 );
816
817 if ( $user->useRCPatrol() ) {
818 $defaultPreferences['hidepatrolled'] = array(
819 'type' => 'toggle',
820 'section' => 'rc/advancedrc',
821 'label-message' => 'tog-hidepatrolled',
822 );
823 $defaultPreferences['newpageshidepatrolled'] = array(
824 'type' => 'toggle',
825 'section' => 'rc/advancedrc',
826 'label-message' => 'tog-newpageshidepatrolled',
827 );
828 }
829
830 if ( $wgRCShowWatchingUsers ) {
831 $defaultPreferences['shownumberswatching'] = array(
832 'type' => 'toggle',
833 'section' => 'rc/advancedrc',
834 'label-message' => 'tog-shownumberswatching',
835 );
836 }
837 }
838
839 /**
840 * @param $user User
841 * @param $context IContextSource
842 * @param $defaultPreferences
843 */
844 static function watchlistPreferences( $user, IContextSource $context, &$defaultPreferences ) {
845 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
846
847 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
848
849 ## Watchlist #####################################
850 $defaultPreferences['watchlistdays'] = array(
851 'type' => 'float',
852 'min' => 0,
853 'max' => $watchlistdaysMax,
854 'section' => 'watchlist/displaywatchlist',
855 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
856 $watchlistdaysMax )->text(),
857 'label-message' => 'prefs-watchlist-days',
858 );
859 $defaultPreferences['wllimit'] = array(
860 'type' => 'int',
861 'min' => 0,
862 'max' => 1000,
863 'label-message' => 'prefs-watchlist-edits',
864 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
865 'section' => 'watchlist/displaywatchlist',
866 );
867 $defaultPreferences['extendwatchlist'] = array(
868 'type' => 'toggle',
869 'section' => 'watchlist/advancedwatchlist',
870 'label-message' => 'tog-extendwatchlist',
871 );
872 $defaultPreferences['watchlisthideminor'] = array(
873 'type' => 'toggle',
874 'section' => 'watchlist/advancedwatchlist',
875 'label-message' => 'tog-watchlisthideminor',
876 );
877 $defaultPreferences['watchlisthidebots'] = array(
878 'type' => 'toggle',
879 'section' => 'watchlist/advancedwatchlist',
880 'label-message' => 'tog-watchlisthidebots',
881 );
882 $defaultPreferences['watchlisthideown'] = array(
883 'type' => 'toggle',
884 'section' => 'watchlist/advancedwatchlist',
885 'label-message' => 'tog-watchlisthideown',
886 );
887 $defaultPreferences['watchlisthideanons'] = array(
888 'type' => 'toggle',
889 'section' => 'watchlist/advancedwatchlist',
890 'label-message' => 'tog-watchlisthideanons',
891 );
892 $defaultPreferences['watchlisthideliu'] = array(
893 'type' => 'toggle',
894 'section' => 'watchlist/advancedwatchlist',
895 'label-message' => 'tog-watchlisthideliu',
896 );
897
898 if ( $wgUseRCPatrol ) {
899 $defaultPreferences['watchlisthidepatrolled'] = array(
900 'type' => 'toggle',
901 'section' => 'watchlist/advancedwatchlist',
902 'label-message' => 'tog-watchlisthidepatrolled',
903 );
904 }
905
906 if ( $wgEnableAPI ) {
907 # Some random gibberish as a proposed default
908 $hash = sha1( mt_rand() . microtime( true ) );
909
910 $defaultPreferences['watchlisttoken'] = array(
911 'type' => 'text',
912 'section' => 'watchlist/advancedwatchlist',
913 'label-message' => 'prefs-watchlist-token',
914 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
915 );
916 }
917
918 $watchTypes = array(
919 'edit' => 'watchdefault',
920 'move' => 'watchmoves',
921 'delete' => 'watchdeletion'
922 );
923
924 // Kinda hacky
925 if ( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
926 $watchTypes['read'] = 'watchcreations';
927 }
928
929 foreach ( $watchTypes as $action => $pref ) {
930 if ( $user->isAllowed( $action ) ) {
931 $defaultPreferences[$pref] = array(
932 'type' => 'toggle',
933 'section' => 'watchlist/advancedwatchlist',
934 'label-message' => "tog-$pref",
935 );
936 }
937 }
938 }
939
940 /**
941 * @param $user User
942 * @param $context IContextSource
943 * @param $defaultPreferences Array
944 */
945 static function searchPreferences( $user, IContextSource $context, &$defaultPreferences ) {
946 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
947
948 ## Search #####################################
949 $defaultPreferences['searchlimit'] = array(
950 'type' => 'int',
951 'label-message' => 'resultsperpage',
952 'section' => 'searchoptions/displaysearchoptions',
953 'min' => 0,
954 );
955
956 if ( $wgEnableMWSuggest ) {
957 $defaultPreferences['disablesuggest'] = array(
958 'type' => 'toggle',
959 'label-message' => 'mwsuggest-disable',
960 'section' => 'searchoptions/displaysearchoptions',
961 );
962 }
963
964 if ( $wgVectorUseSimpleSearch ) {
965 $defaultPreferences['vector-simplesearch'] = array(
966 'type' => 'toggle',
967 'label-message' => 'vector-simplesearch-preference',
968 'section' => 'searchoptions/displaysearchoptions'
969 );
970 }
971
972 $defaultPreferences['searcheverything'] = array(
973 'type' => 'toggle',
974 'label-message' => 'searcheverything-enable',
975 'section' => 'searchoptions/advancedsearchoptions',
976 );
977
978 $nsOptions = array();
979
980 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
981 if ( $ns < 0 ) {
982 continue;
983 }
984
985 $displayNs = str_replace( '_', ' ', $name );
986
987 if ( !$displayNs ) {
988 $displayNs = $context->msg( 'blanknamespace' )->text();
989 }
990
991 $displayNs = htmlspecialchars( $displayNs );
992 $nsOptions[$displayNs] = $ns;
993 }
994
995 $defaultPreferences['searchnamespaces'] = array(
996 'type' => 'multiselect',
997 'label-message' => 'defaultns',
998 'options' => $nsOptions,
999 'section' => 'searchoptions/advancedsearchoptions',
1000 'prefix' => 'searchNs',
1001 );
1002 }
1003
1004 /**
1005 * @param $user User
1006 * @param $context IContextSource
1007 * @param $defaultPreferences Array
1008 */
1009 static function miscPreferences( $user, IContextSource $context, &$defaultPreferences ) {
1010 global $wgContLang;
1011
1012 ## Misc #####################################
1013 $defaultPreferences['diffonly'] = array(
1014 'type' => 'toggle',
1015 'section' => 'misc/diffs',
1016 'label-message' => 'tog-diffonly',
1017 );
1018 $defaultPreferences['norollbackdiff'] = array(
1019 'type' => 'toggle',
1020 'section' => 'misc/diffs',
1021 'label-message' => 'tog-norollbackdiff',
1022 );
1023
1024 // Stuff from Language::getExtraUserToggles()
1025 $toggles = $wgContLang->getExtraUserToggles();
1026
1027 foreach ( $toggles as $toggle ) {
1028 $defaultPreferences[$toggle] = array(
1029 'type' => 'toggle',
1030 'section' => 'personal/i18n',
1031 'label-message' => "tog-$toggle",
1032 );
1033 }
1034 }
1035
1036 /**
1037 * @param $user User The User object
1038 * @param $context IContextSource
1039 * @return Array: text/links to display as key; $skinkey as value
1040 */
1041 static function generateSkinOptions( $user, IContextSource $context ) {
1042 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1043 $ret = array();
1044
1045 $mptitle = Title::newMainPage();
1046 $previewtext = $context->msg( 'skin-preview' )->text();
1047
1048 # Only show members of Skin::getSkinNames() rather than
1049 # $skinNames (skins is all skin names from Language.php)
1050 $validSkinNames = Skin::getUsableSkins();
1051
1052 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1053 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1054 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1055 $msg = $context->msg( "skinname-{$skinkey}" );
1056 if ( $msg->exists() ) {
1057 $skinname = htmlspecialchars( $msg->text() );
1058 }
1059 }
1060 asort( $validSkinNames );
1061
1062 foreach ( $validSkinNames as $skinkey => $sn ) {
1063 $linkTools = array();
1064
1065 # Mark the default skin
1066 if ( $skinkey == $wgDefaultSkin ) {
1067 $linkTools[] = $context->msg( 'default' )->escaped();
1068 }
1069
1070 # Create preview link
1071 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1072 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1073
1074 # Create links to user CSS/JS pages
1075 if ( $wgAllowUserCss ) {
1076 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
1077 $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1078 }
1079
1080 if ( $wgAllowUserJs ) {
1081 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
1082 $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1083 }
1084
1085 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1086 $ret[$display] = $skinkey;
1087 }
1088
1089 return $ret;
1090 }
1091
1092 /**
1093 * @param $context IContextSource
1094 * @return array
1095 */
1096 static function getDateOptions( IContextSource $context ) {
1097 $lang = $context->getLanguage();
1098 $dateopts = $lang->getDatePreferences();
1099
1100 $ret = array();
1101
1102 if ( $dateopts ) {
1103 if ( !in_array( 'default', $dateopts ) ) {
1104 $dateopts[] = 'default'; // Make sure default is always valid
1105 // Bug 19237
1106 }
1107
1108 // KLUGE: site default might not be valid for user language
1109 global $wgDefaultUserOptions;
1110 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1111 $wgDefaultUserOptions['date'] = 'default';
1112 }
1113
1114 $epoch = wfTimestampNow();
1115 foreach ( $dateopts as $key ) {
1116 if ( $key == 'default' ) {
1117 $formatted = $context->msg( 'datedefault' )->escaped();
1118 } else {
1119 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1120 }
1121 $ret[$formatted] = $key;
1122 }
1123 }
1124 return $ret;
1125 }
1126
1127 /**
1128 * @param $context IContextSource
1129 * @return array
1130 */
1131 static function getImageSizes( IContextSource $context ) {
1132 global $wgImageLimits;
1133
1134 $ret = array();
1135 $pixels = $context->msg( 'unit-pixel' )->text();
1136
1137 foreach ( $wgImageLimits as $index => $limits ) {
1138 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1139 $ret[$display] = $index;
1140 }
1141
1142 return $ret;
1143 }
1144
1145 /**
1146 * @param $context IContextSource
1147 * @return array
1148 */
1149 static function getThumbSizes( IContextSource $context ) {
1150 global $wgThumbLimits;
1151
1152 $ret = array();
1153 $pixels = $context->msg( 'unit-pixel' )->text();
1154
1155 foreach ( $wgThumbLimits as $index => $size ) {
1156 $display = $size . $pixels;
1157 $ret[$display] = $index;
1158 }
1159
1160 return $ret;
1161 }
1162
1163 /**
1164 * @param $signature string
1165 * @param $alldata array
1166 * @param $form HTMLForm
1167 * @return bool|string
1168 */
1169 static function validateSignature( $signature, $alldata, $form ) {
1170 global $wgParser, $wgMaxSigChars;
1171 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1172 return Xml::element( 'span', array( 'class' => 'error' ),
1173 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1174 } elseif ( isset( $alldata['fancysig'] ) &&
1175 $alldata['fancysig'] &&
1176 false === $wgParser->validateSig( $signature ) ) {
1177 return Xml::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1178 } else {
1179 return true;
1180 }
1181 }
1182
1183 /**
1184 * @param $signature string
1185 * @param $alldata array
1186 * @param $form HTMLForm
1187 * @return string
1188 */
1189 static function cleanSignature( $signature, $alldata, $form ) {
1190 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1191 global $wgParser;
1192 $signature = $wgParser->cleanSig( $signature );
1193 } else {
1194 // When no fancy sig used, make sure ~{3,5} get removed.
1195 $signature = Parser::cleanSigInSig( $signature );
1196 }
1197
1198 return $signature;
1199 }
1200
1201 /**
1202 * @param $user User
1203 * @param $context IContextSource
1204 * @param $formClass string
1205 * @param $remove Array: array of items to remove
1206 * @return HtmlForm
1207 */
1208 static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm', array $remove = array() ) {
1209 $formDescriptor = Preferences::getPreferences( $user, $context );
1210 if ( count( $remove ) ) {
1211 $removeKeys = array_flip( $remove );
1212 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1213 }
1214 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1215
1216 $htmlForm->setModifiedUser( $user );
1217 $htmlForm->setId( 'mw-prefs-form' );
1218 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1219 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1220 $htmlForm->setSubmitTooltip( 'preferences-save' );
1221 $htmlForm->setSubmitID( 'prefsubmit' );
1222 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1223
1224 return $htmlForm;
1225 }
1226
1227 /**
1228 * @return array
1229 */
1230 static function getTimezoneOptions( IContextSource $context ) {
1231 $opt = array();
1232
1233 global $wgLocalTZoffset, $wgLocaltimezone;
1234 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1235 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1236 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1237 } else {
1238 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
1239 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1240 }
1241 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1242 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1243 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1244
1245 if ( function_exists( 'timezone_identifiers_list' ) ) {
1246 # Read timezone list
1247 $tzs = timezone_identifiers_list();
1248 sort( $tzs );
1249
1250 $tzRegions = array();
1251 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1252 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1253 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1254 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1255 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1256 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1257 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1258 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1259 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1260 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1261 asort( $tzRegions );
1262
1263 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1264 $opt = array_merge( $opt, $prefill );
1265
1266 $now = date_create( 'now' );
1267
1268 foreach ( $tzs as $tz ) {
1269 $z = explode( '/', $tz, 2 );
1270
1271 # timezone_identifiers_list() returns a number of
1272 # backwards-compatibility entries. This filters them out of the
1273 # list presented to the user.
1274 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) ) {
1275 continue;
1276 }
1277
1278 # Localize region
1279 $z[0] = $tzRegions[$z[0]];
1280
1281 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1282
1283 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1284 $value = "ZoneInfo|$minDiff|$tz";
1285
1286 $opt[$z[0]][$display] = $value;
1287 }
1288 }
1289 return $opt;
1290 }
1291
1292 /**
1293 * @param $value
1294 * @param $alldata
1295 * @return int
1296 */
1297 static function filterIntval( $value, $alldata ){
1298 return intval( $value );
1299 }
1300
1301 /**
1302 * @param $tz
1303 * @param $alldata
1304 * @return string
1305 */
1306 static function filterTimezoneInput( $tz, $alldata ) {
1307 $data = explode( '|', $tz, 3 );
1308 switch ( $data[0] ) {
1309 case 'ZoneInfo':
1310 case 'System':
1311 return $tz;
1312 default:
1313 $data = explode( ':', $tz, 2 );
1314 if ( count( $data ) == 2 ) {
1315 $data[0] = intval( $data[0] );
1316 $data[1] = intval( $data[1] );
1317 $minDiff = abs( $data[0] ) * 60 + $data[1];
1318 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1319 } else {
1320 $minDiff = intval( $data[0] ) * 60;
1321 }
1322
1323 # Max is +14:00 and min is -12:00, see:
1324 # http://en.wikipedia.org/wiki/Timezone
1325 $minDiff = min( $minDiff, 840 ); # 14:00
1326 $minDiff = max( $minDiff, - 720 ); # -12:00
1327 return 'Offset|' . $minDiff;
1328 }
1329 }
1330
1331 /**
1332 * @param $formData
1333 * @param $form HTMLForm
1334 * @param $entryPoint string
1335 * @return bool|Status|string
1336 */
1337 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1338 global $wgHiddenPrefs;
1339
1340 $user = $form->getModifiedUser();
1341 $result = true;
1342
1343 // Filter input
1344 foreach ( array_keys( $formData ) as $name ) {
1345 if ( isset( self::$saveFilters[$name] ) ) {
1346 $formData[$name] =
1347 call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1348 }
1349 }
1350
1351 // Stuff that shouldn't be saved as a preference.
1352 $saveBlacklist = array(
1353 'realname',
1354 'emailaddress',
1355 );
1356
1357 // Fortunately, the realname field is MUCH simpler
1358 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1359 $realName = $formData['realname'];
1360 $user->setRealName( $realName );
1361 }
1362
1363 foreach ( $saveBlacklist as $b ) {
1364 unset( $formData[$b] );
1365 }
1366
1367 # If users have saved a value for a preference which has subsequently been disabled
1368 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1369 # is subsequently re-enabled
1370 # TODO: maintenance script to actually delete these
1371 foreach( $wgHiddenPrefs as $pref ){
1372 # If the user has not set a non-default value here, the default will be returned
1373 # and subsequently discarded
1374 $formData[$pref] = $user->getOption( $pref, null, true );
1375 }
1376
1377 // Keeps old preferences from interfering due to back-compat
1378 // code, etc.
1379 $user->resetOptions();
1380
1381 foreach ( $formData as $key => $value ) {
1382 $user->setOption( $key, $value );
1383 }
1384
1385 $user->saveSettings();
1386
1387 return $result;
1388 }
1389
1390 /**
1391 * @param $formData
1392 * @return Status
1393 */
1394 public static function tryUISubmit( $formData, $form ) {
1395 $res = self::tryFormSubmit( $formData, $form, 'ui' );
1396
1397 if ( $res ) {
1398 $urlOptions = array( 'success' => 1 );
1399
1400 if ( $res === 'eauth' ) {
1401 $urlOptions['eauth'] = 1;
1402 }
1403
1404 $urlOptions += $form->getExtraSuccessRedirectParameters();
1405
1406 $url = $form->getTitle()->getFullURL( $urlOptions );
1407
1408 $form->getContext()->getOutput()->redirect( $url );
1409 }
1410
1411 return Status::newGood();
1412 }
1413
1414 /**
1415 * Try to set a user's email address.
1416 * This does *not* try to validate the address.
1417 * Caller is responsible for checking $wgAuth.
1418 * @param $user User
1419 * @param $newaddr string New email address
1420 * @return Array (true on success or Status on failure, info string)
1421 */
1422 public static function trySetUserEmail( User $user, $newaddr ) {
1423 global $wgEnableEmail, $wgEmailAuthentication;
1424 $info = ''; // none
1425
1426 if ( $wgEnableEmail ) {
1427 $oldaddr = $user->getEmail();
1428 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1429 # The user has supplied a new email address on the login page
1430 # new behaviour: set this new emailaddr from login-page into user database record
1431 $user->setEmail( $newaddr );
1432 if ( $wgEmailAuthentication ) {
1433 # Mail a temporary password to the dirty address.
1434 # User can come back through the confirmation URL to re-enable email.
1435 $type = $oldaddr != '' ? 'changed' : 'set';
1436 $result = $user->sendConfirmationMail( $type );
1437 if ( !$result->isGood() ) {
1438 return array( $result, 'mailerror' );
1439 }
1440 $info = 'eauth';
1441 }
1442 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1443 $user->setEmail( $newaddr );
1444 }
1445 if ( $oldaddr != $newaddr ) {
1446 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1447 }
1448 }
1449
1450 return array( true, $info );
1451 }
1452
1453 /**
1454 * @param $user User
1455 * @return array
1456 */
1457 public static function loadOldSearchNs( $user ) {
1458 $searchableNamespaces = SearchEngine::searchableNamespaces();
1459 // Back compat with old format
1460 $arr = array();
1461
1462 foreach ( $searchableNamespaces as $ns => $name ) {
1463 if ( $user->getOption( 'searchNs' . $ns ) ) {
1464 $arr[] = $ns;
1465 }
1466 }
1467
1468 return $arr;
1469 }
1470 }
1471
1472 /** Some tweaks to allow js prefs to work */
1473 class PreferencesForm extends HTMLForm {
1474 // Override default value from HTMLForm
1475 protected $mSubSectionBeforeFields = false;
1476
1477 private $modifiedUser;
1478
1479 public function setModifiedUser( $user ) {
1480 $this->modifiedUser = $user;
1481 }
1482
1483 public function getModifiedUser() {
1484 if ( $this->modifiedUser === null ) {
1485 return $this->getUser();
1486 } else {
1487 return $this->modifiedUser;
1488 }
1489 }
1490
1491 /**
1492 * Get extra parameters for the query string when redirecting after
1493 * successful save.
1494 *
1495 * @return array()
1496 */
1497 public function getExtraSuccessRedirectParameters() {
1498 return array();
1499 }
1500
1501 /**
1502 * @param $html string
1503 * @return String
1504 */
1505 function wrapForm( $html ) {
1506 $html = Xml::tags( 'div', array( 'id' => 'preferences' ), $html );
1507
1508 return parent::wrapForm( $html );
1509 }
1510
1511 /**
1512 * @return String
1513 */
1514 function getButtons() {
1515 $html = parent::getButtons();
1516
1517 $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1518
1519 $html .= "\n" . Linker::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1520
1521 $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1522
1523 return $html;
1524 }
1525
1526 /**
1527 * @param $data array
1528 * @return array
1529 */
1530 function filterDataForSubmit( $data ) {
1531 // Support for separating MultiSelect preferences into multiple preferences
1532 // Due to lack of array support.
1533 foreach ( $this->mFlatFields as $fieldname => $field ) {
1534 $info = $field->mParams;
1535 if ( $field instanceof HTMLMultiSelectField ) {
1536 $options = HTMLFormField::flattenOptions( $info['options'] );
1537 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
1538
1539 foreach ( $options as $opt ) {
1540 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1541 }
1542
1543 unset( $data[$fieldname] );
1544 }
1545 }
1546
1547 return $data;
1548 }
1549 /**
1550 * Get the whole body of the form.
1551 */
1552 function getBody() {
1553 return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
1554 }
1555
1556 /**
1557 * Get the <legend> for a given section key. Normally this is the
1558 * prefs-$key message but we'll allow extensions to override it.
1559 */
1560 function getLegend( $key ) {
1561 $legend = parent::getLegend( $key );
1562 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );
1563 return $legend;
1564 }
1565 }