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