* Make tests work better together. Tests are now skipped or marked incomplete.
[lhc/web/wiklou.git] / includes / Preferences.php
index 013e3da..70d88ec 100644 (file)
@@ -1,5 +1,30 @@
 <?php
 
+/**
+ * We're now using the HTMLForm object with some customisation to generate the
+ * Preferences form. This object handles generic submission, CSRF protection,
+ * layout and other logic in a reusable manner. We subclass it as a PreferencesForm
+ * to make some minor customisations.
+ *
+ * In order to generate the form, the HTMLForm object needs an array structure
+ * detailing the form fields available, and that's what this class is for. Each
+ * element of the array is a basic property-list, including the type of field,
+ * the label it is to be given in the form, callbacks for validation and
+ * 'filtering', and other pertinent information. Note that the 'default' field
+ * is named for generic forms, and does not represent the preference's default
+ * (which is stored in $wgDefaultUserOptions), but the default for the form
+ * field, which should be whatever the user has set for that preference. There
+ * is no need to override it unless you have some special storage logic (for
+ * instance, those not presently stored as options, but which are best set from
+ * the user preferences view).
+ *
+ * Field types are implemented as subclasses of the generic HTMLFormField
+ * object, and typically implement at least getInputHTML, which generates the
+ * HTML for the input field to be placed in the table.
+ *
+ * Once fields have been retrieved and validated, submission logic is handed
+ * over to the tryUISubmit static method of this class.
+ */
 class Preferences {
        static $defaultPreferences = null;
        static $saveFilters =
@@ -57,7 +82,7 @@ class Preferences {
                        } elseif( $field->validate( $globalDefault, $user->mOptions ) === true ) {
                                $info['default'] = $globalDefault;
                        } else {
-                               throw new MWException( "Global default $globalDefault is invalid for field $name" );
+                               throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
                        }
                }
 
@@ -89,7 +114,7 @@ class Preferences {
        }
 
        static function profilePreferences( $user, &$defaultPreferences ) {
-               global $wgLang;
+               global $wgLang, $wgUser;
                ## User info #####################################
                // Information panel
                $defaultPreferences['username'] =
@@ -110,22 +135,30 @@ class Preferences {
 
                # Get groups to which the user belongs
                $userEffectiveGroups = $user->getEffectiveGroups();
-               $userEffectiveGroupsArray = array();
+               $userGroups = $userMembers = array();
                foreach( $userEffectiveGroups as $ueg ) {
                        if( $ueg == '*' ) {
                                // Skip the default * group, seems useless here
                                continue;
                        }
-                       $userEffectiveGroupsArray[] = User::makeGroupLinkHTML( $ueg );
+                       $groupName  = User::getGroupName( $ueg );
+                       $userGroups[] = User::makeGroupLinkHTML( $ueg, $groupName );
+
+                       $memberName = User::getGroupMember( $ueg );
+                       $userMembers[] = User::makeGroupLinkHTML( $ueg, $memberName );
                }
-               asort( $userEffectiveGroupsArray );
+               asort( $userGroups );
+               asort( $userMembers );
 
                $defaultPreferences['usergroups'] =
                                array(
                                        'type' => 'info',
                                        'label' => wfMsgExt( 'prefs-memberingroups', 'parseinline',
-                                                               count( $userEffectiveGroupsArray ) ),
-                                       'default' => $wgLang->commaList( $userEffectiveGroupsArray ),
+                                               $wgLang->formatNum( count($userGroups) ) ),
+                                       'default' => wfMsgExt( 'prefs-memberingroups-type', array(),
+                                               $wgLang->commaList( $userGroups ),
+                                               $wgLang->commaList( $userMembers )
+                                       ),
                                        'raw' => true,
                                        'section' => 'personal/info',
                                );
@@ -134,7 +167,7 @@ class Preferences {
                                array(
                                        'type' => 'info',
                                        'label-message' => 'prefs-edits',
-                                       'default' => $user->getEditCount(),
+                                       'default' => $wgLang->formatNum( $user->getEditCount() ),
                                        'section' => 'personal/info',
                                );
 
@@ -143,7 +176,10 @@ class Preferences {
                                        array(
                                                'type' => 'info',
                                                'label-message' => 'prefs-registration',
-                                               'default' => $wgLang->timeanddate( $user->getRegistration() ),
+                                               'default' => wfMsgExt( 'prefs-registration-date-time', 'parsemag',
+                                                       $wgLang->timeanddate( $user->getRegistration(), true ),
+                                                       $wgLang->date( $user->getRegistration(), true ),
+                                                       $wgLang->time( $user->getRegistration(), true ) ),
                                                'section' => 'personal/info',
                                        );
                }
@@ -173,7 +209,6 @@ class Preferences {
                                );
 
                if( $wgAuth->allowPasswordChange() ) {
-                       global $wgUser; // For skin.
                        $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'Resetpass' ),
                                wfMsgHtml( 'prefs-resetpass' ), array(),
                                array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
@@ -205,7 +240,7 @@ class Preferences {
 
                $options = array();
                foreach( $languages as $code => $name ) {
-                       $display = "$code - $name";
+                       $display = wfBCP47( $code ) . ' - ' . $name;
                        $options[$display] = $code;
                }
                $defaultPreferences['language'] =
@@ -217,6 +252,7 @@ class Preferences {
                                );
 
                global $wgContLang, $wgDisableLangConversion;
+               global $wgDisableTitleConversion;
                /* see if there are multiple language variants to choose from*/
                $variantArray = array();
                if( !$wgDisableLangConversion ) {
@@ -233,7 +269,7 @@ class Preferences {
 
                        $options = array();
                        foreach( $variantArray as $code => $name ) {
-                               $display = "$code - $name";
+                               $display = wfBCP47( $code ) . ' - ' . $name;
                                $options[$display] = $code;
                        }
 
@@ -257,7 +293,19 @@ class Preferences {
                                        );
                }
 
-               global $wgMaxSigChars;
+               global $wgMaxSigChars, $wgOut, $wgParser;
+
+               // show a preview of the old signature first
+               $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title , $user, new ParserOptions );
+               $oldsigHTML = $wgOut->parseInline( $oldsigWikiText );
+               $defaultPreferences['oldsig'] =
+                       array(
+                                       'type' => 'info',
+                                       'raw' => true,
+                                       'label-message' => 'tog-oldsig',
+                                       'default' => $oldsigHTML,
+                                       'section' => 'personal/signature',
+                       );
                $defaultPreferences['nickname'] =
                                array(
                                        'type' => $wgAuth->allowPropChange( 'nickname' ) ? 'text' : 'info',
@@ -272,70 +320,74 @@ class Preferences {
                                array(
                                        'type' => 'toggle',
                                        'label-message' => 'tog-fancysig',
+                                       'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
                                        'section' => 'personal/signature'
                                );
-
+                               
                ## Email stuff
-               global $wgEmailConfirmToEdit;
-
-               $defaultPreferences['emailaddress'] =
-                               array(
-                                       'type' => $wgAuth->allowPropChange( 'emailaddress' ) ? 'text' : 'info',
-                                       'default' => $user->getEmail(),
-                                       'section' => 'personal/email',
-                                       'label-message' => 'youremail',
-                                       'help-message' => $wgEmailConfirmToEdit
-                                                                               ? 'prefs-help-email-required'
-                                                                               : 'prefs-help-email',
-                                       'validation-callback' => array( 'Preferences', 'validateEmail' ),
-                               );
-
-               global $wgEnableEmail, $wgEnableUserEmail, $wgEmailAuthentication;
-
-               $disableEmailPrefs = false;
-
-               if ( $wgEmailAuthentication ) {
-                       if ( $user->getEmail() ) {
-                               if( $user->getEmailAuthenticationTimestamp() ) {
-                                       // date and time are separate parameters to facilitate localisation.
-                                       // $time is kept for backward compat reasons.
-                                       // 'emailauthenticated' is also used in SpecialConfirmemail.php
-                                       $time = $wgLang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true );
-                                       $d = $wgLang->date( $user->getEmailAuthenticationTimestamp(), true );
-                                       $t = $wgLang->time( $user->getEmailAuthenticationTimestamp(), true );
-                                       $emailauthenticated = htmlspecialchars( wfMsg( 'emailauthenticated', $time, $d, $t ) ) . '<br />';
-                                       $disableEmailPrefs = false;
+               
+               global $wgEnableEmail;
+               if ($wgEnableEmail) {
+               
+                       global $wgEmailConfirmToEdit;
+       
+                       $defaultPreferences['emailaddress'] =
+                                       array(
+                                               'type' => $wgAuth->allowPropChange( 'emailaddress' ) ? 'email' : 'info',
+                                               'default' => $user->getEmail(),
+                                               'section' => 'personal/email',
+                                               'label-message' => 'youremail',
+                                               'help-message' => $wgEmailConfirmToEdit
+                                                                                       ? 'prefs-help-email-required'
+                                                                                       : 'prefs-help-email',
+                                               'validation-callback' => array( 'Preferences', 'validateEmail' ),
+                                       );
+       
+                       global $wgEnableUserEmail, $wgEmailAuthentication;
+       
+                       $disableEmailPrefs = false;
+       
+                       if ( $wgEmailAuthentication ) {
+                               if ( $user->getEmail() ) {
+                                       if( $user->getEmailAuthenticationTimestamp() ) {
+                                               // date and time are separate parameters to facilitate localisation.
+                                               // $time is kept for backward compat reasons.
+                                               // 'emailauthenticated' is also used in SpecialConfirmemail.php
+                                               $time = $wgLang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true );
+                                               $d = $wgLang->date( $user->getEmailAuthenticationTimestamp(), true );
+                                               $t = $wgLang->time( $user->getEmailAuthenticationTimestamp(), true );
+                                               $emailauthenticated = wfMsgExt( 'emailauthenticated', 'parseinline',
+                                                                                               array($time, $d, $t ) ) . '<br />';
+                                               $disableEmailPrefs = false;
+                                       } else {
+                                               $disableEmailPrefs = true;
+                                               $skin = $wgUser->getSkin();
+                                               $emailauthenticated = wfMsgExt( 'emailnotauthenticated', 'parseinline' ) . '<br />' .
+                                                       $skin->link(
+                                                               SpecialPage::getTitleFor( 'Confirmemail' ),
+                                                               wfMsg( 'emailconfirmlink' ),
+                                                               array(),
+                                                               array(),
+                                                               array( 'known', 'noclasses' )
+                                                       ) . '<br />';
+                                       }
                                } else {
                                        $disableEmailPrefs = true;
-                                       global $wgUser; // wgUser is okay here, it's for display
-                                       $skin = $wgUser->getSkin();
-                                       $emailauthenticated = wfMsgHtml( 'emailnotauthenticated' ) . '<br />' .
-                                               $skin->link(
-                                                       SpecialPage::getTitleFor( 'Confirmemail' ),
-                                                       wfMsg( 'emailconfirmlink' ),
-                                                       array(),
-                                                       array(),
-                                                       array( 'known', 'noclasses' )
-                                               ) . '<br />';
+                                       $emailauthenticated = wfMsgHtml( 'noemailprefs' );
                                }
-                       } else {
-                               $disableEmailPrefs = true;
-                               $emailauthenticated = wfMsgHtml( 'noemailprefs' );
+       
+                               $defaultPreferences['emailauthentication'] =
+                                               array(
+                                                       'type' => 'info',
+                                                       'raw' => true,
+                                                       'section' => 'personal/email',
+                                                       'label-message' => 'prefs-emailconfirm-label',
+                                                       'default' => $emailauthenticated,
+                                               );
+       
                        }
-
-                       $defaultPreferences['emailauthentication'] =
-                                       array(
-                                               'type' => 'info',
-                                               'raw' => true,
-                                               'section' => 'personal/email',
-                                               'label-message' => 'prefs-emailconfirm-label',
-                                               'default' => $emailauthenticated,
-                                       );
-
-               }
-
-               if( $wgEnableEmail ) {
-                       if( $wgEnableUserEmail ) {
+       
+                       if( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
                                $defaultPreferences['disablemail'] =
                                                array(
                                                        'type' => 'toggle',
@@ -352,28 +404,36 @@ class Preferences {
                                                        'disabled' => $disableEmailPrefs,
                                                );
                        }
-
-                       $defaultPreferences['enotifwatchlistpages'] =
-                                       array(
-                                               'type' => 'toggle',
-                                               'section' => 'personal/email',
-                                               'label-message' => 'tog-enotifwatchlistpages',
-                                               'disabled' => $disableEmailPrefs,
-                                       );
-                       $defaultPreferences['enotifusertalkpages'] =
-                                       array(
-                                               'type' => 'toggle',
-                                               'section' => 'personal/email',
-                                               'label-message' => 'tog-enotifusertalkpages',
-                                               'disabled' => $disableEmailPrefs,
-                                       );
-                       $defaultPreferences['enotifminoredits'] =
-                                       array(
-                                               'type' => 'toggle',
-                                               'section' => 'personal/email',
-                                               'label-message' => 'tog-enotifminoredits',
-                                               'disabled' => $disableEmailPrefs,
-                                       );
+                       
+                       global $wgEnotifWatchlist;
+                       if ( $wgEnotifWatchlist ) {
+                               $defaultPreferences['enotifwatchlistpages'] =
+                                               array(
+                                                       'type' => 'toggle',
+                                                       'section' => 'personal/email',
+                                                       'label-message' => 'tog-enotifwatchlistpages',
+                                                       'disabled' => $disableEmailPrefs,
+                                               );
+                       }
+                       global $wgEnotifUserTalk;
+                       if( $wgEnotifUserTalk ) {
+                               $defaultPreferences['enotifusertalkpages'] =
+                                               array(
+                                                       'type' => 'toggle',
+                                                       'section' => 'personal/email',
+                                                       'label-message' => 'tog-enotifusertalkpages',
+                                                       'disabled' => $disableEmailPrefs,
+                                               );
+                       }
+                       if( $wgEnotifUserTalk || $wgEnotifWatchlist ) {
+                               $defaultPreferences['enotifminoredits'] =
+                                               array(
+                                                       'type' => 'toggle',
+                                                       'section' => 'personal/email',
+                                                       'label-message' => 'tog-enotifminoredits',
+                                                       'disabled' => $disableEmailPrefs,
+                                               );
+                       }
                        $defaultPreferences['enotifrevealaddr'] =
                                        array(
                                                'type' => 'toggle',
@@ -497,6 +557,7 @@ class Preferences {
                                        'label-message' => 'timezonelegend',
                                        'options' => self::getTimezoneOptions(),
                                        'default' => $tzSetting,
+                                       'size' => 20,
                                        'section' => 'datetime/timeoffset',
                                );
        }
@@ -526,6 +587,7 @@ class Preferences {
                                        'type' => 'selectorother',
                                        'section' => 'rendering/advancedrendering',
                                        'options' => $stubThresholdOptions,
+                                       'size' => 20,
                                        'label' => wfMsg( 'stub-threshold' ), // Raw HTML message. Yay?
                                );
                $defaultPreferences['highlightbroken'] =
@@ -592,6 +654,19 @@ class Preferences {
                                        'min' => 4,
                                        'max' => 1000,
                                );
+
+               $defaultPreferences['editfont'] =
+                               array(
+                                       'type' => 'select',
+                                       'section' => 'editing/advancedediting',
+                                       'label-message' => 'editfont-style',
+                                       'options' => array(
+                                               wfMsg( 'editfont-default' ) => 'default',
+                                               wfMsg( 'editfont-monospace' ) => 'monospace',
+                                               wfMsg( 'editfont-sansserif' ) => 'sans-serif',
+                                               wfMsg( 'editfont-serif' ) => 'serif',
+                                       )
+                               );
                $defaultPreferences['previewontop'] =
                                array(
                                        'type' => 'toggle',
@@ -673,15 +748,16 @@ class Preferences {
        }
 
        static function rcPreferences( $user, &$defaultPreferences ) {
-               global $wgRCMaxAge, $wgUseRCPatrol;
+               global $wgRCMaxAge, $wgUseRCPatrol, $wgLang;
                ## RecentChanges #####################################
                $defaultPreferences['rcdays'] =
                                array(
-                                       'type' => 'int',
+                                       'type' => 'float',
                                        'label-message' => 'recentchangesdays',
                                        'section' => 'rc/display',
                                        'min' => 1,
                                        'max' => ceil( $wgRCMaxAge / ( 3600*24 ) ),
+                                       'help' => wfMsgExt( 'recentchangesdays-max', array( 'parsemag' ), $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600*24 ) ) ) ),
                                );
                $defaultPreferences['rclimit'] =
                                array(
@@ -703,7 +779,6 @@ class Preferences {
                                        'section' => 'rc/advancedrc',
                                );
 
-               global $wgUseRCPatrol;
                if( $wgUseRCPatrol ) {
                        $defaultPreferences['hidepatrolled'] =
                                        array(
@@ -731,14 +806,15 @@ class Preferences {
        }
 
        static function watchlistPreferences( $user, &$defaultPreferences ) {
-               global $wgUseRCPatrol;
+               global $wgUseRCPatrol, $wgEnableAPI;
                ## Watchlist #####################################
                $defaultPreferences['watchlistdays'] =
                                array(
-                                       'type' => 'int',
+                                       'type' => 'float',
                                        'min' => 0,
                                        'max' => 7,
                                        'section' => 'watchlist/display',
+                                       'help' => wfMsgHtml( 'prefs-watchlist-days-max' ),
                                        'label-message' => 'prefs-watchlist-days',
                                );
                $defaultPreferences['wllimit'] =
@@ -747,7 +823,8 @@ class Preferences {
                                        'min' => 0,
                                        'max' => 1000,
                                        'label-message' => 'prefs-watchlist-edits',
-                                       'section' => 'watchlist/display'
+                                       'help' => wfMsgHtml( 'prefs-watchlist-edits-max' ),
+                                       'section' => 'watchlist/display',
                                );
                $defaultPreferences['extendwatchlist'] =
                                array(
@@ -785,6 +862,17 @@ class Preferences {
                                        'section' => 'watchlist/advancedwatchlist',
                                        'label-message' => 'tog-watchlisthideliu',
                                );
+               if ( $wgEnableAPI ) {
+                       # Some random gibberish as a proposed default
+                       $hash = sha1( mt_rand() . microtime( true ) );
+                       $defaultPreferences['watchlisttoken'] =
+                                       array(
+                                               'type' => 'text',
+                                               'section' => 'watchlist/advancedwatchlist',
+                                               'label-message' => 'prefs-watchlist-token',
+                                               'help' => wfMsgHtml( 'prefs-help-watchlist-token', $hash )
+                                       );
+               }
 
                if ( $wgUseRCPatrol ) {
                        $defaultPreferences['watchlisthidepatrolled'] =
@@ -913,8 +1001,12 @@ class Preferences {
                }
        }
 
+       /**
+        * @param object $user The user object
+        * @return array Text/links to display as key; $skinkey as value
+        */
        static function generateSkinOptions( $user ) {
-               global $wgDefaultSkin;
+               global $wgDefaultSkin, $wgLang, $wgAllowUserCss, $wgAllowUserJs;
                $ret = array();
 
                $mptitle = Title::newMainPage();
@@ -935,23 +1027,28 @@ class Preferences {
                $sk = $user->getSkin();
 
                foreach( $validSkinNames as $skinkey => $sn ) {
+                       $linkTools = array();
+
+                       # Mark the default skin
+                       if( $skinkey == $wgDefaultSkin ) {
+                               $linkTools[] = wfMsgHtml( 'default' );
+                       }
+
+                       # Create preview link
                        $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
-                       $previewlink = "(<a target='_blank' href=\"$mplink\">$previewtext</a>)";
-                       $extraLinks = '';
-                       global $wgAllowUserCss, $wgAllowUserJs;
+                       $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
+
+                       # Create links to user CSS/JS pages
                        if( $wgAllowUserCss ) {
                                $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
-                               $customCSS = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
-                               $extraLinks .= " ($customCSS)";
+                               $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
                        }
                        if( $wgAllowUserJs ) {
                                $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
-                               $customJS = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
-                               $extraLinks .= " ($customJS)";
+                               $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
                        }
-                       if( $skinkey == $wgDefaultSkin )
-                               $sn .= ' (' . wfMsgHtml( 'default' ) . ')';
-                       $display = "$sn $previewlink{$extraLinks}";
+
+                       $display = $sn . ' ' . wfMsg( 'parentheses', $wgLang->pipeList( $linkTools ) );
                        $ret[$display] = $skinkey;
                }
 
@@ -971,7 +1068,7 @@ class Preferences {
                        }
 
                        $idCnt = 0;
-                       $epoch = '20010115161234'; # Wikipedia day
+                       $epoch = wfTimestampNow();
                        foreach( $dateopts as $key ) {
                                if( $key == 'default' ) {
                                        $formatted = wfMsgHtml( 'datedefault' );