2013-10-28 11:19:15 +01:00
|
|
|
<?php
|
|
|
|
class Model_Post_QueryBuilder implements AbstractQueryBuilder
|
|
|
|
{
|
2013-11-24 21:41:38 +01:00
|
|
|
private static $enableTokenLimit = true;
|
2013-10-28 11:19:15 +01:00
|
|
|
|
2013-11-24 21:41:38 +01:00
|
|
|
public static function enableTokenLimit($enable)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-24 21:41:38 +01:00
|
|
|
self::$enableTokenLimit = $enable;
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static function filterUserSafety($dbQuery)
|
|
|
|
{
|
2013-10-30 16:22:46 +01:00
|
|
|
$allowedSafety = PrivilegesHelper::getAllowedSafety();
|
2013-10-28 11:19:15 +01:00
|
|
|
$dbQuery->addSql('safety')->in('(' . R::genSlots($allowedSafety) . ')');
|
|
|
|
foreach ($allowedSafety as $s)
|
|
|
|
$dbQuery->put($s);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function filterUserHidden($dbQuery)
|
|
|
|
{
|
|
|
|
if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden'))
|
|
|
|
$dbQuery->not()->addSql('hidden');
|
|
|
|
else
|
|
|
|
$dbQuery->addSql('1');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function filterChain($dbQuery)
|
|
|
|
{
|
|
|
|
if (isset($dbQuery->__chained))
|
|
|
|
$dbQuery->and();
|
|
|
|
else
|
|
|
|
$dbQuery->where();
|
|
|
|
$dbQuery->__chained = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function filterNegate($dbQuery)
|
|
|
|
{
|
|
|
|
$dbQuery->not();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function filterTag($dbQuery, $val)
|
|
|
|
{
|
2013-11-30 19:07:39 +01:00
|
|
|
$tag = Model_Tag::locate($val);
|
2013-10-28 11:19:15 +01:00
|
|
|
$dbQuery
|
|
|
|
->exists()
|
|
|
|
->open()
|
|
|
|
->select('1')
|
|
|
|
->from('post_tag')
|
|
|
|
->where('post_id = post.id')
|
2013-11-30 19:07:39 +01:00
|
|
|
->and('post_tag.tag_id = ?')->put($tag->id)
|
2013-10-28 11:19:15 +01:00
|
|
|
->close();
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenId($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$ids = preg_split('/[;,]/', $val);
|
|
|
|
$ids = array_map('intval', $ids);
|
|
|
|
$dbQuery->addSql('id')->in('(' . R::genSlots($ids) . ')');
|
|
|
|
foreach ($ids as $id)
|
|
|
|
$dbQuery->put($id);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenIdMin($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('id >= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenIdMax($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('id <= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenScoreMin($searchContext, $dbQuery, $val)
|
2013-11-10 12:23:59 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('score >= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenScoreMax($searchContext, $dbQuery, $val)
|
2013-11-10 12:23:59 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('score <= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenTagMin($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('tag_count >= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenTagMax($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('tag_count <= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenFavMin($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('fav_count >= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenFavMax($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('fav_count <= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenCommentMin($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('comment_count >= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenCommentMax($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql('comment_count <= ?')->put(intval($val));
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenSpecial($searchContext, $dbQuery, $val)
|
2013-11-22 11:15:38 +01:00
|
|
|
{
|
|
|
|
$context = \Chibi\Registry::getContext();
|
|
|
|
|
|
|
|
switch (strtolower($val))
|
|
|
|
{
|
|
|
|
case 'liked':
|
|
|
|
case 'likes':
|
|
|
|
$dbQuery
|
|
|
|
->exists()
|
|
|
|
->open()
|
|
|
|
->select('1')
|
|
|
|
->from('postscore')
|
|
|
|
->where('post_id = post.id')
|
|
|
|
->and('score > 0')
|
|
|
|
->and('user_id = ?')->put($context->user->id)
|
|
|
|
->close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'disliked':
|
|
|
|
case 'dislikes':
|
|
|
|
$dbQuery
|
|
|
|
->exists()
|
|
|
|
->open()
|
|
|
|
->select('1')
|
|
|
|
->from('postscore')
|
|
|
|
->where('post_id = post.id')
|
|
|
|
->and('score < 0')
|
|
|
|
->and('user_id = ?')->put($context->user->id)
|
|
|
|
->close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new SimpleException('Unknown special "' . $val . '"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenType($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-21 22:22:13 +01:00
|
|
|
switch (strtolower($val))
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
case 'swf':
|
|
|
|
$type = PostType::Flash;
|
|
|
|
break;
|
|
|
|
case 'img':
|
|
|
|
$type = PostType::Image;
|
|
|
|
break;
|
|
|
|
case 'yt':
|
|
|
|
case 'youtube':
|
|
|
|
$type = PostType::Youtube;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new SimpleException('Unknown type "' . $val . '"');
|
|
|
|
}
|
|
|
|
$dbQuery->addSql('type = ?')->put($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function __filterTokenDateParser($val)
|
|
|
|
{
|
|
|
|
list ($year, $month, $day) = explode('-', $val . '-0-0');
|
|
|
|
$yearMin = $yearMax = intval($year);
|
|
|
|
$monthMin = $monthMax = intval($month);
|
|
|
|
$monthMin = $monthMin ?: 1;
|
|
|
|
$monthMax = $monthMax ?: 12;
|
|
|
|
$dayMin = $dayMax = intval($day);
|
|
|
|
$dayMin = $dayMin ?: 1;
|
|
|
|
$dayMax = $dayMax ?: intval(date('t', mktime(0, 0, 0, $monthMax, 1, $year)));
|
|
|
|
$timeMin = mktime(0, 0, 0, $monthMin, $dayMin, $yearMin);
|
|
|
|
$timeMax = mktime(0, 0, -1, $monthMax, $dayMax+1, $yearMax);
|
|
|
|
return [$timeMin, $timeMax];
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenDate($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
list ($timeMin, $timeMax) = self::__filterTokenDateParser($val);
|
|
|
|
$dbQuery
|
2013-11-30 19:07:39 +01:00
|
|
|
->addSql('upload_date >= ?')->put($timeMin)
|
|
|
|
->and('upload_date <= ?')->put($timeMax);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenDateMin($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
list ($timeMin, $timeMax) = self::__filterTokenDateParser($val);
|
|
|
|
$dbQuery->addSql('upload_date >= ?')->put($timeMin);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenDateMax($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
list ($timeMin, $timeMax) = self::__filterTokenDateParser($val);
|
|
|
|
$dbQuery->addSql('upload_date <= ?')->put($timeMax);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenFav($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-30 19:07:39 +01:00
|
|
|
$user = Model_User::locate($val);
|
2013-10-28 11:19:15 +01:00
|
|
|
$dbQuery
|
|
|
|
->exists()
|
|
|
|
->open()
|
|
|
|
->select('1')
|
|
|
|
->from('favoritee')
|
|
|
|
->where('post_id = post.id')
|
2013-11-30 19:07:39 +01:00
|
|
|
->and('favoritee.user_id = ?')->put($user->id)
|
2013-10-28 11:19:15 +01:00
|
|
|
->close();
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenFavs($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
return self::filterTokenFav($searchContext, $dbQuery, $val);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenComment($searchContext, $dbQuery, $val)
|
2013-11-21 14:13:26 +01:00
|
|
|
{
|
2013-11-30 19:07:39 +01:00
|
|
|
$user = Model_User::locate($val);
|
2013-11-21 14:13:26 +01:00
|
|
|
$dbQuery
|
|
|
|
->exists()
|
|
|
|
->open()
|
|
|
|
->select('1')
|
|
|
|
->from('comment')
|
|
|
|
->where('post_id = post.id')
|
2013-11-30 19:07:39 +01:00
|
|
|
->and('commenter_id = ?')->put($user->id)
|
2013-11-21 14:13:26 +01:00
|
|
|
->close();
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenCommenter($searchContext, $dbQuery, $val)
|
2013-11-21 14:13:26 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
return self::filterTokenComment($searchContext, $dbQuery, $val);
|
2013-11-21 14:13:26 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenSubmit($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-30 19:07:39 +01:00
|
|
|
$user = Model_User::locate($val);
|
|
|
|
$dbQuery->addSql('uploader_id = ?')->put($user->id);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenUploader($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
return self::filterTokenSubmit($searchContext, $dbQuery, $val);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenUpload($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
return self::filterTokenSubmit($searchContext, $dbQuery, $val);
|
2013-11-24 21:41:38 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenUploaded($searchContext, $dbQuery, $val)
|
2013-11-24 21:41:38 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
return self::filterTokenSubmit($searchContext, $dbQuery, $val);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenPrev($searchContext, $dbQuery, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
self::__filterTokenPrevNext($searchContext, $dbQuery, $val);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function filterTokenNext($searchContext, $dbQuery, $val)
|
2013-11-24 21:50:46 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
$searchContext->orderDir *= -1;
|
|
|
|
self::__filterTokenPrevNext($searchContext, $dbQuery, $val);
|
2013-11-24 21:50:46 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function __filterTokenPrevNext($searchContext, $dbQuery, $val)
|
2013-11-24 21:50:46 +01:00
|
|
|
{
|
2013-12-05 22:19:21 +01:00
|
|
|
$op1 = $searchContext->orderDir == 1 ? '<' : '>';
|
|
|
|
$op2 = $searchContext->orderDir != 1 ? '<' : '>';
|
2013-11-24 21:50:46 +01:00
|
|
|
$dbQuery
|
|
|
|
->open()
|
|
|
|
->open()
|
2013-12-05 22:19:21 +01:00
|
|
|
->addSql($searchContext->orderColumn . ' ' . $op1 . ' ')
|
2013-11-24 21:50:46 +01:00
|
|
|
->open()
|
2013-12-05 22:19:21 +01:00
|
|
|
->select($searchContext->orderColumn)
|
2013-11-24 21:50:46 +01:00
|
|
|
->from('post p2')
|
|
|
|
->where('p2.id = ?')->put(intval($val))
|
|
|
|
->close()
|
|
|
|
->and('id != ?')->put($val)
|
|
|
|
->close()
|
|
|
|
->or()
|
|
|
|
->open()
|
2013-12-05 22:19:21 +01:00
|
|
|
->addSql($searchContext->orderColumn . ' = ')
|
2013-11-24 21:50:46 +01:00
|
|
|
->open()
|
2013-12-05 22:19:21 +01:00
|
|
|
->select($searchContext->orderColumn)
|
2013-11-24 21:50:46 +01:00
|
|
|
->from('post p2')
|
|
|
|
->where('p2.id = ?')->put(intval($val))
|
|
|
|
->close()
|
|
|
|
->and('id ' . $op1 . ' ?')->put(intval($val))
|
|
|
|
->close()
|
|
|
|
->close();
|
|
|
|
}
|
2013-10-28 11:19:15 +01:00
|
|
|
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
protected static function parseOrderToken($searchContext, $val)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
|
|
|
$randomReset = true;
|
|
|
|
|
|
|
|
$orderDir = 1;
|
|
|
|
if (substr($val, -4) == 'desc')
|
|
|
|
{
|
|
|
|
$orderDir = 1;
|
|
|
|
$val = rtrim(substr($val, 0, -4), ',');
|
|
|
|
}
|
|
|
|
elseif (substr($val, -3) == 'asc')
|
|
|
|
{
|
|
|
|
$orderDir = -1;
|
|
|
|
$val = rtrim(substr($val, 0, -3), ',');
|
|
|
|
}
|
|
|
|
if ($val{0} == '-')
|
|
|
|
{
|
|
|
|
$orderDir *= -1;
|
|
|
|
$val = substr($val, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($val)
|
|
|
|
{
|
|
|
|
case 'id':
|
2013-11-24 21:41:38 +01:00
|
|
|
$orderColumn = 'id';
|
2013-10-28 11:19:15 +01:00
|
|
|
break;
|
|
|
|
case 'date':
|
2013-11-24 21:41:38 +01:00
|
|
|
$orderColumn = 'upload_date';
|
2013-10-28 11:19:15 +01:00
|
|
|
break;
|
|
|
|
case 'comment':
|
|
|
|
case 'comments':
|
|
|
|
case 'commentcount':
|
|
|
|
$orderColumn = 'comment_count';
|
|
|
|
break;
|
|
|
|
case 'fav':
|
|
|
|
case 'favs':
|
|
|
|
case 'favcount':
|
|
|
|
$orderColumn = 'fav_count';
|
|
|
|
break;
|
2013-11-10 12:23:59 +01:00
|
|
|
case 'score':
|
|
|
|
$orderColumn = 'score';
|
|
|
|
break;
|
2013-10-28 11:19:15 +01:00
|
|
|
case 'tag':
|
|
|
|
case 'tags':
|
|
|
|
case 'tagcount':
|
|
|
|
$orderColumn = 'tag_count';
|
|
|
|
break;
|
|
|
|
case 'random':
|
|
|
|
//seeding works like this: if you visit anything
|
|
|
|
//that triggers order other than random, the seed
|
|
|
|
//is going to reset. however, it stays the same as
|
|
|
|
//long as you keep visiting pages with order:random
|
|
|
|
//specified.
|
|
|
|
$randomReset = false;
|
|
|
|
if (!isset($_SESSION['browsing-seed']))
|
|
|
|
$_SESSION['browsing-seed'] = mt_rand();
|
|
|
|
$seed = $_SESSION['browsing-seed'];
|
|
|
|
$orderColumn = 'SUBSTR(id * ' . $seed .', LENGTH(id) + 2)';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new SimpleException('Unknown key "' . $val . '"');
|
|
|
|
}
|
|
|
|
|
2013-10-30 22:38:59 +01:00
|
|
|
if ($randomReset and isset($_SESSION['browsing-seed']))
|
2013-10-28 11:19:15 +01:00
|
|
|
unset($_SESSION['browsing-seed']);
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
$searchContext->orderColumn = $orderColumn;
|
|
|
|
$searchContext->orderDir = $orderDir;
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-24 21:41:38 +01:00
|
|
|
protected static function iterateTokens($tokens, $callback)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-24 21:41:38 +01:00
|
|
|
$unparsedTokens = [];
|
2013-10-28 11:19:15 +01:00
|
|
|
|
2013-11-30 19:07:39 +01:00
|
|
|
foreach ($tokens as $origToken)
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-30 19:07:39 +01:00
|
|
|
$token = $origToken;
|
|
|
|
$neg = false;
|
2013-10-28 11:19:15 +01:00
|
|
|
if ($token{0} == '-')
|
|
|
|
{
|
|
|
|
$token = substr($token, 1);
|
|
|
|
$neg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pos = strpos($token, ':');
|
|
|
|
if ($pos === false)
|
|
|
|
{
|
2013-11-24 21:41:38 +01:00
|
|
|
$key = null;
|
|
|
|
$val = $token;
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
2013-11-24 21:41:38 +01:00
|
|
|
else
|
2013-10-28 11:19:15 +01:00
|
|
|
{
|
2013-11-24 21:41:38 +01:00
|
|
|
$key = strtolower(substr($token, 0, $pos));
|
|
|
|
$val = substr($token, $pos + 1);
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-11-24 21:41:38 +01:00
|
|
|
$parsed = $callback($neg, $key, $val);
|
2013-10-28 11:19:15 +01:00
|
|
|
|
2013-11-24 21:41:38 +01:00
|
|
|
if (!$parsed)
|
2013-11-30 19:07:39 +01:00
|
|
|
$unparsedTokens []= $origToken;
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
2013-11-24 21:41:38 +01:00
|
|
|
return $unparsedTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function build($dbQuery, $query)
|
|
|
|
{
|
|
|
|
$config = \Chibi\Registry::getConfig();
|
|
|
|
|
|
|
|
$dbQuery->from('post');
|
|
|
|
|
|
|
|
self::filterChain($dbQuery);
|
|
|
|
self::filterUserSafety($dbQuery);
|
|
|
|
self::filterChain($dbQuery);
|
|
|
|
self::filterUserHidden($dbQuery);
|
|
|
|
|
|
|
|
/* query tokens */
|
|
|
|
$tokens = array_filter(array_unique(explode(' ', $query)), function($x) { return $x != ''; });
|
|
|
|
if (self::$enableTokenLimit and count($tokens) > $config->browsing->maxSearchTokens)
|
|
|
|
throw new SimpleException('Too many search tokens (maximum: ' . $config->browsing->maxSearchTokens . ')');
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
if (\Chibi\Registry::getContext()->user->hasEnabledHidingDislikedPosts())
|
|
|
|
$tokens []= '-special:disliked';
|
2013-10-28 11:19:15 +01:00
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
$searchContext = new StdClass;
|
|
|
|
$searchContext->orderColumn = 'id';
|
|
|
|
$searchContext->orderDir = 1;
|
|
|
|
|
|
|
|
$tokens = self::iterateTokens($tokens, function($neg, $key, $val) use ($searchContext, $dbQuery, &$orderToken)
|
2013-11-24 21:41:38 +01:00
|
|
|
{
|
|
|
|
if ($key != 'order')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ($neg)
|
|
|
|
$orderToken = '-' . $val;
|
|
|
|
else
|
|
|
|
$orderToken = $val;
|
2013-12-05 22:19:21 +01:00
|
|
|
self::parseOrderToken($searchContext, $orderToken);
|
2013-11-24 21:41:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
$tokens = self::iterateTokens($tokens, function($neg, $key, $val) use ($searchContext, $dbQuery)
|
2013-11-24 21:41:38 +01:00
|
|
|
{
|
|
|
|
if ($key !== null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
self::filterChain($dbQuery);
|
|
|
|
if ($neg)
|
|
|
|
self::filterNegate($dbQuery);
|
|
|
|
self::filterTag($dbQuery, $val);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
$tokens = self::iterateTokens($tokens, function($neg, $key, $val) use ($searchContext, $dbQuery)
|
2013-11-24 21:41:38 +01:00
|
|
|
{
|
|
|
|
$methodName = 'filterToken' . TextHelper::kebabCaseToCamelCase($key);
|
|
|
|
if (!method_exists(__CLASS__, $methodName))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
self::filterChain($dbQuery);
|
|
|
|
if ($neg)
|
|
|
|
self::filterNegate($dbQuery);
|
2013-12-05 22:19:21 +01:00
|
|
|
self::$methodName($searchContext, $dbQuery, $val);
|
2013-11-24 21:41:38 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!empty($tokens))
|
|
|
|
throw new SimpleException('Unknown search token "' . array_shift($tokens) . '"');
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
$dbQuery->orderBy($searchContext->orderColumn);
|
|
|
|
if ($searchContext->orderDir == 1)
|
2013-11-24 21:41:38 +01:00
|
|
|
$dbQuery->desc();
|
|
|
|
else
|
|
|
|
$dbQuery->asc();
|
|
|
|
|
2013-12-05 22:19:21 +01:00
|
|
|
if ($searchContext->orderColumn != 'id')
|
2013-11-30 19:07:39 +01:00
|
|
|
{
|
|
|
|
$dbQuery->addSql(', id');
|
2013-12-05 22:19:21 +01:00
|
|
|
if ($searchContext->orderDir == 1)
|
2013-11-30 19:07:39 +01:00
|
|
|
$dbQuery->desc();
|
|
|
|
else
|
|
|
|
$dbQuery->asc();
|
|
|
|
}
|
2013-10-28 11:19:15 +01:00
|
|
|
}
|
|
|
|
}
|