From 4b837c9bf9558fc29c5798027a95e26e49b9f892 Mon Sep 17 00:00:00 2001 From: "V.Krishn" Date: Tue, 21 Jun 2016 17:33:51 +0530 Subject: add search opt sort(simple) --- docs/api-json.md | 11 +++++------ docs/install-alpinelinux.md | 7 ++++--- docs/install.md | 6 ++++-- index.php | 32 ++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/docs/api-json.md b/docs/api-json.md index 9855f96..c1f5af1 100644 --- a/docs/api-json.md +++ b/docs/api-json.md @@ -1,10 +1,9 @@ -# **JSONAPI** implementations for aports packages. - +# **JSONAPI** implementations for aports packages Api is build around packages data and its relationships with other tables in database, -using, simple one-to-one relationships (based on foreign-key/pid/id). +using, simple one-to-one relationships (based on *foreign-key/pid/id*). Currently, any JOINS in database is being avoided and maybe used if needed. @@ -84,7 +83,7 @@ __api uri__: `/maintainer/names/page/` ## Resources -1. JsonApi - [http://jsonapi.org/] -2. Jq - a lightweight and flexible command-line JSON processor [http://stedolan.github.io/jq/] -3. Curl [http://curl.haxx.se/] +1. __JsonApi__ - [http://jsonapi.org/] +2. __Jq__ - a lightweight and flexible command-line JSON processor [http://stedolan.github.io/jq/] +3. __Curl__ [http://curl.haxx.se/] diff --git a/docs/install-alpinelinux.md b/docs/install-alpinelinux.md index 8d182ba..aefb663 100644 --- a/docs/install-alpinelinux.md +++ b/docs/install-alpinelinux.md @@ -1,17 +1,18 @@ -Requirements: -============== +## Requirements: * php-apache2 * php-pdo_sqlite * php-phalcon * php-xml + 1. download and install aport-api 2. edit index.php to change variables 3. `apk add php-apache2 php-pdo_sqlite php-phalcon php-xml` 4. edit httpd.conf to enable rewrite rules 5. start apache2 -Note: php available maybe in name of format php[57]- +__Note__: php pkgs available could have prefixes of format +php5- or php7-. Do check branch/repo/arch for prefixes or do `apk search php` diff --git a/docs/install.md b/docs/install.md index e526d50..932dd1b 100644 --- a/docs/install.md +++ b/docs/install.md @@ -1,14 +1,16 @@ -Requirements: -============== +## Requirements: * php-apache2 * php-pdo_sqlite * php-phalcon * php-xml + 1. download and install aport-api 2. edit index.php to change variables 3. install packages listed above using pkg manager of your linux distro. 4. edit httpd.conf to enable rewrite rules 5. start apache2 + + diff --git a/index.php b/index.php index 3202cc0..4d0a536 100644 --- a/index.php +++ b/index.php @@ -174,19 +174,19 @@ The Aports API consists of the following methods: # TODO - clean text import fro Search filters are split into key/value pairs, order/presence in uri are optional, following keys/values are recognized 1. category/branch:repo:arch eg. category/v3.4:main:x86 ('all' keyword for any) - defaults to category/edge:main:x86 + defaults to category/edge:main:x86 if error in keywords 2. name/ ( wildcard recognized '_' ) - 3. maintainer/ ( wildcard recognized '_' ) # TODO + 3. maintainer/ ( wildcard recognized '_' ) 4. flagged/[yes|no] */ $app->get('/search/{where:[a-z0-9\_]+}/{filters:.*}', function($where, $filters) use ($app) { $data = initJapiData($app, 'search'); - //$filter = (array)sanitize_filters($filters, $where, $app); $_w = array('packages', 'contents'); //$_k = array('category', 'name', 'maintainer', 'flagged'); # TODO if ( ! in_array($where, $_w)) return; + //$filter = (array)sanitize_filters($filters, $where, $app); $f = explode('/', single_slash(urldecode($filters))); for($c=0; $c<=count($f); $c=$c+2) { # limit key/value to 56 chars each if($f[$c]) $filter[mb_substr(@$f[$c], 0, 56)] = mb_substr(@$f[$c+1], 0, 56); @@ -196,13 +196,14 @@ $app->get('/search/{where:[a-z0-9\_]+}/{filters:.*}', function($where, $filters) $filter['filter'] = array(); # Create customs filters # TODO $filter = set_search_category($filter); - $filter = set_search_name_pkg($filter); $filter = set_search_flagged($filter); $filter = set_search_maint($filter); if(isset($filter['page'])) $app->myapi->reqPage = (int)$filter['page']; if('packages' === $where) { + $filter = set_search_name_pkg($filter); + $filter = set_search_orderby_pkg($filter); $data = get_package($filter, $data, $app); } @@ -217,7 +218,7 @@ $app->get('/search/{where:[a-z0-9\_]+}/{filters:.*}', function($where, $filters) Advance Search by POST (expects simple key/value pairs in post data) eg. '{"name":"bas_","category":"edge:main:x86"}' */ -$app->post('/search/{where:[a-z0-9\_]+}', function($where, $filters) use ($app) { +$app->post('/search/{where:[a-z0-9\_]+}', function($where) use ($app) { $data = initJapiData($app, 'search'); $filter = array(); @@ -237,6 +238,7 @@ $app->post('/search/{where:[a-z0-9\_]+}', function($where, $filters) use ($app) if('packages' === $where) { $filter = set_search_name_pkg($filter); + $filter = set_search_orderby_pkg($filter); $data = get_package($filter, $data, $app); } @@ -295,6 +297,7 @@ function set_search_name_pkg($f) { } function set_search_maint($f) { if( ! array_key_exists('maintainer', $f) ) return $f; + # add char sanitizing # TODO $f = set_search_glob($f, 'maintainer', $f['maintainer'], 0); $_f = $f['filter']['maintainer']; @@ -322,6 +325,18 @@ function set_search_flagged($f) { } return $f; } +function set_search_orderby_pkg($f) { #only simple order/sort for now + $f['filter']['sort'] = "id DESC"; + if( ! array_key_exists('sort', $f) ) return $f; + list( $fld, $or ) = explode(':', $f['sort']); + $fields = array('id', 'name', 'maintainer', 'build_time', + 'fid', 'arch','branch', 'repo'); + $order = array('asc', 'desc'); + $fld = in_array($fld, $fields) ? $fld : 'id'; + $or = in_array(@$or, $order) ? $or : 'DESC'; + $f['filter']['sort'] = "$fld $or"; + return $f; +} // Retrieves all categories $app->get('/categories', function() use ($app) { @@ -343,16 +358,18 @@ $app->get('/packages', function() use ($app) { function get_package($filter=array(), $data=array(), $app) { $condt = implode(' AND ', @$filter['filter2']); - $params = array( 'conditions' => "$condt" ); + $sort = $filter['filter']['sort']; # get Packages count + $params = array( 'conditions' => "$condt" ); $res = Packages::find( $params ); $tnum = count($res); + setPageLinks('page', $tnum, $data, $app); $params = array( "conditions" => "$condt", - "order" => "id DESC", # add other simple orderby # TODO + "order" => "$sort", "limit" => $app->myapi->pglimit, "offset" => $app->myapi->offset ); @@ -412,7 +429,6 @@ $app->get('/packages/{id:[0-9]+}/relationships/{type}', function($id, $type) use } return $app->handle("/$type/$subtype/$id"); - //$app->handle('/404'); }); -- cgit v1.2.3