MarkTraceur has submitted this change and it was merged.
Change subject: Add convenience functions, fix auto-enroll
......................................................................
Add convenience functions, fix auto-enroll
Adds a function that will check whether a user is enrolled in a
particular feature experiment.
Also changes the structure of the auto-enroll system so it works a
little more sanely. A little.
And introduced a concept of required fields for features...but not
using it yet, since I've erased the only required field from existence
since writing that bit.
Change-Id: Icfe6ad42d6d8c1a4c6ac00cf8bdbe9f8a0604a53
---
M BetaFeatures.php
M BetaFeaturesHooks.php
M SpecialBetaFeatures.php
A includes/BetaFeaturesUtil.php
M includes/HTMLBetaFeatureField.php
5 files changed, 105 insertions(+), 4 deletions(-)
Approvals:
BryanDavis: Looks good to me, approved
MarkTraceur: Verified
diff --git a/BetaFeatures.php b/BetaFeatures.php
index 27b6572..ad0bbbd 100644
--- a/BetaFeatures.php
+++ b/BetaFeatures.php
@@ -21,9 +21,10 @@
* @copyright Copyright © 2013, Mark Holmquist
*/
-$wgAutoloadClasses['HTMLBetaFeatureField'] = __DIR__ .
'/includes/HTMLBetaFeatureField.php';
+$wgAutoloadClasses['HTMLFeatureField'] = __DIR__ .
'/includes/HTMLBetaFeatureField.php';
$wgAutoloadClasses['BetaFeaturesHooks'] = __DIR__ . '/BetaFeaturesHooks.php';
$wgAutoloadClasses['SpecialBetaFeatures'] = __DIR__ .
'/SpecialBetaFeatures.php';
+$wgAutoloadClasses['BetaFeatures'] = __DIR__ .
'/includes/BetaFeaturesUtil.php';
$wgExtensionMessagesFiles['BetaFeatures'] = __DIR__ . '/BetaFeatures.i18n.php';
$wgExtensionMessagesFiles[ 'BetaFeaturesAlias' ] = __DIR__ .
'/BetaFeatures.alias.php';
diff --git a/BetaFeaturesHooks.php b/BetaFeaturesHooks.php
index 57b9347..de5c88e 100644
--- a/BetaFeaturesHooks.php
+++ b/BetaFeaturesHooks.php
@@ -37,9 +37,53 @@
$opt['label-message'] = $info['label-message'];
}
- $prefs['beta-feature-' . $key] = $opt;
+ // We now have a concept of required fields, but we
don't
+ // actually require any. (removed version)
+ $requiredFields = array(
+ );
+
+ $complete = true;
+
+ foreach ( $requiredFields as $field => $required ) {
+ if ( array_key_exists( $field, $info ) ) {
+ $opt[$field] = $info[$field];
+ } elseif ( $required ) {
+ // A required field isn't present in
the info array
+ // we got from the
GetBetaFeaturePreferences hook.
+ // Don't add this feature to the form.
+ $complete = false;
+ break;
+ }
+ }
+
+ if ( $complete ) {
+ $prefname = 'beta-feature-' . $key;
+
+ $prefs[$prefname] = $opt;
+
+ $currentValue = $user->getOption( $prefname );
+ if ( $currentValue !==
HTMLFeatureField::OPTION_ENABLED &&
+ $currentValue !==
HTMLFeatureField::OPTION_DISABLED &&
+ $user->getOption(
'beta-feature-auto-enroll' ) === HTMLFeatureField::OPTION_ENABLED ) {
+ // We haven't seen this before, and the
user has auto-enroll enabled!
+ // Set the option to true.
+ $user->setOption( $prefname,
HTMLFeatureField::OPTION_ENABLED );
+ }
+ }
}
return true;
}
+
+ static function getAutoEnrollPreference( $user, &$prefs ) {
+ global $wgExtensionAssetsPath;
+
+ $prefs['auto-enroll'] = array(
+ 'label-message' => 'betafeatures-auto-enroll',
+ 'desc-message' => 'betafeatures-auto-enroll-desc',
+ 'info-link' =>
'https://mediawiki.org/wiki/Extension:BetaFeatures/Auto-enrollment',
+ 'discussion-link' =>
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures/Auto-enrollment',
+ 'screenshot' => $wgExtensionAssetsPath .
'/BetaFeatures/images/all-beta.png',
+ );
+ }
}
diff --git a/SpecialBetaFeatures.php b/SpecialBetaFeatures.php
index f513562..bb58c4f 100644
--- a/SpecialBetaFeatures.php
+++ b/SpecialBetaFeatures.php
@@ -60,7 +60,7 @@
foreach ( $betaOpts as $label => $opt ) {
$formFields[$label] = array(
'label' => $this->msg( $opt['label-message']
)->escaped(),
- 'class' => 'HTMLBetaFeatureField',
+ 'class' => 'HTMLFeatureField',
'id' => 'checkbox-for-' . $label,
'default' => $user->getOption( 'beta-feature-'
. $label ),
);
diff --git a/includes/BetaFeaturesUtil.php b/includes/BetaFeaturesUtil.php
new file mode 100644
index 0000000..5c5d169
--- /dev/null
+++ b/includes/BetaFeaturesUtil.php
@@ -0,0 +1,36 @@
+<?php
+/*
+ * This file is part of the MediaWiki extension BetaFeatures.
+ *
+ * BetaFeatures is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * BetaFeatures is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with BetaFeatures. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @file
+ * @ingroup extensions
+ * @author Mark Holmquist <[email protected]>
+ * @copyright Copyright © 2013, Mark Holmquist
+ */
+
+class BetaFeatures {
+
+ /**
+ * Check if a user has a beta feature enabled.
+ *
+ * @param $user User The user to check
+ * @param $feature string The key passed back to BetaFeatures from the
GetBetaFeaturePreferences hook
+ * @return bool
+ */
+ static function isFeatureEnabled( $user, $feature ) {
+ return $user->getOption( 'beta-feature-' . $feature ) === 1;
+ }
+}
diff --git a/includes/HTMLBetaFeatureField.php
b/includes/HTMLBetaFeatureField.php
index 9873ccb..a4d73a8 100644
--- a/includes/HTMLBetaFeatureField.php
+++ b/includes/HTMLBetaFeatureField.php
@@ -18,7 +18,10 @@
// This is sort of a forward-looking class, it doesn't do much yet, but we
// intend to have the label be more sexy in the future.
-class HTMLBetaFeatureField extends HTMLCheckField {
+class HTMLFeatureField extends HTMLCheckField {
+
+ const OPTION_DISABLED = '0';
+ const OPTION_ENABLED = '1';
function getInputHTML( $value ) {
global $wgExtensionAssetsPath;
@@ -78,4 +81,21 @@
return implode( '', $htmls );
}
+
+ /**
+ * Override to use integers, so we don't lose the database rows on
+ * unset...
+ */
+ function loadDataFromRequest( $request ) {
+ $res = parent::loadDataFromRequest( $request );
+
+ if ( $res === true ) {
+ return HTMLFeatureField::OPTION_ENABLED;
+ } else if ( $res === false ) {
+ return HTMLFeatureField::OPTION_DISABLED;
+ } else {
+ // Dunno what happened, but I'm not gonna fight it.
+ return $res;
+ }
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/76210
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Icfe6ad42d6d8c1a4c6ac00cf8bdbe9f8a0604a53
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/BetaFeatures
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>
Gerrit-Reviewer: Brian Wolff <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits