List repositories from an installation of Gitea on Wordpress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snippet.php 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Snippet from the "page.php" of a Wordpress-Theme which displays the public repositories of a Gitea-installation from another server
  4. ***/
  5. if(is_page(362)){ //better put it directly on the site which should list the repositories
  6. $entriesPerPage = 15;
  7. $entries = [];
  8. $pageNo = get_query_var('paged', 1); //wordpress "GETs" the page number as parameter from a url formated like yourwp/yourpage/?paged=1 and kindly redirects to yourwp/yourpage/1/
  9. if($pageNo == 0) $pageNo = 1; //wordpress makes page number 1 become page number 0.. skip 0 later and reset 0 to 1 here.
  10. //get how many repos there are overall (listed by Gitea in the http-header as entry [11], 'X-Total-Count'
  11. $headers = get_headers("https://giers10.uber.space/api/v1/repos/search");
  12. $entryCount = str_replace('X-Total-Count: ','',$headers[11]);
  13. //the api call
  14. $json = file_get_contents('https://giers10.uber.space/api/v1/repos/search?private=false&sort=updated&order=desc&limit=' . $entriesPerPage . '&page=' . $pageNo);
  15. $data = json_decode($json,true);
  16. foreach ($data['data'] as $entry){
  17. array_push($entries,$entry);
  18. }
  19. //this function is for a nice display of when the repository was created and last updated like on Gitea.
  20. function time_elapsed_string($datetime, $full = false) {
  21. $now = new DateTime;
  22. $ago = new DateTime($datetime);
  23. $diff = $now->diff($ago);
  24. $diff->w = floor($diff->d / 7);
  25. $diff->d -= $diff->w * 7;
  26. $string = array(
  27. 'y' => 'Jahr',
  28. 'm' => 'Monat',
  29. 'w' => 'Woche',
  30. 'd' => 'Tag',
  31. 'h' => 'Stunde',
  32. 'i' => 'Minute',
  33. 's' => 'Sekunde',
  34. );
  35. foreach ($string as $k => &$v) {
  36. if ($diff->$k) {
  37. //you won't need $pc ('plural character') if you i.e. just need to add an "s" because you deal with the English language
  38. $pc = (($v == 'Jahr' || $v == 'Monat' || $v == 'Tag') ? 'en' : 'n');
  39. $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $pc : '');
  40. } else {
  41. unset($string[$k]);
  42. }
  43. }
  44. if (!$full) $string = array_slice($string, 0, 1);
  45. return $string ? implode(', ', $string) . '' : 'Gerade eben';
  46. }
  47. //list the repositories
  48. for($i = 0; $i < count($entries); $i++){
  49. echo("<div class='repository'><p><a href='" . $entries[$i]['html_url'] . "' target='_blank'><h2 class='repoheadline'>" . $entries[$i]['name'] . "</h2></a></p>" . "\n");
  50. echo("<span class='repotime'>Zuletzt bearbeitet vor " . time_elapsed_string($entries[$i]['updated_at']) . " &#xb7; Erstellt vor " . time_elapsed_string($entries[$i]['created_at']) . "</span><br />\n");
  51. if($entries[$i]['description'] != '') echo($entries[$i]['description'] . "<br />\n");
  52. if($entries[$i]['website'] != '') echo("<a href='" . $entries[$i]['website'] . "' target='_blank'>Link zur Website</a><br />\n");
  53. echo("</div>\n");
  54. }
  55. ?>
  56. <!-- create paginator -->
  57. <div class="paging">
  58. <?php
  59. if($pageNo > 1){
  60. ?><a class="prev page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$pageNo-1 ?>/">« Zurück</a><?php
  61. }
  62. for($i = 1; $i < $entryCount / $entriesPerPage +1; $i++){
  63. if($i == $pageNo){
  64. ?><span aria-current="page" class="page-numbers current"><?=$pageNo ?></span><?php
  65. } else {
  66. ?><a class="page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$i ?>/"><?=$i ?></a><?php
  67. }
  68. }
  69. if($pageNo < $entryCount / $entriesPerPage){
  70. ?><a class="next page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$pageNo+1 ?>/">Weiter »</a><?php
  71. }
  72. ?>
  73. </div>
  74. <?php
  75. }
  76. ?>