-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grok.php
76 lines (66 loc) · 1.81 KB
/
Grok.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace dokuwiki\plugin\xref;
use dokuwiki\HTTP\DokuHTTPClient;
class Grok
{
protected $baseUrl;
protected $def;
protected $path;
public function __construct($reference, $baseUrl = 'https://codesearch.dokuwiki.org')
{
$heuristic = new Heuristics($reference);
$this->def = $heuristic->getDef();
$this->path = $heuristic->getPath();
$this->baseUrl = rtrim($baseUrl, '/');
}
/**
* Return the URL that leads to the search interface
*
* @return string
*/
public function getSearchUrl()
{
if ($this->def === '' && $this->path === '') return $this->baseUrl;
$url = $this->baseUrl . '/search?';
$param = [
'project' => 'dokuwiki',
'defs' => $this->def,
'path' => $this->path,
];
$url .= buildURLparams($param, '&');
return $url;
}
/**
* Return the URL that allows to query the API
*
* @return string
*/
public function getAPIUrl()
{
$url = $this->baseUrl . '/api/v1/search?';
$param = [
'projects' => 'dokuwiki',
'def' => $this->def,
'path' => $this->path,
];
$url .= buildURLparams($param, '&');
return $url;
}
/**
* Return the number of results to expect
*
* @return false|int false on errors
*/
public function getResultCount()
{
if ($this->def === '' && $this->path === '') return 0;
$http = new DokuHTTPClient();
$http->timeout = 5;
$json = $http->get($this->getAPIUrl());
if (!$json) return false;
$data = json_decode($json, true);
if (!$data) return false;
if (!isset($data['resultCount'])) return false;
return $data['resultCount'];
}
}