4 class Repository_LinkbackReceiver
5 implements \PEAR2\Services\Linkback\Server\Callback\IStorage
9 public function __construct($repo)
15 * Stores the linkback as remote fork in the paste repository.
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
24 * @throws SPb\Exception When storing the linkback fatally failed
26 public function storeLinkback(
27 $target, $source, $sourceBody, \HTTP_Request2_Response $res
32 $hp = new HtmlParser();
33 $ok = $hp->extractGitUrls($source, $sourceBody);
35 //failed to extract git URL from linkback source
36 //FIXME: send exception
41 $ci = $this->repo->getConnectionInfo();
42 $forks = $ci->getForks();
44 $arRemoteCloneUrls = $this->localizeGitUrls($hp->getGitUrls());
46 $remoteCloneUrl = $remoteTitle = null;
47 if (count($arRemoteCloneUrls)) {
48 reset($arRemoteCloneUrls);
49 list($remoteCloneUrl, $remoteTitle) = each($arRemoteCloneUrls);
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();
58 } else if ($source == $remote->getWebURL(true)) {
59 $remoteid = $remote->getName();
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)
72 if ($remoteTitle !== null) {
73 $vc->getCommand('config')
74 ->addArgument('remote.' . $remoteid . '.title')
75 ->addArgument($remoteTitle)
78 if ($remoteCloneUrl !== null) {
79 $vc->getCommand('config')
80 ->addArgument('remote.' . $remoteid . '.url')
81 ->addArgument($remoteCloneUrl)
87 * Check if the given full URL is the URL of a local repository
91 protected function isLocalWebUrl($url)
93 $base = Tools::fullUrl();
94 if (substr($url, 0, strlen($base)) != $base) {
99 $remainder = substr($url, strlen($base));
100 if (!is_numeric($remainder)) {
104 $repo = new Repository();
105 $repo->loadById($remainder);
106 } catch (\Exception $e) {
113 * Convert an array of git urls to local URLs if possible and serialize them
114 * into a simple array.
116 * @param array $arGitUrls Array of array of urls. Main key is the title of
119 * @return array Key is the git clone URL, value the title of the remote
121 protected function localizeGitUrls($arGitUrls)
123 $pub = $GLOBALS['phorkie']['cfg']['git']['public'];
124 $pri = $GLOBALS['phorkie']['cfg']['git']['private'];
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'
132 $id = substr($remoteCloneUrl, strlen($pub), -4);
133 $repo = new Repository();
135 $repo->loadById($id);
136 $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
137 } catch (Exception $e) {
139 } else if (substr($remoteCloneUrl, 0, strlen($pri)) == $pri
140 && substr($remoteCloneUrl, -4) == '.git'
142 $id = substr($remoteCloneUrl, strlen($pri), -4);
143 $repo = new Repository();
145 $repo->loadById($id);
146 $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
147 } catch (Exception $e) {
150 $arRemoteCloneUrls[$remoteCloneUrl] = $remoteTitle;
154 return $arRemoteCloneUrls;