/
ajax.php
144 lines (109 loc) · 4.64 KB
/
ajax.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
require_once 'phpQuery-onefile.php';
switch ($_REQUEST ["action"]){
case "view" :
$owner = $_REQUEST ["owner"];
$repo = $_REQUEST ["name"];
$url = "https://api.github.com/repos/" . $owner . "/" . $repo . "/readme";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_FAILONERROR, false );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 300 );
curl_setopt ( $ch, CURLOPT_POST, 0 );
$json = json_decode ( curl_exec ( $ch ), true );
$markdown = base64_decode ( $json ["content"] );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, "https://api.github.com/markdown/raw" );
curl_setopt ( $ch, CURLOPT_FAILONERROR, false );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 300 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $markdown );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ("Content-type: text/plain" ) );
$markdown = curl_exec ( $ch );
echo json_encode ( array ("content" => $markdown ) );
break;
case "search" :
$queryParams = array ("repo" => "", "langOverride" => "", "type" => "Repositories", "start_value" => "1" );
$filterParts = array ("forks" => array ("*", "*" ), "followers" => array ("*", "*" ), "pushed" => array ("*", "*" ) );
foreach ( $_REQUEST as $k => $v ) {
$v = trim ( $v ) ? $v : "*";
switch ($k) {
case "forks_min" :
$filterParts ["forks"] [0] = $v;
break;
case "forks_max" :
$filterParts ["forks"] [1] = $v;
break;
case "followers_min" :
$filterParts ["followers"] [0] = $v;
break;
case "followers_max" :
$filterParts ["followers"] [1] = $v;
break;
case "push_min" :
$filterParts ["pushed"] [0] = $v;
break;
case "push_max" :
$filterParts ["pushed"] [1] = $v;
break;
case "lastPage" :
$queryParams ["start_value"] = $v;
break;
case "language" :
$queryParams ["language"] = $v;
break;
default :
break;
}
}
$luceneFilters = array ();
foreach ( $filterParts as $key => $values ) {
if ($values [0] == "*" && $values [1] == "*") {
continue;
}
$luceneFilters [] = $key . ":[" . join ( " TO ", $values ) . "]";
}
$queryParams ["q"] = join ( " ", $luceneFilters );
$returnJSON = array ();
for($i = 0; $i < 3; $i ++) {
$returnJSON = array_merge ( $returnJSON, getGitHubResults ( $queryParams ) );
$queryParams ["start_value"] += 1;
}
shuffle($returnJSON);
echo json_encode ( $returnJSON );
break;
default: break;
}
function getGitHubResults($queryParams) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, "https://github.com/search/?" . http_build_query ( $queryParams ) );
curl_setopt ( $ch, CURLOPT_FAILONERROR, false );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 300 );
curl_setopt ( $ch, CURLOPT_POST, 0 );
$html = curl_exec ( $ch );
$doc = phpQuery::newDocument ( $html );
$returnJSON = array ();
foreach ( $doc [".result"] as $resultDiv ) {
$obj = array ();
list ( $user, $repo ) = explode ( "/", pq ( $resultDiv )->find ( ".title a" )->text () );
$language = trim ( str_replace ( array (")", "(" ), "", pq ( $resultDiv )->find ( ".title .language" )->text () ) );
$detailsParts = explode ( "|", pq ( $resultDiv )->find ( ".details" )->text () );
$obj ["owner"] = trim ( $user );
$obj ["name"] = trim ( $repo );
$obj ["language"] = $language;
$obj ["description"] = trim ( pq ( $resultDiv )->find ( ".description" )->text () );
$obj ["size"] = trim ( $detailsParts [0] );
$obj ["forks"] = trim ( str_replace ( "forks", "", $detailsParts [1] ) );
$obj ["watchers"] = trim ( str_replace ( "watchers", "", $detailsParts [2] ) );
$obj ["last_push"] = trim ( str_replace ( "last activity", "", $detailsParts [3] ) );
$obj ["url"] = pq ( $resultDiv )->find ( ".title a" )->attr ( "href" );
$returnJSON [] = $obj;
}
return $returnJSON;
}