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