Package.php 26 KB

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