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