Samwilson has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/368984 )
Change subject: Update coding standards ...................................................................... Update coding standards This doesn't introduce any code changes, just some documentation and code formatting, in order to pass phpcs. Change-Id: I6772823a8b47b38c6741d8a58b9a94571bdaafed --- M .gitignore M GlobalPreferences.body.php M GlobalPreferences.hooks.php M GlobalPreferences.php M SpecialGlobalPreferences.php A composer.json A phpcs.xml 7 files changed, 114 insertions(+), 50 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GlobalPreferences refs/changes/84/368984/1 diff --git a/.gitignore b/.gitignore index 3c3629e..8ec4b92 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules +node_modules/ +vendor/ +composer.lock diff --git a/GlobalPreferences.body.php b/GlobalPreferences.body.php index aa7fc53..d681ac5 100644 --- a/GlobalPreferences.body.php +++ b/GlobalPreferences.body.php @@ -19,7 +19,7 @@ public static function getPrefsDB( $type = DB_SLAVE ) { global $wgGlobalPreferencesDB; if ( $wgGlobalPreferencesDB ) { - return wfGetDB( $type, array(), $wgGlobalPreferencesDB ); + return wfGetDB( $type, [], $wgGlobalPreferencesDB ); } else { return wfGetDB( $type ); } @@ -27,7 +27,7 @@ /** * Checks if the user is globalized - * @param User $user + * @param User $user The user * @return bool */ public static function isUserGlobalized( User $user ) { @@ -42,7 +42,7 @@ /** * Gets the user's ID that we're using in the table * Returns 0 if the user is not global - * @param User $user + * @param User $user The user for whom to get the ID. * @return int */ public static function getUserID( User $user ) { @@ -53,37 +53,36 @@ /** * Deletes all of a user's global prefs * Assumes that the user is globalized - * @param User $user + * @param User $user The user. */ public static function resetGlobalUserSettings( User $user ) { if ( !isset( $user->mGlobalPrefs ) ) { - $user->getOption( '' ); // Trigger loading + // Triggers User::loadOptions. + $user->getOption( '' ); } if ( count( $user->mGlobalPrefs ) ) { self::getPrefsDB( DB_MASTER )->delete( 'global_preferences', - array( 'gp_user' => self::getUserID( $user ) ), + [ 'gp_user' => self::getUserID( $user ) ], __METHOD__ ); } } /** - * Convenience function to check if we're on the global - * prefs page - * @param IContextSource $context + * Convenience function to check if we're on the global prefs page. + * @param IContextSource $context The context to use; if not set main request context is used. * @return bool */ public static function onGlobalPrefsPage( $context = null ) { $context = $context ?: RequestContext::getMain(); - return $context->getTitle() - && $context->getTitle()->isSpecial( 'GlobalPreferences' ); + return $context->getTitle() && $context->getTitle()->isSpecial( 'GlobalPreferences' ); } /** * Convenience function to check if we're on the local * prefs page - * @param IContextSource $context + * @param IContextSource $context The context to use; if not set main request context is used. * @return bool */ public static function onLocalPrefsPage( $context = null ) { diff --git a/GlobalPreferences.hooks.php b/GlobalPreferences.hooks.php index 23b4fea..fecc178 100644 --- a/GlobalPreferences.hooks.php +++ b/GlobalPreferences.hooks.php @@ -7,23 +7,27 @@ * Special:GlobalPrefs * @var array */ - static $badPrefs = array( - 'realname', // Stored in user table, doesn't work yet - 'userid', // @todo Show CA user id / shared user table id? - 'usergroups', // @todo Show CA global groups instead? - 'editcount', // @todo Should global edit count instead? + protected static $badPrefs = [ + // Stored in user table, doesn't work yet + 'realname', + // @todo Show CA user id / shared user table id? + 'userid', + // @todo Show CA global groups instead? + 'usergroups', + // @todo Should global edit count instead? + 'editcount', 'registrationdate', - ); + ]; /** * Preference types that we should not add a checkbox for * @var array */ - static $badTypes = array( + protected static $badTypes = [ 'info', 'hidden', 'api', - ); + ]; /** * @FIXME This is terrible @@ -36,26 +40,28 @@ /** * Load our global prefs - * @param User $user - * @param array $options + * @link https://www.mediawiki.org/wiki/Manual:Hooks/UserLoadOptions + * @param User $user The user for whom options are being loaded. + * @param array &$options The user's options; can be modified. * @return bool */ public static function onUserLoadOptions( User $user, &$options ) { $id = GlobalPreferences::getUserID( $user ); - if ( !$id ) { // Not a global user :( + if ( !$id ) { + // Not a global user. return true; } $dbr = GlobalPreferences::getPrefsDB( DB_SLAVE ); $res = $dbr->select( 'global_preferences', - array( 'gp_property', 'gp_value' ), - array( 'gp_user' => $id ), + [ 'gp_property', 'gp_value' ], + [ 'gp_user' => $id ], __METHOD__ ); - $user->mGlobalPrefs = array(); - $user->mLocalPrefs = array(); + $user->mGlobalPrefs = []; + $user->mLocalPrefs = []; foreach ( $res as $row ) { if ( isset( $user->mOptions[$row->gp_property] ) ) { @@ -71,8 +77,9 @@ /** * Don't save global prefs - * @param User $user - * @param $options + * @link https://www.mediawiki.org/wiki/Manual:Hooks/UserSaveOptions + * @param User $user The user for whom options are being saved. + * @param array &$options The user's options; can be modified. * @return bool */ public static function onUserSaveOptions( User $user, &$options ) { @@ -95,14 +102,27 @@ return true; } - public static function onPreferencesFormPreSave( array $formData, PreferencesForm $form, User $user, &$result ) { + /** + * @link https://www.mediawiki.org/wiki/Manual:Hooks/PreferencesFormPreSave + * @param array $formData An associative array containing the data from the preferences form. + * @param PreferencesForm $form The PreferencesForm object that represents the preferences form. + * @param User $user The User object that can be used to change the user's preferences. + * @param array &$result The boolean return value of the Preferences::tryFormSubmit method. + * @return bool + */ + public static function onPreferencesFormPreSave( + array $formData, + PreferencesForm $form, + User $user, + &$result + ) { if ( !GlobalPreferences::onGlobalPrefsPage( $form ) ) { // Don't interfere with local preferences return true; } - $rows = array(); - $prefs = array(); + $rows = []; + $prefs = []; foreach ( $formData as $name => $value ) { if ( substr( $name, -strlen( 'global' ) ) === 'global' && $value === true ) { $realName = substr( $name, 0, -strlen( '-global' ) ); @@ -120,11 +140,11 @@ $id = GlobalPreferences::getUserID( $user ); foreach ( $prefs as $prop => $value ) { - $rows[] = array( + $rows[] = [ 'gp_user' => $id, 'gp_property' => $prop, 'gp_value' => $value, - ); + ]; } @@ -134,7 +154,7 @@ $dbw = GlobalPreferences::getPrefsDB( DB_MASTER ); $dbw->replace( 'global_preferences', - array( 'gp_user', 'gp_property' ), + [ 'gp_user', 'gp_property' ], $rows, __METHOD__ ); @@ -143,6 +163,11 @@ return false; } + /** + * @link https://www.mediawiki.org/wiki/Manual:Hooks/LoadExtensionSchemaUpdates + * @param DatabaseUpdater $updater The database updater. + * @return bool + */ public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) { global $wgGlobalPreferencesDB; if ( is_null( $wgGlobalPreferencesDB ) || $wgGlobalPreferencesDB === wfWikiID() ) { @@ -153,6 +178,12 @@ return true; } + /** + * @link https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences + * @param User $user User whose preferences are being modified. + * @param array &$prefs Preferences description array, to be fed to an HTMLForm object. + * @return bool + */ public static function onGetPreferences( User $user, &$prefs ) { if ( !GlobalPreferences::isUserGlobalized( $user ) ) { return true; @@ -160,8 +191,8 @@ if ( GlobalPreferences::onGlobalPrefsPage() ) { if ( !isset( $user->mGlobalPrefs ) ) { - // Just in case the user hasn't been loaded yet. - $user->getOption(''); // Triggers User::loadOptions + // Just in case the user hasn't been loaded yet. Triggers User::loadOptions. + $user->getOption( '' ); } foreach ( $prefs as $name => $info ) { // FIXME: This whole code section sucks @@ -171,23 +202,23 @@ && !in_array( $info['type'], self::$badTypes ) && !in_array( $name, self::$badPrefs ) ) { - $prefs = wfArrayInsertAfter( $prefs, array( - "$name-global" => array( + $prefs = wfArrayInsertAfter( $prefs, [ + "$name-global" => [ 'type' => 'toggle', 'label-message' => 'globalprefs-check-label', 'default' => in_array( $name, $user->mGlobalPrefs ), 'section' => $info['section'], 'cssclass' => 'mw-globalprefs-global-check', - ) - ), $name ); + ] + ], $name ); } elseif ( in_array( $name, self::$badPrefs ) ) { $prefs[$name]['type'] = 'hidden'; } } } elseif ( GlobalPreferences::onLocalPrefsPage() ) { if ( !isset( $user->mGlobalPrefs ) ) { - // Just in case the user hasn't been loaded yet. - $user->getOption(''); // Triggers User::loadOptions + // Just in case the user hasn't been loaded yet. Triggers User::loadOptions. + $user->getOption( '' ); } foreach ( $user->mGlobalPrefs as $name ) { if ( isset( $prefs[$name] ) ) { @@ -211,7 +242,7 @@ // Provide a link to Special:GlobalPreferences // if we're not on that page. if ( !GlobalPreferences::onGlobalPrefsPage() ) { - $prefs['global-info'] = array( + $prefs['global-info'] = [ 'type' => 'info', 'section' => 'personal/info', 'label-message' => 'globalprefs-info-label', @@ -220,7 +251,7 @@ SpecialPage::getTitleFor( 'GlobalPreferences' ), wfMessage( 'globalprefs-info-link' )->escaped() ), - ); + ]; } return true; diff --git a/GlobalPreferences.php b/GlobalPreferences.php index f8a5505..b1a1a0e 100644 --- a/GlobalPreferences.php +++ b/GlobalPreferences.php @@ -18,10 +18,11 @@ $wgMessagesDirs['GlobalPreferences'] = __DIR__ . '/i18n'; $wgExtensionMessagesFiles['GlobalPreferencesAlias'] = __DIR__ . '/GlobalPreferences.alias.php'; wfWarn( - 'Deprecated PHP entry point used for GlobalPreferences extension. Please use wfLoadExtension instead, ' . - 'see https://www.mediawiki.org/wiki/Extension_registration for more details.' + 'Deprecated PHP entry point used for GlobalPreferences extension. ' + . 'Please use wfLoadExtension instead, ' + . 'see https://www.mediawiki.org/wiki/Extension_registration for more details.' ); return true; } else { die( 'This version of the GlobalPreferences extension requires MediaWiki 1.25+' ); -} \ No newline at end of file +} diff --git a/SpecialGlobalPreferences.php b/SpecialGlobalPreferences.php index ac0ab5e..33ef788 100644 --- a/SpecialGlobalPreferences.php +++ b/SpecialGlobalPreferences.php @@ -5,6 +5,12 @@ SpecialPage::__construct( 'GlobalPreferences' ); } + /** + * Execute the special page. + * @param null|string $par The subpage name, if any. + * @throws ErrorPageError + * @throws UserNotLoggedIn + */ public function execute( $par ) { // Dirty override to check user can set global prefs. if ( $this->getUser()->isAnon() ) { @@ -23,6 +29,12 @@ parent::execute( $par ); } + /** + * Handle reset submission (subpage '/reset'). + * @param string[] $formData The submitted data (not used). + * @return bool + * @throws PermissionsError + */ public function submitReset( $formData ) { // TODO: Should we have our own userright here? if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) { @@ -38,4 +50,4 @@ return true; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..266d587 --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "require-dev": { + "jakub-onderka/php-parallel-lint": "^0.9", + "mediawiki/mediawiki-codesniffer": "^0.10", + "jakub-onderka/php-console-highlighter": "^0.3" + }, + "scripts": { + "fix": "phpcbf", + "test": [ + "parallel-lint . --exclude vendor", + "phpcs -p -s" + ] + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..14a0ffd --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,5 @@ +<?xml version="1.0"?> +<ruleset> + <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" /> + <file>.</file> +</ruleset> -- To view, visit https://gerrit.wikimedia.org/r/368984 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6772823a8b47b38c6741d8a58b9a94571bdaafed Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/GlobalPreferences Gerrit-Branch: master Gerrit-Owner: Samwilson <s...@samwilson.id.au> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits