Updater.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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\Package;
  12. use Composer\Package\AliasPackage;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Repository\RepositoryInterface;
  15. use Composer\Repository\InvalidRepositoryException;
  16. use Composer\Util\ErrorHandler;
  17. use Packagist\WebBundle\Entity\Author;
  18. use Packagist\WebBundle\Entity\Package;
  19. use Packagist\WebBundle\Entity\Tag;
  20. use Packagist\WebBundle\Entity\Version;
  21. use Packagist\WebBundle\Entity\SuggestLink;
  22. use Symfony\Bridge\Doctrine\RegistryInterface;
  23. /**
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class Updater
  27. {
  28. const UPDATE_TAGS = 1;
  29. const DELETE_BEFORE = 2;
  30. /**
  31. * Doctrine
  32. * @var RegistryInterface
  33. */
  34. protected $doctrine;
  35. /**
  36. * Supported link types
  37. * @var array
  38. */
  39. protected $supportedLinkTypes = array(
  40. 'require' => array(
  41. 'method' => 'getRequires',
  42. 'entity' => 'RequireLink',
  43. ),
  44. 'conflict' => array(
  45. 'method' => 'getConflicts',
  46. 'entity' => 'ConflictLink',
  47. ),
  48. 'provide' => array(
  49. 'method' => 'getProvides',
  50. 'entity' => 'ProvideLink',
  51. ),
  52. 'replace' => array(
  53. 'method' => 'getReplaces',
  54. 'entity' => 'ReplaceLink',
  55. ),
  56. 'devRequire' => array(
  57. 'method' => 'getDevRequires',
  58. 'entity' => 'DevRequireLink',
  59. ),
  60. );
  61. /**
  62. * Constructor
  63. *
  64. * @param RegistryInterface $doctrine
  65. */
  66. public function __construct(RegistryInterface $doctrine)
  67. {
  68. $this->doctrine = $doctrine;
  69. ErrorHandler::register();
  70. }
  71. /**
  72. * Update a project
  73. *
  74. * @param \Packagist\WebBundle\Entity\Package $package
  75. * @param RepositoryInterface $repository the repository instance used to update from
  76. * @param int $flags a few of the constants of this class
  77. * @param \DateTime $start
  78. */
  79. public function update(Package $package, RepositoryInterface $repository, $flags = 0, \DateTime $start = null)
  80. {
  81. $blacklist = '{^symfony/symfony (2.0.[456]|dev-charset|dev-console)}i';
  82. if (null === $start) {
  83. $start = new \DateTime();
  84. }
  85. $pruneDate = clone $start;
  86. $pruneDate->modify('-8days');
  87. $versions = $repository->getPackages();
  88. $em = $this->doctrine->getManager();
  89. if ($repository->hadInvalidBranches()) {
  90. throw new InvalidRepositoryException('Some branches contained invalid data and were discarded, it is advised to review the log and fix any issues present in branches');
  91. }
  92. usort($versions, function ($a, $b) {
  93. $aVersion = $a->getVersion();
  94. $bVersion = $b->getVersion();
  95. if ($aVersion === '9999999-dev' || 'dev-' === substr($aVersion, 0, 4)) {
  96. $aVersion = 'dev';
  97. }
  98. if ($bVersion === '9999999-dev' || 'dev-' === substr($bVersion, 0, 4)) {
  99. $bVersion = 'dev';
  100. }
  101. if ($aVersion === $bVersion) {
  102. return $a->getReleaseDate() > $b->getReleaseDate() ? 1 : -1;
  103. }
  104. return version_compare($a->getVersion(), $b->getVersion());
  105. });
  106. $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');
  107. if ($flags & self::DELETE_BEFORE) {
  108. foreach ($package->getVersions() as $version) {
  109. $versionRepository->remove($version);
  110. }
  111. $em->flush();
  112. $em->refresh($package);
  113. }
  114. foreach ($versions as $version) {
  115. if ($version instanceof AliasPackage) {
  116. continue;
  117. }
  118. if (preg_match($blacklist, $version->getName().' '.$version->getPrettyVersion())) {
  119. continue;
  120. }
  121. $this->updateInformation($package, $version, $flags);
  122. $em->flush();
  123. }
  124. // remove outdated versions
  125. foreach ($package->getVersions() as $version) {
  126. if ($version->getUpdatedAt() < $pruneDate) {
  127. $versionRepository->remove($version);
  128. }
  129. }
  130. $package->setUpdatedAt(new \DateTime);
  131. $package->setCrawledAt(new \DateTime);
  132. $em->flush();
  133. }
  134. private function updateInformation(Package $package, PackageInterface $data, $flags)
  135. {
  136. $em = $this->doctrine->getManager();
  137. $version = new Version();
  138. $version->setNormalizedVersion($data->getVersion());
  139. // check if we have that version yet
  140. foreach ($package->getVersions() as $existingVersion) {
  141. if ($existingVersion->getNormalizedVersion() === $version->getNormalizedVersion()) {
  142. if ($existingVersion->getDevelopment() || ($flags & self::UPDATE_TAGS)) {
  143. $version = $existingVersion;
  144. break;
  145. }
  146. // mark it updated to avoid it being pruned
  147. $existingVersion->setUpdatedAt(new \DateTime);
  148. return;
  149. }
  150. }
  151. $version->setName($package->getName());
  152. $version->setVersion($data->getPrettyVersion());
  153. $version->setDevelopment($data->isDev());
  154. $em->persist($version);
  155. $version->setDescription($data->getDescription());
  156. $package->setDescription($data->getDescription());
  157. $version->setHomepage($data->getHomepage());
  158. $version->setLicense($data->getLicense() ?: array());
  159. $version->setPackage($package);
  160. $version->setUpdatedAt(new \DateTime);
  161. $version->setReleasedAt($data->getReleaseDate());
  162. if ($data->getSourceType()) {
  163. $source['type'] = $data->getSourceType();
  164. $source['url'] = $data->getSourceUrl();
  165. $source['reference'] = $data->getSourceReference();
  166. $version->setSource($source);
  167. } else {
  168. $version->setSource(null);
  169. }
  170. if ($data->getDistType()) {
  171. $dist['type'] = $data->getDistType();
  172. $dist['url'] = $data->getDistUrl();
  173. $dist['reference'] = $data->getDistReference();
  174. $dist['shasum'] = $data->getDistSha1Checksum();
  175. $version->setDist($dist);
  176. } else {
  177. $version->setDist(null);
  178. }
  179. if ($data->getType()) {
  180. $version->setType($data->getType());
  181. if ($data->getType() && $data->getType() !== $package->getType()) {
  182. $package->setType($data->getType());
  183. }
  184. }
  185. $version->setTargetDir($data->getTargetDir());
  186. $version->setAutoload($data->getAutoload());
  187. $version->setExtra($data->getExtra());
  188. $version->setBinaries($data->getBinaries());
  189. $version->setIncludePaths($data->getIncludePaths());
  190. $version->setSupport($data->getSupport());
  191. $version->getTags()->clear();
  192. if ($data->getKeywords()) {
  193. $keywords = array();
  194. foreach ($data->getKeywords() as $keyword) {
  195. $keywords[mb_strtolower($keyword, 'UTF-8')] = $keyword;
  196. }
  197. foreach ($keywords as $keyword) {
  198. $tag = Tag::getByName($em, $keyword, true);
  199. if (!$version->getTags()->contains($tag)) {
  200. $version->addTag($tag);
  201. }
  202. }
  203. }
  204. $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
  205. $version->getAuthors()->clear();
  206. if ($data->getAuthors()) {
  207. foreach ($data->getAuthors() as $authorData) {
  208. $author = null;
  209. // skip authors with no information
  210. if (empty($authorData['email']) && empty($authorData['name'])) {
  211. continue;
  212. }
  213. if (!empty($authorData['email'])) {
  214. $author = $authorRepository->findOneByEmail($authorData['email']);
  215. }
  216. if (!$author && !empty($authorData['homepage'])) {
  217. $author = $authorRepository->findOneBy(array(
  218. 'name' => $authorData['name'],
  219. 'homepage' => $authorData['homepage']
  220. ));
  221. }
  222. if (!$author && !empty($authorData['name'])) {
  223. $author = $authorRepository->findOneByNameAndPackage($authorData['name'], $package);
  224. }
  225. if (!$author) {
  226. $author = new Author();
  227. $em->persist($author);
  228. }
  229. foreach (array('email', 'name', 'homepage', 'role') as $field) {
  230. if (isset($authorData[$field])) {
  231. $author->{'set'.$field}($authorData[$field]);
  232. }
  233. }
  234. $author->setUpdatedAt(new \DateTime);
  235. if (!$version->getAuthors()->contains($author)) {
  236. $version->addAuthor($author);
  237. }
  238. if (!$author->getVersions()->contains($version)) {
  239. $author->addVersion($version);
  240. }
  241. }
  242. }
  243. // handle links
  244. foreach ($this->supportedLinkTypes as $linkType => $opts) {
  245. $links = array();
  246. foreach ($data->{$opts['method']}() as $link) {
  247. $constraint = $link->getPrettyConstraint();
  248. if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
  249. $constraint = preg_replace_callback('{([><]=?\s*[^@]+?)@([a-z]+)}i', function ($matches) {
  250. if ($matches[2] === 'stable') {
  251. return $matches[1];
  252. }
  253. return $matches[1].'-'.$matches[2];
  254. }, $constraint);
  255. }
  256. $links[$link->getTarget()] = $constraint;
  257. }
  258. foreach ($version->{'get'.$linkType}() as $link) {
  259. // clear links that have changed/disappeared (for updates)
  260. if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
  261. $version->{'get'.$linkType}()->removeElement($link);
  262. $em->remove($link);
  263. } else {
  264. // clear those that are already set
  265. unset($links[$link->getPackageName()]);
  266. }
  267. }
  268. foreach ($links as $linkPackageName => $linkPackageVersion) {
  269. $class = 'Packagist\WebBundle\Entity\\'.$opts['entity'];
  270. $link = new $class;
  271. $link->setPackageName($linkPackageName);
  272. $link->setPackageVersion($linkPackageVersion);
  273. $version->{'add'.$linkType.'Link'}($link);
  274. $link->setVersion($version);
  275. $em->persist($link);
  276. }
  277. }
  278. // handle suggests
  279. if ($suggests = $data->getSuggests()) {
  280. foreach ($version->getSuggest() as $link) {
  281. // clear links that have changed/disappeared (for updates)
  282. if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
  283. $version->getSuggest()->removeElement($link);
  284. $em->remove($link);
  285. } else {
  286. // clear those that are already set
  287. unset($suggests[$link->getPackageName()]);
  288. }
  289. }
  290. foreach ($suggests as $linkPackageName => $linkPackageVersion) {
  291. $link = new SuggestLink;
  292. $link->setPackageName($linkPackageName);
  293. $link->setPackageVersion($linkPackageVersion);
  294. $version->addSuggestLink($link);
  295. $link->setVersion($version);
  296. $em->persist($link);
  297. }
  298. } elseif (count($version->getSuggest())) {
  299. // clear existing suggests if present
  300. foreach ($version->getSuggest() as $link) {
  301. $em->remove($link);
  302. }
  303. $version->getSuggest()->clear();
  304. }
  305. if (!$package->getVersions()->contains($version)) {
  306. $package->addVersions($version);
  307. }
  308. }
  309. }