1717use App \Entity \DevRequireLink ;
1818use App \Entity \Package ;
1919use App \Entity \PackageFreezeReason ;
20+ use App \Entity \PackageReadme ;
2021use App \Entity \ProvideLink ;
2122use App \Entity \ReplaceLink ;
2223use App \Entity \RequireLink ;
@@ -623,7 +624,9 @@ private function updateReadme(IOInterface $io, Package $package, VcsDriverInterf
623624 case '.txt ' :
624625 $ source = $ driver ->getFileContent ($ readmeFile , $ driver ->getRootIdentifier ());
625626 if (!empty ($ source )) {
626- $ package ->setReadme ('<pre> ' .htmlspecialchars ($ source ).'</pre> ' );
627+ $ this ->updatePackageReadme ($ package , '<pre> ' .htmlspecialchars ($ source ).'</pre> ' );
628+ } else {
629+ $ this ->updatePackageReadme ($ package , null );
627630 }
628631 break ;
629632
@@ -635,9 +638,9 @@ private function updateReadme(IOInterface $io, Package $package, VcsDriverInterf
635638
636639 if (!empty ($ readme )) {
637640 if (Preg::isMatch ('{^(?:git://|git@|https?://)(gitlab.com|bitbucket.org)[:/]([^/]+)/(.+?)(?:\.git|/)?$}i ' , $ package ->getRepository (), $ match )) {
638- $ package -> setReadme ( $ this ->prepareReadme ($ readme , $ match [1 ], $ match [2 ], $ match [3 ]));
641+ $ this -> updatePackageReadme ( $ package , $ this ->prepareReadme ($ readme , $ match [1 ], $ match [2 ], $ match [3 ]));
639642 } else {
640- $ package -> setReadme ( $ this ->prepareReadme ($ readme ));
643+ $ this -> updatePackageReadme ( $ package , $ this ->prepareReadme ($ readme ));
641644 }
642645 }
643646 }
@@ -653,6 +656,26 @@ private function updateReadme(IOInterface $io, Package $package, VcsDriverInterf
653656 }
654657 }
655658
659+ private function updatePackageReadme (Package $ package , ?string $ contents ): void
660+ {
661+ $ readme = $ this ->getEM ()->find (PackageReadme::class, $ package ->getId ());
662+
663+ if ($ contents === '' || $ contents === null ) {
664+ if ($ readme ) {
665+ $ this ->getEM ()->remove ($ readme );
666+ }
667+ return ;
668+ }
669+
670+ if (!$ readme ) {
671+ $ readme = new PackageReadme ($ package , $ contents );
672+ } else {
673+ $ readme ->contents = $ contents ;
674+ }
675+
676+ $ this ->getEM ()->persist ($ readme );
677+ }
678+
656679 private function updateGitHubInfo (HttpDownloader $ httpDownloader , Package $ package , string $ owner , string $ repo , VcsDriverInterface $ driver ): void
657680 {
658681 if (!$ driver instanceof GitHubDriver) {
@@ -663,22 +686,6 @@ private function updateGitHubInfo(HttpDownloader $httpDownloader, Package $packa
663686
664687 $ repoData = $ driver ->getRepoData ();
665688
666- try {
667- $ opts = ['http ' => ['header ' => ['Accept: application/vnd.github.html+json ' , 'X-GitHub-Api-Version: 2022-11-28 ' ]]];
668- $ readme = $ httpDownloader ->get ($ baseApiUrl .'/readme ' , $ opts )->getBody ();
669- } catch (\Exception $ e ) {
670- if (!$ e instanceof \Composer \Downloader \TransportException || $ e ->getCode () !== 404 ) {
671- return ;
672- }
673- // 404s just mean no readme present so we proceed with the rest
674- }
675-
676- if (!empty ($ readme )) {
677- // The content of all readmes, regardless of file type,
678- // is returned as HTML by GitHub API
679- $ package ->setReadme ($ this ->prepareReadme ($ readme , 'github.com ' , $ owner , $ repo ));
680- }
681-
682689 if (!empty ($ repoData ['language ' ]) && \is_string ($ repoData ['language ' ])) {
683690 $ package ->setLanguage ($ repoData ['language ' ]);
684691 }
@@ -694,6 +701,21 @@ private function updateGitHubInfo(HttpDownloader $httpDownloader, Package $packa
694701 if (isset ($ repoData ['open_issues_count ' ]) && is_numeric ($ repoData ['open_issues_count ' ])) {
695702 $ package ->setGitHubOpenIssues ((int ) $ repoData ['open_issues_count ' ]);
696703 }
704+
705+ try {
706+ $ opts = ['http ' => ['header ' => ['Accept: application/vnd.github.html+json ' , 'X-GitHub-Api-Version: 2022-11-28 ' ]]];
707+ $ readme = $ httpDownloader ->get ($ baseApiUrl .'/readme ' , $ opts )->getBody ();
708+ } catch (\Exception $ e ) {
709+ if (!$ e instanceof \Composer \Downloader \TransportException || $ e ->getCode () !== 404 ) {
710+ return ;
711+ }
712+ // 404s just mean no readme present so we proceed with the rest
713+ $ readme = null ;
714+ }
715+
716+ // The content of all readmes, regardless of file type,
717+ // is returned as HTML by GitHub API
718+ $ this ->updatePackageReadme ($ package , $ this ->prepareReadme ($ readme , 'github.com ' , $ owner , $ repo ));
697719 }
698720
699721 private function updateGitLabInfo (HttpDownloader $ httpDownloader , IOInterface $ io , Package $ package , string $ owner , string $ repo , VcsDriverInterface $ driver ): void
@@ -727,8 +749,12 @@ private function updateGitLabInfo(HttpDownloader $httpDownloader, IOInterface $i
727749 /**
728750 * Prepare the readme by stripping elements and attributes that are not supported .
729751 */
730- private function prepareReadme (string $ readme , ?string $ host = null , ?string $ owner = null , ?string $ repo = null ): string
752+ private function prepareReadme (? string $ readme , ?string $ host = null , ?string $ owner = null , ?string $ repo = null ): ? string
731753 {
754+ if ($ readme === null ) {
755+ return null ;
756+ }
757+
732758 // detect base path for github readme if file is located in a subfolder like docs/README.md
733759 $ basePath = '' ;
734760 if ($ host === 'github.com ' && Preg::isMatchStrictGroups ('{^<div id="readme" [^>]+?data-path="([^"]+)"} ' , $ readme , $ match ) && str_contains ($ match [1 ], '/ ' )) {
0 commit comments