ggoffy /
wggithub
| 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); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 162 | } else {
|
||
| 163 | $readmesObj = $readmesHandler->create(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | if (\is_array($readme)) {
|
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 167 | $readmesObj->setVar('rm_repoid', $repoId);
|
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 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 | $githubClient = new Wggithub\Github\GithubClient(); |
||
| 201 | |||
| 202 | $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
|
||
| 203 | |||
| 204 | $readme = $githubClient->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 | $readmesObj->setVar('rm_content', $readme['content']);
|
||
| 231 | $readmesObj->setVar('rm_encoding', $readme['encoding']);
|
||
| 232 | $readmesObj->setVar('rm_downloadurl', $readme['download_url']);
|
||
| 233 | $baseurl = \substr($readme['html_url'], 0, \strrpos($readme['html_url'], '/') + 1); |
||
| 234 | $readmesObj->setVar('rm_baseurl', $baseurl);
|
||
| 235 | $readmesObj->setVar('rm_datecreated',\time());
|
||
| 236 | $readmesObj->setVar('rm_submitter', $submitter);
|
||
| 237 | // Insert Data |
||
| 238 | if (!$readmesHandler->insert($readmesObj)) {
|
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | return true; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * convert md file content into clean text |
||
| 248 | * |
||
| 249 | * @param $contentDecoded |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function convertMD($contentDecoded) |
||
| 253 | {
|
||
| 254 | // parse MD file |
||
| 255 | $Parsedown = new MDParser\Parsedown(); |
||
| 256 | $contentClean = $Parsedown->text($contentDecoded); |
||
| 257 | |||
| 258 | return $contentClean; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Update table repositories with readme information |
||
| 263 | * |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | public function updateRepoReadme() |
||
| 267 | {
|
||
| 268 | // update repo_readme |
||
| 269 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . ' INNER JOIN ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes');
|
||
| 270 | $sql .= ' ON ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_id = ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_repoid ';
|
||
| 271 | $sql .= 'SET ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_readme = 1 ';
|
||
| 272 | $sql .= 'WHERE (((' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_id)>0));';
|
||
| 273 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 274 | |||
| 275 | return true; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @public function getForm |
||
| 280 | * @param bool $action |
||
| 281 | * @param int $start |
||
| 282 | * @param int $limit |
||
| 283 | * @param string $filterValue |
||
| 284 | * @return \XoopsSimpleForm |
||
| 285 | */ |
||
| 286 | public function getFormFilterReadmes($action = false, $start = 0, $limit = 0, $filterValue = '') |
||
| 287 | {
|
||
| 288 | if (!$action) {
|
||
| 289 | $action = $_SERVER['REQUEST_URI']; |
||
| 290 | } |
||
| 291 | // Get Theme Form |
||
| 292 | \xoops_load('XoopsFormLoader');
|
||
| 293 | $form = new \XoopsSimpleForm('', 'formFilterAdmin', $action, 'post', true);
|
||
| 294 | $form->setExtra('enctype="multipart/form-data"');
|
||
| 295 | $filterTray = new \XoopsFormElementTray('', ' ');
|
||
| 296 | // Form Select field |
||
| 297 | $fieldSelect = new \XoopsFormSelect(\_AM_WGGITHUB_FILTER, 'filter_field', 'repo_name'); |
||
| 298 | $fieldSelect->addOption('', ' ');
|
||
| 299 | $fieldSelect->addOption('repo_name', \_AM_WGGITHUB_README_REPOID);
|
||
| 300 | $filterTray->addElement($fieldSelect, true); |
||
| 301 | // Form Select operand |
||
| 302 | $operandsSelect = new \XoopsFormSelect('', 'filter_operand', Constants::FILTER_OPERAND_LIKE);
|
||
| 303 | $operandsSelect->addOption(Constants::FILTER_OPERAND_EQUAL, \_AM_WGGITHUB_FILTER_OPERAND_EQUAL); |
||
| 304 | $operandsSelect->addOption(Constants::FILTER_OPERAND_LIKE, \_AM_WGGITHUB_FILTER_OPERAND_LIKE); |
||
| 305 | $filterTray->addElement($operandsSelect); |
||
| 306 | // Form Text value |
||
| 307 | $filterTray->addElement(new \XoopsFormText('', 'filter_value', 20, 255, $filterValue), true);
|
||
| 308 | // Form button |
||
| 309 | $filterTray->addElement(new \XoopsFormButton('', 'confirm_submit', \_SUBMIT, 'submit'));
|
||
| 310 | $form->addElement($filterTray); |
||
| 311 | // To Save |
||
| 312 | $form->addElement(new \XoopsFormHidden('op', 'filter'));
|
||
| 313 | $form->addElement(new \XoopsFormHidden('start', $start));
|
||
| 314 | $form->addElement(new \XoopsFormHidden('limit', $limit));
|
||
| 315 | |||
| 316 | return $form; |
||
| 317 | } |
||
| 318 | |||
| 319 | } |
||
| 320 |