|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Wggithub; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits |
|
7
|
|
|
of supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* wgGitHub module for xoops |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2020 XOOPS Project (https://xooops.org) |
|
19
|
|
|
* @license GPL 2.0 or later |
|
20
|
|
|
* @package wggithub |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
* @min_xoops 2.5.10 |
|
23
|
|
|
* @author Goffy - XOOPS Development Team - Email:<[email protected]> - Website:<https://wedega.com> |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use XoopsModules\Wggithub; |
|
27
|
|
|
use XoopsModules\Wggithub\{ |
|
28
|
|
|
Constants, |
|
|
|
|
|
|
29
|
|
|
MDParser |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class Object Handler Readmes |
|
35
|
|
|
*/ |
|
36
|
|
|
class ReadmesHandler extends \XoopsPersistableObjectHandler |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor |
|
40
|
|
|
* |
|
41
|
|
|
* @param \XoopsDatabase $db |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(\XoopsDatabase $db) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($db, 'wggithub_readmes', Readmes::class, 'rm_id', 'rm_name'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param bool $isNew |
|
50
|
|
|
* |
|
51
|
|
|
* @return object |
|
52
|
|
|
*/ |
|
53
|
|
|
public function create($isNew = true) |
|
54
|
|
|
{ |
|
55
|
|
|
return parent::create($isNew); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* retrieve a field |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $i field id |
|
62
|
|
|
* @param null fields |
|
|
|
|
|
|
63
|
|
|
* @return mixed reference to the {@link Get} object |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get($i = null, $fields = null) |
|
66
|
|
|
{ |
|
67
|
|
|
return parent::get($i, $fields); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* get inserted id |
|
72
|
|
|
* |
|
73
|
|
|
* @param null |
|
74
|
|
|
* @return int reference to the {@link Get} object |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getInsertId() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->db->getInsertId(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get Count Readmes in the database |
|
83
|
|
|
* @param int $start |
|
84
|
|
|
* @param int $limit |
|
85
|
|
|
* @param string $sort |
|
86
|
|
|
* @param string $order |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getCountReadmes($start = 0, $limit = 0, $sort = 'rm_id ASC, rm_name', $order = 'ASC') |
|
90
|
|
|
{ |
|
91
|
|
|
$crCountReadmes = new \CriteriaCompo(); |
|
|
|
|
|
|
92
|
|
|
$crCountReadmes = $this->getReadmesCriteria($crCountReadmes, $start, $limit, $sort, $order); |
|
93
|
|
|
return $this->getCount($crCountReadmes); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get All Readmes in the database |
|
98
|
|
|
* @param int $start |
|
99
|
|
|
* @param int $limit |
|
100
|
|
|
* @param string $sort |
|
101
|
|
|
* @param string $order |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getAllReadmes($start = 0, $limit = 0, $sort = 'rm_id ASC, rm_name', $order = 'ASC') |
|
105
|
|
|
{ |
|
106
|
|
|
$crAllReadmes = new \CriteriaCompo(); |
|
107
|
|
|
$crAllReadmes = $this->getReadmesCriteria($crAllReadmes, $start, $limit, $sort, $order); |
|
108
|
|
|
return $this->getAll($crAllReadmes); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get Criteria Readmes |
|
113
|
|
|
* @param $crReadmes |
|
114
|
|
|
* @param int $start |
|
115
|
|
|
* @param int $limit |
|
116
|
|
|
* @param string $sort |
|
117
|
|
|
* @param string $order |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
|
|
private function getReadmesCriteria($crReadmes, $start, $limit, $sort, $order) |
|
121
|
|
|
{ |
|
122
|
|
|
$crReadmes->setStart($start); |
|
123
|
|
|
$crReadmes->setLimit($limit); |
|
124
|
|
|
$crReadmes->setSort($sort); |
|
125
|
|
|
$crReadmes->setOrder($order); |
|
126
|
|
|
return $crReadmes; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Update table requests |
|
131
|
|
|
* |
|
132
|
|
|
* @return boolean |
|
133
|
|
|
*/ |
|
134
|
|
|
public function updateTableReadmes() |
|
135
|
|
|
{ |
|
136
|
|
|
$helper = Wggithub\Helper::getInstance(); |
|
137
|
|
|
$repositoriesHandler = $helper->getHandler('Repositories'); |
|
138
|
|
|
$readmesHandler = $helper->getHandler('Readmes'); |
|
139
|
|
|
|
|
140
|
|
|
$libRepositories = new Wggithub\Github\Repositories(); |
|
141
|
|
|
|
|
142
|
|
|
$submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
|
143
|
|
|
|
|
144
|
|
|
$repositoriesCount = $repositoriesHandler->getCount(); |
|
145
|
|
|
if ($repositoriesCount > 0) { |
|
146
|
|
|
$repositoriesAll = $repositoriesHandler->getAll(); |
|
147
|
|
|
foreach (\array_keys($repositoriesAll) as $i) { |
|
148
|
|
|
$repoId = $repositoriesAll[$i]->getVar('repo_id'); |
|
149
|
|
|
$repoStatus = $repositoriesAll[$i]->getVar('repo_status'); |
|
150
|
|
|
$crReadmes = new \CriteriaCompo(); |
|
151
|
|
|
$crReadmes->add(new \Criteria('rm_repoid', $repoId)); |
|
|
|
|
|
|
152
|
|
|
$readmesCount = $readmesHandler->getCount($crReadmes); |
|
153
|
|
|
if (Constants::STATUS_NEW === $repoStatus || Constants::STATUS_UPDATED === $repoStatus || 0 == $readmesCount) { |
|
154
|
|
|
$readme = $libRepositories->getReadme($repositoriesAll[$i]->getVar('repo_user'), $repositoriesAll[$i]->getVar('repo_name')); |
|
155
|
|
|
if ($readmesCount > 0) { |
|
156
|
|
|
$readmesAll = $readmesHandler->getAll($crReadmes); |
|
157
|
|
|
foreach (\array_keys($readmesAll) as $j) { |
|
158
|
|
|
$rmId = $readmesAll[$j]->getVar('rm_id'); |
|
159
|
|
|
} |
|
160
|
|
|
unset($crReadmes, $readmesAll); |
|
161
|
|
|
$readmesObj = $readmesHandler->get($rmId); |
|
|
|
|
|
|
162
|
|
|
} else { |
|
163
|
|
|
$readmesObj = $readmesHandler->create(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
if (\is_array($readme)) { |
|
|
|
|
|
|
167
|
|
|
$readmesObj->setVar('rm_repoid', $repoId); |
|
|
|
|
|
|
168
|
|
|
$readmesObj->setVar('rm_name', $readme['name']); |
|
169
|
|
|
$readmesObj->setVar('rm_type', $readme['type']); |
|
170
|
|
|
$readmesObj->setVar('rm_content', $readme['content']); |
|
171
|
|
|
$readmesObj->setVar('rm_encoding', $readme['encoding']); |
|
172
|
|
|
$readmesObj->setVar('rm_downloadurl', $readme['download_url']); |
|
173
|
|
|
$readmesObj->setVar('rm_datecreated',time()); |
|
174
|
|
|
$readmesObj->setVar('rm_submitter', $submitter); |
|
175
|
|
|
// Insert Data |
|
176
|
|
|
if (!$readmesHandler->insert($readmesObj)) { |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Update table requests |
|
189
|
|
|
* |
|
190
|
|
|
* @param $repoId |
|
191
|
|
|
* @param $userName |
|
192
|
|
|
* @param $repoName |
|
193
|
|
|
* @return boolean |
|
194
|
|
|
*/ |
|
195
|
|
|
public function updateReadmes($repoId, $userName, $repoName) |
|
196
|
|
|
{ |
|
197
|
|
|
$helper = Wggithub\Helper::getInstance(); |
|
198
|
|
|
$repositoriesHandler = $helper->getHandler('Repositories'); |
|
|
|
|
|
|
199
|
|
|
$readmesHandler = $helper->getHandler('Readmes'); |
|
200
|
|
|
|
|
201
|
|
|
$libRepositories = new Wggithub\Github\Repositories(); |
|
202
|
|
|
|
|
203
|
|
|
$submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
|
204
|
|
|
|
|
205
|
|
|
$readme = $libRepositories->getReadme($userName, $repoName); |
|
206
|
|
|
if (false === $readme) { |
|
|
|
|
|
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
if (count($readme) > 0) { |
|
|
|
|
|
|
210
|
|
|
if (array_key_exists('message', $readme)) { |
|
|
|
|
|
|
211
|
|
|
// not readme found |
|
212
|
|
|
// must return true otherwise releases will not be loaded |
|
213
|
|
|
return true; |
|
214
|
|
|
} |
|
215
|
|
|
$rmId = 0; |
|
216
|
|
|
$crReadmes = new \CriteriaCompo(); |
|
217
|
|
|
$crReadmes->add(new \Criteria('rm_repoid', $repoId)); |
|
218
|
|
|
$readmesAll = $readmesHandler->getAll($crReadmes); |
|
219
|
|
|
foreach (\array_keys($readmesAll) as $j) { |
|
220
|
|
|
$rmId = $readmesAll[$j]->getVar('rm_id'); |
|
221
|
|
|
} |
|
222
|
|
|
unset($crReadmes, $readmesAll); |
|
223
|
|
|
if ($rmId > 0) { |
|
224
|
|
|
$readmesObj = $readmesHandler->get($rmId); |
|
225
|
|
|
} else { |
|
226
|
|
|
$readmesObj = $readmesHandler->create(); |
|
227
|
|
|
} |
|
228
|
|
|
$readmesObj->setVar('rm_repoid', $repoId); |
|
229
|
|
|
$readmesObj->setVar('rm_name', $readme['name']); |
|
230
|
|
|
$readmesObj->setVar('rm_type', $readme['type']); |
|
231
|
|
|
/* |
|
232
|
|
|
* TODO |
|
233
|
|
|
$contentDecoded = base64_decode($this->getVar('rm_content', 'n')); |
|
234
|
|
|
if ('.MD' == substr(strtoupper($readme['name']), -3)) { |
|
235
|
|
|
$contentClean = convertMD($contentDecoded); |
|
236
|
|
|
} else { |
|
237
|
|
|
$contentClean = $contentDecoded; |
|
238
|
|
|
} |
|
239
|
|
|
$readmesObj->setVar('rm_content', $contentClean); |
|
240
|
|
|
*/ |
|
241
|
|
|
$readmesObj->setVar('rm_content', $readme['content']); |
|
242
|
|
|
$readmesObj->setVar('rm_encoding', $readme['encoding']); |
|
243
|
|
|
$readmesObj->setVar('rm_downloadurl', $readme['download_url']); |
|
244
|
|
|
$readmesObj->setVar('rm_datecreated',time()); |
|
245
|
|
|
$readmesObj->setVar('rm_submitter', $submitter); |
|
246
|
|
|
// Insert Data |
|
247
|
|
|
if (!$readmesHandler->insert($readmesObj)) { |
|
248
|
|
|
return false; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* TODO |
|
257
|
|
|
* convert md file content into clean text |
|
258
|
|
|
* and replace relative path for images into full path |
|
259
|
|
|
* |
|
260
|
|
|
* @param $contentDecoded |
|
261
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
|
|
public function convertMD($contentDecoded) |
|
264
|
|
|
{ |
|
265
|
|
|
$contentClean = ''; |
|
|
|
|
|
|
266
|
|
|
// parse MD file |
|
267
|
|
|
$Parsedown = new MDParser\Parsedown(); |
|
268
|
|
|
$contentClean = $Parsedown->text($contentDecoded); |
|
269
|
|
|
|
|
270
|
|
|
//replace image links |
|
271
|
|
|
|
|
272
|
|
|
return $contentClean; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: