c9b3d0369422b9193e09073f2488f8c56bed3878
[phorkie.git] / src / phorkie / Repository / LinkbackReceiver.php
1 <?php
2 namespace phorkie;
3
4 class Repository_LinkbackReceiver
5     implements \PEAR2\Services\Linkback\Server\Callback\IStorage
6 {
7     protected $repo;
8
9     public function __construct($repo)
10     {
11         $this->repo = $repo;
12     }
13
14     /**
15      * Stores the linkback as remote fork in the paste repository.
16      *
17      * @param string $target     Target URI that should be linked in $source
18      * @param string $source     Linkback source URI that should link to target
19      * @param string $sourceBody Content of $source URI
20      * @param object $res        HTTP response from fetching $source
21      *
22      * @return void
23      *
24      * @throws SPb\Exception When storing the linkback fatally failed
25      */
26     public function storeLinkback(
27         $target, $source, $sourceBody, \HTTP_Request2_Response $res
28     ) {
29         //FIXME: deleted
30         //FIXME: cleanuptask
31
32         $hp = new HtmlParser();
33         $ok = $hp->extractGitUrls($source, $sourceBody);
34         if ($ok === false) {
35             //failed to extract git URL from linkback source
36             //FIXME: send exception
37             //$hp->error
38             return;
39         }
40
41         $ci = $this->repo->getConnectionInfo();
42         $forks = $ci->getForks();
43
44         $arRemoteCloneUrls = $this->localizeGitUrls($hp->getGitUrls());
45
46         $remoteCloneUrl = $remoteTitle = null;
47         if (count($arRemoteCloneUrls)) {
48             reset($arRemoteCloneUrls);
49             list($remoteCloneUrl, $remoteTitle) = each($arRemoteCloneUrls);
50         }
51         $remoteid = 'fork-' . uniqid();
52         //check if we already know this remote
53         foreach ($forks as $remote) {
54             if (isset($arRemoteCloneUrls[$remote->getCloneUrl()])) {
55                 $remoteTitle = $arRemoteCloneUrls[$remote->getCloneUrl()];
56                 $remoteid = $remote->getName();
57                 break;
58             } else if ($source == $remote->getWebURL(true)) {
59                 $remoteid = $remote->getName();
60                 break;
61             }
62         }
63
64         $vc = $this->repo->getVc();
65         if (!$this->isLocalWebUrl($source)) {
66             //only add remote homepage; we can calculate local ones ourselves
67             $vc->getCommand('config')
68                 ->addArgument('remote.' . $remoteid . '.homepage')
69                 ->addArgument($source)
70                 ->execute();
71         }
72         if ($remoteTitle !== null) {
73             $vc->getCommand('config')
74                 ->addArgument('remote.' . $remoteid . '.title')
75                 ->addArgument($remoteTitle)
76                 ->execute();
77         }
78         if ($remoteCloneUrl !== null) {
79             $vc->getCommand('config')
80                 ->addArgument('remote.' . $remoteid . '.url')
81                 ->addArgument($remoteCloneUrl)
82                 ->execute();
83         }
84     }
85
86     /**
87      * Check if the given full URL is the URL of a local repository
88      *
89      * @return Repository
90      */
91     protected function isLocalWebUrl($url)
92     {
93         $base = Tools::fullUrl();
94         if (substr($url, 0, strlen($base)) != $base) {
95             //base does not match
96             return false;
97         }
98
99         $remainder = substr($url, strlen($base));
100         if (!is_numeric($remainder)) {
101             return false;
102         }
103         try {
104             $repo = new Repository();
105             $repo->loadById($remainder);
106         } catch (\Exception $e) {
107             return false;
108         }
109         return true;
110     }
111
112     /**
113      * Convert an array of git urls to local URLs if possible and serialize them
114      * into a simple array.
115      *
116      * @param array $arGitUrls Array of array of urls. Main key is the title of
117      *                         the URL array.
118      *
119      * @return array Key is the git clone URL, value the title of the remote
120      */
121     protected function localizeGitUrls($arGitUrls)
122     {
123         $pub = $GLOBALS['phorkie']['cfg']['git']['public'];
124         $pri = $GLOBALS['phorkie']['cfg']['git']['private'];
125
126         $arRemoteCloneUrls = array();
127         foreach ($arGitUrls as $remoteTitle => $arUrls) {
128             foreach ($arUrls as $remoteCloneUrl) {
129                 if (substr($remoteCloneUrl, 0, strlen($pub)) == $pub
130                     && substr($remoteCloneUrl, -4) == '.git'
131                 ) {
132                     $id = substr($remoteCloneUrl, strlen($pub), -4);
133                     $repo = new Repository();
134                     try {
135                         $repo->loadById($id);
136                         $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
137                     } catch (Exception $e) {
138                     }
139                 } else if (substr($remoteCloneUrl, 0, strlen($pri)) == $pri
140                     && substr($remoteCloneUrl, -4) == '.git'
141                 ) {
142                     $id = substr($remoteCloneUrl, strlen($pri), -4);
143                     $repo = new Repository();
144                     try {
145                         $repo->loadById($id);
146                         $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
147                     } catch (Exception $e) {
148                     }
149                 } else {
150                     $arRemoteCloneUrls[$remoteCloneUrl] = $remoteTitle;
151                 }
152             }
153         }
154         return $arRemoteCloneUrls;
155     }
156 }
157 ?>