Repos.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Repos is a collection of repos.
  5. *
  6. * @author Joachim M. Giaever (joachim[]giaever.org)
  7. * @version 0.1.3
  8. */
  9. final class Repos extends Collection {
  10. const SORT_UPDATED = Collection::SORT_INDEX << 1;
  11. const SORT_CREATED = Collection::SORT_INDEX << 2;
  12. const SORT_OWNER = Collection::SORT_INDEX << 3;
  13. protected $owner;
  14. /**
  15. * Initialize a repos collection
  16. *
  17. * If owner is not set it will query the whole
  18. * repo archive on Gogs.
  19. *
  20. * @param string $api_url The api-url
  21. * @param string $api_token The api-token
  22. * @param User $owner The owner of the collection
  23. */
  24. public function __construct(string $api_url, string $api_token, User $owner = null) {
  25. parent::__construct($api_url, $api_token);
  26. $this->owner = $owner;
  27. }
  28. /**
  29. * @see Base
  30. */
  31. protected function set_scope(string $method) {
  32. switch ($method) {
  33. case "get":
  34. case "load":
  35. if ($this->owner instanceof Org)
  36. $this->scope = "/orgs/" . $this->owner->username . "/repos";
  37. else
  38. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username ) . "/repos";
  39. break;
  40. case "search":
  41. $this->scope = "/repos/search";
  42. break;
  43. default:
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * Get a single repository by name.
  50. *
  51. * If the `owner` is set, the name can be just the
  52. * actual name of the repo
  53. *
  54. * @param string $name
  55. * @return
  56. */
  57. public function get(string $name) {
  58. if (isset($this->owner) && strpos($name, "/") === false )
  59. $name = sprintf("%s/%s", $this->owner->username, $name);
  60. if ($repo = $this->by_key($name))
  61. return $repo;
  62. $owner = !empty($this->owner) ? $this->owner : (
  63. ($pos = strpos($name, "/")) !== false ?
  64. new User($this->url, $this->token, substr($name, 0, $pos)) : null
  65. );
  66. $repo = (new Repo(
  67. $this->url,
  68. $this->token,
  69. $owner,
  70. ($pos = strpos($name, "/")) ? substr($name, $pos + 1) : $name
  71. ))->load();
  72. $this->add($repo, $repo->full_name);
  73. return $repo;
  74. }
  75. /**
  76. * @see Collection
  77. * @return Repo
  78. */
  79. public function create(...$args) {
  80. $repo = new Repo($this->url, $this->token, $this->owner);
  81. if (count($args) > 0) {
  82. $repo->create(...$args);
  83. $this->add($repo, $repo->full_name);
  84. }
  85. return $repo;
  86. }
  87. /**
  88. * Searches for a repo.
  89. *
  90. * If the owner is specified the search will be
  91. * limited to the actual user.
  92. *
  93. * Params can be an array of
  94. * ```php
  95. * $repos->search(array(
  96. * "name" => "name", // alt. "q". required
  97. * "limit" => 10, // not required, default: 10
  98. * ));
  99. * ```
  100. *
  101. * If repositories is allready loaded it will do a match
  102. * on the existing collection.
  103. *
  104. * @see Base
  105. * @see Collection
  106. * @throws Exception\SearchParamException on missing parameters
  107. * @return Repos
  108. */
  109. public function search(array $params = array(), bool $strict = false) {
  110. if (!isset($params["name"]) && !isset($params["q"]))
  111. throw new Exception\SearchParamException("Missing param <name>|<q>");
  112. if (isset($params["name"])) {
  113. $params["q"] = $params["name"];
  114. unset($params["name"]);
  115. }
  116. if (!isset($params["uid"]) || isset($this->owner)) {
  117. if (!isset($this->owner->id))
  118. $this->owner->load();
  119. $params["uid"] = isset($this->owner) ? $this->owner->id : 0;
  120. }
  121. if (!isset($params["limit"]))
  122. $params["limit"] = 10;
  123. $repos = new Repos($this->url, $this->token, $this->owner);
  124. if ($this->loaded) {
  125. foreach($this->all() as $key => $repo) {
  126. $search = $repo->search(array(
  127. "name" => $params["q"],
  128. "description" => $params["q"]
  129. ),
  130. $strict
  131. );
  132. if ($search)
  133. $repos->add($repo, $key);
  134. if ($repos->len() == $params["limit"])
  135. break;
  136. }
  137. } else {
  138. $this->set_scope("search");
  139. $jenc = $this->method_get($params);
  140. $jdec = $this->json_decode($jenc);
  141. foreach($this->json_set_property($jdec) as $key)
  142. $repos->add($this->by_key($key), $key);
  143. }
  144. return $repos;
  145. }
  146. /**
  147. * Sort repos by `method`.
  148. *
  149. * Valid methods:
  150. *
  151. * * SORT_UPDATED: Sort on `updated_at` value
  152. * * SORT_CREATED: Sort on `created_at` value
  153. * * SORT_OWNER: Sort on `owner` (organization repos etc may appear)
  154. *
  155. * @param int $flag Defines sorting algorithm to use
  156. * @param bool $asc Ascending order
  157. * @return \Gogs\Lib\Collection
  158. */
  159. public function sort_by(int $flag = Collection::SORT_INDEX, bool $asc = false) {
  160. switch ($flag) {
  161. case self::SORT_CREATED:
  162. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  163. $adate = new \DateTime($a->created_at);
  164. $bdate = new \DateTime($b->created_at);
  165. return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
  166. })) ? ($asc ? $sort->reverse() : $sort) : false;
  167. case self::SORT_UPDATED:
  168. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  169. $adate = new \DateTime($a->updated_at);
  170. $bdate = new \DateTime($b->updated_at);
  171. return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
  172. })) ? ($asc ? $sort->reverse() : $sort) : false;
  173. case self::SORT_OWNER:
  174. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  175. return strcmp($a->owner->username, $b->owner->username);
  176. })) ? ($asc ? $sort->reverse() : $sort) : false;
  177. default:
  178. return ($sort = $this->sort("ksort")) ? ($asc ? $sort->reverse() : $sort) : false;
  179. }
  180. }
  181. /**
  182. * @see Collection
  183. */
  184. protected function add_object(\stdClass $obj) {
  185. $repo = new Repo($this->url, $this->token);
  186. $repo->json_set_property($obj);
  187. return $this->add($repo, $repo->full_name);
  188. }
  189. /**
  190. * Get private repositories
  191. *
  192. * @return \Gogs\Lib\Collection
  193. */
  194. public function privates() {
  195. return $this->filter(function(Repo $r) {
  196. return $r->private;
  197. });
  198. }
  199. /**
  200. * Get public repositories
  201. *
  202. * @return \Gogs\Lib\Collection
  203. */
  204. public function publics() {
  205. return $this->filter(function(Repo $r) {
  206. return !$r->private;
  207. });
  208. }
  209. /**
  210. * Get personal repositories
  211. *
  212. * @return \Gogs\Lib\Collection
  213. */
  214. public function personals() {
  215. if (empty($this->owner))
  216. return new \Gogs\Lib\Collection();
  217. return $this->filter(function(Repo $r) {
  218. return $this->owner->username == $r->owner->username;
  219. });
  220. }
  221. /**
  222. * Get repositories contributed to
  223. *
  224. * @return \Gogs\Lib\Collection
  225. */
  226. public function contributions() {
  227. if (empty($this->owner))
  228. return new \Gogs\Lib\Collection();
  229. return $this->filter(function(Repo $r) {
  230. return $this->owner->username != $r->owner->username;
  231. });
  232. }
  233. /**
  234. * Get personal private repositories
  235. *
  236. * @return \Gogs\Lib\Collection
  237. */
  238. public function personals_privates() {
  239. return $this->personals()->filter(function(Repo $r) {
  240. return $r->private;
  241. });
  242. }
  243. /**
  244. * Get personal public repositories
  245. *
  246. * @return \Gogs\Lib\Collection
  247. */
  248. public function personals_publics() {
  249. return $this->personals()->filter(function(Repo $r) {
  250. return !$r->private;
  251. });
  252. }
  253. /**
  254. * Get private contributions
  255. *
  256. * @return \Gogs\Lib\Collection
  257. */
  258. public function contributions_privates() {
  259. return $this->contributions()->filter(function(Repo $r) {
  260. return $r->private;
  261. });
  262. }
  263. /**
  264. * Get public contributions
  265. *
  266. * @return \Gogs\Lib\Collection
  267. */
  268. public function contributions_publics() {
  269. return $this->contributions()->filter(function(Repo $r) {
  270. return !$r->private;
  271. });
  272. }
  273. }
  274. }