generate.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. This file is part of Resumer
  4. Copyright (C) 2016 Sylvia van Os
  5. Resumer is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as
  7. published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. declare(strict_types=1);
  17. require_once 'vendor/autoload.php';
  18. require_once 'RepoParsers/github.php';
  19. $twigLoader = new Twig_Loader_Filesystem('templates/');
  20. $twig = new Twig_Environment($twigLoader, array(
  21. ));
  22. $services = [];
  23. $parsers = [];
  24. foreach ($_POST["services"] as $service)
  25. {
  26. if (empty($service["name"]) || empty($service["username"]))
  27. continue;
  28. $class = $service["name"] . "RepoParser";
  29. $parser = new $class($service["username"]);
  30. $repoService["name"] = $parser->getIdentifier();
  31. $repoService["username"] = $service["username"];
  32. $repoService["url"] = $parser->getProfileUrl($repoService["username"]);
  33. $services[] = $repoService;
  34. $parsers[] = $parser;
  35. }
  36. $namedRepositories = [];
  37. $namedLanguages = [];
  38. foreach ($parsers as $parser)
  39. {
  40. // Get all information. Initially, we store this as an associative array
  41. // based on the repository name to group repositories from several sources
  42. // with the same name
  43. foreach ($parser->getRepos() as $repo => $data)
  44. {
  45. $repoName = $parser->getName($repo);
  46. $repoHomepage = $parser->getHomepage($repo);
  47. $repoRepo = $parser->getRepositoryUrl($repo);
  48. $repoDescription = $parser->getDescription($repo);
  49. $repoLanguage = $parser->getLanguage($repo);
  50. $repoCreated = $parser->getCreationDate($repo);
  51. $repoUpdated = $parser->getLastUpdatedDate($repo);
  52. $repoStars = $parser->getStarCount($repo);
  53. $repoWatchers = $parser->getWatcherCount($repo);
  54. $repoForks = $parser->getForkCount($repo);
  55. if (!array_key_exists($repoName, $namedRepositories))
  56. @$namedLanguages[$repoLanguage]++;
  57. $namedRepositories[$repoName]["homepage"] = $repoHomepage;
  58. $namedRepositories[$repoName]["repo"] = $repoRepo;
  59. $namedRepositories[$repoName]["description"] = $repoDescription;
  60. $namedRepositories[$repoName]["language"] = $repoLanguage;
  61. $namedRepositories[$repoName]["created"] = $repoCreated;
  62. $namedRepositories[$repoName]["updated"] = $repoUpdated;
  63. @$namedRepositories[$repoName]["stars"] += $repoStars;
  64. @$namedRepositories[$repoName]["watchers"] += $repoWatchers;
  65. @$namedRepositories[$repoName]["forks"] += $repoForks;
  66. }
  67. }
  68. // Convert repositories to a normal array for sorting
  69. $repositories = [];
  70. foreach ($namedRepositories as $name => $data)
  71. {
  72. $repo = $namedRepositories[$name];
  73. $repo["name"] = $name;
  74. $repositories[] = $repo;
  75. }
  76. // Sort repositories on popularity
  77. usort($repositories, function($a, $b) {
  78. return $b["stars"] + $b["watchers"] + $b["forks"] <=> $a["stars"] + $a["watchers"] + $a["forks"];
  79. });
  80. // Convert languages to a normal array so twig can sort it
  81. $languages = [];
  82. foreach ($namedLanguages as $name => $data)
  83. {
  84. $language = ['name' => $name,
  85. 'count' => $namedLanguages[$name]];
  86. $languages[] = $language;
  87. }
  88. $name = $_POST['name'];
  89. echo $twig->render('report.erb', array('name' => $name,
  90. 'services' => $services,
  91. 'languages' => $languages,
  92. 'repositories' => $repositories));