Package.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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 Composer\Factory;
  13. use Composer\IO\NullIO;
  14. use Composer\Repository\VcsRepository;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  19. use Composer\Repository\Vcs\GitHubDriver;
  20. /**
  21. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
  22. * @ORM\Table(
  23. * name="package",
  24. * uniqueConstraints={@ORM\UniqueConstraint(name="package_name_idx", columns={"name"})},
  25. * indexes={
  26. * @ORM\Index(name="indexed_idx",columns={"indexedAt"}),
  27. * @ORM\Index(name="crawled_idx",columns={"crawledAt"}),
  28. * @ORM\Index(name="dumped_idx",columns={"dumpedAt"}),
  29. * @ORM\Index(name="repository_idx",columns={"repository"})
  30. * }
  31. * )
  32. * @Assert\Callback(callback="isPackageUnique")
  33. * @Assert\Callback(callback="isVendorWritable")
  34. * @Assert\Callback(callback="isRepositoryValid", groups={"Update", "Default"})
  35. * @author Jordi Boggiano <j.boggiano@seld.be>
  36. */
  37. class Package
  38. {
  39. const AUTO_MANUAL_HOOK = 1;
  40. const AUTO_GITHUB_HOOK = 2;
  41. /**
  42. * @ORM\Id
  43. * @ORM\Column(type="integer")
  44. * @ORM\GeneratedValue(strategy="AUTO")
  45. */
  46. private $id;
  47. /**
  48. * Unique package name
  49. *
  50. * @ORM\Column(length=191)
  51. */
  52. private $name;
  53. /**
  54. * @ORM\Column(nullable=true)
  55. */
  56. private $type;
  57. /**
  58. * @ORM\Column(type="text", nullable=true)
  59. */
  60. private $description;
  61. /**
  62. * @ORM\Column(type="string", nullable=true)
  63. */
  64. private $language;
  65. /**
  66. * @ORM\Column(type="text", nullable=true)
  67. */
  68. private $readme;
  69. /**
  70. * @ORM\Column(type="integer", nullable=true, name="github_stars")
  71. */
  72. private $gitHubStars;
  73. /**
  74. * @ORM\Column(type="integer", nullable=true, name="github_watches")
  75. */
  76. private $gitHubWatches;
  77. /**
  78. * @ORM\Column(type="integer", nullable=true, name="github_forks")
  79. */
  80. private $gitHubForks;
  81. /**
  82. * @ORM\Column(type="integer", nullable=true, name="github_open_issues")
  83. */
  84. private $gitHubOpenIssues;
  85. /**
  86. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
  87. */
  88. private $versions;
  89. /**
  90. * @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
  91. * @ORM\JoinTable(name="maintainers_packages")
  92. */
  93. private $maintainers;
  94. /**
  95. * @ORM\Column()
  96. * @Assert\NotBlank(groups={"Update", "Default"})
  97. */
  98. private $repository;
  99. // dist-tags / rel or runtime?
  100. /**
  101. * @ORM\Column(type="datetime")
  102. */
  103. private $createdAt;
  104. /**
  105. * @ORM\Column(type="datetime", nullable=true)
  106. */
  107. private $updatedAt;
  108. /**
  109. * @ORM\Column(type="datetime", nullable=true)
  110. */
  111. private $crawledAt;
  112. /**
  113. * @ORM\Column(type="datetime", nullable=true)
  114. */
  115. private $indexedAt;
  116. /**
  117. * @ORM\Column(type="datetime", nullable=true)
  118. */
  119. private $dumpedAt;
  120. /**
  121. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Download", mappedBy="package")
  122. */
  123. private $downloads;
  124. /**
  125. * @ORM\Column(type="smallint")
  126. */
  127. private $autoUpdated = 0;
  128. /**
  129. * @var bool
  130. * @ORM\Column(type="boolean")
  131. */
  132. private $abandoned = false;
  133. /**
  134. * @var string
  135. * @ORM\Column(type="string", length=255, nullable=true)
  136. */
  137. private $replacementPackage;
  138. /**
  139. * @ORM\Column(type="boolean", options={"default"=false})
  140. */
  141. private $updateFailureNotified = false;
  142. private $entityRepository;
  143. private $router;
  144. /**
  145. * @var \Composer\Repository\Vcs\VcsDriverInterface
  146. */
  147. private $vcsDriver = true;
  148. private $vcsDriverError;
  149. /**
  150. * @var array lookup table for versions
  151. */
  152. private $cachedVersions;
  153. public function __construct()
  154. {
  155. $this->versions = new ArrayCollection();
  156. $this->createdAt = new \DateTime;
  157. }
  158. public function toArray(VersionRepository $versionRepo)
  159. {
  160. $versions = array();
  161. $partialVersions = $this->getVersions()->toArray();
  162. while ($partialVersions) {
  163. $slice = array_splice($partialVersions, 0, 100);
  164. $fullVersions = $versionRepo->refreshVersions($slice);
  165. $versionData = $versionRepo->getVersionData(array_map(function ($v) { return $v->getId(); }, $fullVersions));
  166. $versions = array_merge($versions, $versionRepo->detachToArray($fullVersions, $versionData));
  167. }
  168. $maintainers = array();
  169. foreach ($this->getMaintainers() as $maintainer) {
  170. /** @var $maintainer User */
  171. $maintainers[] = $maintainer->toArray();
  172. }
  173. $data = array(
  174. 'name' => $this->getName(),
  175. 'description' => $this->getDescription(),
  176. 'time' => $this->getCreatedAt()->format('c'),
  177. 'maintainers' => $maintainers,
  178. 'versions' => $versions,
  179. 'type' => $this->getType(),
  180. 'repository' => $this->getRepository(),
  181. 'github_stars' => $this->getGitHubStars(),
  182. 'github_watchers' => $this->getGitHubWatches(),
  183. 'github_forks' => $this->getGitHubForks(),
  184. 'github_open_issues' => $this->getGitHubOpenIssues(),
  185. 'language' => $this->getLanguage(),
  186. );
  187. if ($this->isAbandoned()) {
  188. $data['abandoned'] = $this->getReplacementPackage() ?: true;
  189. }
  190. return $data;
  191. }
  192. public function isRepositoryValid(ExecutionContextInterface $context)
  193. {
  194. // vcs driver was not nulled which means the repository was not set/modified and is still valid
  195. if (true === $this->vcsDriver && null !== $this->getName()) {
  196. return;
  197. }
  198. $property = 'repository';
  199. $driver = $this->vcsDriver;
  200. if (!is_object($driver)) {
  201. if (preg_match('{^http://}', $this->repository)) {
  202. $context->buildViolation('Non-secure HTTP URLs are not supported, make sure you use an HTTPS or SSH URL')
  203. ->atPath($property)
  204. ->addViolation()
  205. ;
  206. } elseif (preg_match('{https?://.+@}', $this->repository)) {
  207. $context->buildViolation('URLs with user@host are not supported, use a read-only public URL')
  208. ->atPath($property)
  209. ->addViolation()
  210. ;
  211. } elseif (is_string($this->vcsDriverError)) {
  212. $context->buildViolation('Uncaught Exception: '.htmlentities($this->vcsDriverError, ENT_COMPAT, 'utf-8'))
  213. ->atPath($property)
  214. ->addViolation()
  215. ;
  216. } else {
  217. $context->buildViolation('No valid/supported repository was found at the given URL')
  218. ->atPath($property)
  219. ->addViolation()
  220. ;
  221. }
  222. return;
  223. }
  224. try {
  225. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  226. if (false === $information) {
  227. $context->buildViolation('No composer.json was found in the '.$driver->getRootIdentifier().' branch.')
  228. ->atPath($property)
  229. ->addViolation()
  230. ;
  231. return;
  232. }
  233. if (empty($information['name'])) {
  234. $context->buildViolation('The package name was not found in the composer.json, make sure there is a name present.')
  235. ->atPath($property)
  236. ->addViolation()
  237. ;
  238. return;
  239. }
  240. if (!preg_match('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}iD', $information['name'])) {
  241. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*".')
  242. ->atPath($property)
  243. ->addViolation()
  244. ;
  245. return;
  246. }
  247. if (preg_match('{(free.*watch|watch.*free|movie.*free|free.*movie|watch.*movie|watch.*full|generate.*resource|generate.*unlimited|hack.*coin|coin.*hack|v[.-]?bucks|(fortnite|pubg).*free|hack.*cheat|cheat.*hack|putlocker)}i', $information['name'])) {
  248. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is blocked, if you think this is a mistake please get in touch with us.')
  249. ->atPath($property)
  250. ->addViolation()
  251. ;
  252. return;
  253. }
  254. $reservedNames = ['nul', 'con', 'prn', 'aux', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'];
  255. $bits = explode('/', strtolower($information['name']));
  256. if (in_array($bits[0], $reservedNames, true) || in_array($bits[1], $reservedNames, true)) {
  257. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is reserved, package and vendor names can not match any of: '.implode(', ', $reservedNames).'.')
  258. ->atPath($property)
  259. ->addViolation()
  260. ;
  261. return;
  262. }
  263. if (preg_match('{\.json$}', $information['name'])) {
  264. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.')
  265. ->atPath($property)
  266. ->addViolation()
  267. ;
  268. return;
  269. }
  270. if (preg_match('{[A-Z]}', $information['name'])) {
  271. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
  272. $suggestName = strtolower($suggestName);
  273. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.')
  274. ->atPath($property)
  275. ->addViolation()
  276. ;
  277. return;
  278. }
  279. } catch (\Exception $e) {
  280. $context->buildViolation('We had problems parsing your composer.json file, the parser reports: '.htmlentities($e->getMessage(), ENT_COMPAT, 'utf-8'))
  281. ->atPath($property)
  282. ->addViolation()
  283. ;
  284. }
  285. if (null === $this->getName()) {
  286. $context->buildViolation('An unexpected error has made our parser fail to find a package name in your repository, if you think this is incorrect please try again')
  287. ->atPath($property)
  288. ->addViolation()
  289. ;
  290. }
  291. }
  292. public function setEntityRepository($repository)
  293. {
  294. $this->entityRepository = $repository;
  295. }
  296. public function setRouter($router)
  297. {
  298. $this->router = $router;
  299. }
  300. public function isPackageUnique(ExecutionContextInterface $context)
  301. {
  302. try {
  303. if ($this->entityRepository->findOneByName($this->name)) {
  304. $context->buildViolation('A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.')
  305. ->atPath('repository')
  306. ->addViolation()
  307. ;
  308. }
  309. } catch (\Doctrine\ORM\NoResultException $e) {}
  310. }
  311. public function isVendorWritable(ExecutionContextInterface $context)
  312. {
  313. try {
  314. $vendor = $this->getVendor();
  315. if ($vendor && $this->entityRepository->isVendorTaken($vendor, reset($this->maintainers))) {
  316. $context->buildViolation('The vendor is already taken by someone else. '
  317. . 'You may ask them to add your package and give you maintainership access. '
  318. . 'If they add you as a maintainer on any package in that vendor namespace, '
  319. . 'you will then be able to add new packages in that namespace. '
  320. . 'The packages already in that vendor namespace can be found at '
  321. . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>')
  322. ->atPath('repository')
  323. ->addViolation()
  324. ;
  325. }
  326. } catch (\Doctrine\ORM\NoResultException $e) {}
  327. }
  328. /**
  329. * Get id
  330. *
  331. * @return string
  332. */
  333. public function getId()
  334. {
  335. return $this->id;
  336. }
  337. /**
  338. * Set name
  339. *
  340. * @param string $name
  341. */
  342. public function setName($name)
  343. {
  344. $this->name = $name;
  345. }
  346. /**
  347. * Get name
  348. *
  349. * @return string
  350. */
  351. public function getName()
  352. {
  353. return $this->name;
  354. }
  355. /**
  356. * Get vendor prefix
  357. *
  358. * @return string
  359. */
  360. public function getVendor()
  361. {
  362. return preg_replace('{/.*$}', '', $this->name);
  363. }
  364. /**
  365. * Get package name without vendor
  366. *
  367. * @return string
  368. */
  369. public function getPackageName()
  370. {
  371. return preg_replace('{^[^/]*/}', '', $this->name);
  372. }
  373. /**
  374. * Set description
  375. *
  376. * @param string $description
  377. */
  378. public function setDescription($description)
  379. {
  380. $this->description = $description;
  381. }
  382. /**
  383. * Get description
  384. *
  385. * @return string
  386. */
  387. public function getDescription()
  388. {
  389. return $this->description;
  390. }
  391. /**
  392. * Set language
  393. *
  394. * @param string $language
  395. */
  396. public function setLanguage($language)
  397. {
  398. $this->language = $language;
  399. }
  400. /**
  401. * Get language
  402. *
  403. * @return string
  404. */
  405. public function getLanguage()
  406. {
  407. return $this->language;
  408. }
  409. /**
  410. * Set readme
  411. *
  412. * @param string $readme
  413. */
  414. public function setReadme($readme)
  415. {
  416. $this->readme = $readme;
  417. }
  418. /**
  419. * Get readme
  420. *
  421. * @return string
  422. */
  423. public function getReadme()
  424. {
  425. return $this->readme;
  426. }
  427. /**
  428. * Get readme with transformations that should not be done in the stored readme as they might not be valid in the long run
  429. *
  430. * @return string
  431. */
  432. public function getOptimizedReadme()
  433. {
  434. return str_replace(['<img src="https://raw.github.com/', '<img src="https://raw.githubusercontent.com/'], '<img src="https://rawcdn.githack.com/', $this->readme);
  435. }
  436. /**
  437. * @param int $val
  438. */
  439. public function setGitHubStars($val)
  440. {
  441. $this->gitHubStars = $val;
  442. }
  443. /**
  444. * @return int
  445. */
  446. public function getGitHubStars()
  447. {
  448. return $this->gitHubStars;
  449. }
  450. /**
  451. * @param int $val
  452. */
  453. public function setGitHubWatches($val)
  454. {
  455. $this->gitHubWatches = $val;
  456. }
  457. /**
  458. * @return int
  459. */
  460. public function getGitHubWatches()
  461. {
  462. return $this->gitHubWatches;
  463. }
  464. /**
  465. * @param int $val
  466. */
  467. public function setGitHubForks($val)
  468. {
  469. $this->gitHubForks = $val;
  470. }
  471. /**
  472. * @return int
  473. */
  474. public function getGitHubForks()
  475. {
  476. return $this->gitHubForks;
  477. }
  478. /**
  479. * @param int $val
  480. */
  481. public function setGitHubOpenIssues($val)
  482. {
  483. $this->gitHubOpenIssues = $val;
  484. }
  485. /**
  486. * @return int
  487. */
  488. public function getGitHubOpenIssues()
  489. {
  490. return $this->gitHubOpenIssues;
  491. }
  492. /**
  493. * Set createdAt
  494. *
  495. * @param \DateTime $createdAt
  496. */
  497. public function setCreatedAt($createdAt)
  498. {
  499. $this->createdAt = $createdAt;
  500. }
  501. /**
  502. * Get createdAt
  503. *
  504. * @return \DateTime
  505. */
  506. public function getCreatedAt()
  507. {
  508. return $this->createdAt;
  509. }
  510. /**
  511. * Set repository
  512. *
  513. * @param string $repository
  514. */
  515. public function setRepository($repoUrl)
  516. {
  517. $this->vcsDriver = null;
  518. // prevent local filesystem URLs
  519. if (preg_match('{^(\.|[a-z]:|/)}i', $repoUrl)) {
  520. return;
  521. }
  522. $repoUrl = preg_replace('{^git@github.com:}i', 'https://github.com/', $repoUrl);
  523. $repoUrl = preg_replace('{^git://github.com/}i', 'https://github.com/', $repoUrl);
  524. $repoUrl = preg_replace('{^(https://github.com/.*?)\.git$}i', '$1', $repoUrl);
  525. // normalize protocol case
  526. $repoUrl = preg_replace_callback('{^(https?|git|svn)://}i', function ($match) { return strtolower($match[1]) . '://'; }, $repoUrl);
  527. $this->repository = $repoUrl;
  528. // avoid user@host URLs
  529. if (preg_match('{https?://.+@}', $repoUrl)) {
  530. return;
  531. }
  532. try {
  533. $io = new NullIO();
  534. $config = Factory::createConfig();
  535. $io->loadConfiguration($config);
  536. $repository = new VcsRepository(array('url' => $this->repository), $io, $config);
  537. $driver = $this->vcsDriver = $repository->getDriver();
  538. if (!$driver) {
  539. return;
  540. }
  541. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  542. if (!isset($information['name'])) {
  543. return;
  544. }
  545. if (null === $this->getName()) {
  546. $this->setName(trim($information['name']));
  547. }
  548. if ($driver instanceof GitHubDriver) {
  549. $this->repository = $driver->getRepositoryUrl();
  550. }
  551. } catch (\Exception $e) {
  552. $this->vcsDriverError = '['.get_class($e).'] '.$e->getMessage();
  553. }
  554. }
  555. /**
  556. * Get repository
  557. *
  558. * @return string $repository
  559. */
  560. public function getRepository()
  561. {
  562. return $this->repository;
  563. }
  564. /**
  565. * Get a user-browsable version of the repository URL
  566. *
  567. * @return string $repository
  568. */
  569. public function getBrowsableRepository()
  570. {
  571. if (preg_match('{(://|@)bitbucket.org[:/]}i', $this->repository)) {
  572. return preg_replace('{^(?:git@|https://|git://)bitbucket.org[:/](.+?)(?:\.git)?$}i', 'https://bitbucket.org/$1', $this->repository);
  573. }
  574. return preg_replace('{^(git://github.com/|git@github.com:)}', 'https://github.com/', $this->repository);
  575. }
  576. /**
  577. * Add versions
  578. *
  579. * @param Version $versions
  580. */
  581. public function addVersions(Version $versions)
  582. {
  583. $this->versions[] = $versions;
  584. }
  585. /**
  586. * Get versions
  587. *
  588. * @return \Doctrine\Common\Collections\Collection
  589. */
  590. public function getVersions()
  591. {
  592. return $this->versions;
  593. }
  594. public function getVersion($normalizedVersion)
  595. {
  596. if (null === $this->cachedVersions) {
  597. $this->cachedVersions = array();
  598. foreach ($this->getVersions() as $version) {
  599. $this->cachedVersions[strtolower($version->getNormalizedVersion())] = $version;
  600. }
  601. }
  602. if (isset($this->cachedVersions[strtolower($normalizedVersion)])) {
  603. return $this->cachedVersions[strtolower($normalizedVersion)];
  604. }
  605. }
  606. /**
  607. * Set updatedAt
  608. *
  609. * @param \DateTime $updatedAt
  610. */
  611. public function setUpdatedAt($updatedAt)
  612. {
  613. $this->updatedAt = $updatedAt;
  614. $this->setUpdateFailureNotified(false);
  615. }
  616. /**
  617. * Get updatedAt
  618. *
  619. * @return \DateTime
  620. */
  621. public function getUpdatedAt()
  622. {
  623. return $this->updatedAt;
  624. }
  625. /**
  626. * Set crawledAt
  627. *
  628. * @param \DateTime|null $crawledAt
  629. */
  630. public function setCrawledAt($crawledAt)
  631. {
  632. $this->crawledAt = $crawledAt;
  633. }
  634. /**
  635. * Get crawledAt
  636. *
  637. * @return \DateTime
  638. */
  639. public function getCrawledAt()
  640. {
  641. return $this->crawledAt;
  642. }
  643. /**
  644. * Set indexedAt
  645. *
  646. * @param \DateTime $indexedAt
  647. */
  648. public function setIndexedAt($indexedAt)
  649. {
  650. $this->indexedAt = $indexedAt;
  651. }
  652. /**
  653. * Get indexedAt
  654. *
  655. * @return \DateTime
  656. */
  657. public function getIndexedAt()
  658. {
  659. return $this->indexedAt;
  660. }
  661. /**
  662. * Set dumpedAt
  663. *
  664. * @param \DateTime $dumpedAt
  665. */
  666. public function setDumpedAt($dumpedAt)
  667. {
  668. $this->dumpedAt = $dumpedAt;
  669. }
  670. /**
  671. * Get dumpedAt
  672. *
  673. * @return \DateTime
  674. */
  675. public function getDumpedAt()
  676. {
  677. return $this->dumpedAt;
  678. }
  679. /**
  680. * Add maintainers
  681. *
  682. * @param User $maintainer
  683. */
  684. public function addMaintainer(User $maintainer)
  685. {
  686. $this->maintainers[] = $maintainer;
  687. }
  688. /**
  689. * Get maintainers
  690. *
  691. * @return \Doctrine\Common\Collections\Collection
  692. */
  693. public function getMaintainers()
  694. {
  695. return $this->maintainers;
  696. }
  697. /**
  698. * Set type
  699. *
  700. * @param string $type
  701. */
  702. public function setType($type)
  703. {
  704. $this->type = $type;
  705. }
  706. /**
  707. * Get type
  708. *
  709. * @return string
  710. */
  711. public function getType()
  712. {
  713. return $this->type;
  714. }
  715. /**
  716. * Set autoUpdated
  717. *
  718. * @param int $autoUpdated
  719. */
  720. public function setAutoUpdated($autoUpdated)
  721. {
  722. $this->autoUpdated = $autoUpdated;
  723. }
  724. /**
  725. * Get autoUpdated
  726. *
  727. * @return int
  728. */
  729. public function getAutoUpdated()
  730. {
  731. return $this->autoUpdated;
  732. }
  733. /**
  734. * Get autoUpdated
  735. *
  736. * @return Boolean
  737. */
  738. public function isAutoUpdated()
  739. {
  740. return $this->autoUpdated > 0;
  741. }
  742. /**
  743. * Set updateFailureNotified
  744. *
  745. * @param Boolean $updateFailureNotified
  746. */
  747. public function setUpdateFailureNotified($updateFailureNotified)
  748. {
  749. $this->updateFailureNotified = $updateFailureNotified;
  750. }
  751. /**
  752. * Get updateFailureNotified
  753. *
  754. * @return Boolean
  755. */
  756. public function isUpdateFailureNotified()
  757. {
  758. return $this->updateFailureNotified;
  759. }
  760. /**
  761. * @return boolean
  762. */
  763. public function isAbandoned()
  764. {
  765. return $this->abandoned;
  766. }
  767. /**
  768. * @param boolean $abandoned
  769. */
  770. public function setAbandoned($abandoned)
  771. {
  772. $this->abandoned = $abandoned;
  773. }
  774. /**
  775. * @return string
  776. */
  777. public function getReplacementPackage()
  778. {
  779. return $this->replacementPackage;
  780. }
  781. /**
  782. * @param string $replacementPackage
  783. */
  784. public function setReplacementPackage($replacementPackage)
  785. {
  786. $this->replacementPackage = $replacementPackage;
  787. }
  788. public static function sortVersions($a, $b)
  789. {
  790. $aVersion = $a->getNormalizedVersion();
  791. $bVersion = $b->getNormalizedVersion();
  792. $aVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
  793. $bVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $bVersion);
  794. // equal versions are sorted by date
  795. if ($aVersion === $bVersion) {
  796. // make sure sort is stable
  797. if ($a->getReleasedAt() === $a->getReleasedAt()) {
  798. return $a->getNormalizedVersion() <=> $b->getNormalizedVersion();
  799. }
  800. return $b->getReleasedAt() > $a->getReleasedAt() ? 1 : -1;
  801. }
  802. // the rest is sorted by version
  803. return version_compare($bVersion, $aVersion);
  804. }
  805. }