Yurik has uploaded a new change for review.
https://gerrit.wikimedia.org/r/163897
Change subject: Enabled parameter now allows enabled range
......................................................................
Enabled parameter now allows enabled range
Enabled can now be an array of two elements,
each being either a date or an integer.
If first is an integer, represents number of hours since now
If second is an integer, represents the duration (in hours)
Change-Id: I23839878a376e73324384b042e809d668f954308
---
M i18n/en.json
M i18n/qqq.json
M includes/ZeroConfig.php
3 files changed, 85 insertions(+), 3 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroBanner
refs/changes/97/163897/1
diff --git a/i18n/en.json b/i18n/en.json
index de212ca..b4d27b6 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -43,6 +43,7 @@
"zero-config-ipsets": "Parameter \"$1\" must be the name of an existing
ip set declared in \"ipsets\" field",
"zero-config-admins": "Parameter \"$1\" must be an existing user
account",
"zero-config-proxies": "Parameter \"$1\" must be a list of proxies
supporting zero-rating for this carrier",
+ "zero-config-enabled": "Parameter \"$1\" must be either true, false, or
a list with two timestamps. Timestamp could be in any time format. First array
can also be a number of hours after (before) now, and second value can be the
duration in hours.",
"group-zeroadmin": "Zero administrators",
"group-zeroadmin-member": "{{GENDER:$1|Zero administrator}}",
"grouppage-zeroadmin": "{{ns:project}}:Zero administrators",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index f8e90d0..0d1cc03 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -59,6 +59,7 @@
"zero-config-ipsets": "ipsets parameter validation error.
Parameters:\n* $1 - field name\n{{Related|Zeroconfig}}",
"zero-config-admins": "Admins parameter validation error.
Parameters:\n* $1 - field name\n{{Related|Zeroconfig}}",
"zero-config-proxies": "Proxies parameter validation error.
Parameters:\n* $1 - field name\n{{Related|Zeroconfig}}",
+ "zero-config-enabled": "Enabled parameter validation error.
Parameters:\n* $1 - field name\n{{Related|Zeroconfig}}",
"group-zeroadmin": "{{doc-group|zeroadmin}}",
"group-zeroadmin-member": "{{doc-group|zeroadmin|member}}",
"grouppage-zeroadmin": "{{doc-group|zeroadmin|page}}",
diff --git a/includes/ZeroConfig.php b/includes/ZeroConfig.php
index 5801e43..caada8c 100644
--- a/includes/ZeroConfig.php
+++ b/includes/ZeroConfig.php
@@ -1,11 +1,16 @@
<?php
namespace ZeroBanner;
+use DateInterval;
+use DateTime;
+use DateTimeZone;
use JsonConfig\JCObjContent;
use JsonConfig\JCUtils;
use JsonConfig\JCValidators;
use JsonConfig\JCValue;
+use MWTimestamp;
use stdClass;
+use TimestampException;
/**
* JSON Zero Config
@@ -25,6 +30,7 @@
private $mode = null;
private $ipset = null;
private $proxy = null;
+ private $enabledValue = false;
const ignoreNetwork = 0x1;
const ignoreDisabled = 0x2;
@@ -100,7 +106,7 @@
}
public function enabled() {
- return $this->config->enabled &&
$this->getDataWithDefaults()->enabled;
+ return $this->enabledValue &&
$this->getDataWithDefaults()->enabled;
}
public function name() {
@@ -205,7 +211,7 @@
// Optional comment
$this->testOptional( 'comment', '', $isString );
// Config is enabled
- $this->testOptional( 'enabled', true, $isBool );
+ $this->testOptional( 'enabled', true,
$this->getEnabledValidator() );
// Map of localized partner names
$this->test( 'name', self::getLangToStrValidator() );
// Map of localized banner texts with {{PARTNER}} placeholder
@@ -322,6 +328,76 @@
$this->testOptional( 'testInfoScreen', false, $isBool );
}
+ private function getEnabledValidator() {
+ $self = $this;
+ return function ( JCValue $jcv, array $path ) use ( $self ) {
+ if ( $jcv->isMissing() ) {
+ $jcv->setValue( true );
+ $self->setEnabled( true );
+ return true;
+ }
+ $v = $jcv->getValue();
+ if ( is_bool( $v ) ) {
+ $self->setEnabled( $v );
+ return true;
+ }
+ if ( JCUtils::isList( $v ) && count( $v ) === 2 ) {
+ /** @var DateTime[] $range */
+ $range = array();
+ $now = null;
+ foreach ( $v as $time ) {
+ if ( is_int( $time ) ) {
+ // first digit - relative to
now, second digit - relative to the starting time
+ if ( count( $range ) === 0 ) {
+ $t = new DateTime(
'now', new DateTimeZone( 'GMT' ) );
+ $interval = new
DateInterval( 'PT' . $t->format("i") . 'M' . $t->format("s") . 'S' );
+ $interval->invert = 1;
+ $t->add( $interval );
+ } else {
+ $t = clone $range[0];
+ }
+ if ( $time !== 0 ) {
+ $interval = new
DateInterval( 'PT' . abs( $time ) . 'H' );
+ if ( $time < 0 ) {
+
$interval->invert = 1;
+ }
+ $t->add( $interval );
+ }
+ $range[] = $t;
+ } elseif ( is_string( $time ) ) {
+ try {
+ $t = new MWTimestamp(
$time );
+ $range[] =
$t->timestamp;
+ } catch ( TimestampException $e
) {
+ $range = null;
+ break;
+ }
+ } else {
+ $range = null;
+ break;
+ }
+ }
+ if ( $range ) {
+ $from = $range[0];
+ $until = $range[1];
+ $diff = $until->diff( $from );
+ if ( $diff->days >= 0 || $diff->days <
7 ) {
+ // TODO: we should probably
reuse MWDateTime object here once it accepts DateTime
+ $jcv->setValue( array(
+ $from->format(
'Y-m-d\TH:i:s\Z' ),
+ $until->format(
'Y-m-d\TH:i:s\Z' )
+ ) );
+ $now = new DateTime( 'now', new
DateTimeZone( 'UTC' ) );
+ $self->setEnabled( $now > $from
&& $now < $until );
+ return true;
+ }
+ }
+ }
+ $jcv->error( 'zero-config-enabled', $path );
+ return false;
+ };
+ }
+
private static function getLangToStrValidator() {
return function ( JCValue $jcv, array $path ) {
if ( $jcv->isMissing() ) {
@@ -351,7 +427,7 @@
return true;
}
}
- $jcv->error( "zero-config-" . end( $path ), $path );
+ $jcv->error( 'zero-config-' . end( $path ), $path );
return false;
};
}
@@ -597,4 +673,8 @@
return true;
};
}
+
+ private function setEnabled( $value ) {
+ $this->enabledValue = $value;
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/163897
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I23839878a376e73324384b042e809d668f954308
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ZeroBanner
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits