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
|
|
|
$readmesHandler = $helper->getHandler('Readmes'); |
199
|
|
|
|
200
|
|
|
$libRepositories = new Wggithub\Github\Repositories(); |
201
|
|
|
|
202
|
|
|
$submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
203
|
|
|
|
204
|
|
|
$readme = $libRepositories->getReadme($userName, $repoName); |
205
|
|
|
if (false === $readme) { |
|
|
|
|
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
if (count($readme) > 0) { |
|
|
|
|
209
|
|
|
if (array_key_exists('message', $readme)) { |
|
|
|
|
210
|
|
|
// not readme found |
211
|
|
|
// must return true otherwise releases will not be loaded |
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
$rmId = 0; |
215
|
|
|
$crReadmes = new \CriteriaCompo(); |
216
|
|
|
$crReadmes->add(new \Criteria('rm_repoid', $repoId)); |
217
|
|
|
$readmesAll = $readmesHandler->getAll($crReadmes); |
218
|
|
|
foreach (\array_keys($readmesAll) as $j) { |
219
|
|
|
$rmId = $readmesAll[$j]->getVar('rm_id'); |
220
|
|
|
} |
221
|
|
|
unset($crReadmes, $readmesAll); |
222
|
|
|
if ($rmId > 0) { |
223
|
|
|
$readmesObj = $readmesHandler->get($rmId); |
224
|
|
|
} else { |
225
|
|
|
$readmesObj = $readmesHandler->create(); |
226
|
|
|
} |
227
|
|
|
$readmesObj->setVar('rm_repoid', $repoId); |
228
|
|
|
$readmesObj->setVar('rm_name', $readme['name']); |
229
|
|
|
$readmesObj->setVar('rm_type', $readme['type']); |
230
|
|
|
/* |
231
|
|
|
* TODO |
232
|
|
|
$contentDecoded = base64_decode($this->getVar('rm_content', 'n')); |
233
|
|
|
if ('.MD' == substr(strtoupper($readme['name']), -3)) { |
234
|
|
|
$contentClean = convertMD($contentDecoded); |
235
|
|
|
} else { |
236
|
|
|
$contentClean = $contentDecoded; |
237
|
|
|
} |
238
|
|
|
$readmesObj->setVar('rm_content', $contentClean); |
239
|
|
|
*/ |
240
|
|
|
$readmesObj->setVar('rm_content', $readme['content']); |
241
|
|
|
$readmesObj->setVar('rm_encoding', $readme['encoding']); |
242
|
|
|
$readmesObj->setVar('rm_downloadurl', $readme['download_url']); |
243
|
|
|
$readmesObj->setVar('rm_datecreated',time()); |
244
|
|
|
$readmesObj->setVar('rm_submitter', $submitter); |
245
|
|
|
// Insert Data |
246
|
|
|
if (!$readmesHandler->insert($readmesObj)) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return true; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* TODO |
256
|
|
|
* convert md file content into clean text |
257
|
|
|
* and replace relative path for images into full path |
258
|
|
|
* |
259
|
|
|
* @param $contentDecoded |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function convertMD($contentDecoded) |
263
|
|
|
{ |
264
|
|
|
// parse MD file |
265
|
|
|
$Parsedown = new MDParser\Parsedown(); |
266
|
|
|
$contentClean = $Parsedown->text($contentDecoded); |
267
|
|
|
|
268
|
|
|
//replace image links |
269
|
|
|
|
270
|
|
|
return $contentClean; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Update table repositories with readme information |
275
|
|
|
* |
276
|
|
|
* @return boolean |
277
|
|
|
*/ |
278
|
|
|
public function updateRepoReadme() |
279
|
|
|
{ |
280
|
|
|
// update repo_readme |
281
|
|
|
$sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . ' INNER JOIN ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes'); |
282
|
|
|
$sql .= ' ON ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_id = ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_repoid '; |
283
|
|
|
$sql .= 'SET ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_readme = 1 '; |
284
|
|
|
$sql .= 'WHERE (((' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_id)>0));'; |
285
|
|
|
$GLOBALS['xoopsDB']->queryF($sql); |
286
|
|
|
|
287
|
|
|
return true; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: