From 226508cd8d3e8c147ad314a0de483e08be71c254 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 3 Feb 2016 06:21:30 +0100 Subject: first frontend --- src/Elasticsearch.php | 138 --------------------------------- src/Elasticsearch/Request.php | 35 --------- src/Html/Pager.php | 66 ---------------- src/functions.php | 11 --- src/init.php | 6 ++ src/phinde/Autoloader.php | 21 +++++ src/phinde/Elasticsearch.php | 146 +++++++++++++++++++++++++++++++++++ src/phinde/Elasticsearch/Request.php | 35 +++++++++ src/phinde/Helper.php | 15 ++++ src/phinde/Html/Pager.php | 66 ++++++++++++++++ 10 files changed, 289 insertions(+), 250 deletions(-) delete mode 100644 src/Elasticsearch.php delete mode 100644 src/Elasticsearch/Request.php delete mode 100644 src/Html/Pager.php delete mode 100644 src/functions.php create mode 100644 src/init.php create mode 100644 src/phinde/Autoloader.php create mode 100644 src/phinde/Elasticsearch.php create mode 100644 src/phinde/Elasticsearch/Request.php create mode 100644 src/phinde/Helper.php create mode 100644 src/phinde/Html/Pager.php (limited to 'src') diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php deleted file mode 100644 index b3f3067..0000000 --- a/src/Elasticsearch.php +++ /dev/null @@ -1,138 +0,0 @@ -baseUrl = $baseUrl; - } - - /** - * @link https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html - */ - public function isKnown($url) - { - $r = new Elasticsearch_Request( - $this->baseUrl . 'document/_search/exists', - \HTTP_Request2::METHOD_GET - ); - $r->allow404 = true; - $r->setBody( - json_encode( - array( - 'query' => array( - 'filtered' => array( - 'filter' => array( - 'term' => array( - 'url' => $url - ) - ) - ) - ) - ) - ) - ); - $res = json_decode($r->send()->getBody()); - return $res->exists; - } - - public function get($url) - { - $r = new Elasticsearch_Request( - $this->baseUrl . 'document/' . rawurlencode($url), - \HTTP_Request2::METHOD_GET - ); - $r->allow404 = true; - $res = $r->send(); - if ($res->getStatus() != 200) { - return null; - } - $d = json_decode($res->getBody()); - return $d->_source; - } - - public function markQueued($url) - { - $r = new Elasticsearch_Request( - $this->baseUrl . 'document/' . rawurlencode($url), - \HTTP_Request2::METHOD_PUT - ); - $doc = array( - 'status' => 'queued', - 'url' => $url - ); - $r->setBody(json_encode($doc)); - $r->send(); - } - - public function search($query, $page, $perPage) - { - $r = new Elasticsearch_Request( - $this->baseUrl . 'document/_search', - \HTTP_Request2::METHOD_GET - ); - $doc = array( - '_source' => array( - 'url', - 'title', - 'author', - 'modate', - ), - 'query' => array( - 'bool' => array( - 'must' => array( - array( - 'query_string' => array( - 'default_field' => '_all', - 'query' => $query - ) - ), - array( - 'term' => array( - 'status' => 'indexed' - ) - ) - ) - ) - ), - 'aggregations' => array( - 'tags' => array( - 'terms' => array( - 'field' => 'tags' - ) - ), - 'language' => array( - 'terms' => array( - 'field' => 'language' - ) - ), - 'domain' => array( - 'terms' => array( - 'field' => 'domain' - ) - ), - 'type' => array( - 'terms' => array( - 'field' => 'type' - ) - ) - ), - 'from' => $page * $perPage, - 'size' => $perPage, - 'sort' => array( - //array('modate' => array('order' => 'desc')) - ) - ); - //unset($doc['_source']); - - //ini_set('xdebug.var_display_max_depth', 10); - //return json_decode(json_encode($doc)); - $r->setBody(json_encode($doc)); - $res = $r->send(); - return json_decode($res->getBody()); - } -} -?> diff --git a/src/Elasticsearch/Request.php b/src/Elasticsearch/Request.php deleted file mode 100644 index 7bb6add..0000000 --- a/src/Elasticsearch/Request.php +++ /dev/null @@ -1,35 +0,0 @@ -getStatus() / 100); - if ($mainCode === 2) { - return $res; - } - - if ($this->allow404 && $res->getStatus() == 404) { - return $res; - } - $js = json_decode($res->getBody()); - if (isset($js->error)) { - $error = json_encode($js->error); - } else { - $error = $res->getBody(); - } - - throw new \Exception( - 'Error in elasticsearch communication at ' - . $this->getMethod() . ' ' . (string) $this->getUrl() - . ' (status code ' . $res->getStatus() . '): ' - . $error - ); - } -} - -?> diff --git a/src/Html/Pager.php b/src/Html/Pager.php deleted file mode 100644 index a14a53d..0000000 --- a/src/Html/Pager.php +++ /dev/null @@ -1,66 +0,0 @@ -pager = \Pager::factory( - array( - 'mode' => 'Sliding', - 'perPage' => $perPage, - 'delta' => 2, - 'totalItems' => $itemCount, - 'currentPage' => $currentPage, - 'urlVar' => 'page', - 'append' => $append, - 'path' => '', - 'fileName' => $filename, - 'separator' => '###', - 'spacesBeforeSeparator' => 0, - 'spacesAfterSeparator' => 0, - 'curPageSpanPre' => '', - 'curPageSpanPost' => '', - 'firstPagePre' => '', - 'firstPageText' => 'first', - 'firstPagePost' => '', - 'lastPagePre' => '', - 'lastPageText' => 'last', - 'lastPagePost' => '', - 'prevImg' => '« prev', - 'nextImg' => 'next »', - ) - ); - } - - - public function getLinks() - { - $arLinks = $this->pager->getLinks(); - $arLinks['pages'] = explode('###', $arLinks['pages']); - return $arLinks; - } - - public function numPages() - { - return $this->pager->numPages(); - } -} - -?> diff --git a/src/functions.php b/src/functions.php deleted file mode 100644 index fd91360..0000000 --- a/src/functions.php +++ /dev/null @@ -1,11 +0,0 @@ - diff --git a/src/init.php b/src/init.php new file mode 100644 index 0000000..d6aa35f --- /dev/null +++ b/src/init.php @@ -0,0 +1,6 @@ + diff --git a/src/phinde/Autoloader.php b/src/phinde/Autoloader.php new file mode 100644 index 0000000..4717e1c --- /dev/null +++ b/src/phinde/Autoloader.php @@ -0,0 +1,21 @@ + diff --git a/src/phinde/Elasticsearch.php b/src/phinde/Elasticsearch.php new file mode 100644 index 0000000..4bc4637 --- /dev/null +++ b/src/phinde/Elasticsearch.php @@ -0,0 +1,146 @@ +baseUrl = $baseUrl; + } + + /** + * @link https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html + */ + public function isKnown($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/_search/exists', + \HTTP_Request2::METHOD_GET + ); + $r->allow404 = true; + $r->setBody( + json_encode( + array( + 'query' => array( + 'filtered' => array( + 'filter' => array( + 'term' => array( + 'url' => $url + ) + ) + ) + ) + ) + ) + ); + $res = json_decode($r->send()->getBody()); + return $res->exists; + } + + public function get($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/' . rawurlencode($url), + \HTTP_Request2::METHOD_GET + ); + $r->allow404 = true; + $res = $r->send(); + if ($res->getStatus() != 200) { + return null; + } + $d = json_decode($res->getBody()); + return $d->_source; + } + + public function markQueued($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/' . rawurlencode($url), + \HTTP_Request2::METHOD_PUT + ); + $doc = array( + 'status' => 'queued', + 'url' => $url + ); + $r->setBody(json_encode($doc)); + $r->send(); + } + + public function search($query, $filters, $page, $perPage) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/_search', + \HTTP_Request2::METHOD_GET + ); + $doc = array( + '_source' => array( + 'url', + 'title', + 'author', + 'modate', + ), + 'query' => array( + 'bool' => array( + 'must' => array( + array( + 'query_string' => array( + 'default_field' => '_all', + 'query' => $query + ) + ), + array( + 'term' => array( + 'status' => 'indexed' + ) + ), + ) + ) + ), + 'aggregations' => array( + 'tags' => array( + 'terms' => array( + 'field' => 'tags' + ) + ), + 'language' => array( + 'terms' => array( + 'field' => 'language' + ) + ), + 'domain' => array( + 'terms' => array( + 'field' => 'domain' + ) + ), + 'type' => array( + 'terms' => array( + 'field' => 'type' + ) + ) + ), + 'from' => $page * $perPage, + 'size' => $perPage, + 'sort' => array( + //array('modate' => array('order' => 'desc')) + ) + ); + foreach ($filters as $type => $value) { + $doc['query']['bool']['must'][] = array( + 'term' => array( + $type => $value + ) + ); + } + + //unset($doc['_source']); + + //ini_set('xdebug.var_display_max_depth', 10); + //return json_decode(json_encode($doc)); + $r->setBody(json_encode($doc)); + $res = $r->send(); + return json_decode($res->getBody()); + } +} +?> diff --git a/src/phinde/Elasticsearch/Request.php b/src/phinde/Elasticsearch/Request.php new file mode 100644 index 0000000..7bb6add --- /dev/null +++ b/src/phinde/Elasticsearch/Request.php @@ -0,0 +1,35 @@ +getStatus() / 100); + if ($mainCode === 2) { + return $res; + } + + if ($this->allow404 && $res->getStatus() == 404) { + return $res; + } + $js = json_decode($res->getBody()); + if (isset($js->error)) { + $error = json_encode($js->error); + } else { + $error = $res->getBody(); + } + + throw new \Exception( + 'Error in elasticsearch communication at ' + . $this->getMethod() . ' ' . (string) $this->getUrl() + . ' (status code ' . $res->getStatus() . '): ' + . $error + ); + } +} + +?> diff --git a/src/phinde/Helper.php b/src/phinde/Helper.php new file mode 100644 index 0000000..4863961 --- /dev/null +++ b/src/phinde/Helper.php @@ -0,0 +1,15 @@ + diff --git a/src/phinde/Html/Pager.php b/src/phinde/Html/Pager.php new file mode 100644 index 0000000..a14a53d --- /dev/null +++ b/src/phinde/Html/Pager.php @@ -0,0 +1,66 @@ +pager = \Pager::factory( + array( + 'mode' => 'Sliding', + 'perPage' => $perPage, + 'delta' => 2, + 'totalItems' => $itemCount, + 'currentPage' => $currentPage, + 'urlVar' => 'page', + 'append' => $append, + 'path' => '', + 'fileName' => $filename, + 'separator' => '###', + 'spacesBeforeSeparator' => 0, + 'spacesAfterSeparator' => 0, + 'curPageSpanPre' => '', + 'curPageSpanPost' => '', + 'firstPagePre' => '', + 'firstPageText' => 'first', + 'firstPagePost' => '', + 'lastPagePre' => '', + 'lastPageText' => 'last', + 'lastPagePost' => '', + 'prevImg' => '« prev', + 'nextImg' => 'next »', + ) + ); + } + + + public function getLinks() + { + $arLinks = $this->pager->getLinks(); + $arLinks['pages'] = explode('###', $arLinks['pages']); + return $arLinks; + } + + public function numPages() + { + return $this->pager->numPages(); + } +} + +?> -- cgit v1.2.3