Updater.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_EQUAL_REFS = 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. $aIsDev = $aVersion === 'dev' || substr($aVersion, -4) === '-dev';
  102. $bIsDev = $bVersion === 'dev' || substr($bVersion, -4) === '-dev';
  103. // push dev versions to the end
  104. if ($aIsDev !== $bIsDev) {
  105. return $aIsDev ? 1 : -1;
  106. }
  107. // equal versions are sorted by date
  108. if ($aVersion === $bVersion) {
  109. return $a->getReleaseDate() > $b->getReleaseDate() ? 1 : -1;
  110. }
  111. // the rest is sorted by version
  112. return version_compare($aVersion, $bVersion);
  113. });
  114. $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');
  115. if ($flags & self::DELETE_BEFORE) {
  116. foreach ($package->getVersions() as $version) {
  117. $versionRepository->remove($version);
  118. }
  119. $em->flush();
  120. $em->refresh($package);
  121. }
  122. $lastUpdated = true;
  123. foreach ($versions as $version) {
  124. if ($version instanceof AliasPackage) {
  125. continue;
  126. }
  127. if (preg_match($blacklist, $version->getName().' '.$version->getPrettyVersion())) {
  128. continue;
  129. }
  130. $lastUpdated = $this->updateInformation($package, $version, $flags);
  131. if ($lastUpdated) {
  132. $em->flush();
  133. }
  134. }
  135. if (!$lastUpdated) {
  136. $em->flush();
  137. }
  138. // remove outdated versions
  139. foreach ($package->getVersions() as $version) {
  140. if ($version->getUpdatedAt() < $pruneDate) {
  141. $versionRepository->remove($version);
  142. }
  143. }
  144. $package->setUpdatedAt(new \DateTime);
  145. $package->setCrawledAt(new \DateTime);
  146. $em->flush();
  147. }
  148. private function updateInformation(Package $package, PackageInterface $data, $flags)
  149. {
  150. $em = $this->doctrine->getManager();
  151. $version = new Version();
  152. $normVersion = $data->getVersion();
  153. $existingVersion = $package->getVersion($normVersion);
  154. if ($existingVersion) {
  155. $source = $existingVersion->getSource();
  156. // update if the right flag is set, or the source reference has changed (re-tag or new commit on branch)
  157. if ($source['reference'] !== $data->getSourceReference() || ($flags & self::UPDATE_EQUAL_REFS)) {
  158. $version = $existingVersion;
  159. } else {
  160. // mark it updated to avoid it being pruned
  161. $existingVersion->setUpdatedAt(new \DateTime);
  162. return false;
  163. }
  164. }
  165. $version->setName($package->getName());
  166. $version->setVersion($data->getPrettyVersion());
  167. $version->setNormalizedVersion($normVersion);
  168. $version->setDevelopment($data->isDev());
  169. $em->persist($version);
  170. $version->setDescription($data->getDescription());
  171. $package->setDescription($data->getDescription());
  172. $version->setHomepage($data->getHomepage());
  173. $version->setLicense($data->getLicense() ?: array());
  174. $version->setPackage($package);
  175. $version->setUpdatedAt(new \DateTime);
  176. $version->setReleasedAt($data->getReleaseDate());
  177. if ($data->getSourceType()) {
  178. $source['type'] = $data->getSourceType();
  179. $source['url'] = $data->getSourceUrl();
  180. $source['reference'] = $data->getSourceReference();
  181. $version->setSource($source);
  182. } else {
  183. $version->setSource(null);
  184. }
  185. if ($data->getDistType()) {
  186. $dist['type'] = $data->getDistType();
  187. $dist['url'] = $data->getDistUrl();
  188. $dist['reference'] = $data->getDistReference();
  189. $dist['shasum'] = $data->getDistSha1Checksum();
  190. $version->setDist($dist);
  191. } else {
  192. $version->setDist(null);
  193. }
  194. if ($data->getType()) {
  195. $version->setType($data->getType());
  196. if ($data->getType() && $data->getType() !== $package->getType()) {
  197. $package->setType($data->getType());
  198. }
  199. }
  200. $version->setTargetDir($data->getTargetDir());
  201. $version->setAutoload($data->getAutoload());
  202. $version->setExtra($data->getExtra());
  203. $version->setBinaries($data->getBinaries());
  204. $version->setIncludePaths($data->getIncludePaths());
  205. $version->setSupport($data->getSupport());
  206. $version->getTags()->clear();
  207. if ($data->getKeywords()) {
  208. $keywords = array();
  209. foreach ($data->getKeywords() as $keyword) {
  210. $keywords[mb_strtolower($keyword, 'UTF-8')] = $keyword;
  211. }
  212. foreach ($keywords as $keyword) {
  213. $tag = Tag::getByName($em, $keyword, true);
  214. if (!$version->getTags()->contains($tag)) {
  215. $version->addTag($tag);
  216. }
  217. }
  218. }
  219. $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
  220. $version->getAuthors()->clear();
  221. if ($data->getAuthors()) {
  222. foreach ($data->getAuthors() as $authorData) {
  223. $author = null;
  224. foreach (array('email', 'name', 'homepage', 'role') as $field) {
  225. if (isset($authorData[$field])) {
  226. $authorData[$field] = trim($authorData[$field]);
  227. if ('' === $authorData[$field]) {
  228. $authorData[$field] = null;
  229. }
  230. } else {
  231. $authorData[$field] = null;
  232. }
  233. }
  234. // skip authors with no information
  235. if (!isset($authorData['email']) && !isset($authorData['name'])) {
  236. continue;
  237. }
  238. $author = $authorRepository->findOneBy(array(
  239. 'email' => $authorData['email'],
  240. 'name' => $authorData['name'],
  241. 'homepage' => $authorData['homepage'],
  242. 'role' => $authorData['role'],
  243. ));
  244. if (!$author) {
  245. $author = new Author();
  246. $em->persist($author);
  247. }
  248. foreach (array('email', 'name', 'homepage', 'role') as $field) {
  249. if (isset($authorData[$field])) {
  250. $author->{'set'.$field}($authorData[$field]);
  251. }
  252. }
  253. $author->setUpdatedAt(new \DateTime);
  254. if (!$version->getAuthors()->contains($author)) {
  255. $version->addAuthor($author);
  256. }
  257. if (!$author->getVersions()->contains($version)) {
  258. $author->addVersion($version);
  259. }
  260. }
  261. }
  262. // handle links
  263. foreach ($this->supportedLinkTypes as $linkType => $opts) {
  264. $links = array();
  265. foreach ($data->{$opts['method']}() as $link) {
  266. $constraint = $link->getPrettyConstraint();
  267. if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
  268. $constraint = preg_replace_callback('{([><]=?\s*[^@]+?)@([a-z]+)}i', function ($matches) {
  269. if ($matches[2] === 'stable') {
  270. return $matches[1];
  271. }
  272. return $matches[1].'-'.$matches[2];
  273. }, $constraint);
  274. }
  275. $links[$link->getTarget()] = $constraint;
  276. }
  277. foreach ($version->{'get'.$linkType}() as $link) {
  278. // clear links that have changed/disappeared (for updates)
  279. if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
  280. $version->{'get'.$linkType}()->removeElement($link);
  281. $em->remove($link);
  282. } else {
  283. // clear those that are already set
  284. unset($links[$link->getPackageName()]);
  285. }
  286. }
  287. foreach ($links as $linkPackageName => $linkPackageVersion) {
  288. $class = 'Packagist\WebBundle\Entity\\'.$opts['entity'];
  289. $link = new $class;
  290. $link->setPackageName($linkPackageName);
  291. $link->setPackageVersion($linkPackageVersion);
  292. $version->{'add'.$linkType.'Link'}($link);
  293. $link->setVersion($version);
  294. $em->persist($link);
  295. }
  296. }
  297. // handle suggests
  298. if ($suggests = $data->getSuggests()) {
  299. foreach ($version->getSuggest() as $link) {
  300. // clear links that have changed/disappeared (for updates)
  301. if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
  302. $version->getSuggest()->removeElement($link);
  303. $em->remove($link);
  304. } else {
  305. // clear those that are already set
  306. unset($suggests[$link->getPackageName()]);
  307. }
  308. }
  309. foreach ($suggests as $linkPackageName => $linkPackageVersion) {
  310. $link = new SuggestLink;
  311. $link->setPackageName($linkPackageName);
  312. $link->setPackageVersion($linkPackageVersion);
  313. $version->addSuggestLink($link);
  314. $link->setVersion($version);
  315. $em->persist($link);
  316. }
  317. } elseif (count($version->getSuggest())) {
  318. // clear existing suggests if present
  319. foreach ($version->getSuggest() as $link) {
  320. $em->remove($link);
  321. }
  322. $version->getSuggest()->clear();
  323. }
  324. if (!$package->getVersions()->contains($version)) {
  325. $package->addVersions($version);
  326. }
  327. return true;
  328. }
  329. }