bce76434e62aa7190b112b10320c931cfae603dc
[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     public function storeLinkback(
15         $target, $source, $sourceBody, \HTTP_Request2_Response $res
16     ) {
17         //FIXME: deleted
18         //FIXME: updated
19         //FIXME: cleanuptask
20
21         $hp = new HtmlParser();
22         $ok = $hp->extractGitUrls($source, $sourceBody);
23         if ($ok === false) {
24             //failed to extract git URL from linkback source
25             //FIXME: send exception
26             //$hp->error
27             return;
28         }
29
30         $ci = $this->repo->getConnectionInfo();
31         $forks = $ci->getForks();
32
33         $remoteCloneUrl = $remoteTitle = null;
34         $arRemoteCloneUrls = array();
35         $arGitUrls = $hp->getGitUrls();
36         foreach ($arGitUrls as $remoteTitle => $arUrls) {
37             foreach ($arUrls as $remoteCloneUrl) {
38                 $arRemoteCloneUrls[$remoteCloneUrl] = $remoteTitle;
39             }
40         }
41
42         $remoteid = 'fork-' . uniqid();
43         //check if we already know this remote
44         foreach ($forks as $remote) {
45             if (isset($arRemoteCloneUrls[$remote->getCloneUrl()])
46                 || $source == $remote->getWebURL(true)
47             ) {
48                 $remoteid = $remote->getName();
49                 break;
50             }
51         }
52
53         if ($this->isLocalWebUrl($source)) {
54             //convert both web and clone url to local urls
55         }
56
57         $vc = $this->repo->getVc();
58         $vc->getCommand('config')
59             ->addArgument('remote.' . $remoteid . '.homepage')
60             ->addArgument($source)
61             ->execute();
62         if ($remoteTitle !== null) {
63             $vc->getCommand('config')
64                 ->addArgument('remote.' . $remoteid . '.title')
65                 ->addArgument($remoteTitle)
66                 ->execute();
67         }
68         if ($remoteCloneUrl !== null) {
69             $vc->getCommand('config')
70                 ->addArgument('remote.' . $remoteid . '.url')
71                 ->addArgument($remoteCloneUrl)
72                 ->execute();
73         }
74     }
75
76     protected function isLocalWebUrl($url)
77     {
78         $base = Tools::fullUrl();
79         if (substr($url, 0, strlen($base)) != $base) {
80             //base does not match
81             return false;
82         }
83
84         $remainder = substr($url, strlen($base));
85         //FIXME: check if it exists
86     }
87 }
88 ?>