Added ability to search posts by edit dates

This commit is contained in:
rr- 2015-11-24 18:47:14 +01:00
parent 2df43201ba
commit d769eaed61
3 changed files with 82 additions and 69 deletions

View file

@ -20,6 +20,7 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_TAG = 'tag'; const REQUIREMENT_TAG = 'tag';
const REQUIREMENT_ID = 'id'; const REQUIREMENT_ID = 'id';
const REQUIREMENT_CREATION_TIME = 'creationTime'; const REQUIREMENT_CREATION_TIME = 'creationTime';
const REQUIREMENT_LAST_EDIT_TIME = 'lastEditTime';
const REQUIREMENT_HASH = 'name'; const REQUIREMENT_HASH = 'name';
const REQUIREMENT_TAG_COUNT = 'tagCount'; const REQUIREMENT_TAG_COUNT = 'tagCount';
const REQUIREMENT_FAV_COUNT = 'favCount'; const REQUIREMENT_FAV_COUNT = 'favCount';

View file

@ -96,6 +96,31 @@ abstract class AbstractSearchParser
$filter->addRequirement($requirement); $filter->addRequirement($requirement);
} }
protected function addRequirementFromDateRangeToken($filter, $token, $type)
{
if (substr_count($token->getValue(), '..') === 1)
{
list ($dateMin, $dateMax) = explode('..', $token->getValue());
$timeMin = $this->dateToTime($dateMin)[0];
$timeMax = $this->dateToTime($dateMax)[1];
}
else
{
$date = $token->getValue();
list ($timeMin, $timeMax) = $this->dateToTime($date);
}
$finalString = '';
if ($timeMin)
$finalString .= date('c', $timeMin);
$finalString .= '..';
if ($timeMax)
$finalString .= date('c', $timeMax);
$token->setValue($finalString);
$this->addRequirementFromToken($filter, $token, $type, self::ALLOW_RANGES);
}
private function getOrderColumn($tokenText) private function getOrderColumn($tokenText)
{ {
$map = $this->getOrderColumnMap(); $map = $this->getOrderColumnMap();
@ -204,4 +229,47 @@ abstract class AbstractSearchParser
return str_replace('_', '', strtolower($text)); return str_replace('_', '', strtolower($text));
} }
private function dateToTime($value)
{
$value = strtolower(trim($value));
if (!$value)
{
return null;
}
elseif ($value === 'today')
{
$timeMin = mktime(0, 0, 0);
$timeMax = mktime(24, 0, -1);
}
elseif ($value === 'yesterday')
{
$timeMin = mktime(-24, 0, 0);
$timeMax = mktime(0, 0, -1);
}
elseif (preg_match('/^(\d{4})$/', $value, $matches))
{
$year = intval($matches[1]);
$timeMin = mktime(0, 0, 0, 1, 1, $year);
$timeMax = mktime(0, 0, -1, 1, 1, $year + 1);
}
elseif (preg_match('/^(\d{4})-(\d{1,2})$/', $value, $matches))
{
$year = intval($matches[1]);
$month = intval($matches[2]);
$timeMin = mktime(0, 0, 0, $month, 1, $year);
$timeMax = mktime(0, 0, -1, $month + 1, 1, $year);
}
elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value, $matches))
{
$year = intval($matches[1]);
$month = intval($matches[2]);
$day = intval($matches[3]);
$timeMin = mktime(0, 0, 0, $month, $day, $year);
$timeMax = mktime(0, 0, -1, $month, $day + 1, $year);
}
else
throw new \Exception('Invalid date format: ' . $value);
return [$timeMin, $timeMax];
}
} }

View file

@ -66,7 +66,8 @@ class PostSearchParser extends AbstractSearchParser
[ [
[['id'], [$this, 'addIdRequirement']], [['id'], [$this, 'addIdRequirement']],
[['hash', 'name'], [$this, 'addHashRequirement']], [['hash', 'name'], [$this, 'addHashRequirement']],
[['date', 'time'], [$this, 'addDateRequirement']], [['date', 'time', 'creation_date', 'creation_time'], [$this, 'addCreationTimeRequirement']],
[['edit_date', 'edit_time'], [$this, 'addLastEditTimeRequirement']],
[['tag_count', 'tags'], [$this, 'addTagCountRequirement']], [['tag_count', 'tags'], [$this, 'addTagCountRequirement']],
[['fav_count', 'favs'], [$this, 'addFavCountRequirement']], [['fav_count', 'favs'], [$this, 'addFavCountRequirement']],
[['comment_count', 'comments'], [$this, 'addCommentCountRequirement']], [['comment_count', 'comments'], [$this, 'addCommentCountRequirement']],
@ -181,33 +182,20 @@ class PostSearchParser extends AbstractSearchParser
self::ALLOW_COMPOSITE); self::ALLOW_COMPOSITE);
} }
private function addDateRequirement($filter, $token) private function addCreationTimeRequirement($filter, $token)
{ {
if (substr_count($token->getValue(), '..') === 1) $this->addRequirementFromDateRangeToken(
{
list ($dateMin, $dateMax) = explode('..', $token->getValue());
$timeMin = $this->dateToTime($dateMin)[0];
$timeMax = $this->dateToTime($dateMax)[1];
}
else
{
$date = $token->getValue();
list ($timeMin, $timeMax) = $this->dateToTime($date);
}
$finalString = '';
if ($timeMin)
$finalString .= date('c', $timeMin);
$finalString .= '..';
if ($timeMax)
$finalString .= date('c', $timeMax);
$token->setValue($finalString);
$this->addRequirementFromToken(
$filter, $filter,
$token, $token,
PostFilter::REQUIREMENT_CREATION_TIME, PostFilter::REQUIREMENT_CREATION_TIME);
self::ALLOW_RANGES); }
private function addLastEditTimeRequirement($filter, $token)
{
$this->addRequirementFromDateRangeToken(
$filter,
$token,
PostFilter::REQUIREMENT_LAST_EDIT_TIME);
} }
private function addTagCountRequirement($filter, $token) private function addTagCountRequirement($filter, $token)
@ -318,48 +306,4 @@ class PostSearchParser extends AbstractSearchParser
$requirement->setNegated($isNegated); $requirement->setNegated($isNegated);
$filter->addRequirement($requirement); $filter->addRequirement($requirement);
} }
private function dateToTime($value)
{
$value = strtolower(trim($value));
if (!$value)
{
return null;
}
elseif ($value === 'today')
{
$timeMin = mktime(0, 0, 0);
$timeMax = mktime(24, 0, -1);
}
elseif ($value === 'yesterday')
{
$timeMin = mktime(-24, 0, 0);
$timeMax = mktime(0, 0, -1);
}
elseif (preg_match('/^(\d{4})$/', $value, $matches))
{
$year = intval($matches[1]);
$timeMin = mktime(0, 0, 0, 1, 1, $year);
$timeMax = mktime(0, 0, -1, 1, 1, $year + 1);
}
elseif (preg_match('/^(\d{4})-(\d{1,2})$/', $value, $matches))
{
$year = intval($matches[1]);
$month = intval($matches[2]);
$timeMin = mktime(0, 0, 0, $month, 1, $year);
$timeMax = mktime(0, 0, -1, $month + 1, 1, $year);
}
elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value, $matches))
{
$year = intval($matches[1]);
$month = intval($matches[2]);
$day = intval($matches[3]);
$timeMin = mktime(0, 0, 0, $month, $day, $year);
$timeMax = mktime(0, 0, -1, $month, $day + 1, $year);
}
else
throw new \Exception('Invalid date format: ' . $value);
return [$timeMin, $timeMax];
}
} }