Package.php 20 KB

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