ggoffy /
wggithub
| 1 | <?php |
||
| 2 | /* |
||
| 3 | You may not change or alter any portion of this comment or credits |
||
| 4 | of supporting developers from this source code or any supporting source code |
||
| 5 | which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 6 | |||
| 7 | This program is distributed in the hope that it will be useful, |
||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | /** |
||
| 13 | * wgGitHub module for xoops |
||
| 14 | * |
||
| 15 | * @copyright 2020 XOOPS Project (https://xooops.org) |
||
| 16 | * @license GPL 2.0 or later |
||
| 17 | * @package wggithub |
||
| 18 | * @since 1.0 |
||
| 19 | * @min_xoops 2.5.10 |
||
| 20 | * @author Goffy - XOOPS Development Team - Email:<[email protected]> - Website:<https://wedega.com> |
||
| 21 | */ |
||
| 22 | |||
| 23 | use Xmf\Request; |
||
| 24 | use XoopsModules\Wggithub; |
||
| 25 | use XoopsModules\Wggithub\{ |
||
| 26 | Constants, |
||
| 27 | Helper, |
||
| 28 | Github\Github, |
||
| 29 | Github\Repositories, |
||
| 30 | Github\Releases |
||
| 31 | }; |
||
| 32 | |||
| 33 | require __DIR__ . '/header.php'; |
||
| 34 | $GLOBALS['xoopsOption']['template_main'] = 'wggithub_index.tpl'; |
||
| 35 | include_once \XOOPS_ROOT_PATH . '/header.php'; |
||
| 36 | |||
| 37 | // Permissions |
||
| 38 | $permGlobalView = $permissionsHandler->getPermGlobalView(); |
||
| 39 | if (!$permGlobalView) { |
||
| 40 | $GLOBALS['xoopsTpl']->assign('error', _NOPERM); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 41 | require __DIR__ . '/footer.php'; |
||
| 42 | } |
||
| 43 | $permGlobalRead = $permissionsHandler->getPermGlobalRead(); |
||
| 44 | $permReadmeUpdate = $permissionsHandler->getPermReadmeUpdate(); |
||
| 45 | |||
| 46 | $op = Request::getCmd('op', 'list'); |
||
| 47 | $filterRelease = Request::getString('release', 'all'); |
||
| 48 | $filterSortby = Request::getString('sortby', 'update'); |
||
| 49 | |||
| 50 | $GLOBALS['xoopsTpl']->assign('release', $filterRelease); |
||
| 51 | $GLOBALS['xoopsTpl']->assign('sortby', $filterSortby); |
||
| 52 | |||
| 53 | // Define Stylesheet |
||
| 54 | $GLOBALS['xoTheme']->addStylesheet($style, null); |
||
| 55 | $GLOBALS['xoTheme']->addStylesheet(WGGITHUB_URL . '/assets/css/tabs.css', null); |
||
| 56 | $keywords = []; |
||
| 57 | // |
||
| 58 | $GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL); |
||
| 59 | $GLOBALS['xoopsTpl']->assign('wggithub_url', WGGITHUB_URL); |
||
| 60 | $GLOBALS['xoopsTpl']->assign('wggithub_image_url', WGGITHUB_IMAGE_URL); |
||
| 61 | // |
||
| 62 | $GLOBALS['xoopsTpl']->assign('permReadmeUpdate', $permissionsHandler->getPermReadmeUpdate()); |
||
| 63 | |||
| 64 | switch ($op) { |
||
| 65 | case 'show': |
||
| 66 | case 'list': |
||
| 67 | case 'apiexceed': |
||
| 68 | default: |
||
| 69 | //check number of API calls |
||
| 70 | $crRequests = new \CriteriaCompo(); |
||
| 71 | $crRequests->add(new \Criteria('req_datecreated', (time() - 3600), '>')); |
||
| 72 | $requestsCount = $requestsHandler->getCount($crRequests); |
||
| 73 | if ($permGlobalRead && $requestsCount < 60 && 'list' == $op) { |
||
| 74 | executeUpdate(); |
||
| 75 | } |
||
| 76 | unset($crRequests); |
||
| 77 | $crRequests = new \CriteriaCompo(); |
||
| 78 | $crRequests->add(new \Criteria('req_result', 'OK')); |
||
| 79 | $crRequests->setStart(0); |
||
| 80 | $crRequests->setLimit(1); |
||
| 81 | $crRequests->setSort('req_id'); |
||
| 82 | $crRequests->setOrder('DESC'); |
||
| 83 | $requestsAll = $requestsHandler->getAll($crRequests); |
||
| 84 | foreach (\array_keys($requestsAll) as $i) { |
||
| 85 | $lastUpdate = $requestsAll[$i]->getVar('req_datecreated'); |
||
| 86 | $GLOBALS['xoopsTpl']->assign('lastUpdate', \formatTimestamp($lastUpdate, 'm')); |
||
| 87 | } |
||
| 88 | unset($crRequests); |
||
| 89 | $crRequests = new \CriteriaCompo(); |
||
| 90 | $crRequests->setStart(0); |
||
| 91 | $crRequests->setLimit(1); |
||
| 92 | $crRequests->setSort('req_id'); |
||
| 93 | $crRequests->setOrder('DESC'); |
||
| 94 | $requestsAll = $requestsHandler->getAll($crRequests); |
||
| 95 | foreach (\array_keys($requestsAll) as $i) { |
||
| 96 | if ($lastUpdate < $requestsAll[$i]->getVar('req_datecreated')) { |
||
| 97 | if (\strpos($requestsAll[$i]->getVar('req_result'), 'API rate limit exceeded') > 0) { |
||
| 98 | $GLOBALS['xoopsTpl']->assign('apiexceed', true); |
||
| 99 | } else { |
||
| 100 | $GLOBALS['xoopsTpl']->assign('apierror', true); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | unset($crRequests); |
||
| 105 | |||
| 106 | $github = Github::getInstance(); |
||
| 107 | $start = Request::getInt('start', 0); |
||
| 108 | $limit = Request::getInt('limit', $helper->getConfig('userpager')); |
||
| 109 | $menu = Request::getInt('menu', 0); |
||
| 110 | |||
| 111 | $crDirectories = new \CriteriaCompo(); |
||
| 112 | $crDirectories->add(new \Criteria('dir_online', 1)); |
||
| 113 | $directoriesCount = $directoriesHandler->getCount($crDirectories); |
||
| 114 | $GLOBALS['xoopsTpl']->assign('directoriesCount', $directoriesCount); |
||
| 115 | if ($directoriesCount > 0) { |
||
| 116 | $directoriesAll = $directoriesHandler->getAll($crDirectories); |
||
| 117 | // Get All Directories |
||
| 118 | $directories = []; |
||
| 119 | foreach (\array_keys($directoriesAll) as $i) { |
||
| 120 | $directories[$i] = $directoriesAll[$i]->getValuesDirectories(); |
||
| 121 | $dirName = $directoriesAll[$i]->getVar('dir_name'); |
||
| 122 | $dirFilterRelease = (bool)$directoriesAll[$i]->getVar('dir_filterrelease'); |
||
| 123 | $repos = []; |
||
| 124 | if (Constants::DIRECTORY_TYPE_ORG == $directoriesAll[$i]->getVar('dir_type')) { |
||
| 125 | //$github->readOrgRepositories($dirName, 100, 1); |
||
| 126 | //$github->readOrgRepositories($dirName, 100, 2); |
||
| 127 | } else { |
||
| 128 | //$github->readUserRepositories($dirName); |
||
| 129 | } |
||
| 130 | $crRepositories = new \CriteriaCompo(); |
||
| 131 | $crRepositories->add(new \Criteria('repo_user', $dirName)); |
||
| 132 | $repositoriesCountTotal = $repositoriesHandler->getCount($crRepositories); |
||
| 133 | if ('any' === $filterRelease && $dirFilterRelease) { |
||
| 134 | $crRepositories->add(new \Criteria('repo_prerelease', 1) ); |
||
| 135 | $crRepositories->add(new \Criteria('repo_release', 1), 'OR'); |
||
| 136 | } |
||
| 137 | if ('final' === $filterRelease && $dirFilterRelease) { |
||
| 138 | $crRepositories->add(new \Criteria('repo_release', 1)); |
||
| 139 | } |
||
| 140 | $repositoriesCount = $repositoriesHandler->getCount($crRepositories); |
||
| 141 | $crRepositories->setStart($start); |
||
| 142 | $crRepositories->setLimit($limit); |
||
| 143 | switch ($filterSortby) { |
||
| 144 | case 'name': |
||
| 145 | default: |
||
| 146 | $crRepositories->setSort('repo_name'); |
||
| 147 | $crRepositories->setOrder('ASC'); |
||
| 148 | break; |
||
| 149 | case 'update': |
||
| 150 | $crRepositories->setSort('repo_updatedat'); |
||
| 151 | $crRepositories->setOrder('DESC'); |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | if ($repositoriesCount > 0) { |
||
| 155 | $repositoriesAll = $repositoriesHandler->getAll($crRepositories); |
||
| 156 | foreach (\array_keys($repositoriesAll) as $j) { |
||
| 157 | $repoId = $repositoriesAll[$j]->getVar('repo_id'); |
||
| 158 | $repos[$j] = $repositoriesAll[$j]->getValuesRepositories(); |
||
| 159 | $repos[$j]['readme'] = ['content_clean' => _MA_WGGITHUB_README_NOFILE]; |
||
| 160 | $crReadmes = new \CriteriaCompo(); |
||
| 161 | $crReadmes->add(new \Criteria('rm_repoid', $repoId)); |
||
| 162 | $readmesCount = $readmesHandler->getCount($crReadmes); |
||
| 163 | if ($readmesCount > 0) { |
||
| 164 | $readmesAll = $readmesHandler->getAll($crReadmes); |
||
| 165 | foreach ($readmesAll as $readme) { |
||
| 166 | $repos[$j]['readme'] = $readme->getValuesReadmes(); |
||
| 167 | } |
||
| 168 | unset($crReadmes, $readmesAll); |
||
| 169 | } |
||
| 170 | //$repos[$j]['releases'] = []; |
||
| 171 | $crReleases = new \CriteriaCompo(); |
||
| 172 | $crReleases->add(new \Criteria('rel_repoid', $repoId)); |
||
| 173 | $releasesCount = $releasesHandler->getCount($crReleases); |
||
| 174 | if ($releasesCount > 0) { |
||
| 175 | $releasesAll = $releasesHandler->getAll($crReleases); |
||
| 176 | foreach ($releasesAll as $release) { |
||
| 177 | $repos[$j]['releases'][] = $release->getValuesReleases(); |
||
| 178 | } |
||
| 179 | unset($crReleases, $releasesAll); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | unset($crRepositories, $repositoriesAll); |
||
| 183 | } |
||
| 184 | if ($repositoriesCount === $repositoriesCountTotal) { |
||
| 185 | $directories[$i]['countRepos'] = str_replace(['%s', '%t'], [$dirName, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT2); |
||
| 186 | } else { |
||
| 187 | $directories[$i]['countRepos'] = str_replace(['%s', '%r', '%t'], [$dirName, $repositoriesCount, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT1); |
||
| 188 | } |
||
| 189 | $directories[$i]['repos'] = $repos; |
||
| 190 | $directories[$i]['previousRepos'] = $start > 0; |
||
| 191 | $directories[$i]['previousOp'] = '&start=' . ($start - $limit) . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
||
| 192 | $directories[$i]['nextRepos'] = ($repositoriesCount - $start) > $limit; |
||
| 193 | $directories[$i]['nextOp'] = '&start=' . ($start + $limit) . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
||
| 194 | $GLOBALS['xoopsTpl']->assign('start', $start); |
||
| 195 | $GLOBALS['xoopsTpl']->assign('limit', $limit); |
||
| 196 | $GLOBALS['xoopsTpl']->assign('menu', $menu); |
||
| 197 | |||
| 198 | $GLOBALS['xoopsTpl']->assign('directories', $directories); |
||
| 199 | } |
||
| 200 | |||
| 201 | unset($crDirectories, $directories); |
||
| 202 | // Display Navigation |
||
| 203 | if ($directoriesCount > $limit) { |
||
| 204 | include_once \XOOPS_ROOT_PATH . '/class/pagenav.php'; |
||
| 205 | $pagenav = new \XoopsPageNav($directoriesCount, $limit, $start, 'start', 'op=list&limit=' . $limit); |
||
| 206 | $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); |
||
| 207 | } |
||
| 208 | $GLOBALS['xoopsTpl']->assign('lang_thereare', \sprintf(\_MA_WGGITHUB_INDEX_THEREARE, $directoriesCount)); |
||
| 209 | $GLOBALS['xoopsTpl']->assign('divideby', $helper->getConfig('divideby')); |
||
| 210 | $GLOBALS['xoopsTpl']->assign('numb_col', $helper->getConfig('numb_col')); |
||
| 211 | } |
||
| 212 | |||
| 213 | break; |
||
| 214 | case 'update': |
||
| 215 | // Permissions |
||
| 216 | if (!$permGlobalRead) { |
||
| 217 | $GLOBALS['xoopsTpl']->assign('error', _NOPERM); |
||
| 218 | require __DIR__ . '/footer.php'; |
||
| 219 | } |
||
| 220 | executeUpdate(); |
||
| 221 | break; |
||
| 222 | case 'update_readme': |
||
| 223 | // Permissions |
||
| 224 | if (!$permReadmeUpdate) { |
||
| 225 | $GLOBALS['xoopsTpl']->assign('error', _NOPERM); |
||
| 226 | require __DIR__ . '/footer.php'; |
||
| 227 | } |
||
| 228 | $repoId = Request::getInt('repo_id', 0); |
||
| 229 | $repoUser = Request::getString('repo_user', 'none'); |
||
| 230 | $repoName = Request::getString('repo_name', 'none'); |
||
| 231 | $res = $helper->getHandler('Readmes')->updateReadmes($repoId, $repoUser, $repoName); |
||
| 232 | break; |
||
| 233 | } |
||
| 234 | |||
| 235 | $GLOBALS['xoopsTpl']->assign('table_type', $helper->getConfig('table_type')); |
||
| 236 | // Breadcrumbs |
||
| 237 | $xoBreadcrumbs[] = ['title' => \_MA_WGGITHUB_INDEX]; |
||
| 238 | // Keywords |
||
| 239 | wggithubMetaKeywords($helper->getConfig('keywords') . ', ' . \implode(',', $keywords)); |
||
| 240 | unset($keywords); |
||
| 241 | // Description |
||
| 242 | wggithubMetaDescription(\_MA_WGGITHUB_INDEX_DESC); |
||
| 243 | $GLOBALS['xoopsTpl']->assign('xoops_mpageurl', WGGITHUB_URL.'/index.php'); |
||
| 244 | $GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL); |
||
| 245 | $GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL); |
||
| 246 | require __DIR__ . '/footer.php'; |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Execute update of repositories and all related tables |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | function executeUpdate(){ |
||
| 256 | |||
| 257 | $github = Github::getInstance(); |
||
| 258 | $helper = Helper::getInstance(); |
||
| 259 | $directoriesHandler = $helper->getHandler('Directories'); |
||
| 260 | $releasesHandler = $helper->getHandler('Releases'); |
||
| 261 | $crDirectories = new \CriteriaCompo(); |
||
| 262 | $crDirectories->add(new \Criteria('dir_autoupdate', 1)); |
||
| 263 | $crDirectories->add(new \Criteria('dir_online', 1)); |
||
| 264 | $directoriesAll = $directoriesHandler->getAll($crDirectories); |
||
| 265 | // Get All Directories |
||
| 266 | $directories = []; |
||
| 267 | foreach (\array_keys($directoriesAll) as $i) { |
||
| 268 | $directories[$i] = $directoriesAll[$i]->getValuesDirectories(); |
||
| 269 | $dirName = $directoriesAll[$i]->getVar('dir_name'); |
||
| 270 | $repos = []; |
||
| 271 | for ($j = 1; $j <= 9; $j++) { |
||
| 272 | $repos[$j] = []; |
||
| 273 | if (Constants::DIRECTORY_TYPE_ORG == $directoriesAll[$i]->getVar('dir_type')) { |
||
| 274 | $repos = $github->readOrgRepositories($dirName, 100, $j); |
||
| 275 | } else { |
||
| 276 | $repos = $github->readUserRepositories($dirName, 100, $j); |
||
| 277 | } |
||
| 278 | if (false === $repos) { |
||
| 279 | return false; |
||
| 280 | break 1; |
||
| 281 | } |
||
| 282 | if (count($repos) > 0) { |
||
| 283 | $github->updateTableRepositories($dirName, $repos, false); |
||
| 284 | } else { |
||
| 285 | break 1; |
||
| 286 | } |
||
| 287 | if (count($repos) < 100) { |
||
| 288 | break 1; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | unset($directories); |
||
| 293 | |||
| 294 | $releasesHandler->updateRepoReleases(); |
||
| 295 | |||
| 296 | return true; |
||
| 297 | } |
||
| 298 |