Package.php 20 KB

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