aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2016-02-01 20:18:59 +0100
committerChristian Weiske <cweiske@cweiske.de>2016-02-01 20:18:59 +0100
commit7b4425b096fa8c18d0db9fd9b1ae96d63ee8af55 (patch)
tree2dc5d998a1fd0238592b451ff88b5c4cec064f9b /src
downloadphinde-7b4425b096fa8c18d0db9fd9b1ae96d63ee8af55.tar.gz
phinde-7b4425b096fa8c18d0db9fd9b1ae96d63ee8af55.zip
first kinda working version
Diffstat (limited to 'src')
-rw-r--r--src/Elasticsearch.php138
-rw-r--r--src/Elasticsearch/Request.php35
-rw-r--r--src/Html/Pager.php66
-rw-r--r--src/functions.php11
4 files changed, 250 insertions, 0 deletions
diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php
new file mode 100644
index 0000000..b3f3067
--- /dev/null
+++ b/src/Elasticsearch.php
@@ -0,0 +1,138 @@
+<?php
+namespace phinde;
+
+class Elasticsearch
+{
+ protected $baseUrl;
+
+ public function __construct($baseUrl)
+ {
+ $this->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
new file mode 100644
index 0000000..7bb6add
--- /dev/null
+++ b/src/Elasticsearch/Request.php
@@ -0,0 +1,35 @@
+<?php
+namespace phinde;
+
+class Elasticsearch_Request extends \HTTP_Request2
+{
+ public $allow404 = false;
+
+ public function send()
+ {
+ $res = parent::send();
+ $mainCode = intval($res->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
new file mode 100644
index 0000000..a14a53d
--- /dev/null
+++ b/src/Html/Pager.php
@@ -0,0 +1,66 @@
+<?php
+namespace phinde;
+
+class Html_Pager
+{
+ protected $pager;
+
+ /**
+ * Create a new pager
+ *
+ * @param integer $itemCount Number of items in total
+ * @param integer $perPage Number of items on one page
+ * @param integer $currentPage Current page, beginning with 1
+ * @param string $filename URL the page number shall be appended
+ */
+ public function __construct($itemCount, $perPage, $currentPage, $filename)
+ {
+ $append = true;
+ if (strpos($filename, '%d') !== false) {
+ $append = false;
+ }
+ //fix non-static factory method error
+ error_reporting(error_reporting() & ~E_STRICT);
+ $this->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
new file mode 100644
index 0000000..fd91360
--- /dev/null
+++ b/src/functions.php
@@ -0,0 +1,11 @@
+<?php
+function isUrlAllowed($url)
+{
+ $urlDomain = parse_url($url, PHP_URL_HOST);
+ if (!in_array($urlDomain, $GLOBALS['phinde']['domains'])) {
+ return false;
+ }
+ return true;
+}
+
+?>