VersionRepository.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\DBAL\Connection;
  14. use Predis\Client;
  15. /**
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class VersionRepository extends EntityRepository
  19. {
  20. private $redis;
  21. protected $supportedLinkTypes = array(
  22. 'require',
  23. 'conflict',
  24. 'provide',
  25. 'replace',
  26. 'devRequire',
  27. 'suggest',
  28. );
  29. public function setRedis(Client $client)
  30. {
  31. $this->redis = $client;
  32. }
  33. public function remove(Version $version)
  34. {
  35. $em = $this->getEntityManager();
  36. $version->getPackage()->getVersions()->removeElement($version);
  37. $version->getPackage()->setCrawledAt(new \DateTime);
  38. $version->getPackage()->setUpdatedAt(new \DateTime);
  39. $em->getConnection()->executeQuery('DELETE FROM version_author WHERE version_id=:id', array('id' => $version->getId()));
  40. $em->getConnection()->executeQuery('DELETE FROM version_tag WHERE version_id=:id', array('id' => $version->getId()));
  41. $em->getConnection()->executeQuery('DELETE FROM link_suggest WHERE version_id=:id', array('id' => $version->getId()));
  42. $em->getConnection()->executeQuery('DELETE FROM link_conflict WHERE version_id=:id', array('id' => $version->getId()));
  43. $em->getConnection()->executeQuery('DELETE FROM link_replace WHERE version_id=:id', array('id' => $version->getId()));
  44. $em->getConnection()->executeQuery('DELETE FROM link_provide WHERE version_id=:id', array('id' => $version->getId()));
  45. $em->getConnection()->executeQuery('DELETE FROM link_require_dev WHERE version_id=:id', array('id' => $version->getId()));
  46. $em->getConnection()->executeQuery('DELETE FROM link_require WHERE version_id=:id', array('id' => $version->getId()));
  47. $em->getConnection()->executeQuery('DELETE FROM download WHERE id=:id AND type = :type', ['id' => $version->getId(), 'type' => Download::TYPE_VERSION]);
  48. $em->remove($version);
  49. }
  50. public function refreshVersions($versions)
  51. {
  52. $versionIds = [];
  53. foreach ($versions as $version) {
  54. $versionIds[] = $version->getId();
  55. $this->getEntityManager()->detach($version);
  56. }
  57. $refreshedVersions = $this->findBy(['id' => $versionIds]);
  58. $versionsById = [];
  59. foreach ($refreshedVersions as $version) {
  60. $versionsById[$version->getId()] = $version;
  61. }
  62. $refreshedVersions = [];
  63. foreach ($versions as $version) {
  64. $refreshedVersions[] = $versionsById[$version->getId()];
  65. }
  66. return $refreshedVersions;
  67. }
  68. /**
  69. * @param Version[] $versions
  70. */
  71. public function detachToArray(array $versions, array $versionData): array
  72. {
  73. $res = [];
  74. $em = $this->getEntityManager();
  75. foreach ($versions as $version) {
  76. $res[$version->getVersion()] = $version->toArray($versionData);
  77. $em->detach($version);
  78. }
  79. return $res;
  80. }
  81. public function getVersionData(array $versionIds)
  82. {
  83. $links = [
  84. 'require' => 'link_require',
  85. 'devRequire' => 'link_require_dev',
  86. 'suggest' => 'link_suggest',
  87. 'conflict' => 'link_conflict',
  88. 'provide' => 'link_provide',
  89. 'replace' => 'link_replace',
  90. ];
  91. $result = [];
  92. foreach ($versionIds as $id) {
  93. $result[$id] = [
  94. 'require' => [],
  95. 'devRequire' => [],
  96. 'suggest' => [],
  97. 'conflict' => [],
  98. 'provide' => [],
  99. 'replace' => [],
  100. 'authors' => [],
  101. 'tags' => [],
  102. ];
  103. }
  104. foreach ($links as $link => $table) {
  105. $rows = $this->getEntityManager()->getConnection()->fetchAll(
  106. 'SELECT version_id, packageName name, packageVersion version FROM '.$table.' WHERE version_id IN (:ids)',
  107. ['ids' => $versionIds],
  108. ['ids' => Connection::PARAM_INT_ARRAY]
  109. );
  110. foreach ($rows as $row) {
  111. $result[$row['version_id']][$link][] = $row;
  112. }
  113. }
  114. $rows = $this->getEntityManager()->getConnection()->fetchAll(
  115. 'SELECT va.version_id, name, email, homepage, role FROM author a JOIN version_author va ON va.author_id = a.id WHERE va.version_id IN (:ids)',
  116. ['ids' => $versionIds],
  117. ['ids' => Connection::PARAM_INT_ARRAY]
  118. );
  119. foreach ($rows as $row) {
  120. $versionId = $row['version_id'];
  121. unset($row['version_id']);
  122. $result[$versionId]['authors'][] = array_filter($row);
  123. }
  124. $rows = $this->getEntityManager()->getConnection()->fetchAll(
  125. 'SELECT vt.version_id, name FROM tag t JOIN version_tag vt ON vt.tag_id = t.id WHERE vt.version_id IN (:ids)',
  126. ['ids' => $versionIds],
  127. ['ids' => Connection::PARAM_INT_ARRAY]
  128. );
  129. foreach ($rows as $row) {
  130. $versionId = $row['version_id'];
  131. $result[$versionId]['tags'][] = $row['name'];
  132. }
  133. return $result;
  134. }
  135. public function getVersionMetadataForUpdate(Package $package)
  136. {
  137. $rows = $this->getEntityManager()->getConnection()->fetchAll(
  138. 'SELECT id, version, normalizedVersion, source, softDeletedAt, `authors` IS NULL as needs_author_migration FROM package_version v WHERE v.package_id = :id',
  139. ['id' => $package->getId()]
  140. );
  141. $versions = [];
  142. foreach ($rows as $row) {
  143. if ($row['source']) {
  144. $row['source'] = json_decode($row['source'], true);
  145. }
  146. $row['needs_author_migration'] = (int) $row['needs_author_migration'];
  147. $versions[strtolower($row['normalizedVersion'])] = $row;
  148. }
  149. return $versions;
  150. }
  151. public function getFullVersion($versionId)
  152. {
  153. $qb = $this->getEntityManager()->createQueryBuilder();
  154. $qb->select('v', 't', 'a')
  155. ->from('Packagist\WebBundle\Entity\Version', 'v')
  156. ->leftJoin('v.tags', 't')
  157. ->leftJoin('v.authors', 'a')
  158. ->where('v.id = :id')
  159. ->setParameter('id', $versionId);
  160. return $qb->getQuery()->getSingleResult();
  161. }
  162. /**
  163. * Returns the latest versions released
  164. *
  165. * @param string $vendor optional vendor filter
  166. * @param string $package optional vendor/package filter
  167. * @return \Doctrine\ORM\QueryBuilder
  168. */
  169. public function getQueryBuilderForLatestVersionWithPackage($vendor = null, $package = null)
  170. {
  171. $qb = $this->getEntityManager()->createQueryBuilder();
  172. $qb->select('v')
  173. ->from('Packagist\WebBundle\Entity\Version', 'v')
  174. ->where('v.development = false')
  175. ->andWhere('v.releasedAt <= ?0')
  176. ->orderBy('v.releasedAt', 'DESC');
  177. $qb->setParameter(0, date('Y-m-d H:i:s'));
  178. if ($vendor || $package) {
  179. $qb->innerJoin('v.package', 'p')
  180. ->addSelect('p');
  181. }
  182. if ($vendor) {
  183. $qb->andWhere('p.name LIKE ?1');
  184. $qb->setParameter(1, $vendor.'/%');
  185. } elseif ($package) {
  186. $qb->andWhere('p.name = ?1')
  187. ->setParameter(1, $package);
  188. }
  189. return $qb;
  190. }
  191. public function getLatestReleases($count = 10)
  192. {
  193. if ($cached = $this->redis->get('new_releases')) {
  194. return json_decode($cached, true);
  195. }
  196. $qb = $this->getEntityManager()->createQueryBuilder();
  197. $qb->select('v.name, v.version, v.description')
  198. ->from('Packagist\WebBundle\Entity\Version', 'v')
  199. ->where('v.development = false')
  200. ->andWhere('v.releasedAt < :now')
  201. ->orderBy('v.releasedAt', 'DESC')
  202. ->setMaxResults($count)
  203. ->setParameter('now', date('Y-m-d H:i:s'));
  204. $res = $qb->getQuery()->getResult();
  205. $this->redis->setex('new_releases', 600, json_encode($res));
  206. return $res;
  207. }
  208. }