Added post searching by date

This commit is contained in:
Marcin Kurczewski 2014-09-27 15:42:02 +02:00
parent 5159214e80
commit 6d4a4f11d1
3 changed files with 74 additions and 3 deletions

3
TODO
View file

@ -37,9 +37,6 @@ everything related to posts:
- fav:3..5
- score:3..5
- tag:3..5
- date:today
- date:yesterday
- date:yyyy[-mm[-dd]]..yyyy[-mm[-dd]]
- filesize:3K..5M
- imgsize:huge/large/medium/small
- hash:postName

View file

@ -5,4 +5,5 @@ class PostFilter extends BasicFilter implements IFilter
{
const REQUIREMENT_TAG = 'tag';
const REQUIREMENT_ID = 'id';
const REQUIREMENT_DATE = 'uploadTime';
}

View file

@ -27,6 +27,35 @@ class PostSearchParser extends AbstractSearchParser
$requirement->setNegated($token->isNegated());
$filter->addRequirement($requirement);
}
elseif ($token->getKey() === 'date')
{
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);
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
$requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_DATE);
$requirement->setValue($this->createRequirementValue($finalString, self::ALLOW_RANGES));
$requirement->setNegated($token->isNegated());
$filter->addRequirement($requirement);
}
else
{
throw new \BadMethodCallException('Not supported');
@ -37,4 +66,48 @@ class PostSearchParser extends AbstractSearchParser
{
throw new \BadMethodCallException('Not supported');
}
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];
}
}