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)
124 if (isset($GLOBALS['phorkie']['cfg']['git']['public'])) {
125 $pub = $GLOBALS['phorkie']['cfg']['git']['public'];
127 if (isset($GLOBALS['phorkie']['cfg']['git']['private'])) {
128 $pri = $GLOBALS['phorkie']['cfg']['git']['private'];
131 $arRemoteCloneUrls = array();
132 foreach ($arGitUrls as $remoteTitle => $arUrls) {
133 foreach ($arUrls as $remoteCloneUrl) {
135 && substr($remoteCloneUrl, 0, strlen($pub)) == $pub
136 && substr($remoteCloneUrl, -4) == '.git'
138 $id = substr($remoteCloneUrl, strlen($pub), -4);
139 $repo = new Repository();
141 $repo->loadById($id);
142 $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
143 } catch (Exception $e) {
145 } else if ($pri !== null
146 && substr($remoteCloneUrl, 0, strlen($pri)) == $pri
147 && substr($remoteCloneUrl, -4) == '.git'
149 $id = substr($remoteCloneUrl, strlen($pri), -4);
150 $repo = new Repository();
152 $repo->loadById($id);
153 $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
154 } catch (Exception $e) {
157 $arRemoteCloneUrls[$remoteCloneUrl] = $remoteTitle;
161 return $arRemoteCloneUrls;