MediaWiki  master
Preferences.php
Go to the documentation of this file.
1 <?php
24 
50 class Preferences {
52  protected static $defaultPreferences = null;
53 
55  protected static $saveFilters = [
56  'timecorrection' => [ 'Preferences', 'filterTimezoneInput' ],
57  'cols' => [ 'Preferences', 'filterIntval' ],
58  'rows' => [ 'Preferences', 'filterIntval' ],
59  'rclimit' => [ 'Preferences', 'filterIntval' ],
60  'wllimit' => [ 'Preferences', 'filterIntval' ],
61  'searchlimit' => [ 'Preferences', 'filterIntval' ],
62  ];
63 
64  // Stuff that shouldn't be saved as a preference.
65  private static $saveBlacklist = [
66  'realname',
67  'emailaddress',
68  ];
69 
73  static function getSaveBlacklist() {
74  return self::$saveBlacklist;
75  }
76 
84  if ( self::$defaultPreferences ) {
85  return self::$defaultPreferences;
86  }
87 
89 
90  self::profilePreferences( $user, $context, $defaultPreferences );
91  self::skinPreferences( $user, $context, $defaultPreferences );
92  self::datetimePreferences( $user, $context, $defaultPreferences );
93  self::filesPreferences( $user, $context, $defaultPreferences );
94  self::renderingPreferences( $user, $context, $defaultPreferences );
95  self::editingPreferences( $user, $context, $defaultPreferences );
96  self::rcPreferences( $user, $context, $defaultPreferences );
97  self::watchlistPreferences( $user, $context, $defaultPreferences );
98  self::searchPreferences( $user, $context, $defaultPreferences );
99  self::miscPreferences( $user, $context, $defaultPreferences );
100 
101  Hooks::run( 'GetPreferences', [ $user, &$defaultPreferences ] );
102 
103  self::loadPreferenceValues( $user, $context, $defaultPreferences );
104  self::$defaultPreferences = $defaultPreferences;
105  return $defaultPreferences;
106  }
107 
117  # # Remove preferences that wikis don't want to use
118  foreach ( $context->getConfig()->get( 'HiddenPrefs' ) as $pref ) {
119  if ( isset( $defaultPreferences[$pref] ) ) {
120  unset( $defaultPreferences[$pref] );
121  }
122  }
123 
124  # # Make sure that form fields have their parent set. See bug 41337.
125  $dummyForm = new HTMLForm( [], $context );
126 
127  $disable = !$user->isAllowed( 'editmyoptions' );
128 
129  $defaultOptions = User::getDefaultOptions();
130  # # Prod in defaults from the user
131  foreach ( $defaultPreferences as $name => &$info ) {
132  $prefFromUser = self::getOptionFromUser( $name, $info, $user );
133  if ( $disable && !in_array( $name, self::$saveBlacklist ) ) {
134  $info['disabled'] = 'disabled';
135  }
136  $field = HTMLForm::loadInputFromParameters( $name, $info, $dummyForm ); // For validation
137  $globalDefault = isset( $defaultOptions[$name] )
138  ? $defaultOptions[$name]
139  : null;
140 
141  // If it validates, set it as the default
142  if ( isset( $info['default'] ) ) {
143  // Already set, no problem
144  continue;
145  } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
146  $field->validate( $prefFromUser, $user->getOptions() ) === true ) {
147  $info['default'] = $prefFromUser;
148  } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
149  $info['default'] = $globalDefault;
150  } else {
151  throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
152  }
153  }
154 
155  return $defaultPreferences;
156  }
157 
166  static function getOptionFromUser( $name, $info, $user ) {
167  $val = $user->getOption( $name );
168 
169  // Handling for multiselect preferences
170  if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
171  ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
172  $options = HTMLFormField::flattenOptions( $info['options'] );
173  $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
174  $val = [];
175 
176  foreach ( $options as $value ) {
177  if ( $user->getOption( "$prefix$value" ) ) {
178  $val[] = $value;
179  }
180  }
181  }
182 
183  // Handling for checkmatrix preferences
184  if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' ) ||
185  ( isset( $info['class'] ) && $info['class'] == 'HTMLCheckMatrix' ) ) {
186  $columns = HTMLFormField::flattenOptions( $info['columns'] );
187  $rows = HTMLFormField::flattenOptions( $info['rows'] );
188  $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
189  $val = [];
190 
191  foreach ( $columns as $column ) {
192  foreach ( $rows as $row ) {
193  if ( $user->getOption( "$prefix$column-$row" ) ) {
194  $val[] = "$column-$row";
195  }
196  }
197  }
198  }
199 
200  return $val;
201  }
202 
211 
212  $config = $context->getConfig();
213  // retrieving user name for GENDER and misc.
214  $userName = $user->getName();
215 
216  # # User info #####################################
217  // Information panel
218  $defaultPreferences['username'] = [
219  'type' => 'info',
220  'label-message' => [ 'username', $userName ],
221  'default' => $userName,
222  'section' => 'personal/info',
223  ];
224 
225  # Get groups to which the user belongs
226  $userEffectiveGroups = $user->getEffectiveGroups();
227  $userGroups = $userMembers = [];
228  foreach ( $userEffectiveGroups as $ueg ) {
229  if ( $ueg == '*' ) {
230  // Skip the default * group, seems useless here
231  continue;
232  }
233  $groupName = User::getGroupName( $ueg );
234  $userGroups[] = User::makeGroupLinkHTML( $ueg, $groupName );
235 
236  $memberName = User::getGroupMember( $ueg, $userName );
237  $userMembers[] = User::makeGroupLinkHTML( $ueg, $memberName );
238  }
239  asort( $userGroups );
240  asort( $userMembers );
241 
242  $lang = $context->getLanguage();
243 
244  $defaultPreferences['usergroups'] = [
245  'type' => 'info',
246  'label' => $context->msg( 'prefs-memberingroups' )->numParams(
247  count( $userGroups ) )->params( $userName )->parse(),
248  'default' => $context->msg( 'prefs-memberingroups-type' )
249  ->rawParams( $lang->commaList( $userGroups ), $lang->commaList( $userMembers ) )
250  ->escaped(),
251  'raw' => true,
252  'section' => 'personal/info',
253  ];
254 
255  $editCount = Linker::link( SpecialPage::getTitleFor( "Contributions", $userName ),
256  $lang->formatNum( $user->getEditCount() ) );
257 
258  $defaultPreferences['editcount'] = [
259  'type' => 'info',
260  'raw' => true,
261  'label-message' => 'prefs-edits',
262  'default' => $editCount,
263  'section' => 'personal/info',
264  ];
265 
266  if ( $user->getRegistration() ) {
267  $displayUser = $context->getUser();
268  $userRegistration = $user->getRegistration();
269  $defaultPreferences['registrationdate'] = [
270  'type' => 'info',
271  'label-message' => 'prefs-registration',
272  'default' => $context->msg(
273  'prefs-registration-date-time',
274  $lang->userTimeAndDate( $userRegistration, $displayUser ),
275  $lang->userDate( $userRegistration, $displayUser ),
276  $lang->userTime( $userRegistration, $displayUser )
277  )->parse(),
278  'section' => 'personal/info',
279  ];
280  }
281 
282  $canViewPrivateInfo = $user->isAllowed( 'viewmyprivateinfo' );
283  $canEditPrivateInfo = $user->isAllowed( 'editmyprivateinfo' );
284 
285  // Actually changeable stuff
286  $realnameChangeAllowed = $wgDisableAuthManager ? $wgAuth->allowPropChange( 'realname' )
287  : AuthManager::singleton()->allowsPropertyChange( 'realname' );
288  $defaultPreferences['realname'] = [
289  // (not really "private", but still shouldn't be edited without permission)
290  'type' => $canEditPrivateInfo && $realnameChangeAllowed ? 'text' : 'info',
291  'default' => $user->getRealName(),
292  'section' => 'personal/info',
293  'label-message' => 'yourrealname',
294  'help-message' => 'prefs-help-realname',
295  ];
296 
297  $allowPasswordChange = $wgDisableAuthManager ? $wgAuth->allowPasswordChange()
298  : AuthManager::singleton()->allowsAuthenticationDataChange(
299  new PasswordAuthenticationRequest(), false )->isGood();
300  if ( $canEditPrivateInfo && $allowPasswordChange ) {
301  $link = Linker::link( SpecialPage::getTitleFor( 'ChangePassword' ),
302  $context->msg( 'prefs-resetpass' )->escaped(), [],
303  [ 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ] );
304 
305  $defaultPreferences['password'] = [
306  'type' => 'info',
307  'raw' => true,
308  'default' => $link,
309  'label-message' => 'yourpassword',
310  'section' => 'personal/info',
311  ];
312  }
313  // Only show prefershttps if secure login is turned on
314  if ( $config->get( 'SecureLogin' ) && wfCanIPUseHTTPS( $context->getRequest()->getIP() ) ) {
315  $defaultPreferences['prefershttps'] = [
316  'type' => 'toggle',
317  'label-message' => 'tog-prefershttps',
318  'help-message' => 'prefs-help-prefershttps',
319  'section' => 'personal/info'
320  ];
321  }
322 
323  // Language
325  $languageCode = $config->get( 'LanguageCode' );
326  if ( !array_key_exists( $languageCode, $languages ) ) {
327  $languages[$languageCode] = $languageCode;
328  }
329  ksort( $languages );
330 
331  $options = [];
332  foreach ( $languages as $code => $name ) {
333  $display = wfBCP47( $code ) . ' - ' . $name;
334  $options[$display] = $code;
335  }
336  $defaultPreferences['language'] = [
337  'type' => 'select',
338  'section' => 'personal/i18n',
339  'options' => $options,
340  'label-message' => 'yourlanguage',
341  ];
342 
343  $defaultPreferences['gender'] = [
344  'type' => 'radio',
345  'section' => 'personal/i18n',
346  'options' => [
347  $context->msg( 'parentheses' )
348  ->params( $context->msg( 'gender-unknown' )->plain() )
349  ->escaped() => 'unknown',
350  $context->msg( 'gender-female' )->escaped() => 'female',
351  $context->msg( 'gender-male' )->escaped() => 'male',
352  ],
353  'label-message' => 'yourgender',
354  'help-message' => 'prefs-help-gender',
355  ];
356 
357  // see if there are multiple language variants to choose from
358  if ( !$config->get( 'DisableLangConversion' ) ) {
359  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
360  if ( $langCode == $wgContLang->getCode() ) {
361  $variants = $wgContLang->getVariants();
362 
363  if ( count( $variants ) <= 1 ) {
364  continue;
365  }
366 
367  $variantArray = [];
368  foreach ( $variants as $v ) {
369  $v = str_replace( '_', '-', strtolower( $v ) );
370  $variantArray[$v] = $lang->getVariantname( $v, false );
371  }
372 
373  $options = [];
374  foreach ( $variantArray as $code => $name ) {
375  $display = wfBCP47( $code ) . ' - ' . $name;
376  $options[$display] = $code;
377  }
378 
379  $defaultPreferences['variant'] = [
380  'label-message' => 'yourvariant',
381  'type' => 'select',
382  'options' => $options,
383  'section' => 'personal/i18n',
384  'help-message' => 'prefs-help-variant',
385  ];
386  } else {
387  $defaultPreferences["variant-$langCode"] = [
388  'type' => 'api',
389  ];
390  }
391  }
392  }
393 
394  // Stuff from Language::getExtraUserToggles()
395  // FIXME is this dead code? $extraUserToggles doesn't seem to be defined for any language
396  $toggles = $wgContLang->getExtraUserToggles();
397 
398  foreach ( $toggles as $toggle ) {
399  $defaultPreferences[$toggle] = [
400  'type' => 'toggle',
401  'section' => 'personal/i18n',
402  'label-message' => "tog-$toggle",
403  ];
404  }
405 
406  // show a preview of the old signature first
407  $oldsigWikiText = $wgParser->preSaveTransform(
408  '~~~',
409  $context->getTitle(),
410  $user,
412  );
413  $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
414  $defaultPreferences['oldsig'] = [
415  'type' => 'info',
416  'raw' => true,
417  'label-message' => 'tog-oldsig',
418  'default' => $oldsigHTML,
419  'section' => 'personal/signature',
420  ];
421  $nicknameChangeAllowed = $wgDisableAuthManager ? $wgAuth->allowPropChange( 'nickname' )
422  : AuthManager::singleton()->allowsPropertyChange( 'nickname' );
423  $defaultPreferences['nickname'] = [
424  'type' => $nicknameChangeAllowed ? 'text' : 'info',
425  'maxlength' => $config->get( 'MaxSigChars' ),
426  'label-message' => 'yournick',
427  'validation-callback' => [ 'Preferences', 'validateSignature' ],
428  'section' => 'personal/signature',
429  'filter-callback' => [ 'Preferences', 'cleanSignature' ],
430  ];
431  $defaultPreferences['fancysig'] = [
432  'type' => 'toggle',
433  'label-message' => 'tog-fancysig',
434  // show general help about signature at the bottom of the section
435  'help-message' => 'prefs-help-signature',
436  'section' => 'personal/signature'
437  ];
438 
439  # # Email stuff
440 
441  if ( $config->get( 'EnableEmail' ) ) {
442  if ( $canViewPrivateInfo ) {
443  $helpMessages[] = $config->get( 'EmailConfirmToEdit' )
444  ? 'prefs-help-email-required'
445  : 'prefs-help-email';
446 
447  if ( $config->get( 'EnableUserEmail' ) ) {
448  // additional messages when users can send email to each other
449  $helpMessages[] = 'prefs-help-email-others';
450  }
451 
452  $emailAddress = $user->getEmail() ? htmlspecialchars( $user->getEmail() ) : '';
453  $emailChangeAllowed = $wgDisableAuthManager ? $wgAuth->allowPropChange( 'emailaddress' )
454  : AuthManager::singleton()->allowsPropertyChange( 'emailaddress' );
455  if ( $canEditPrivateInfo && $emailChangeAllowed ) {
457  SpecialPage::getTitleFor( 'ChangeEmail' ),
458  $context->msg( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' )->escaped(),
459  [],
460  [ 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ] );
461 
462  $emailAddress .= $emailAddress == '' ? $link : (
463  $context->msg( 'word-separator' )->escaped()
464  . $context->msg( 'parentheses' )->rawParams( $link )->escaped()
465  );
466  }
467 
468  $defaultPreferences['emailaddress'] = [
469  'type' => 'info',
470  'raw' => true,
471  'default' => $emailAddress,
472  'label-message' => 'youremail',
473  'section' => 'personal/email',
474  'help-messages' => $helpMessages,
475  # 'cssclass' chosen below
476  ];
477  }
478 
479  $disableEmailPrefs = false;
480 
481  if ( $config->get( 'EmailAuthentication' ) ) {
482  $emailauthenticationclass = 'mw-email-not-authenticated';
483  if ( $user->getEmail() ) {
484  if ( $user->getEmailAuthenticationTimestamp() ) {
485  // date and time are separate parameters to facilitate localisation.
486  // $time is kept for backward compat reasons.
487  // 'emailauthenticated' is also used in SpecialConfirmemail.php
488  $displayUser = $context->getUser();
489  $emailTimestamp = $user->getEmailAuthenticationTimestamp();
490  $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
491  $d = $lang->userDate( $emailTimestamp, $displayUser );
492  $t = $lang->userTime( $emailTimestamp, $displayUser );
493  $emailauthenticated = $context->msg( 'emailauthenticated',
494  $time, $d, $t )->parse() . '<br />';
495  $disableEmailPrefs = false;
496  $emailauthenticationclass = 'mw-email-authenticated';
497  } else {
498  $disableEmailPrefs = true;
499  $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
501  SpecialPage::getTitleFor( 'Confirmemail' ),
502  $context->msg( 'emailconfirmlink' )->escaped()
503  ) . '<br />';
504  $emailauthenticationclass = "mw-email-not-authenticated";
505  }
506  } else {
507  $disableEmailPrefs = true;
508  $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
509  $emailauthenticationclass = 'mw-email-none';
510  }
511 
512  if ( $canViewPrivateInfo ) {
513  $defaultPreferences['emailauthentication'] = [
514  'type' => 'info',
515  'raw' => true,
516  'section' => 'personal/email',
517  'label-message' => 'prefs-emailconfirm-label',
518  'default' => $emailauthenticated,
519  # Apply the same CSS class used on the input to the message:
520  'cssclass' => $emailauthenticationclass,
521  ];
522  }
523  }
524 
525  if ( $config->get( 'EnableUserEmail' ) && $user->isAllowed( 'sendemail' ) ) {
526  $defaultPreferences['disablemail'] = [
527  'type' => 'toggle',
528  'invert' => true,
529  'section' => 'personal/email',
530  'label-message' => 'allowemail',
531  'disabled' => $disableEmailPrefs,
532  ];
533  $defaultPreferences['ccmeonemails'] = [
534  'type' => 'toggle',
535  'section' => 'personal/email',
536  'label-message' => 'tog-ccmeonemails',
537  'disabled' => $disableEmailPrefs,
538  ];
539  }
540 
541  if ( $config->get( 'EnotifWatchlist' ) ) {
542  $defaultPreferences['enotifwatchlistpages'] = [
543  'type' => 'toggle',
544  'section' => 'personal/email',
545  'label-message' => 'tog-enotifwatchlistpages',
546  'disabled' => $disableEmailPrefs,
547  ];
548  }
549  if ( $config->get( 'EnotifUserTalk' ) ) {
550  $defaultPreferences['enotifusertalkpages'] = [
551  'type' => 'toggle',
552  'section' => 'personal/email',
553  'label-message' => 'tog-enotifusertalkpages',
554  'disabled' => $disableEmailPrefs,
555  ];
556  }
557  if ( $config->get( 'EnotifUserTalk' ) || $config->get( 'EnotifWatchlist' ) ) {
558  if ( $config->get( 'EnotifMinorEdits' ) ) {
559  $defaultPreferences['enotifminoredits'] = [
560  'type' => 'toggle',
561  'section' => 'personal/email',
562  'label-message' => 'tog-enotifminoredits',
563  'disabled' => $disableEmailPrefs,
564  ];
565  }
566 
567  if ( $config->get( 'EnotifRevealEditorAddress' ) ) {
568  $defaultPreferences['enotifrevealaddr'] = [
569  'type' => 'toggle',
570  'section' => 'personal/email',
571  'label-message' => 'tog-enotifrevealaddr',
572  'disabled' => $disableEmailPrefs,
573  ];
574  }
575  }
576  }
577  }
578 
586  # # Skin #####################################
587 
588  // Skin selector, if there is at least one valid skin
589  $skinOptions = self::generateSkinOptions( $user, $context );
590  if ( $skinOptions ) {
591  $defaultPreferences['skin'] = [
592  'type' => 'radio',
593  'options' => $skinOptions,
594  'label' => '&#160;',
595  'section' => 'rendering/skin',
596  ];
597  }
598 
599  $config = $context->getConfig();
600  $allowUserCss = $config->get( 'AllowUserCss' );
601  $allowUserJs = $config->get( 'AllowUserJs' );
602  # Create links to user CSS/JS pages for all skins
603  # This code is basically copied from generateSkinOptions(). It'd
604  # be nice to somehow merge this back in there to avoid redundancy.
605  if ( $allowUserCss || $allowUserJs ) {
606  $linkTools = [];
607  $userName = $user->getName();
608 
609  if ( $allowUserCss ) {
610  $cssPage = Title::makeTitleSafe( NS_USER, $userName . '/common.css' );
611  $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
612  }
613 
614  if ( $allowUserJs ) {
615  $jsPage = Title::makeTitleSafe( NS_USER, $userName . '/common.js' );
616  $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
617  }
618 
619  $defaultPreferences['commoncssjs'] = [
620  'type' => 'info',
621  'raw' => true,
622  'default' => $context->getLanguage()->pipeList( $linkTools ),
623  'label-message' => 'prefs-common-css-js',
624  'section' => 'rendering/skin',
625  ];
626  }
627  }
628 
635  # # Files #####################################
636  $defaultPreferences['imagesize'] = [
637  'type' => 'select',
638  'options' => self::getImageSizes( $context ),
639  'label-message' => 'imagemaxsize',
640  'section' => 'rendering/files',
641  ];
642  $defaultPreferences['thumbsize'] = [
643  'type' => 'select',
644  'options' => self::getThumbSizes( $context ),
645  'label-message' => 'thumbsize',
646  'section' => 'rendering/files',
647  ];
648  }
649 
657  # # Date and time #####################################
658  $dateOptions = self::getDateOptions( $context );
659  if ( $dateOptions ) {
660  $defaultPreferences['date'] = [
661  'type' => 'radio',
662  'options' => $dateOptions,
663  'label' => '&#160;',
664  'section' => 'rendering/dateformat',
665  ];
666  }
667 
668  // Info
669  $now = wfTimestampNow();
670  $lang = $context->getLanguage();
671  $nowlocal = Xml::element( 'span', [ 'id' => 'wpLocalTime' ],
672  $lang->userTime( $now, $user ) );
673  $nowserver = $lang->userTime( $now, $user,
674  [ 'format' => false, 'timecorrection' => false ] ) .
675  Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
676 
677  $defaultPreferences['nowserver'] = [
678  'type' => 'info',
679  'raw' => 1,
680  'label-message' => 'servertime',
681  'default' => $nowserver,
682  'section' => 'rendering/timeoffset',
683  ];
684 
685  $defaultPreferences['nowlocal'] = [
686  'type' => 'info',
687  'raw' => 1,
688  'label-message' => 'localtime',
689  'default' => $nowlocal,
690  'section' => 'rendering/timeoffset',
691  ];
692 
693  // Grab existing pref.
694  $tzOffset = $user->getOption( 'timecorrection' );
695  $tz = explode( '|', $tzOffset, 3 );
696 
697  $tzOptions = self::getTimezoneOptions( $context );
698 
699  $tzSetting = $tzOffset;
700  if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
701  $minDiff = $tz[1];
702  $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 );
703  } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
704  !in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) )
705  ) {
706  # Timezone offset can vary with DST
707  $userTZ = timezone_open( $tz[2] );
708  if ( $userTZ !== false ) {
709  $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
710  $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
711  }
712  }
713 
714  $defaultPreferences['timecorrection'] = [
715  'class' => 'HTMLSelectOrOtherField',
716  'label-message' => 'timezonelegend',
717  'options' => $tzOptions,
718  'default' => $tzSetting,
719  'size' => 20,
720  'section' => 'rendering/timeoffset',
721  ];
722  }
723 
730  # # Diffs ####################################
731  $defaultPreferences['diffonly'] = [
732  'type' => 'toggle',
733  'section' => 'rendering/diffs',
734  'label-message' => 'tog-diffonly',
735  ];
736  $defaultPreferences['norollbackdiff'] = [
737  'type' => 'toggle',
738  'section' => 'rendering/diffs',
739  'label-message' => 'tog-norollbackdiff',
740  ];
741 
742  # # Page Rendering ##############################
743  if ( $context->getConfig()->get( 'AllowUserCssPrefs' ) ) {
744  $defaultPreferences['underline'] = [
745  'type' => 'select',
746  'options' => [
747  $context->msg( 'underline-never' )->text() => 0,
748  $context->msg( 'underline-always' )->text() => 1,
749  $context->msg( 'underline-default' )->text() => 2,
750  ],
751  'label-message' => 'tog-underline',
752  'section' => 'rendering/advancedrendering',
753  ];
754  }
755 
756  $stubThresholdValues = [ 50, 100, 500, 1000, 2000, 5000, 10000 ];
757  $stubThresholdOptions = [ $context->msg( 'stub-threshold-disabled' )->text() => 0 ];
758  foreach ( $stubThresholdValues as $value ) {
759  $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
760  }
761 
762  $defaultPreferences['stubthreshold'] = [
763  'type' => 'select',
764  'section' => 'rendering/advancedrendering',
765  'options' => $stubThresholdOptions,
766  // This is not a raw HTML message; label-raw is needed for the manual <a></a>
767  'label-raw' => $context->msg( 'stub-threshold' )->rawParams(
768  '<a href="#" class="stub">' .
769  $context->msg( 'stub-threshold-sample-link' )->parse() .
770  '</a>' )->parse(),
771  ];
772 
773  $defaultPreferences['showhiddencats'] = [
774  'type' => 'toggle',
775  'section' => 'rendering/advancedrendering',
776  'label-message' => 'tog-showhiddencats'
777  ];
778 
779  $defaultPreferences['numberheadings'] = [
780  'type' => 'toggle',
781  'section' => 'rendering/advancedrendering',
782  'label-message' => 'tog-numberheadings',
783  ];
784  }
785 
792  # # Editing #####################################
793  $defaultPreferences['editsectiononrightclick'] = [
794  'type' => 'toggle',
795  'section' => 'editing/advancedediting',
796  'label-message' => 'tog-editsectiononrightclick',
797  ];
798  $defaultPreferences['editondblclick'] = [
799  'type' => 'toggle',
800  'section' => 'editing/advancedediting',
801  'label-message' => 'tog-editondblclick',
802  ];
803 
804  if ( $context->getConfig()->get( 'AllowUserCssPrefs' ) ) {
805  $defaultPreferences['editfont'] = [
806  'type' => 'select',
807  'section' => 'editing/editor',
808  'label-message' => 'editfont-style',
809  'options' => [
810  $context->msg( 'editfont-default' )->text() => 'default',
811  $context->msg( 'editfont-monospace' )->text() => 'monospace',
812  $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
813  $context->msg( 'editfont-serif' )->text() => 'serif',
814  ]
815  ];
816  }
817  $defaultPreferences['cols'] = [
818  'type' => 'int',
819  'label-message' => 'columns',
820  'section' => 'editing/editor',
821  'min' => 4,
822  'max' => 1000,
823  ];
824  $defaultPreferences['rows'] = [
825  'type' => 'int',
826  'label-message' => 'rows',
827  'section' => 'editing/editor',
828  'min' => 4,
829  'max' => 1000,
830  ];
831  if ( $user->isAllowed( 'minoredit' ) ) {
832  $defaultPreferences['minordefault'] = [
833  'type' => 'toggle',
834  'section' => 'editing/editor',
835  'label-message' => 'tog-minordefault',
836  ];
837  }
838  $defaultPreferences['forceeditsummary'] = [
839  'type' => 'toggle',
840  'section' => 'editing/editor',
841  'label-message' => 'tog-forceeditsummary',
842  ];
843  $defaultPreferences['useeditwarning'] = [
844  'type' => 'toggle',
845  'section' => 'editing/editor',
846  'label-message' => 'tog-useeditwarning',
847  ];
848  $defaultPreferences['showtoolbar'] = [
849  'type' => 'toggle',
850  'section' => 'editing/editor',
851  'label-message' => 'tog-showtoolbar',
852  ];
853 
854  $defaultPreferences['previewonfirst'] = [
855  'type' => 'toggle',
856  'section' => 'editing/preview',
857  'label-message' => 'tog-previewonfirst',
858  ];
859  $defaultPreferences['previewontop'] = [
860  'type' => 'toggle',
861  'section' => 'editing/preview',
862  'label-message' => 'tog-previewontop',
863  ];
864  $defaultPreferences['uselivepreview'] = [
865  'type' => 'toggle',
866  'section' => 'editing/preview',
867  'label-message' => 'tog-uselivepreview',
868  ];
869 
870  }
871 
878  $config = $context->getConfig();
879  $rcMaxAge = $config->get( 'RCMaxAge' );
880  # # RecentChanges #####################################
881  $defaultPreferences['rcdays'] = [
882  'type' => 'float',
883  'label-message' => 'recentchangesdays',
884  'section' => 'rc/displayrc',
885  'min' => 1,
886  'max' => ceil( $rcMaxAge / ( 3600 * 24 ) ),
887  'help' => $context->msg( 'recentchangesdays-max' )->numParams(
888  ceil( $rcMaxAge / ( 3600 * 24 ) ) )->escaped()
889  ];
890  $defaultPreferences['rclimit'] = [
891  'type' => 'int',
892  'label-message' => 'recentchangescount',
893  'help-message' => 'prefs-help-recentchangescount',
894  'section' => 'rc/displayrc',
895  ];
896  $defaultPreferences['usenewrc'] = [
897  'type' => 'toggle',
898  'label-message' => 'tog-usenewrc',
899  'section' => 'rc/advancedrc',
900  ];
901  $defaultPreferences['hideminor'] = [
902  'type' => 'toggle',
903  'label-message' => 'tog-hideminor',
904  'section' => 'rc/advancedrc',
905  ];
906 
907  if ( $config->get( 'RCWatchCategoryMembership' ) ) {
908  $defaultPreferences['hidecategorization'] = [
909  'type' => 'toggle',
910  'label-message' => 'tog-hidecategorization',
911  'section' => 'rc/advancedrc',
912  ];
913  }
914 
915  if ( $user->useRCPatrol() ) {
916  $defaultPreferences['hidepatrolled'] = [
917  'type' => 'toggle',
918  'section' => 'rc/advancedrc',
919  'label-message' => 'tog-hidepatrolled',
920  ];
921  }
922 
923  if ( $user->useNPPatrol() ) {
924  $defaultPreferences['newpageshidepatrolled'] = [
925  'type' => 'toggle',
926  'section' => 'rc/advancedrc',
927  'label-message' => 'tog-newpageshidepatrolled',
928  ];
929  }
930 
931  if ( $config->get( 'RCShowWatchingUsers' ) ) {
932  $defaultPreferences['shownumberswatching'] = [
933  'type' => 'toggle',
934  'section' => 'rc/advancedrc',
935  'label-message' => 'tog-shownumberswatching',
936  ];
937  }
938  }
939 
946  $config = $context->getConfig();
947  $watchlistdaysMax = ceil( $config->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
948 
949  # # Watchlist #####################################
950  if ( $user->isAllowed( 'editmywatchlist' ) ) {
951  $editWatchlistLinks = [];
952  $editWatchlistModes = [
953  'edit' => [ 'EditWatchlist', false ],
954  'raw' => [ 'EditWatchlist', 'raw' ],
955  'clear' => [ 'EditWatchlist', 'clear' ],
956  ];
957  foreach ( $editWatchlistModes as $editWatchlistMode => $mode ) {
958  // Messages: prefs-editwatchlist-edit, prefs-editwatchlist-raw, prefs-editwatchlist-clear
959  $editWatchlistLinks[] = Linker::linkKnown(
960  SpecialPage::getTitleFor( $mode[0], $mode[1] ),
961  $context->msg( "prefs-editwatchlist-{$editWatchlistMode}" )->parse()
962  );
963  }
964 
965  $defaultPreferences['editwatchlist'] = [
966  'type' => 'info',
967  'raw' => true,
968  'default' => $context->getLanguage()->pipeList( $editWatchlistLinks ),
969  'label-message' => 'prefs-editwatchlist-label',
970  'section' => 'watchlist/editwatchlist',
971  ];
972  }
973 
974  $defaultPreferences['watchlistdays'] = [
975  'type' => 'float',
976  'min' => 0,
977  'max' => $watchlistdaysMax,
978  'section' => 'watchlist/displaywatchlist',
979  'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
980  $watchlistdaysMax )->escaped(),
981  'label-message' => 'prefs-watchlist-days',
982  ];
983  $defaultPreferences['wllimit'] = [
984  'type' => 'int',
985  'min' => 0,
986  'max' => 1000,
987  'label-message' => 'prefs-watchlist-edits',
988  'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
989  'section' => 'watchlist/displaywatchlist',
990  ];
991  $defaultPreferences['extendwatchlist'] = [
992  'type' => 'toggle',
993  'section' => 'watchlist/advancedwatchlist',
994  'label-message' => 'tog-extendwatchlist',
995  ];
996  $defaultPreferences['watchlisthideminor'] = [
997  'type' => 'toggle',
998  'section' => 'watchlist/advancedwatchlist',
999  'label-message' => 'tog-watchlisthideminor',
1000  ];
1001  $defaultPreferences['watchlisthidebots'] = [
1002  'type' => 'toggle',
1003  'section' => 'watchlist/advancedwatchlist',
1004  'label-message' => 'tog-watchlisthidebots',
1005  ];
1006  $defaultPreferences['watchlisthideown'] = [
1007  'type' => 'toggle',
1008  'section' => 'watchlist/advancedwatchlist',
1009  'label-message' => 'tog-watchlisthideown',
1010  ];
1011  $defaultPreferences['watchlisthideanons'] = [
1012  'type' => 'toggle',
1013  'section' => 'watchlist/advancedwatchlist',
1014  'label-message' => 'tog-watchlisthideanons',
1015  ];
1016  $defaultPreferences['watchlisthideliu'] = [
1017  'type' => 'toggle',
1018  'section' => 'watchlist/advancedwatchlist',
1019  'label-message' => 'tog-watchlisthideliu',
1020  ];
1021  $defaultPreferences['watchlistreloadautomatically'] = [
1022  'type' => 'toggle',
1023  'section' => 'watchlist/advancedwatchlist',
1024  'label-message' => 'tog-watchlistreloadautomatically',
1025  ];
1026 
1027  if ( $config->get( 'RCWatchCategoryMembership' ) ) {
1028  $defaultPreferences['watchlisthidecategorization'] = [
1029  'type' => 'toggle',
1030  'section' => 'watchlist/advancedwatchlist',
1031  'label-message' => 'tog-watchlisthidecategorization',
1032  ];
1033  }
1034 
1035  if ( $user->useRCPatrol() ) {
1036  $defaultPreferences['watchlisthidepatrolled'] = [
1037  'type' => 'toggle',
1038  'section' => 'watchlist/advancedwatchlist',
1039  'label-message' => 'tog-watchlisthidepatrolled',
1040  ];
1041  }
1042 
1043  $watchTypes = [
1044  'edit' => 'watchdefault',
1045  'move' => 'watchmoves',
1046  'delete' => 'watchdeletion'
1047  ];
1048 
1049  // Kinda hacky
1050  if ( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
1051  $watchTypes['read'] = 'watchcreations';
1052  }
1053 
1054  if ( $user->isAllowed( 'rollback' ) ) {
1055  $watchTypes['rollback'] = 'watchrollback';
1056  }
1057 
1058  if ( $user->isAllowed( 'upload' ) ) {
1059  $watchTypes['upload'] = 'watchuploads';
1060  }
1061 
1062  foreach ( $watchTypes as $action => $pref ) {
1063  if ( $user->isAllowed( $action ) ) {
1064  // Messages:
1065  // tog-watchdefault, tog-watchmoves, tog-watchdeletion, tog-watchcreations, tog-watchuploads
1066  // tog-watchrollback
1067  $defaultPreferences[$pref] = [
1068  'type' => 'toggle',
1069  'section' => 'watchlist/advancedwatchlist',
1070  'label-message' => "tog-$pref",
1071  ];
1072  }
1073  }
1074 
1075  if ( $config->get( 'EnableAPI' ) ) {
1076  $defaultPreferences['watchlisttoken'] = [
1077  'type' => 'api',
1078  ];
1079  $defaultPreferences['watchlisttoken-info'] = [
1080  'type' => 'info',
1081  'section' => 'watchlist/tokenwatchlist',
1082  'label-message' => 'prefs-watchlist-token',
1083  'default' => $user->getTokenFromOption( 'watchlisttoken' ),
1084  'help-message' => 'prefs-help-watchlist-token2',
1085  ];
1086  }
1087  }
1088 
1095  foreach ( MWNamespace::getValidNamespaces() as $n ) {
1096  $defaultPreferences['searchNs' . $n] = [
1097  'type' => 'api',
1098  ];
1099  }
1100  }
1101 
1106  }
1107 
1114  $ret = [];
1115 
1116  $mptitle = Title::newMainPage();
1117  $previewtext = $context->msg( 'skin-preview' )->escaped();
1118 
1119  # Only show skins that aren't disabled in $wgSkipSkins
1120  $validSkinNames = Skin::getAllowedSkins();
1121 
1122  # Sort by UI skin name. First though need to update validSkinNames as sometimes
1123  # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1124  foreach ( $validSkinNames as $skinkey => &$skinname ) {
1125  $msg = $context->msg( "skinname-{$skinkey}" );
1126  if ( $msg->exists() ) {
1127  $skinname = htmlspecialchars( $msg->text() );
1128  }
1129  }
1130  asort( $validSkinNames );
1131 
1132  $config = $context->getConfig();
1133  $defaultSkin = $config->get( 'DefaultSkin' );
1134  $allowUserCss = $config->get( 'AllowUserCss' );
1135  $allowUserJs = $config->get( 'AllowUserJs' );
1136 
1137  $foundDefault = false;
1138  foreach ( $validSkinNames as $skinkey => $sn ) {
1139  $linkTools = [];
1140 
1141  # Mark the default skin
1142  if ( strcasecmp( $skinkey, $defaultSkin ) === 0 ) {
1143  $linkTools[] = $context->msg( 'default' )->escaped();
1144  $foundDefault = true;
1145  }
1146 
1147  # Create preview link
1148  $mplink = htmlspecialchars( $mptitle->getLocalURL( [ 'useskin' => $skinkey ] ) );
1149  $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1150 
1151  # Create links to user CSS/JS pages
1152  if ( $allowUserCss ) {
1153  $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
1154  $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1155  }
1156 
1157  if ( $allowUserJs ) {
1158  $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
1159  $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1160  }
1161 
1162  $display = $sn . ' ' . $context->msg( 'parentheses' )
1163  ->rawParams( $context->getLanguage()->pipeList( $linkTools ) )
1164  ->escaped();
1165  $ret[$display] = $skinkey;
1166  }
1167 
1168  if ( !$foundDefault ) {
1169  // If the default skin is not available, things are going to break horribly because the
1170  // default value for skin selector will not be a valid value. Let's just not show it then.
1171  return [];
1172  }
1173 
1174  return $ret;
1175  }
1176 
1182  $lang = $context->getLanguage();
1183  $dateopts = $lang->getDatePreferences();
1184 
1185  $ret = [];
1186 
1187  if ( $dateopts ) {
1188  if ( !in_array( 'default', $dateopts ) ) {
1189  $dateopts[] = 'default'; // Make sure default is always valid
1190  // Bug 19237
1191  }
1192 
1193  // FIXME KLUGE: site default might not be valid for user language
1195  if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1196  $wgDefaultUserOptions['date'] = 'default';
1197  }
1198 
1199  $epoch = wfTimestampNow();
1200  foreach ( $dateopts as $key ) {
1201  if ( $key == 'default' ) {
1202  $formatted = $context->msg( 'datedefault' )->escaped();
1203  } else {
1204  $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1205  }
1206  $ret[$formatted] = $key;
1207  }
1208  }
1209  return $ret;
1210  }
1211 
1217  $ret = [];
1218  $pixels = $context->msg( 'unit-pixel' )->text();
1219 
1220  foreach ( $context->getConfig()->get( 'ImageLimits' ) as $index => $limits ) {
1221  $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1222  $ret[$display] = $index;
1223  }
1224 
1225  return $ret;
1226  }
1227 
1233  $ret = [];
1234  $pixels = $context->msg( 'unit-pixel' )->text();
1235 
1236  foreach ( $context->getConfig()->get( 'ThumbLimits' ) as $index => $size ) {
1237  $display = $size . $pixels;
1238  $ret[$display] = $index;
1239  }
1240 
1241  return $ret;
1242  }
1243 
1250  static function validateSignature( $signature, $alldata, $form ) {
1251  global $wgParser;
1252  $maxSigChars = $form->getConfig()->get( 'MaxSigChars' );
1253  if ( mb_strlen( $signature ) > $maxSigChars ) {
1254  return Xml::element( 'span', [ 'class' => 'error' ],
1255  $form->msg( 'badsiglength' )->numParams( $maxSigChars )->text() );
1256  } elseif ( isset( $alldata['fancysig'] ) &&
1257  $alldata['fancysig'] &&
1258  $wgParser->validateSig( $signature ) === false
1259  ) {
1260  return Xml::element(
1261  'span',
1262  [ 'class' => 'error' ],
1263  $form->msg( 'badsig' )->text()
1264  );
1265  } else {
1266  return true;
1267  }
1268  }
1269 
1276  static function cleanSignature( $signature, $alldata, $form ) {
1277  if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1278  global $wgParser;
1279  $signature = $wgParser->cleanSig( $signature );
1280  } else {
1281  // When no fancy sig used, make sure ~{3,5} get removed.
1282  $signature = Parser::cleanSigInSig( $signature );
1283  }
1284 
1285  return $signature;
1286  }
1287 
1295  static function getFormObject(
1296  $user,
1298  $formClass = 'PreferencesForm',
1299  array $remove = []
1300  ) {
1301  $formDescriptor = Preferences::getPreferences( $user, $context );
1302  if ( count( $remove ) ) {
1303  $removeKeys = array_flip( $remove );
1304  $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1305  }
1306 
1307  // Remove type=api preferences. They are not intended for rendering in the form.
1308  foreach ( $formDescriptor as $name => $info ) {
1309  if ( isset( $info['type'] ) && $info['type'] === 'api' ) {
1310  unset( $formDescriptor[$name] );
1311  }
1312  }
1313 
1317  $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1318 
1319  $htmlForm->setModifiedUser( $user );
1320  $htmlForm->setId( 'mw-prefs-form' );
1321  $htmlForm->setAutocomplete( 'off' );
1322  $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1323  # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1324  $htmlForm->setSubmitTooltip( 'preferences-save' );
1325  $htmlForm->setSubmitID( 'prefsubmit' );
1326  $htmlForm->setSubmitCallback( [ 'Preferences', 'tryFormSubmit' ] );
1327 
1328  return $htmlForm;
1329  }
1330 
1336  $opt = [];
1337 
1338  $localTZoffset = $context->getConfig()->get( 'LocalTZoffset' );
1339  $timeZoneList = self::getTimeZoneList( $context->getLanguage() );
1340 
1342  // Check that the LocalTZoffset is the same as the local time zone offset
1343  if ( $localTZoffset == $timestamp->format( 'Z' ) / 60 ) {
1344  $timezoneName = $timestamp->getTimezone()->getName();
1345  // Localize timezone
1346  if ( isset( $timeZoneList[$timezoneName] ) ) {
1347  $timezoneName = $timeZoneList[$timezoneName]['name'];
1348  }
1349  $server_tz_msg = $context->msg(
1350  'timezoneuseserverdefault',
1351  $timezoneName
1352  )->text();
1353  } else {
1354  $tzstring = sprintf(
1355  '%+03d:%02d',
1356  floor( $localTZoffset / 60 ),
1357  abs( $localTZoffset ) % 60
1358  );
1359  $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1360  }
1361  $opt[$server_tz_msg] = "System|$localTZoffset";
1362  $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1363  $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1364 
1365  foreach ( $timeZoneList as $timeZoneInfo ) {
1366  $region = $timeZoneInfo['region'];
1367  if ( !isset( $opt[$region] ) ) {
1368  $opt[$region] = [];
1369  }
1370  $opt[$region][$timeZoneInfo['name']] = $timeZoneInfo['timecorrection'];
1371  }
1372  return $opt;
1373  }
1374 
1380  static function filterIntval( $value, $alldata ) {
1381  return intval( $value );
1382  }
1383 
1389  static function filterTimezoneInput( $tz, $alldata ) {
1390  $data = explode( '|', $tz, 3 );
1391  switch ( $data[0] ) {
1392  case 'ZoneInfo':
1393  case 'System':
1394  return $tz;
1395  default:
1396  $data = explode( ':', $tz, 2 );
1397  if ( count( $data ) == 2 ) {
1398  $data[0] = intval( $data[0] );
1399  $data[1] = intval( $data[1] );
1400  $minDiff = abs( $data[0] ) * 60 + $data[1];
1401  if ( $data[0] < 0 ) {
1402  $minDiff = - $minDiff;
1403  }
1404  } else {
1405  $minDiff = intval( $data[0] ) * 60;
1406  }
1407 
1408  # Max is +14:00 and min is -12:00, see:
1409  # https://en.wikipedia.org/wiki/Timezone
1410  $minDiff = min( $minDiff, 840 ); # 14:00
1411  $minDiff = max( $minDiff, - 720 ); # -12:00
1412  return 'Offset|' . $minDiff;
1413  }
1414  }
1415 
1423  static function tryFormSubmit( $formData, $form ) {
1424  $user = $form->getModifiedUser();
1425  $hiddenPrefs = $form->getConfig()->get( 'HiddenPrefs' );
1426  $result = true;
1427 
1428  if ( !$user->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
1429  return Status::newFatal( 'mypreferencesprotected' );
1430  }
1431 
1432  // Filter input
1433  foreach ( array_keys( $formData ) as $name ) {
1434  if ( isset( self::$saveFilters[$name] ) ) {
1435  $formData[$name] =
1436  call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1437  }
1438  }
1439 
1440  // Fortunately, the realname field is MUCH simpler
1441  // (not really "private", but still shouldn't be edited without permission)
1442 
1443  if ( !in_array( 'realname', $hiddenPrefs )
1444  && $user->isAllowed( 'editmyprivateinfo' )
1445  && array_key_exists( 'realname', $formData )
1446  ) {
1447  $realName = $formData['realname'];
1448  $user->setRealName( $realName );
1449  }
1450 
1451  if ( $user->isAllowed( 'editmyoptions' ) ) {
1452  foreach ( self::$saveBlacklist as $b ) {
1453  unset( $formData[$b] );
1454  }
1455 
1456  # If users have saved a value for a preference which has subsequently been disabled
1457  # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1458  # is subsequently re-enabled
1459  foreach ( $hiddenPrefs as $pref ) {
1460  # If the user has not set a non-default value here, the default will be returned
1461  # and subsequently discarded
1462  $formData[$pref] = $user->getOption( $pref, null, true );
1463  }
1464 
1465  // Keep old preferences from interfering due to back-compat code, etc.
1466  $user->resetOptions( 'unused', $form->getContext() );
1467 
1468  foreach ( $formData as $key => $value ) {
1469  $user->setOption( $key, $value );
1470  }
1471 
1472  Hooks::run( 'PreferencesFormPreSave', [ $formData, $form, $user, &$result ] );
1473  }
1474 
1475  MediaWiki\Auth\AuthManager::callLegacyAuthPlugin( 'updateExternalDB', [ $user ] );
1476  $user->saveSettings();
1477 
1478  return $result;
1479  }
1480 
1486  public static function tryUISubmit( $formData, $form ) {
1487  $res = self::tryFormSubmit( $formData, $form );
1488 
1489  if ( $res ) {
1490  $urlOptions = [];
1491 
1492  if ( $res === 'eauth' ) {
1493  $urlOptions['eauth'] = 1;
1494  }
1495 
1496  $urlOptions += $form->getExtraSuccessRedirectParameters();
1497 
1498  $url = $form->getTitle()->getFullURL( $urlOptions );
1499 
1500  $context = $form->getContext();
1501  // Set session data for the success message
1502  $context->getRequest()->setSessionData( 'specialPreferencesSaveSuccess', 1 );
1503 
1504  $context->getOutput()->redirect( $url );
1505  }
1506 
1507  return Status::newGood();
1508  }
1509 
1518  public static function getTimeZoneList( Language $language ) {
1519  $identifiers = DateTimeZone::listIdentifiers();
1520  if ( $identifiers === false ) {
1521  return [];
1522  }
1523  sort( $identifiers );
1524 
1525  $tzRegions = [
1526  'Africa' => wfMessage( 'timezoneregion-africa' )->inLanguage( $language )->text(),
1527  'America' => wfMessage( 'timezoneregion-america' )->inLanguage( $language )->text(),
1528  'Antarctica' => wfMessage( 'timezoneregion-antarctica' )->inLanguage( $language )->text(),
1529  'Arctic' => wfMessage( 'timezoneregion-arctic' )->inLanguage( $language )->text(),
1530  'Asia' => wfMessage( 'timezoneregion-asia' )->inLanguage( $language )->text(),
1531  'Atlantic' => wfMessage( 'timezoneregion-atlantic' )->inLanguage( $language )->text(),
1532  'Australia' => wfMessage( 'timezoneregion-australia' )->inLanguage( $language )->text(),
1533  'Europe' => wfMessage( 'timezoneregion-europe' )->inLanguage( $language )->text(),
1534  'Indian' => wfMessage( 'timezoneregion-indian' )->inLanguage( $language )->text(),
1535  'Pacific' => wfMessage( 'timezoneregion-pacific' )->inLanguage( $language )->text(),
1536  ];
1537  asort( $tzRegions );
1538 
1539  $timeZoneList = [];
1540 
1541  $now = new DateTime();
1542 
1543  foreach ( $identifiers as $identifier ) {
1544  $parts = explode( '/', $identifier, 2 );
1545 
1546  // DateTimeZone::listIdentifiers() returns a number of
1547  // backwards-compatibility entries. This filters them out of the
1548  // list presented to the user.
1549  if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
1550  continue;
1551  }
1552 
1553  // Localize region
1554  $parts[0] = $tzRegions[$parts[0]];
1555 
1556  $dateTimeZone = new DateTimeZone( $identifier );
1557  $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
1558 
1559  $display = str_replace( '_', ' ', $parts[0] . '/' . $parts[1] );
1560  $value = "ZoneInfo|$minDiff|$identifier";
1561 
1562  $timeZoneList[$identifier] = [
1563  'name' => $display,
1564  'timecorrection' => $value,
1565  'region' => $parts[0],
1566  ];
1567  }
1568 
1569  return $timeZoneList;
1570  }
1571 }
1572 
1574 class PreferencesForm extends HTMLForm {
1575  // Override default value from HTMLForm
1576  protected $mSubSectionBeforeFields = false;
1577 
1578  private $modifiedUser;
1579 
1583  public function setModifiedUser( $user ) {
1584  $this->modifiedUser = $user;
1585  }
1586 
1590  public function getModifiedUser() {
1591  if ( $this->modifiedUser === null ) {
1592  return $this->getUser();
1593  } else {
1594  return $this->modifiedUser;
1595  }
1596  }
1597 
1605  return [];
1606  }
1607 
1612  function wrapForm( $html ) {
1613  $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
1614 
1615  return parent::wrapForm( $html );
1616  }
1617 
1621  function getButtons() {
1622 
1623  $attrs = [ 'id' => 'mw-prefs-restoreprefs' ];
1624 
1625  if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
1626  return '';
1627  }
1628 
1629  $html = parent::getButtons();
1630 
1631  if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
1632  $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1633 
1634  $html .= "\n" . Linker::link( $t, $this->msg( 'restoreprefs' )->escaped(),
1635  Html::buttonAttributes( $attrs, [ 'mw-ui-quiet' ] ) );
1636 
1637  $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
1638  }
1639 
1640  return $html;
1641  }
1642 
1649  function filterDataForSubmit( $data ) {
1650  foreach ( $this->mFlatFields as $fieldname => $field ) {
1651  if ( $field instanceof HTMLNestedFilterable ) {
1652  $info = $field->mParams;
1653  $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
1654  foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
1655  $data["$prefix$key"] = $value;
1656  }
1657  unset( $data[$fieldname] );
1658  }
1659  }
1660 
1661  return $data;
1662  }
1663 
1668  function getBody() {
1669  return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
1670  }
1671 
1678  function getLegend( $key ) {
1679  $legend = parent::getLegend( $key );
1680  Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
1681  return $legend;
1682  }
1683 
1689  return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
1690  }
1691 }
static array $saveFilters
Definition: Preferences.php:55
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1816
Interface for objects which can provide a MediaWiki context on request.
filterDataForSubmit($data)
Separate multi-option preferences into multiple preferences, since we have to store them separately...
the array() calling protocol came about after MediaWiki 1.4rc1.
wfCanIPUseHTTPS($ip)
Determine whether the client at a given source IP is likely to be able to access the wiki via HTTPS...
static getImageSizes(IContextSource $context)
$context
Definition: load.php:43
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
static element($element, $attribs=null, $contents= '', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
static filterTimezoneInput($tz, $alldata)
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:548
static skinPreferences($user, IContextSource $context, &$defaultPreferences)
static rcPreferences($user, IContextSource $context, &$defaultPreferences)
getPreferenceSections()
Get the keys of each top level preference section.
static validateSignature($signature, $alldata, $form)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getTitleFor($name, $subpage=false, $fragment= '')
Get a localised Title object for a specified special page name.
Definition: SpecialPage.php:80
msg()
Get a Message object with context set.
$wgParser
Definition: Setup.php:816
static editingPreferences($user, IContextSource $context, &$defaultPreferences)
if(!isset($args[0])) $lang
static loadPreferenceValues($user, $context, &$defaultPreferences)
Loads existing values for a given array of preferences.
static hidden($name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:749
static array $defaultPreferences
Definition: Preferences.php:52
$value
static array static $saveBlacklist
Definition: Preferences.php:65
static getDateOptions(IContextSource $context)
static filesPreferences($user, IContextSource $context, &$defaultPreferences)
static miscPreferences($user, IContextSource $context, &$defaultPreferences)
Dummy, kept for backwards-compatibility.
$wgAuth $wgAuth
Authentication plugin.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static getPreferences($user, IContextSource $context)
Definition: Preferences.php:83
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
static generateSkinOptions($user, IContextSource $context)
static makeGroupLinkHTML($group, $text= '')
Create a link to the group in HTML, if available; else return the group name.
Definition: User.php:4978
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1629
static fetchLanguageNames($inLanguage=null, $include= 'mw')
Get an array of language names, indexed by code.
Definition: Language.php:798
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED!Use HtmlPageLinkRendererBegin instead.Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1814
the value to return A Title object or null for latest to be modified or replaced by the hook handler or if authentication is not possible after cache objects are set for highlighting & $link
Definition: hooks.txt:2621
static getLocalInstance($ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
setModifiedUser($user)
static getTimeZoneList(Language $language)
Get a list of all time zones.
getUser()
Get the User object.
msg()
Get a Message object with context set Parameters are the same as wfMessage()
getConfig()
Get the site configuration.
if($limit) $timestamp
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1020
static searchPreferences($user, IContextSource $context, &$defaultPreferences)
switch($options['output']) $languages
Definition: transstat.php:76
$res
Definition: database.txt:21
static getSaveBlacklist()
Definition: Preferences.php:73
MediaWiki exception.
Definition: MWException.php:26
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Internationalisation code.
Definition: Language.php:39
getExtraSuccessRedirectParameters()
Get extra parameters for the query string when redirecting after successful save. ...
static getFormObject($user, IContextSource $context, $formClass= 'PreferencesForm', array $remove=[])
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:527
static watchlistPreferences($user, IContextSource $context, &$defaultPreferences)
static datetimePreferences($user, IContextSource $context, &$defaultPreferences)
static renderingPreferences($user, IContextSource $context, &$defaultPreferences)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
wfBCP47($code)
Get the normalised IETF language tag See unit test for examples.
static linkKnown($target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:255
static callLegacyAuthPlugin($method, array $params, $return=null)
Call a legacy AuthPlugin method, if necessary.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
This serves as the entry point to the authentication system.
Definition: AuthManager.php:43
getLegend($key)
Get the "<legend>" for a given section key.
getLanguage()
Get the Language object.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1816
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:776
static filterIntval($value, $alldata)
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
static link($target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition: Linker.php:203
static loadInputFromParameters($fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
Definition: HTMLForm.php:457
This is a value object for authentication requests with a username and password.
static getGroupName($group)
Get the localized descriptive name for a group, if it exists.
Definition: User.php:4891
static tryFormSubmit($formData, $form)
Handle the form submission if everything validated properly.
displaySection($fields, $sectionName= '', $fieldsetIDPrefix= '', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1572
Some tweaks to allow js prefs to work.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static tags($element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
static getGroupMember($group, $username= '#')
Get the localized descriptive name for a member of a group, if it exists.
Definition: User.php:4903
static profilePreferences($user, IContextSource $context, &$defaultPreferences)
static buttonAttributes(array $attrs, array $modifiers=[])
Modifies a set of attributes meant for button elements and apply a set of default attributes when $wg...
Definition: Html.php:110
$wgDisableAuthManager
Disable AuthManager.
getTitle()
Get the Title object.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
static cleanSigInSig($text)
Strip 3, 4 or 5 tildes out of signatures.
Definition: Parser.php:4569
getOutput()
Get the OutputPage object.
static cleanSignature($signature, $alldata, $form)
static getTimezoneOptions(IContextSource $context)
getBody()
Get the whole body of the form.
We're now using the HTMLForm object with some customisation to generate the Preferences form...
Definition: Preferences.php:50
static getThumbSizes(IContextSource $context)
static getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: User.php:1561
static array $languagesWithVariants
languages supporting variants
static getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
static flattenOptions($options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
static tryUISubmit($formData, $form)
static getAllowedSkins()
Fetch the list of user-selectable skins in regards to $wgSkipSkins.
Definition: Skin.php:72
getUser()
Get the User object.
getRequest()
Get the WebRequest object.
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101
static getOptionFromUser($name, $info, $user)
Pull option from a user account.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310