diff --git a/items/list.php b/items/list.php index 8e67e3c..83e1abe 100644 --- a/items/list.php +++ b/items/list.php @@ -3,7 +3,7 @@ require_once("../db.php"); require_once("../util.php"); require_once('../SortHelper.php'); - require_once('../FilterHelper.php'); + require_once('../util/DB/DataFilter.php'); require_once("../User.php"); require_once("../Assert.php"); require_once("../modules/semver/semver.php"); @@ -30,7 +30,7 @@ $db_limit = ""; $db_order = ''; - $filter = new FilterHelper($db_connection, DB_TABLE_ITEMS); + $filter = new DataFilter($_GET, DB_TABLE_ITEMS, $db_connection); $filter->add(array('name' => 'type', 'type' => 'custom', 'coerce' => array('ItemType', 'getCode'))); $filter->add(array('name' => 'user', 'type' => 'binary')); # WARN: changes parameter to receive ID instead of name @@ -47,7 +47,7 @@ function coerce_regex($value, $db_connection) { return '"(^|;)' . $db_connection->real_escape_string($value) . '($|;)"'; } - # special filtering (post-MySQL), thus not handled by FilterHelper + # special filtering (post-MySQL), thus not handled by DataFilter if (isset($_GET["version"])) { $version = strtolower($_GET["version"]); @@ -74,7 +74,7 @@ function coerce_regex($value, $db_connection) { } if ($sort_by_version || count($semver_filters) > 0) { - $db_cond = $filter->evaluate($_GET); + $db_cond = $filter->evaluate(); SortHelper::PrepareSemverSorting(DB_TABLE_ITEMS, 'version', $db_cond, $semver_filters); $db_join .= ($db_join ? ', ' : 'LEFT JOIN (') . '`semver_index`'; $db_join_on .= ($db_join_on ? ' AND ' : ' ON (') . '`' . DB_TABLE_ITEMS . '`.`version` = `semver_index`.`version`'; @@ -83,9 +83,9 @@ function coerce_regex($value, $db_connection) { # These must defined below the call to SortHelper::PrepareSemverSorting() as it can not handle table joins $filter->add(array('name' => 'version-min', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '>=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex'))); $filter->add(array('name' => 'version-max', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '<=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex'))); - $db_cond = $filter->evaluate($_GET); # re-evaluate to include the latest filters + $db_cond = $filter->evaluate(); # re-evaluate to include the latest filters - # enable rating filters if necessary (filter with HAVING instead of WHERE, not currently supported by FilterHelper) + # enable rating filters if necessary (filter with HAVING instead of WHERE, not currently supported by DataFilter) if ($get_rating = isset($_GET['rating']) || isset($_GET['rating-min']) || isset($_GET['rating-max']) || $sort_by_rating) { $db_join .= ($db_join ? ', ' : 'LEFT JOIN (') . DB_TABLE_RATINGS; $db_join_on .= ($db_join_on ? ' AND ' : ' ON (') . 'item = id'; diff --git a/stdlib/candidates/Candidate.php b/stdlib/candidates/Candidate.php index bdc00dc..1aaa655 100644 --- a/stdlib/candidates/Candidate.php +++ b/stdlib/candidates/Candidate.php @@ -1,7 +1,7 @@ '`date`', 'approval' => '`approval`')); - $filter = new FilterHelper($db_connection, DB_TABLE_CANDIDATES); + $filter = new DataFilter($filters, DB_TABLE_CANDIDATES, $db_connection); $filter->add(array('name' => 'item', 'type' => 'binary')); $filter->add(array('name' => 'user', 'type' => 'binary')); @@ -156,7 +156,7 @@ public static function listCandidates($filters = array(), $sort = array()) { $filter->add(array('name' => 'owner', 'db-name' => 'user', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS, 'join-ref' => 'item', 'join-key' => 'id')); - $db_cond = $filter->evaluate($filters); + $db_cond = $filter->evaluate(); $db_join = $filter->evaluateJoins(); $db_query = 'SELECT ' . DB_TABLE_CANDIDATES . '.`id`, HEX(' . DB_TABLE_CANDIDATES. '.`item`) AS item FROM ' . DB_TABLE_CANDIDATES . $db_join . $db_cond . ' ' . $db_sort; @@ -173,7 +173,7 @@ public static function listVotings($candidate, $filters = array(), $sort = array } $db_connection = db_ensure_connection(); - $filter = new FilterHelper($db_connection, DB_TABLE_CANDIDATE_VOTING); + $filter = new DataFilter($filters, DB_TABLE_CANDIDATE_VOTING, $db_connection); $filter->add(array('db-name' => 'candidate', 'value' => $candidate, 'type' => 'int')); $filter->add(array('name' => 'user', 'type' => 'binary')); @@ -184,7 +184,7 @@ public static function listVotings($candidate, $filters = array(), $sort = array $filter->add(array('name' => 'voted-before', 'db-name' => 'date', 'operator' => '<')); $filter->add(array('name' => 'voted-after', 'db-name' => 'date', 'operator' => '>')); - $db_cond = $filter->evaluate($filters); + $db_cond = $filter->evaluate(); $db_sort = SortHelper::getOrderClause($sort, array('date' => '`date`')); $db_query = 'SELECT `candidate`, HEX(`user`) AS user, `accept`, `final`, `reason`, `date` FROM ' . DB_TABLE_CANDIDATE_VOTING . $db_cond . ' ' . $db_sort; diff --git a/stdlib/candidates/list.php b/stdlib/candidates/list.php index 264795e..719802a 100644 --- a/stdlib/candidates/list.php +++ b/stdlib/candidates/list.php @@ -3,7 +3,7 @@ require_once('../../util.php'); require_once('../../Assert.php'); require_once('../../SortHelper.php'); -require_once('../../FilterHelper.php'); +require_once('../../util/DB/DataFilter.php'); require_once('Candidate.php'); try { @@ -11,7 +11,7 @@ $content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json'); - $filters = FilterHelper::FromParams(array('user', 'item', 'created', 'created-after', 'created-before', 'approved', 'owner')); + $filters = DataFilter::FromParams(array('user', 'item', 'created', 'created-after', 'created-before', 'approved', 'owner')); $sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : ''); $candidates = Candidate::listCandidates($filters, $sort_list); diff --git a/stdlib/candidates/voting.php b/stdlib/candidates/voting.php index 1dda1a7..349d059 100644 --- a/stdlib/candidates/voting.php +++ b/stdlib/candidates/voting.php @@ -3,6 +3,7 @@ require_once('../../util.php'); require_once('../../Assert.php'); require_once('../../SortHelper.php'); +require_once('../../util/DB/DataFilter.php'); require_once('../../User.php'); require_once('Candidate.php'); require_once('../StdlibPending.php'); @@ -48,7 +49,7 @@ Assert::GetParameters('id'); $content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json'); - $filters = FilterHelper::FromParams(array('user', 'final', 'accept', 'voted', 'voted-after', 'voted-before')); + $filters = DataFilter::FromParams(array('user', 'final', 'accept', 'voted', 'voted-after', 'voted-before')); $sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : ''); $votings = Candidate::listVotings($_GET['id'], $filters, $sort_list); diff --git a/stdlib/items.php b/stdlib/items.php index 41adf04..27e6958 100644 --- a/stdlib/items.php +++ b/stdlib/items.php @@ -5,7 +5,7 @@ require_once('../db.php'); require_once('../Assert.php'); require_once('../SortHelper.php'); -require_once('../FilterHelper.php'); +require_once('../util/DB/DataFilter.php'); try { Assert::RequestMethod(Assert::REQUEST_METHOD_GET); @@ -15,13 +15,13 @@ $db_sort = ''; $db_join = ''; - $filter = new FilterHelper($db_connection, DB_TABLE_STDLIB); + $filter = new DataFilter($_GET, DB_TABLE_STDLIB, $db_connection); $filter->add(array('name' => 'name', 'db-table' => DB_TABLE_ITEMS)); $filter->add(array('name' => 'user', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS)); $filter->add(array('name' => 'id', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS)); - $db_cond = $filter->evaluate($_GET, ' AND '); + $db_cond = $filter->evaluate(' AND '); if (isset($_GET['sort'])) { $sort_list = SortHelper::getListFromParam($_GET['sort']); diff --git a/stdlib/releases/StdlibRelease.php b/stdlib/releases/StdlibRelease.php index c35621d..2391bba 100644 --- a/stdlib/releases/StdlibRelease.php +++ b/stdlib/releases/StdlibRelease.php @@ -1,7 +1,7 @@ add(array('name' => 'version-min', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '>=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex'))); $filter->add(array('name' => 'version-max', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '<=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex'))); - $db_cond .= $filter->evaluate($filters, $db_cond ? ' AND ' : ' WHERE '); + $db_cond .= $filter->evaluate($db_cond ? ' AND ' : ' WHERE '); # get all releases from DB $db_query = "SELECT `release` FROM " . DB_TABLE_STDLIB_RELEASES . $db_join . $db_cond . $db_sort; diff --git a/users/Suspension.php b/users/Suspension.php index bbbf412..de17fa2 100644 --- a/users/Suspension.php +++ b/users/Suspension.php @@ -2,7 +2,7 @@ require_once(dirname(__FILE__) . '/../db.php'); require_once(dirname(__FILE__) . '/../User.php'); require_once(dirname(__FILE__) . '/../SortHelper.php'); -require_once(dirname(__FILE__) . '/../FilterHelper.php'); +require_once(dirname(__FILE__) . '/../util/DB/DataFilter.php'); require_once(dirname(__FILE__) . '/../Assert.php'); require_once(dirname(__FILE__) . '/../sql2array.php'); require_once(dirname(__FILE__) . '/../modules/HttpException/HttpException.php'); @@ -62,7 +62,7 @@ public static function getSuspensionsById($id, $filters = array(), $sort = array throw new HttpException(500, NULL, 'Must pass a valid array as suspension filter!'); } - $filter = new FilterHelper($db_connection, DB_TABLE_SUSPENSIONS); + $filter = new DataFilter($filters, DB_TABLE_SUSPENSIONS, $db_connection); $filter->add(array('db-name' => 'user', 'value' => $id, 'type' => 'binary')); @@ -88,7 +88,7 @@ public static function getSuspensionsById($id, $filters = array(), $sort = array ) )); - $db_cond = $filter->evaluate($filters); + $db_cond = $filter->evaluate(); $sort = SortHelper::getOrderClause($sort, array('created' => '`created`', 'expires' => '`expires`')); $db_query = 'SELECT *, HEX(`user`) AS user FROM ' . DB_TABLE_SUSPENSIONS . $db_cond . $sort; diff --git a/users/suspensions/list.php b/users/suspensions/list.php index 31b27dd..d9c2c3f 100644 --- a/users/suspensions/list.php +++ b/users/suspensions/list.php @@ -3,7 +3,7 @@ require_once('../../modules/HttpException/HttpException.php'); require_once('../../util.php'); require_once('../../SortHelper.php'); -require_once('../../FilterHelper.php'); +require_once('../../util/DB/DataFilter.php'); require_once('../../User.php'); require_once('../Suspension.php'); @@ -29,7 +29,7 @@ # validate accept header of request $content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json'); - $filters = FilterHelper::FromParams(array('active', 'created', 'created-after', 'created-before', 'expires', 'expires-after', 'expires-before', 'infinite', 'restricted')); + $filters = DataFilter::FromParams(array('active', 'created', 'created-after', 'created-before', 'expires', 'expires-after', 'expires-before', 'infinite', 'restricted')); $sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : ''); $suspensions = Suspension::getSuspensionsById($id, $filters, $sort_list); diff --git a/FilterHelper.php b/util/DB/DataFilter.php similarity index 85% rename from FilterHelper.php rename to util/DB/DataFilter.php index 134d598..9d1c54a 100644 --- a/FilterHelper.php +++ b/util/DB/DataFilter.php @@ -1,24 +1,49 @@ SetSource($source); $this->connection = $db_connection; - $this->table = $table; + $this->setDefaultTable($table); } - public function add($data) { #$name, $db_name = NULL, $method = 'GET', $op = '=', $default = NULL, $force = NULL) { - $this->filters[] = $data; #array('name' => $name, 'db-name' => $db_name, $method => 'GET', 'operator' => $op, 'default' => $default, 'force-value' => $force); + public function add($data) { + $this->filters[] = $data; } - public function evaluate($source, $prefix = ' WHERE ') { - $db_cond = ''; + public function define($data) { + return $this->add($data); + } + + public function SetSource($source) { $this->source = $source; + } + + public function setDefaultTable($table) { + $this->table = $table; + } + + public function evaluate($prefix = ' WHERE ', $db_connection = NULL) { + if ($db_connection !== NULL) { + $this->connection = $db_connection; + } + if ($this->connection === NULL) { + throw new HttpException(500, NULL, 'Must specify DB connection for filter!'); + } + if ($this->table === NULL) { + throw new HttpException(500, NULL, 'Must specify DB table for filter!'); + } + + $db_cond = ''; foreach ($this->filters AS $filter) { @@ -111,7 +136,9 @@ private function evaluateConditions($conditions, $filter) { } $key = '`' . (isset($data['db-table']) ? $data['db-table'] : $this->table) . '`.`' . (isset($data['db-name']) ? $data['db-name'] : $data['name']) . '`'; # the name is also used as column name if no other is specified if (isset($data['db-function'])) { - $key = $data['db-function'] . '(' . $key . ')'; + foreach ((array)$data['db-function'] AS $fn) { + $key = $fn . '(' . $key . ')'; + } } # Get the value for comparison @@ -261,5 +288,22 @@ public static function FromParams($filters, $source = NULL) { } return array_intersect_key($source, array_flip($filters)); } + + public static function SimpleFilter($filters, $table = NULL) { + $f = new self($table); + + foreach ($filters AS $name => $value) { + if (is_array($value)) { + $keys = array_keys($value); + $f->add(array('db-name' => $name, 'value' => $keys[0], 'type' => $value[$keys[0]])); + } else if (is_int($name)) { + $f->add(array('name' => $value)); + } else { + $f->add(array('db-name' => $name, 'value' => $value)); + } + } + + return $f; + } } ?> \ No newline at end of file