From 5bdf7bb98c7c8e2275bc384713537c5553fce519 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:09:55 +0200 Subject: [PATCH 01/11] remove old FilterHelper comments --- FilterHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FilterHelper.php b/FilterHelper.php index 134d598..457186b 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -12,8 +12,8 @@ public function __construct($db_connection, $table) { $this->table = $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 ') { From ed82a54219cb3204fe642cf6aa89a62e4621dd90 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:19:36 +0200 Subject: [PATCH 02/11] reverse FilterHelper arguments --- FilterHelper.php | 2 +- items/list.php | 2 +- stdlib/candidates/Candidate.php | 4 ++-- stdlib/items.php | 2 +- stdlib/releases/StdlibRelease.php | 2 +- users/Suspension.php | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/FilterHelper.php b/FilterHelper.php index 457186b..9ed90b5 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -7,7 +7,7 @@ class FilterHelper { /* * Public class instance interface */ - public function __construct($db_connection, $table) { + public function __construct($table, $db_connection) { $this->connection = $db_connection; $this->table = $table; } diff --git a/items/list.php b/items/list.php index 8e67e3c..2333e70 100644 --- a/items/list.php +++ b/items/list.php @@ -30,7 +30,7 @@ $db_limit = ""; $db_order = ''; - $filter = new FilterHelper($db_connection, DB_TABLE_ITEMS); + $filter = new FilterHelper(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 diff --git a/stdlib/candidates/Candidate.php b/stdlib/candidates/Candidate.php index bdc00dc..ebb607f 100644 --- a/stdlib/candidates/Candidate.php +++ b/stdlib/candidates/Candidate.php @@ -143,7 +143,7 @@ public static function listCandidates($filters = array(), $sort = array()) { $db_connection = db_ensure_connection(); $db_sort = SortHelper::getOrderClause($sort, array('date' => '`date`', 'approval' => '`approval`')); - $filter = new FilterHelper($db_connection, DB_TABLE_CANDIDATES); + $filter = new FilterHelper(DB_TABLE_CANDIDATES, $db_connection); $filter->add(array('name' => 'item', 'type' => 'binary')); $filter->add(array('name' => 'user', 'type' => 'binary')); @@ -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 FilterHelper(DB_TABLE_CANDIDATE_VOTING, $db_connection); $filter->add(array('db-name' => 'candidate', 'value' => $candidate, 'type' => 'int')); $filter->add(array('name' => 'user', 'type' => 'binary')); diff --git a/stdlib/items.php b/stdlib/items.php index 41adf04..f938aeb 100644 --- a/stdlib/items.php +++ b/stdlib/items.php @@ -15,7 +15,7 @@ $db_sort = ''; $db_join = ''; - $filter = new FilterHelper($db_connection, DB_TABLE_STDLIB); + $filter = new FilterHelper(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)); diff --git a/stdlib/releases/StdlibRelease.php b/stdlib/releases/StdlibRelease.php index d047deb..d93b408 100644 --- a/stdlib/releases/StdlibRelease.php +++ b/stdlib/releases/StdlibRelease.php @@ -187,7 +187,7 @@ public static function ListReleases($published, $filters = array(), $sort = arra $db_cond = ($t = self::get_publish_cond($published)) == NULL ? '' : " WHERE $t"; $db_connection = db_ensure_connection(); - $filter = new FilterHelper($db_connection, DB_TABLE_STDLIB_RELEASES); + $filter = new FilterHelper(DB_TABLE_STDLIB_RELEASES, $db_connection); $semver_filters = array(); foreach(array('version-min', 'version-max') AS $field) { diff --git a/users/Suspension.php b/users/Suspension.php index bbbf412..ce0e6eb 100644 --- a/users/Suspension.php +++ b/users/Suspension.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 FilterHelper(DB_TABLE_SUSPENSIONS, $db_connection); $filter->add(array('db-name' => 'user', 'value' => $id, 'type' => 'binary')); From 16f83f63a2cb6a8d424c4a6381a78119445697a5 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:22:17 +0200 Subject: [PATCH 03/11] enable passing DB connection to FilterHelper->evaluate() Instead of passing it during construction, it can be passed (and overridden) later when it is actually needed by the code. --- FilterHelper.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/FilterHelper.php b/FilterHelper.php index 9ed90b5..eb65b81 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -7,7 +7,7 @@ class FilterHelper { /* * Public class instance interface */ - public function __construct($table, $db_connection) { + public function __construct($table, $db_connection = NULL) { $this->connection = $db_connection; $this->table = $table; } @@ -16,7 +16,14 @@ public function add($data) { $this->filters[] = $data; } - public function evaluate($source, $prefix = ' WHERE ') { + public function evaluate($source, $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!'); + } + $db_cond = ''; $this->source = $source; From 405ddaee60a44b0d0e8ad0ccec830514d79edbf5 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:32:08 +0200 Subject: [PATCH 04/11] FilterHelper: add a helper method for very simple filters --- FilterHelper.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/FilterHelper.php b/FilterHelper.php index eb65b81..0f031ae 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -268,5 +268,22 @@ public static function FromParams($filters, $source = NULL) { } return array_intersect_key($source, array_flip($filters)); } + + public static function SimpleFilter($table, $filters) { + $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' => $name)); + } else { + $f->add(array('db-name' => $name, 'value' => $value)); + } + } + + return $f; + } } ?> \ No newline at end of file From e48fe2843b9328e4f80585cf5ae1bfeb1e98cd8e Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:38:23 +0200 Subject: [PATCH 05/11] also allow table to be passed to FilterHelper later --- FilterHelper.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/FilterHelper.php b/FilterHelper.php index 0f031ae..ad4c114 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -7,15 +7,19 @@ class FilterHelper { /* * Public class instance interface */ - public function __construct($table, $db_connection = NULL) { + public function __construct($table = NULL, $db_connection = NULL) { $this->connection = $db_connection; - $this->table = $table; + $this->setDefaultTable($table); } public function add($data) { $this->filters[] = $data; } + public function setDefaultTable($table) { + $this->table = $table; + } + public function evaluate($source, $prefix = ' WHERE ', $db_connection = NULL) { if ($db_connection !== NULL) { $this->connection = $db_connection; @@ -23,6 +27,9 @@ public function evaluate($source, $prefix = ' WHERE ', $db_connection = NULL) { 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 = ''; $this->source = $source; @@ -269,7 +276,7 @@ public static function FromParams($filters, $source = NULL) { return array_intersect_key($source, array_flip($filters)); } - public static function SimpleFilter($table, $filters) { + public static function SimpleFilter($filters, $table = NULL) { $f = new self($table); foreach ($filters AS $name => $value) { From bb320ade8d3834cc40950e31acd3dd8cb0689d90 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:40:16 +0200 Subject: [PATCH 06/11] alias FilterHelper->add() to ->define() --- FilterHelper.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/FilterHelper.php b/FilterHelper.php index ad4c114..9b39ab6 100644 --- a/FilterHelper.php +++ b/FilterHelper.php @@ -16,6 +16,10 @@ public function add($data) { $this->filters[] = $data; } + public function define($data) { + return $this->add($data); + } + public function setDefaultTable($table) { $this->table = $table; } From fb1ac0a558e214c4345d6e01940727ceefd424d9 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 11 Apr 2013 22:52:59 +0200 Subject: [PATCH 07/11] Rename FilterHelper class to DataFilter Move the file in a subdirectory, adjust all usages. --- items/list.php | 8 ++++---- stdlib/candidates/Candidate.php | 6 +++--- stdlib/candidates/list.php | 4 ++-- stdlib/candidates/voting.php | 3 ++- stdlib/items.php | 4 ++-- stdlib/releases/StdlibRelease.php | 4 ++-- users/Suspension.php | 4 ++-- users/suspensions/list.php | 4 ++-- FilterHelper.php => util/DB/DataFilter.php | 4 ++-- 9 files changed, 21 insertions(+), 20 deletions(-) rename FilterHelper.php => util/DB/DataFilter.php (98%) diff --git a/items/list.php b/items/list.php index 2333e70..8116cfb 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_TABLE_ITEMS, $db_connection); + $filter = new DataFilter(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"]); @@ -85,7 +85,7 @@ function coerce_regex($value, $db_connection) { $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 - # 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 ebb607f..920aafe 100644 --- a/stdlib/candidates/Candidate.php +++ b/stdlib/candidates/Candidate.php @@ -1,7 +1,7 @@ '`date`', 'approval' => '`approval`')); - $filter = new FilterHelper(DB_TABLE_CANDIDATES, $db_connection); + $filter = new DataFilter(DB_TABLE_CANDIDATES, $db_connection); $filter->add(array('name' => 'item', 'type' => 'binary')); $filter->add(array('name' => 'user', 'type' => 'binary')); @@ -173,7 +173,7 @@ public static function listVotings($candidate, $filters = array(), $sort = array } $db_connection = db_ensure_connection(); - $filter = new FilterHelper(DB_TABLE_CANDIDATE_VOTING, $db_connection); + $filter = new DataFilter(DB_TABLE_CANDIDATE_VOTING, $db_connection); $filter->add(array('db-name' => 'candidate', 'value' => $candidate, 'type' => 'int')); $filter->add(array('name' => 'user', 'type' => 'binary')); 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 f938aeb..f706316 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,7 +15,7 @@ $db_sort = ''; $db_join = ''; - $filter = new FilterHelper(DB_TABLE_STDLIB, $db_connection); + $filter = new DataFilter(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)); diff --git a/stdlib/releases/StdlibRelease.php b/stdlib/releases/StdlibRelease.php index d93b408..9ac0f2c 100644 --- a/stdlib/releases/StdlibRelease.php +++ b/stdlib/releases/StdlibRelease.php @@ -1,7 +1,7 @@ add(array('db-name' => 'user', 'value' => $id, 'type' => 'binary')); 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 98% rename from FilterHelper.php rename to util/DB/DataFilter.php index 9b39ab6..d8d3f41 100644 --- a/FilterHelper.php +++ b/util/DB/DataFilter.php @@ -1,7 +1,7 @@ Date: Fri, 12 Apr 2013 12:14:36 +0200 Subject: [PATCH 08/11] allow an array of functions for DataFilter --- util/DB/DataFilter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/util/DB/DataFilter.php b/util/DB/DataFilter.php index d8d3f41..8728494 100644 --- a/util/DB/DataFilter.php +++ b/util/DB/DataFilter.php @@ -129,7 +129,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 From a523497bdd545ad1dab39c8535f61dbbf91ee5fb Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Wed, 17 Apr 2013 17:56:31 +0200 Subject: [PATCH 09/11] DataFilter: specify source in constructor, not evaluate() --- items/list.php | 6 +++--- stdlib/candidates/Candidate.php | 8 ++++---- stdlib/items.php | 4 ++-- stdlib/releases/StdlibRelease.php | 4 ++-- users/Suspension.php | 4 ++-- util/DB/DataFilter.php | 10 +++++++--- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/items/list.php b/items/list.php index 8116cfb..83e1abe 100644 --- a/items/list.php +++ b/items/list.php @@ -30,7 +30,7 @@ $db_limit = ""; $db_order = ''; - $filter = new DataFilter(DB_TABLE_ITEMS, $db_connection); + $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 @@ -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,7 +83,7 @@ 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 DataFilter) if ($get_rating = isset($_GET['rating']) || isset($_GET['rating-min']) || isset($_GET['rating-max']) || $sort_by_rating) { diff --git a/stdlib/candidates/Candidate.php b/stdlib/candidates/Candidate.php index 920aafe..1aaa655 100644 --- a/stdlib/candidates/Candidate.php +++ b/stdlib/candidates/Candidate.php @@ -143,7 +143,7 @@ public static function listCandidates($filters = array(), $sort = array()) { $db_connection = db_ensure_connection(); $db_sort = SortHelper::getOrderClause($sort, array('date' => '`date`', 'approval' => '`approval`')); - $filter = new DataFilter(DB_TABLE_CANDIDATES, $db_connection); + $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 DataFilter(DB_TABLE_CANDIDATE_VOTING, $db_connection); + $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/items.php b/stdlib/items.php index f706316..27e6958 100644 --- a/stdlib/items.php +++ b/stdlib/items.php @@ -15,13 +15,13 @@ $db_sort = ''; $db_join = ''; - $filter = new DataFilter(DB_TABLE_STDLIB, $db_connection); + $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 9ac0f2c..dcb6d12 100644 --- a/stdlib/releases/StdlibRelease.php +++ b/stdlib/releases/StdlibRelease.php @@ -187,7 +187,7 @@ public static function ListReleases($published, $filters = array(), $sort = arra $db_cond = ($t = self::get_publish_cond($published)) == NULL ? '' : " WHERE $t"; $db_connection = db_ensure_connection(); - $filter = new DataFilter(DB_TABLE_STDLIB_RELEASES, $db_connection); + $filter = new DataFilter($filters, DB_TABLE_STDLIB_RELEASES, $db_connection); $semver_filters = array(); foreach(array('version-min', 'version-max') AS $field) { @@ -209,7 +209,7 @@ public static function ListReleases($published, $filters = array(), $sort = arra # add these below semver preparation 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($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 171ee19..de17fa2 100644 --- a/users/Suspension.php +++ b/users/Suspension.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 DataFilter(DB_TABLE_SUSPENSIONS, $db_connection); + $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/util/DB/DataFilter.php b/util/DB/DataFilter.php index 8728494..033d781 100644 --- a/util/DB/DataFilter.php +++ b/util/DB/DataFilter.php @@ -7,7 +7,8 @@ class DataFilter { /* * Public class instance interface */ - public function __construct($table = NULL, $db_connection = NULL) { + public function __construct($source, $table = NULL, $db_connection = NULL) { + $this->SetSource($source); $this->connection = $db_connection; $this->setDefaultTable($table); } @@ -20,11 +21,15 @@ 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($source, $prefix = ' WHERE ', $db_connection = NULL) { + public function evaluate($prefix = ' WHERE ', $db_connection = NULL) { if ($db_connection !== NULL) { $this->connection = $db_connection; } @@ -36,7 +41,6 @@ public function evaluate($source, $prefix = ' WHERE ', $db_connection = NULL) { } $db_cond = ''; - $this->source = $source; foreach ($this->filters AS $filter) { From e2dc0a166436cb93c204fab832f65a6963568f8f Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Wed, 17 Apr 2013 17:59:53 +0200 Subject: [PATCH 10/11] DataFilter: declare instance variables --- util/DB/DataFilter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/DB/DataFilter.php b/util/DB/DataFilter.php index 033d781..8de3f7e 100644 --- a/util/DB/DataFilter.php +++ b/util/DB/DataFilter.php @@ -3,6 +3,9 @@ class DataFilter { private $filters = array(); + private $source = NULL; + private $table = NULL; + private $connection = NULL; /* * Public class instance interface From 004243cf452dbcd57484ba82a9183431e2b0ae80 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Wed, 17 Apr 2013 18:01:48 +0200 Subject: [PATCH 11/11] fix DataFilter::SimpleFilter --- util/DB/DataFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/DB/DataFilter.php b/util/DB/DataFilter.php index 8de3f7e..9d1c54a 100644 --- a/util/DB/DataFilter.php +++ b/util/DB/DataFilter.php @@ -297,7 +297,7 @@ public static function SimpleFilter($filters, $table = NULL) { $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' => $name)); + $f->add(array('name' => $value)); } else { $f->add(array('db-name' => $name, 'value' => $value)); }