PackageRepository.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Entity;
  12. use Doctrine\ORM\EntityRepository;
  13. use Doctrine\ORM\QueryBuilder;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class PackageRepository extends EntityRepository
  18. {
  19. /**
  20. * Lists all package names array(name => true)
  21. *
  22. * @var array
  23. */
  24. private $packageNames;
  25. public function packageExists($name)
  26. {
  27. $packages = $this->getPackageNames();
  28. return isset($packages[$name]);
  29. }
  30. public function getPackageNames()
  31. {
  32. if (null !== $this->packageNames) {
  33. return $this->packageNames;
  34. }
  35. $names = null;
  36. $apc = extension_loaded('apc');
  37. // TODO use container to set caching key and ttl
  38. if ($apc) {
  39. $names = apc_fetch('packagist_package_names');
  40. }
  41. if (!is_array($names)) {
  42. $names = array();
  43. $query = $this->getEntityManager()
  44. ->createQuery("SELECT p.name FROM Packagist\WebBundle\Entity\Package p");
  45. foreach ($query->getScalarResult() as $package) {
  46. $names[$package['name']] = true;
  47. }
  48. if ($apc) {
  49. apc_store('packagist_package_names', $names, 3600);
  50. }
  51. }
  52. return $this->packageNames = $names;
  53. }
  54. public function getStalePackages()
  55. {
  56. $conn = $this->getEntityManager()->getConnection();
  57. return $conn->fetchAll(
  58. 'SELECT p.id FROM package p
  59. WHERE p.crawledAt IS NULL
  60. OR (p.autoUpdated = 0 AND p.crawledAt < :crawled)
  61. OR (p.crawledAt < :autocrawled)
  62. ORDER BY p.id ASC',
  63. array(
  64. 'crawled' => date('Y-m-d H:i:s', strtotime('-4hours')),
  65. 'autocrawled' => date('Y-m-d H:i:s', strtotime('-1week')),
  66. )
  67. );
  68. }
  69. public function getStalePackagesForIndexing()
  70. {
  71. $conn = $this->getEntityManager()->getConnection();
  72. return $conn->fetchAll('SELECT p.id FROM package p WHERE p.indexedAt IS NULL OR p.indexedAt <= p.crawledAt ORDER BY p.id ASC');
  73. }
  74. public function getStalePackagesForDumping()
  75. {
  76. $conn = $this->getEntityManager()->getConnection();
  77. return $conn->fetchAll('SELECT p.id FROM package p WHERE p.dumpedAt IS NULL OR p.dumpedAt <= p.crawledAt ORDER BY p.id ASC');
  78. }
  79. public function findOneByName($name)
  80. {
  81. $qb = $this->getBaseQueryBuilder()
  82. ->where('p.name = ?0')
  83. ->setParameters(array($name));
  84. return $qb->getQuery()->getSingleResult();
  85. }
  86. public function getFullPackageByName($name)
  87. {
  88. $qb = $this->getBaseQueryBuilder()
  89. ->addSelect('a', 'req', 'devReq', 'sug', 'rep', 'con', 'pro')
  90. ->leftJoin('v.authors', 'a')
  91. ->leftJoin('v.require', 'req')
  92. ->leftJoin('v.devRequire', 'devReq')
  93. ->leftJoin('v.suggest', 'sug')
  94. ->leftJoin('v.replace', 'rep')
  95. ->leftJoin('v.conflict', 'con')
  96. ->leftJoin('v.provide', 'pro')
  97. ->where('p.name = ?0')
  98. ->setParameters(array($name));
  99. return $qb->getQuery()->getSingleResult();
  100. }
  101. public function getFullPackages(array $ids = null, $filters = array())
  102. {
  103. $qb = $this->getEntityManager()->createQueryBuilder();
  104. $qb->select('p', 'v', 't', 'a', 'req', 'devReq', 'sug', 'rep', 'con', 'pro')
  105. ->from('Packagist\WebBundle\Entity\Package', 'p')
  106. ->leftJoin('p.versions', 'v')
  107. ->leftJoin('v.tags', 't')
  108. ->leftJoin('v.authors', 'a')
  109. ->leftJoin('v.require', 'req')
  110. ->leftJoin('v.devRequire', 'devReq')
  111. ->leftJoin('v.suggest', 'sug')
  112. ->leftJoin('v.replace', 'rep')
  113. ->leftJoin('v.conflict', 'con')
  114. ->leftJoin('v.provide', 'pro')
  115. ->orderBy('v.development', 'DESC')
  116. ->addOrderBy('v.releasedAt', 'DESC');
  117. if (null !== $ids) {
  118. $qb->where($qb->expr()->in('p.id', ':ids'))
  119. ->setParameter('ids', $ids);
  120. }
  121. $this->addFilters($qb, $filters);
  122. return $qb->getQuery()->getResult();
  123. }
  124. public function getPackagesWithVersions(array $ids = null, $filters = array())
  125. {
  126. $qb = $this->getEntityManager()->createQueryBuilder();
  127. $qb->select('p', 'v')
  128. ->from('Packagist\WebBundle\Entity\Package', 'p')
  129. ->leftJoin('p.versions', 'v')
  130. ->orderBy('v.development', 'DESC')
  131. ->addOrderBy('v.releasedAt', 'DESC');
  132. if (null !== $ids) {
  133. $qb->where($qb->expr()->in('p.id', ':ids'))
  134. ->setParameter('ids', $ids);
  135. }
  136. $this->addFilters($qb, $filters);
  137. return $qb->getQuery()->getResult();
  138. }
  139. public function getFilteredQueryBuilder(array $filters = array())
  140. {
  141. $qb = $this->getBaseQueryBuilder()
  142. ->select('p', 'v');
  143. $this->addFilters($qb, $filters);
  144. return $qb;
  145. }
  146. private function addFilters(QueryBuilder $qb, array $filters)
  147. {
  148. foreach ($filters as $name => $value) {
  149. if (null === $value) {
  150. continue;
  151. }
  152. switch ($name) {
  153. case 'tag':
  154. $qb->andWhere($qb->expr()->in('t.name', ':'.$name));
  155. break;
  156. case 'maintainer':
  157. $qb->andWhere($qb->expr()->in('m.id', ':'.$name));
  158. break;
  159. default:
  160. $qb->andWhere($qb->expr()->in('p.'.$name, ':'.$name));
  161. break;
  162. }
  163. $qb->setParameter($name, $value);
  164. }
  165. }
  166. public function getBaseQueryBuilder()
  167. {
  168. $qb = $this->getEntityManager()->createQueryBuilder();
  169. $qb->select('p', 'v', 't', 'm')
  170. ->from('Packagist\WebBundle\Entity\Package', 'p')
  171. ->leftJoin('p.versions', 'v')
  172. ->leftJoin('p.maintainers', 'm')
  173. ->leftJoin('v.tags', 't')
  174. ->orderBy('v.development', 'DESC')
  175. ->addOrderBy('v.releasedAt', 'DESC');
  176. return $qb;
  177. }
  178. /**
  179. * Gets the most recent packages created
  180. *
  181. * @param int|null $max
  182. *
  183. * @return array
  184. */
  185. public function getNewestPackages($max = null)
  186. {
  187. $qb = $this->getBaseQueryBuilder();
  188. $qb->orderBy('p.createdAt', 'DESC');
  189. $qb->addOrderBy('v.releasedAt', 'DESC');
  190. if (null !== $max) {
  191. $qb->setMaxResults($max);
  192. }
  193. return $qb->getQuery()->getResult();
  194. }
  195. /**
  196. * Gets the latest packages/versions released by a selected vendor
  197. *
  198. * @param string $vendor
  199. * @param int|null $max
  200. *
  201. * @return array
  202. */
  203. public function getLatestPackagesByVendor($vendor, $max = null)
  204. {
  205. $qb = $this->getBaseQueryBuilder();
  206. $qb->orderBy('v.releasedAt', 'DESC');
  207. $qb->where('p.name LIKE ?0');
  208. $qb->setParameter(0, $vendor.'/%');
  209. if (null !== $max) {
  210. $qb->setMaxResults($max);
  211. }
  212. return $qb->getQuery()->getResult();
  213. }
  214. }