-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
193 lines (165 loc) · 5 KB
/
helper.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* GitHub Module for Joomla!
*
* @package GitHubModule
*
* @copyright Copyright (C) 2011 Michael Babker. All rights reserved.
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
*/
defined('_JEXEC') or die;
/**
* Helper class for the GitHub Module
*
* @package GitHubModule
* @since 1.0
*/
class ModGithubHelper
{
/**
* Function to compile the data to render a formatted object
*
* @param object $params The module parameters
*
* @return mixed An array of data on success, an object with a failure notice otherwise.
*
* @since 1.0
*/
static function compileData($params)
{
// Load the parameters
$uname = $params->get('username', '');
$repo = $params->get('repo', '');
// Convert the list name to a useable string for the JSON
if ($repo)
{
$frepo = self::toAscii($repo);
}
// Initialize the array
$github = array();
$req = 'https://api.github.com/repos/'.$uname.'/'.$frepo.'/commits';
// Fetch the decoded JSON
$obj = self::getJSON($req);
if (is_null($obj))
{
$github->error = 'Error';
return $github;
}
// Process the filtering options and render the feed
$github = self::processData($obj, $params);
return $github;
}
/**
* Function to fetch a JSON feed
*
* @param string $req The URL of the feed to load
*
* @return array The decoded JSON query
*
* @since 1.0
*/
static function getJSON($req)
{
// Create a new CURL resource
$ch = curl_init($req);
// Set options
curl_setopt($ch, CURLOPT_HEADER, false);
// this is for api version 3
$t_vers = curl_version();
curl_setopt( $ch, CURLOPT_USERAGENT, 'curl/' . $t_vers['version'] );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Grab URL and pass it to the browser and store it as $json
$json = curl_exec($ch);
// Close CURL resource
curl_close($ch);
// Decode the fetched JSON
$obj = json_decode($json, true);
return $obj;
}
/**
* Function to process the GitHub data into a formatted object
*
* @param array $obj The JSON data
* @param object $params The module parameters
*
* @return array An array of data for output
*
* @since 1.0
*/
static function processData($obj, $params)
{
// Initialize
$github = array();
$i = 0;
// Load the parameters
$uname = $params->get('username', '');
$repo = $params->get('repo', '');
$count = $params->get('count', 3) - 1;
// Convert the list name to a useable string for the JSON
if ($repo)
{
$frepo = self::toAscii($repo);
}
// Process the feed
foreach ($obj as $o)
{
if ($i <= $count)
{
// Initialize a new object
$github[$i]->commit = new stdClass;
// The commit message linked to the commit
$github[$i]->commit->message = '<a href="https://github.com/'.$uname.'/'.$frepo.'/commit/'.$o['sha'].'" target="_blank" rel="nofollow">'.substr($o['sha'], 0, 7).'</a> - ';
$github[$i]->commit->message .= preg_replace("/#(\w+)/", '#<a href="https://github.com/'.$uname.'/'.$frepo.'/issues/\\1" target="_blank" rel="nofollow">\\1</a>', htmlspecialchars($o['commit']['message']));
// Check if the committer information
if ($o['author']['id'] != $o['committer']['id'])
{
// The committer name formatted with link
$github[$i]->commit->committer = JText::_('MOD_GITHUB_AND_COMMITTED_BY').'<a href="https://github.com/'.$o['committer']['login'].'" target="_blank" rel="nofollow">'.$o['commit']['committer']['name'].'</a>';
// The author wasn't the committer
$github[$i]->commit->author = JText::_('MOD_GITHUB_AUTHORED_BY');
}
else
{
// The author is also the committer
$github[$i]->commit->author = JText::_('MOD_GITHUB_COMMITTED_BY');
}
// The author name formatted with link
$github[$i]->commit->author .= '<a href="https://github.com/'.$o['author']['login'].'" target="_blank" rel="nofollow">'.$o['commit']['author']['name'].'</a>';
// The time of commit
$date = date_create($o['commit']['committer']['date']);
$date = date_format($date, 'r');
if ($params->get('relativeTime', '1') == '1')
{
$ISOtime = JHTML::date($date, 'Y-m-d H:i:s');
// Load the JavaScript; first ensure we have MooTools Core
JHtml::_('behavior.framework');
JHtml::script('modules/mod_github/media/js/prettydate.js', false, false);
$github[$i]->commit->time = ' <span class="commit-time" title="'.$ISOtime.'">'.JHtml::date($date, 'D M d H:i:s O Y').'</span>';
}
else
{
$github[$i]->commit->time = ' '.JHtml::date($date);
}
$i++;
}
}
return $github;
}
/**
* Function to convert a formatted repo name into it's URL equivalent
*
* @param string $repo The user inputted repo name
*
* @return string The repo name converted
*
* @since 1.0
*/
static function toAscii($repo)
{
$clean = preg_replace("/[^a-z'A-Z0-9\/_|+ -]/", '', $repo);
$clean = strtolower(trim($clean, '-'));
$repo = preg_replace("/[\/_|+ -']+/", '-', $clean);
return $repo;
}
}