Example #1
0
 /**
  * Get the expiry time for a given action, by combining the relevant inputs.
  *
  * @param string $action
  *
  * @return string 14-char timestamp or "infinity", or false if the input was invalid
  */
 function getExpiry($action)
 {
     if ($this->mExpirySelection[$action] == 'existing') {
         return $this->mExistingExpiry[$action];
     } elseif ($this->mExpirySelection[$action] == 'othertime') {
         $value = $this->mExpiry[$action];
     } else {
         $value = $this->mExpirySelection[$action];
     }
     if (wfIsInfinity($value)) {
         $time = 'infinity';
     } else {
         $unix = strtotime($value);
         if (!$unix || $unix === -1) {
             return false;
         }
         // @todo FIXME: Non-qualified absolute times are not in users specified timezone
         // and there isn't notice about it in the ui
         $time = wfTimestamp(TS_MW, $unix);
     }
     return $time;
 }
Example #2
0
 /**
  * @todo Maybe translate block durations.  Note that this function is somewhat misnamed: it
  * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
  * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
  * on old expiry lengths recorded in log entries. You'd need to provide the start date to
  * match up with it.
  *
  * @param string $str The validated block duration in English
  * @return string Somehow translated block duration
  * @see LanguageFi.php for example implementation
  */
 function translateBlockExpiry($str)
 {
     $duration = SpecialBlock::getSuggestedDurations($this);
     foreach ($duration as $show => $value) {
         if (strcmp($str, $value) == 0) {
             return htmlspecialchars(trim($show));
         }
     }
     if (wfIsInfinity($str)) {
         foreach ($duration as $show => $value) {
             if (wfIsInfinity($value)) {
                 return htmlspecialchars(trim($show));
             }
         }
     }
     // If all else fails, return a standard duration or timestamp description.
     $time = strtotime($str, 0);
     if ($time === false) {
         // Unknown format. Return it as-is in case.
         return $str;
     } elseif ($time !== strtotime($str, 1)) {
         // It's a relative timestamp.
         // $time is relative to 0 so it's a duration length.
         return $this->formatDuration($time);
     } else {
         // It's an absolute timestamp.
         if ($time === 0) {
             // wfTimestamp() handles 0 as current time instead of epoch.
             return $this->timeanddate('19700101000000');
         } else {
             return $this->timeanddate($time);
         }
     }
 }
 /**
  * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
  * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
  * @param string $expiry Whatever was typed into the form
  * @return string Timestamp or 'infinity'
  */
 public static function parseExpiryInput($expiry)
 {
     if (wfIsInfinity($expiry)) {
         $expiry = 'infinity';
     } else {
         $expiry = strtotime($expiry);
         if ($expiry < 0 || $expiry === false) {
             return false;
         }
         $expiry = wfTimestamp(TS_MW, $expiry);
     }
     return $expiry;
 }
 protected function getParametersForApi()
 {
     $entry = $this->entry;
     $params = $entry->getParameters();
     static $map = array('5::duration', '6:array:flags', '6::flags' => '6:array:flags');
     foreach ($map as $index => $key) {
         if (isset($params[$index])) {
             $params[$key] = $params[$index];
             unset($params[$index]);
         }
     }
     $subtype = $entry->getSubtype();
     if ($subtype === 'block' || $subtype === 'reblock') {
         // Defaults for old log entries missing some fields
         $params += array('5::duration' => 'infinite', '6:array:flags' => array());
         if (!is_array($params['6:array:flags'])) {
             $params['6:array:flags'] = $params['6:array:flags'] === '' ? array() : explode(',', $params['6:array:flags']);
         }
         if (!wfIsInfinity($params['5::duration'])) {
             $ts = wfTimestamp(TS_UNIX, $entry->getTimestamp());
             $expiry = strtotime($params['5::duration'], $ts);
             if ($expiry !== false && $expiry > 0) {
                 $params[':timestamp:expiry'] = $expiry;
             }
         }
     }
     return $params;
 }
Example #5
0
 public function execute()
 {
     global $wgContLang;
     $params = $this->extractRequestParams();
     $pageObj = $this->getTitleOrPageId($params, 'fromdbmaster');
     $titleObj = $pageObj->getTitle();
     $errors = $titleObj->getUserPermissionsErrors('protect', $this->getUser());
     if ($errors) {
         // We don't care about multiple errors, just report one of them
         $this->dieUsageMsg(reset($errors));
     }
     $expiry = (array) $params['expiry'];
     if (count($expiry) != count($params['protections'])) {
         if (count($expiry) == 1) {
             $expiry = array_fill(0, count($params['protections']), $expiry[0]);
         } else {
             $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections'])));
         }
     }
     $restrictionTypes = $titleObj->getRestrictionTypes();
     $db = $this->getDB();
     $protections = array();
     $expiryarray = array();
     $resultProtections = array();
     foreach ($params['protections'] as $i => $prot) {
         $p = explode('=', $prot);
         $protections[$p[0]] = $p[1] == 'all' ? '' : $p[1];
         if ($titleObj->exists() && $p[0] == 'create') {
             $this->dieUsageMsg('create-titleexists');
         }
         if (!$titleObj->exists() && $p[0] != 'create') {
             $this->dieUsageMsg('missingtitle-createonly');
         }
         if (!in_array($p[0], $restrictionTypes) && $p[0] != 'create') {
             $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
         }
         if (!in_array($p[1], $this->getConfig()->get('RestrictionLevels')) && $p[1] != 'all') {
             $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
         }
         if (wfIsInfinity($expiry[$i])) {
             $expiryarray[$p[0]] = 'infinity';
         } else {
             $exp = strtotime($expiry[$i]);
             if ($exp < 0 || !$exp) {
                 $this->dieUsageMsg(array('invalidexpiry', $expiry[$i]));
             }
             $exp = wfTimestamp(TS_MW, $exp);
             if ($exp < wfTimestampNow()) {
                 $this->dieUsageMsg(array('pastexpiry', $expiry[$i]));
             }
             $expiryarray[$p[0]] = $exp;
         }
         $resultProtections[] = array($p[0] => $protections[$p[0]], 'expiry' => $wgContLang->formatExpiry($expiryarray[$p[0]], TS_ISO_8601, 'infinite'));
     }
     $cascade = $params['cascade'];
     if ($params['watch']) {
         $this->logFeatureUsage('action=protect&watch');
     }
     $watch = $params['watch'] ? 'watch' : $params['watchlist'];
     $this->setWatch($watch, $titleObj, 'watchdefault');
     $status = $pageObj->doUpdateRestrictions($protections, $expiryarray, $cascade, $params['reason'], $this->getUser());
     if (!$status->isOK()) {
         $this->dieStatus($status);
     }
     $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
     if ($cascade) {
         $res['cascade'] = true;
     }
     $res['protections'] = $resultProtections;
     $result = $this->getResult();
     ApiResult::setIndexedTagName($res['protections'], 'protection');
     $result->addValue(null, $this->getModuleName(), $res);
 }
Example #6
0
 private function formatExpiry($expiry)
 {
     if (wfIsInfinity($expiry)) {
         return $this->context->msg('protect-expiry-indefinite')->text();
     }
     $lang = $this->context->getLanguage();
     $user = $this->context->getUser();
     return $this->context->msg('protect-expiring-local', $lang->userTimeAndDate($expiry, $user), $lang->userDate($expiry, $user), $lang->userTime($expiry, $user))->text();
 }
 /**
  * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
  * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
  * @param string $expiry Whatever was typed into the form
  * @return string Timestamp or "infinity" string for the DB implementation
  */
 public static function parseExpiryInput($expiry)
 {
     static $infinity;
     if ($infinity == null) {
         $infinity = wfGetDB(DB_SLAVE)->getInfinity();
     }
     if (wfIsInfinity($expiry)) {
         $expiry = $infinity;
     } else {
         $expiry = strtotime($expiry);
         if ($expiry < 0 || $expiry === false) {
             return false;
         }
         $expiry = wfTimestamp(TS_MW, $expiry);
     }
     return $expiry;
 }