PackageRepository.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /**
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class PackageRepository extends EntityRepository
  17. {
  18. /**
  19. * Lists all package names array(name => true)
  20. *
  21. * @var array
  22. */
  23. private $packageNames;
  24. public function packageExists($name)
  25. {
  26. $packages = $this->getPackageNames();
  27. return isset($packages[$name]);
  28. }
  29. public function getPackageNames()
  30. {
  31. if (null !== $this->packageNames) {
  32. return $this->packageNames;
  33. }
  34. $names = null;
  35. $apc = extension_loaded('apc');
  36. // TODO use container to set caching key and ttl
  37. if ($apc) {
  38. $names = apc_fetch('packagist_package_names');
  39. }
  40. if (!is_array($names)) {
  41. $names = array();
  42. $query = $this->getEntityManager()
  43. ->createQuery("SELECT p.name FROM Packagist\WebBundle\Entity\Package p");
  44. foreach ($query->getScalarResult() as $package) {
  45. $names[$package['name']] = true;
  46. }
  47. if ($apc) {
  48. apc_store('packagist_package_names', $names, 3600);
  49. }
  50. }
  51. return $this->packageNames = $names;
  52. }
  53. public function getStalePackages()
  54. {
  55. $conn = $this->getEntityManager()->getConnection();
  56. return $conn->fetchAll(
  57. 'SELECT p.id FROM package p
  58. WHERE p.crawledAt IS NULL
  59. OR (p.autoUpdated = 0 AND p.crawledAt < :crawled)
  60. OR (p.crawledAt < :autocrawled)
  61. ORDER BY p.id ASC',
  62. array(
  63. 'crawled' => date('Y-m-d H:i:s', strtotime('-4hours')),
  64. 'autocrawled' => date('Y-m-d H:i:s', strtotime('-1week')),
  65. )
  66. );
  67. }
  68. public function getStalePackagesForIndexing()
  69. {
  70. $conn = $this->getEntityManager()->getConnection();
  71. return $conn->fetchAll('SELECT p.id FROM package p WHERE p.indexedAt IS NULL OR p.indexedAt <= p.crawledAt ORDER BY p.id ASC');
  72. }
  73. public function getStalePackagesForDumping()
  74. {
  75. $conn = $this->getEntityManager()->getConnection();
  76. return $conn->fetchAll('SELECT p.id FROM package p WHERE p.dumpedAt IS NULL OR p.dumpedAt <= p.crawledAt ORDER BY p.id ASC');
  77. }
  78. public function findOneByName($name)
  79. {
  80. $qb = $this->getBaseQueryBuilder()
  81. ->where('p.name = ?0')
  82. ->setParameters(array($name));
  83. return $qb->getQuery()->getSingleResult();
  84. }
  85. public function getFullPackageByName($name)
  86. {
  87. $qb = $this->getBaseQueryBuilder()
  88. ->addSelect('a', 'req', 'devReq', 'sug', 'rep', 'con', 'pro')
  89. ->leftJoin('v.authors', 'a')
  90. ->leftJoin('v.require', 'req')
  91. ->leftJoin('v.devRequire', 'devReq')
  92. ->leftJoin('v.suggest', 'sug')
  93. ->leftJoin('v.replace', 'rep')
  94. ->leftJoin('v.conflict', 'con')
  95. ->leftJoin('v.provide', 'pro')
  96. ->where('p.name = ?0')
  97. ->setParameters(array($name));
  98. return $qb->getQuery()->getSingleResult();
  99. }
  100. public function getFullPackages(array $ids = null, $filterFields = array())
  101. {
  102. $qb = $this->getEntityManager()->createQueryBuilder();
  103. $qb->select('p', 'v', 't', 'a', 'req', 'devReq', 'sug', 'rep', 'con', 'pro')
  104. ->from('Packagist\WebBundle\Entity\Package', 'p')
  105. ->leftJoin('p.versions', 'v')
  106. ->leftJoin('v.tags', 't')
  107. ->leftJoin('v.authors', 'a')
  108. ->leftJoin('v.require', 'req')
  109. ->leftJoin('v.devRequire', 'devReq')
  110. ->leftJoin('v.suggest', 'sug')
  111. ->leftJoin('v.replace', 'rep')
  112. ->leftJoin('v.conflict', 'con')
  113. ->leftJoin('v.provide', 'pro')
  114. ->orderBy('v.development', 'DESC')
  115. ->addOrderBy('v.releasedAt', 'DESC');
  116. foreach ($filterFields as $name => $value) {
  117. $qb->andWhere('p.' . $name . ' = :' . $name);
  118. }
  119. $qb->setParameters($filterFields);
  120. if (null !== $ids) {
  121. $qb->where($qb->expr()->in('p.id', ':ids'))
  122. ->setParameter('ids', $ids);
  123. }
  124. return $qb->getQuery()->getResult();
  125. }
  126. public function getQueryBuilderByTag($name)
  127. {
  128. return $this->getBaseQueryBuilder()
  129. // eliminate maintainers & tags from the select, because of the groupBy
  130. ->select('p', 'v')
  131. ->where('t.name = ?0')
  132. ->setParameters(array($name));
  133. }
  134. public function getQueryBuilderByMaintainer(User $user)
  135. {
  136. $qb = $this->getBaseQueryBuilder()
  137. // eliminate maintainers & tags from the select, because of the groupBy
  138. ->select('p', 'v')
  139. ->where('m.id = ?0')
  140. ->setParameters(array($user->getId()));
  141. return $qb;
  142. }
  143. public function getBaseQueryBuilder()
  144. {
  145. $qb = $this->getEntityManager()->createQueryBuilder();
  146. $qb->select('p', 'v', 't', 'm')
  147. ->from('Packagist\WebBundle\Entity\Package', 'p')
  148. ->leftJoin('p.versions', 'v')
  149. ->leftJoin('p.maintainers', 'm')
  150. ->leftJoin('v.tags', 't')
  151. ->orderBy('v.development', 'DESC')
  152. ->addOrderBy('v.releasedAt', 'DESC');
  153. return $qb;
  154. }
  155. }