Package.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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\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()
  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()
  151. {
  152. $versions = array();
  153. foreach ($this->getVersions() as $version) {
  154. /** @var $version Version */
  155. $versions[$version->getVersion()] = $version->toArray();
  156. }
  157. $maintainers = array();
  158. foreach ($this->getMaintainers() as $maintainer) {
  159. /** @var $maintainer User */
  160. $maintainers[] = $maintainer->toArray();
  161. }
  162. $data = array(
  163. 'name' => $this->getName(),
  164. 'description' => $this->getDescription(),
  165. 'time' => $this->getCreatedAt()->format('c'),
  166. 'maintainers' => $maintainers,
  167. 'versions' => $versions,
  168. 'type' => $this->getType(),
  169. 'repository' => $this->getRepository(),
  170. );
  171. if ($this->isAbandoned()) {
  172. $data['abandoned'] = $this->getReplacementPackage() ?: true;
  173. }
  174. return $data;
  175. }
  176. public function isRepositoryValid(ExecutionContextInterface $context)
  177. {
  178. // vcs driver was not nulled which means the repository was not set/modified and is still valid
  179. if (true === $this->vcsDriver && null !== $this->getName()) {
  180. return;
  181. }
  182. $property = 'repository';
  183. $driver = $this->vcsDriver;
  184. if (!is_object($driver)) {
  185. if (preg_match('{https?://.+@}', $this->repository)) {
  186. $context->addViolationAt($property, 'URLs with user@host are not supported, use a read-only public URL', array(), null);
  187. } elseif (is_string($this->vcsDriverError)) {
  188. $context->addViolationAt($property, 'Uncaught Exception: '.$this->vcsDriverError, array(), null);
  189. } else {
  190. $context->addViolationAt($property, 'No valid/supported repository was found at the given URL', array(), null);
  191. }
  192. return;
  193. }
  194. try {
  195. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  196. if (false === $information) {
  197. $context->addViolationAt($property, 'No composer.json was found in the '.$driver->getRootIdentifier().' branch.', array(), null);
  198. return;
  199. }
  200. if (empty($information['name'])) {
  201. $context->addViolationAt($property, 'The package name was not found in the composer.json, make sure there is a name present.', array(), null);
  202. return;
  203. }
  204. if (!preg_match('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}i', $information['name'])) {
  205. $context->addViolationAt($property, 'The package name '.$information['name'].' 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]+)*".', array(), null);
  206. return;
  207. }
  208. if (preg_match('{\.json$}', $information['name'])) {
  209. $context->addViolationAt($property, 'The package name '.$information['name'].' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.', array(), null);
  210. return;
  211. }
  212. if (preg_match('{[A-Z]}', $information['name'])) {
  213. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
  214. $suggestName = strtolower($suggestName);
  215. $context->addViolationAt($property, 'The package name '.$information['name'].' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.');
  216. return;
  217. }
  218. } catch (\Exception $e) {
  219. $context->addViolationAt($property, 'We had problems parsing your composer.json file, the parser reports: '.$e->getMessage(), array(), null);
  220. }
  221. if (null === $this->getName()) {
  222. $context->addViolationAt($property, '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', array(), null);
  223. }
  224. }
  225. public function setEntityRepository($repository)
  226. {
  227. $this->entityRepository = $repository;
  228. }
  229. public function setRouter($router)
  230. {
  231. $this->router = $router;
  232. }
  233. public function isPackageUnique(ExecutionContextInterface $context)
  234. {
  235. try {
  236. if ($this->entityRepository->findOneByName($this->name)) {
  237. $context->addViolationAt('repository', 'A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.', array(), null);
  238. }
  239. } catch (\Doctrine\ORM\NoResultException $e) {}
  240. }
  241. public function isVendorWritable(ExecutionContextInterface $context)
  242. {
  243. try {
  244. $vendor = $this->getVendor();
  245. if ($vendor && $this->entityRepository->isVendorTaken($vendor, reset($this->maintainers))) {
  246. $context->addViolationAt(
  247. 'repository',
  248. 'The vendor is already taken by someone else. '
  249. . 'You may ask them to add your package and give you maintainership access. '
  250. . 'The packages already in that vendor namespace can be found at '
  251. . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>',
  252. array(),
  253. null
  254. );
  255. }
  256. } catch (\Doctrine\ORM\NoResultException $e) {}
  257. }
  258. /**
  259. * Get id
  260. *
  261. * @return string
  262. */
  263. public function getId()
  264. {
  265. return $this->id;
  266. }
  267. /**
  268. * Set name
  269. *
  270. * @param string $name
  271. */
  272. public function setName($name)
  273. {
  274. $this->name = $name;
  275. }
  276. /**
  277. * Get name
  278. *
  279. * @return string
  280. */
  281. public function getName()
  282. {
  283. return $this->name;
  284. }
  285. /**
  286. * Get vendor prefix
  287. *
  288. * @return string
  289. */
  290. public function getVendor()
  291. {
  292. return preg_replace('{/.*$}', '', $this->name);
  293. }
  294. /**
  295. * Get package name without vendor
  296. *
  297. * @return string
  298. */
  299. public function getPackageName()
  300. {
  301. return preg_replace('{^[^/]*/}', '', $this->name);
  302. }
  303. /**
  304. * Set description
  305. *
  306. * @param string $description
  307. */
  308. public function setDescription($description)
  309. {
  310. $this->description = $description;
  311. }
  312. /**
  313. * Get description
  314. *
  315. * @return string
  316. */
  317. public function getDescription()
  318. {
  319. return $this->description;
  320. }
  321. /**
  322. * Set language
  323. *
  324. * @param string $language
  325. */
  326. public function setLanguage($language)
  327. {
  328. $this->language = $language;
  329. }
  330. /**
  331. * Get language
  332. *
  333. * @return string
  334. */
  335. public function getLanguage()
  336. {
  337. return $this->language;
  338. }
  339. /**
  340. * Set readme
  341. *
  342. * @param string $readme
  343. */
  344. public function setReadme($readme)
  345. {
  346. $this->readme = $readme;
  347. }
  348. /**
  349. * Get readme
  350. *
  351. * @return string
  352. */
  353. public function getReadme()
  354. {
  355. return $this->readme;
  356. }
  357. /**
  358. * @param int $val
  359. */
  360. public function setGitHubStars($val)
  361. {
  362. $this->gitHubStars = $val;
  363. }
  364. /**
  365. * @return int
  366. */
  367. public function getGitHubStars()
  368. {
  369. return $this->gitHubStars;
  370. }
  371. /**
  372. * @param int $val
  373. */
  374. public function setGitHubWatches($val)
  375. {
  376. $this->gitHubWatches = $val;
  377. }
  378. /**
  379. * @return int
  380. */
  381. public function getGitHubWatches()
  382. {
  383. return $this->gitHubWatches;
  384. }
  385. /**
  386. * @param int $val
  387. */
  388. public function setGitHubForks($val)
  389. {
  390. $this->gitHubForks = $val;
  391. }
  392. /**
  393. * @return int
  394. */
  395. public function getGitHubForks()
  396. {
  397. return $this->gitHubForks;
  398. }
  399. /**
  400. * @param int $val
  401. */
  402. public function setGitHubOpenIssues($val)
  403. {
  404. $this->gitHubOpenIssues = $val;
  405. }
  406. /**
  407. * @return int
  408. */
  409. public function getGitHubOpenIssues()
  410. {
  411. return $this->gitHubOpenIssues;
  412. }
  413. /**
  414. * Set createdAt
  415. *
  416. * @param \DateTime $createdAt
  417. */
  418. public function setCreatedAt($createdAt)
  419. {
  420. $this->createdAt = $createdAt;
  421. }
  422. /**
  423. * Get createdAt
  424. *
  425. * @return \DateTime
  426. */
  427. public function getCreatedAt()
  428. {
  429. return $this->createdAt;
  430. }
  431. /**
  432. * Set repository
  433. *
  434. * @param string $repository
  435. */
  436. public function setRepository($repoUrl)
  437. {
  438. $this->vcsDriver = null;
  439. // prevent local filesystem URLs
  440. if (preg_match('{^(\.|[a-z]:|/)}i', $repoUrl)) {
  441. return;
  442. }
  443. $repoUrl = preg_replace('{^git@github.com:}i', 'https://github.com/', $repoUrl);
  444. $this->repository = $repoUrl;
  445. // avoid user@host URLs
  446. if (preg_match('{https?://.+@}', $repoUrl)) {
  447. return;
  448. }
  449. try {
  450. $io = new NullIO();
  451. $config = Factory::createConfig();
  452. $io->loadConfiguration($config);
  453. $repository = new VcsRepository(array('url' => $this->repository), $io, $config);
  454. $driver = $this->vcsDriver = $repository->getDriver();
  455. if (!$driver) {
  456. return;
  457. }
  458. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  459. if (!isset($information['name'])) {
  460. return;
  461. }
  462. if (null === $this->getName()) {
  463. $this->setName($information['name']);
  464. }
  465. if ($driver instanceof GitHubDriver) {
  466. $this->repository = $driver->getRepositoryUrl();
  467. }
  468. } catch (\Exception $e) {
  469. $this->vcsDriverError = '['.get_class($e).'] '.$e->getMessage();
  470. }
  471. }
  472. /**
  473. * Get repository
  474. *
  475. * @return string $repository
  476. */
  477. public function getRepository()
  478. {
  479. return $this->repository;
  480. }
  481. /**
  482. * Add versions
  483. *
  484. * @param Version $versions
  485. */
  486. public function addVersions(Version $versions)
  487. {
  488. $this->versions[] = $versions;
  489. }
  490. /**
  491. * Get versions
  492. *
  493. * @return \Doctrine\Common\Collections\Collection
  494. */
  495. public function getVersions()
  496. {
  497. return $this->versions;
  498. }
  499. public function getVersion($normalizedVersion)
  500. {
  501. if (null === $this->cachedVersions) {
  502. $this->cachedVersions = array();
  503. foreach ($this->getVersions() as $version) {
  504. $this->cachedVersions[strtolower($version->getNormalizedVersion())] = $version;
  505. }
  506. }
  507. if (isset($this->cachedVersions[strtolower($normalizedVersion)])) {
  508. return $this->cachedVersions[strtolower($normalizedVersion)];
  509. }
  510. }
  511. /**
  512. * Set updatedAt
  513. *
  514. * @param \DateTime $updatedAt
  515. */
  516. public function setUpdatedAt($updatedAt)
  517. {
  518. $this->updatedAt = $updatedAt;
  519. $this->setUpdateFailureNotified(false);
  520. }
  521. /**
  522. * Get updatedAt
  523. *
  524. * @return \DateTime
  525. */
  526. public function getUpdatedAt()
  527. {
  528. return $this->updatedAt;
  529. }
  530. /**
  531. * Set crawledAt
  532. *
  533. * @param \DateTime|null $crawledAt
  534. */
  535. public function setCrawledAt($crawledAt)
  536. {
  537. $this->crawledAt = $crawledAt;
  538. }
  539. /**
  540. * Get crawledAt
  541. *
  542. * @return \DateTime
  543. */
  544. public function getCrawledAt()
  545. {
  546. return $this->crawledAt;
  547. }
  548. /**
  549. * Set indexedAt
  550. *
  551. * @param \DateTime $indexedAt
  552. */
  553. public function setIndexedAt($indexedAt)
  554. {
  555. $this->indexedAt = $indexedAt;
  556. }
  557. /**
  558. * Get indexedAt
  559. *
  560. * @return \DateTime
  561. */
  562. public function getIndexedAt()
  563. {
  564. return $this->indexedAt;
  565. }
  566. /**
  567. * Set dumpedAt
  568. *
  569. * @param \DateTime $dumpedAt
  570. */
  571. public function setDumpedAt($dumpedAt)
  572. {
  573. $this->dumpedAt = $dumpedAt;
  574. }
  575. /**
  576. * Get dumpedAt
  577. *
  578. * @return \DateTime
  579. */
  580. public function getDumpedAt()
  581. {
  582. return $this->dumpedAt;
  583. }
  584. /**
  585. * Add maintainers
  586. *
  587. * @param User $maintainer
  588. */
  589. public function addMaintainer(User $maintainer)
  590. {
  591. $this->maintainers[] = $maintainer;
  592. }
  593. /**
  594. * Get maintainers
  595. *
  596. * @return \Doctrine\Common\Collections\Collection
  597. */
  598. public function getMaintainers()
  599. {
  600. return $this->maintainers;
  601. }
  602. /**
  603. * Set type
  604. *
  605. * @param string $type
  606. */
  607. public function setType($type)
  608. {
  609. $this->type = $type;
  610. }
  611. /**
  612. * Get type
  613. *
  614. * @return string
  615. */
  616. public function getType()
  617. {
  618. return $this->type;
  619. }
  620. /**
  621. * Set autoUpdated
  622. *
  623. * @param Boolean $autoUpdated
  624. */
  625. public function setAutoUpdated($autoUpdated)
  626. {
  627. $this->autoUpdated = $autoUpdated;
  628. }
  629. /**
  630. * Get autoUpdated
  631. *
  632. * @return Boolean
  633. */
  634. public function isAutoUpdated()
  635. {
  636. return $this->autoUpdated;
  637. }
  638. /**
  639. * Set updateFailureNotified
  640. *
  641. * @param Boolean $updateFailureNotified
  642. */
  643. public function setUpdateFailureNotified($updateFailureNotified)
  644. {
  645. $this->updateFailureNotified = $updateFailureNotified;
  646. }
  647. /**
  648. * Get updateFailureNotified
  649. *
  650. * @return Boolean
  651. */
  652. public function isUpdateFailureNotified()
  653. {
  654. return $this->updateFailureNotified;
  655. }
  656. /**
  657. * @return boolean
  658. */
  659. public function isAbandoned()
  660. {
  661. return $this->abandoned;
  662. }
  663. /**
  664. * @param boolean $abandoned
  665. */
  666. public function setAbandoned($abandoned)
  667. {
  668. $this->abandoned = $abandoned;
  669. }
  670. /**
  671. * @return string
  672. */
  673. public function getReplacementPackage()
  674. {
  675. return $this->replacementPackage;
  676. }
  677. /**
  678. * @param string $replacementPackage
  679. */
  680. public function setReplacementPackage($replacementPackage)
  681. {
  682. $this->replacementPackage = $replacementPackage;
  683. }
  684. public static function sortVersions($a, $b)
  685. {
  686. $aVersion = $a->getNormalizedVersion();
  687. $bVersion = $b->getNormalizedVersion();
  688. $aVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
  689. $bVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $bVersion);
  690. // equal versions are sorted by date
  691. if ($aVersion === $bVersion) {
  692. return $b->getReleasedAt() > $a->getReleasedAt() ? 1 : -1;
  693. }
  694. // the rest is sorted by version
  695. return version_compare($bVersion, $aVersion);
  696. }
  697. }