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 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Class Object Handler Releases |
||
| 31 | */ |
||
| 32 | class ReleasesHandler extends \XoopsPersistableObjectHandler |
||
| 33 | {
|
||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * |
||
| 37 | * @param \XoopsDatabase $db |
||
| 38 | */ |
||
| 39 | public function __construct(\XoopsDatabase $db) |
||
| 40 | {
|
||
| 41 | parent::__construct($db, 'wggithub_releases', Releases::class, 'rel_id', 'rel_name'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param bool $isNew |
||
| 46 | * |
||
| 47 | * @return object |
||
| 48 | */ |
||
| 49 | public function create($isNew = true) |
||
| 50 | {
|
||
| 51 | return parent::create($isNew); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * retrieve a field |
||
| 56 | * |
||
| 57 | * @param int $i field id |
||
| 58 | * @param null fields |
||
| 59 | * @return mixed reference to the {@link Get} object
|
||
| 60 | */ |
||
| 61 | public function get($i = null, $fields = null) |
||
| 62 | {
|
||
| 63 | return parent::get($i, $fields); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * get inserted id |
||
| 68 | * |
||
| 69 | * @param null |
||
| 70 | * @return int reference to the {@link Get} object
|
||
| 71 | */ |
||
| 72 | public function getInsertId() |
||
| 73 | {
|
||
| 74 | return $this->db->getInsertId(); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get Count Releases in the database |
||
| 79 | * @param int $start |
||
| 80 | * @param int $limit |
||
| 81 | * @param string $sort |
||
| 82 | * @param string $order |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | public function getCountReleases($start = 0, $limit = 0, $sort = 'rel_id ASC, rel_name', $order = 'ASC') |
||
| 86 | {
|
||
| 87 | $crCountReleases = new \CriteriaCompo(); |
||
| 88 | $crCountReleases = $this->getReleasesCriteria($crCountReleases, $start, $limit, $sort, $order); |
||
| 89 | return $this->getCount($crCountReleases); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get All Releases in the database |
||
| 94 | * @param int $start |
||
| 95 | * @param int $limit |
||
| 96 | * @param string $sort |
||
| 97 | * @param string $order |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function getAllReleases($start = 0, $limit = 0, $sort = 'rel_id ASC, rel_name', $order = 'ASC') |
||
| 101 | {
|
||
| 102 | $crAllReleases = new \CriteriaCompo(); |
||
| 103 | $crAllReleases = $this->getReleasesCriteria($crAllReleases, $start, $limit, $sort, $order); |
||
| 104 | return $this->getAll($crAllReleases); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get Criteria Releases |
||
| 109 | * @param $crReleases |
||
| 110 | * @param int $start |
||
| 111 | * @param int $limit |
||
| 112 | * @param string $sort |
||
| 113 | * @param string $order |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | private function getReleasesCriteria($crReleases, $start, $limit, $sort, $order) |
||
| 117 | {
|
||
| 118 | $crReleases->setStart($start); |
||
| 119 | $crReleases->setLimit($limit); |
||
| 120 | $crReleases->setSort($sort); |
||
| 121 | $crReleases->setOrder($order); |
||
| 122 | return $crReleases; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Update table requests |
||
| 127 | * |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | public function updateTableReleases() |
||
| 131 | {
|
||
| 132 | $helper = Wggithub\Helper::getInstance(); |
||
| 133 | $repositoriesHandler = $helper->getHandler('Repositories');
|
||
| 134 | $releasesHandler = $helper->getHandler('Releases');
|
||
| 135 | |||
| 136 | $githubClient = new Wggithub\Github\GithubClient(); |
||
| 137 | |||
| 138 | $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
|
||
| 139 | |||
| 140 | $repositoriesCount = $repositoriesHandler->getCount(); |
||
| 141 | if ($repositoriesCount > 0) {
|
||
| 142 | $repositoriesAll = $repositoriesHandler->getAll(); |
||
| 143 | foreach (\array_keys($repositoriesAll) as $i) {
|
||
| 144 | $repoId = $repositoriesAll[$i]->getVar('repo_id');
|
||
| 145 | $repoStatus = $repositoriesAll[$i]->getVar('repo_status');
|
||
| 146 | $crReleases = new \CriteriaCompo(); |
||
| 147 | $crReleases->add(new \Criteria('rel_repoid', $repoId));
|
||
| 148 | $releasesCount = $releasesHandler->getCount($crReleases); |
||
| 149 | if (Constants::STATUS_NEW === $repoStatus || Constants::STATUS_UPDATED === $repoStatus || 0 == $releasesCount) {
|
||
| 150 | $ghReleases = $githubClient->getReleases($repositoriesAll[$i]->getVar('repo_user'), $repositoriesAll[$i]->getVar('repo_name'));
|
||
| 151 | if ($releasesCount > 0) {
|
||
| 152 | $sql = 'DELETE FROM `' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '` WHERE `rel_repoid` = ' . $repoId;
|
||
| 153 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
|
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | unset($crReleases); |
||
| 158 | } |
||
| 159 | $first = true; |
||
| 160 | $final = false; |
||
| 161 | foreach ($ghReleases as $ghRelease) {
|
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 162 | if ($first || (!$final && !(bool)$ghRelease['prerelease'])) {
|
||
| 163 | // save first in any case and save first final version |
||
| 164 | $releasesObj = $releasesHandler->create(); |
||
| 165 | $releasesObj->setVar('rel_repoid', $repoId);
|
||
| 166 | $releasesObj->setVar('rel_type', $ghRelease['type']);
|
||
| 167 | $releasesObj->setVar('rel_name', $ghRelease['name']);
|
||
| 168 | $releasesObj->setVar('rel_prerelease', (true == (bool)$ghRelease['prerelease']));
|
||
| 169 | $releasesObj->setVar('rel_publishedat', \strtotime($ghRelease['published_at']));
|
||
| 170 | $releasesObj->setVar('rel_tarballurl', $ghRelease['tarball_url']);
|
||
| 171 | $releasesObj->setVar('rel_zipballurl', $ghRelease['zipball_url']);
|
||
| 172 | $releasesObj->setVar('rel_datecreated', \time());
|
||
| 173 | $releasesObj->setVar('rel_submitter', $submitter);
|
||
| 174 | // Insert Data |
||
| 175 | if (!$releasesHandler->insert($releasesObj)) {
|
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | $first = false; |
||
| 180 | if (false === (bool)$ghRelease['prerelease']) {
|
||
| 181 | $final = true; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return true; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Update table requests |
||
| 192 | * |
||
| 193 | * @param $repoId |
||
| 194 | * @param $userName |
||
| 195 | * @param $repoName |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | public function updateReleases($repoId, $userName, $repoName) |
||
| 199 | {
|
||
| 200 | $helper = Wggithub\Helper::getInstance(); |
||
| 201 | $releasesHandler = $helper->getHandler('Releases');
|
||
| 202 | |||
| 203 | $githubClient = new Wggithub\Github\GithubClient(); |
||
| 204 | |||
| 205 | $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
|
||
| 206 | |||
| 207 | $ghReleases = $githubClient->getReleases($userName, $repoName); |
||
| 208 | if (false === $ghReleases) {
|
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | if (\count($ghReleases) > 0) {
|
||
| 212 | if (\array_key_exists('message', $ghReleases)) {
|
||
| 213 | // not readme found |
||
| 214 | // must return true otherwise releases will not be loaded |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | $sql = 'DELETE FROM `' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '` WHERE `rel_repoid` = ' . $repoId;
|
||
| 218 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
|
||
| 219 | return false; |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | $first = true; |
||
| 224 | $final = false; |
||
| 225 | foreach ($ghReleases as $ghRelease) {
|
||
| 226 | if ($first || (!$final && !(bool)$ghRelease['prerelease'])) {
|
||
| 227 | // save first in any case and save first final version |
||
| 228 | $releasesObj = $releasesHandler->create(); |
||
| 229 | $releasesObj->setVar('rel_repoid', $repoId);
|
||
| 230 | //$releasesObj->setVar('rel_type', $ghRelease['type']);
|
||
| 231 | $releasesObj->setVar('rel_name', $ghRelease['name']);
|
||
| 232 | $releasesObj->setVar('rel_prerelease', (true == (bool)$ghRelease['prerelease']));
|
||
| 233 | $releasesObj->setVar('rel_publishedat', \strtotime($ghRelease['published_at']));
|
||
| 234 | $releasesObj->setVar('rel_tarballurl', $ghRelease['tarball_url']);
|
||
| 235 | $releasesObj->setVar('rel_zipballurl', $ghRelease['zipball_url']);
|
||
| 236 | $releasesObj->setVar('rel_datecreated', \time());
|
||
| 237 | $releasesObj->setVar('rel_submitter', $submitter);
|
||
| 238 | // Insert Data |
||
| 239 | if (!$releasesHandler->insert($releasesObj)) {
|
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | $first = false; |
||
| 244 | if (false === (bool)$ghRelease['prerelease']) {
|
||
| 245 | $final = true; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | return true; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Update table repositories with release information |
||
| 255 | * |
||
| 256 | * @return boolean |
||
| 257 | */ |
||
| 258 | public function updateRepoReleases() |
||
| 259 | {
|
||
| 260 | // update repo_prerelease |
||
| 261 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . ' INNER JOIN ' . $GLOBALS['xoopsDB']->prefix('wggithub_releases');
|
||
| 262 | $sql .= ' ON ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_id = ' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '.rel_repoid ';
|
||
| 263 | $sql .= 'SET ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_prerelease = 1 ';
|
||
| 264 | $sql .= 'WHERE (((' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '.rel_prerelease)=1));';
|
||
| 265 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 266 | |||
| 267 | // update repo_release |
||
| 268 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . ' INNER JOIN ' . $GLOBALS['xoopsDB']->prefix('wggithub_releases');
|
||
| 269 | $sql .= ' ON ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_id = ' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '.rel_repoid ';
|
||
| 270 | $sql .= 'SET ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_release = 1 ';
|
||
| 271 | $sql .= 'WHERE (((' . $GLOBALS['xoopsDB']->prefix('wggithub_releases') . '.rel_prerelease)=0));';
|
||
| 272 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 273 | |||
| 274 | return true; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @public function getForm |
||
| 279 | * @param bool $action |
||
| 280 | * @param int $start |
||
| 281 | * @param int $limit |
||
| 282 | * @param string $filterValue |
||
| 283 | * @return \XoopsSimpleForm |
||
| 284 | */ |
||
| 285 | public function getFormFilterReleases($action = false, $start = 0, $limit = 0, $filterValue = '') |
||
| 286 | {
|
||
| 287 | if (!$action) {
|
||
| 288 | $action = $_SERVER['REQUEST_URI']; |
||
| 289 | } |
||
| 290 | // Get Theme Form |
||
| 291 | \xoops_load('XoopsFormLoader');
|
||
| 292 | $form = new \XoopsSimpleForm('', 'formFilterAdmin', $action, 'post', true);
|
||
| 293 | $form->setExtra('enctype="multipart/form-data"');
|
||
| 294 | $filterTray = new \XoopsFormElementTray('', ' ');
|
||
| 295 | // Form Select field |
||
| 296 | $fieldSelect = new \XoopsFormSelect(\_AM_WGGITHUB_FILTER, 'filter_field', 'rel_name'); |
||
| 297 | $fieldSelect->addOption('', ' ');
|
||
| 298 | $fieldSelect->addOption('rel_name', \_AM_WGGITHUB_RELEASE_NAME);
|
||
| 299 | $filterTray->addElement($fieldSelect, true); |
||
| 300 | // Form Select operand |
||
| 301 | $operandsSelect = new \XoopsFormSelect('', 'filter_operand', Constants::FILTER_OPERAND_LIKE);
|
||
| 302 | $operandsSelect->addOption(Constants::FILTER_OPERAND_EQUAL, \_AM_WGGITHUB_FILTER_OPERAND_EQUAL); |
||
| 303 | $operandsSelect->addOption(Constants::FILTER_OPERAND_LIKE, \_AM_WGGITHUB_FILTER_OPERAND_LIKE); |
||
| 304 | $filterTray->addElement($operandsSelect); |
||
| 305 | // Form Text value |
||
| 306 | $filterTray->addElement(new \XoopsFormText('', 'filter_value', 20, 255, $filterValue), true);
|
||
| 307 | // Form button |
||
| 308 | $filterTray->addElement(new \XoopsFormButton('', 'confirm_submit', \_SUBMIT, 'submit'));
|
||
| 309 | $form->addElement($filterTray); |
||
| 310 | // To Save |
||
| 311 | $form->addElement(new \XoopsFormHidden('op', 'filter'));
|
||
| 312 | $form->addElement(new \XoopsFormHidden('start', $start));
|
||
| 313 | $form->addElement(new \XoopsFormHidden('limit', $limit));
|
||
| 314 | |||
| 315 | return $form; |
||
| 316 | } |
||
| 317 | |||
| 318 | } |
||
| 319 |