aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Repository/LinkbackReceiver.php
blob: c808f4587352c9a8c8d7e1f444d14a9842c687af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace phorkie;

class Repository_LinkbackReceiver
    implements \PEAR2\Services\Linkback\Server\Callback\IStorage
{
    protected $repo;

    public function __construct($repo)
    {
        $this->repo = $repo;
    }

    /**
     * Stores the linkback as remote fork in the paste repository.
     *
     * @param string $target     Target URI that should be linked in $source
     * @param string $source     Linkback source URI that should link to target
     * @param string $sourceBody Content of $source URI
     * @param object $res        HTTP response from fetching $source
     *
     * @return void
     *
     * @throws SPb\Exception When storing the linkback fatally failed
     */
    public function storeLinkback(
        $target, $source, $sourceBody, \HTTP_Request2_Response $res
    ) {
        //FIXME: deleted
        //FIXME: cleanuptask

        $hp = new HtmlParser();
        $ok = $hp->extractGitUrls($source, $sourceBody);
        if ($ok === false) {
            //failed to extract git URL from linkback source
            //FIXME: send exception
            //$hp->error
            return;
        }

        $ci = $this->repo->getConnectionInfo();
        $forks = $ci->getForks();

        $arRemoteCloneUrls = $this->localizeGitUrls($hp->getGitUrls());

        $remoteCloneUrl = $remoteTitle = null;
        if (count($arRemoteCloneUrls)) {
            reset($arRemoteCloneUrls);
            list($remoteCloneUrl, $remoteTitle) = each($arRemoteCloneUrls);
        }
        $remoteid = 'fork-' . uniqid();
        //check if we already know this remote
        foreach ($forks as $remote) {
            if (isset($arRemoteCloneUrls[$remote->getCloneUrl()])) {
                $remoteTitle = $arRemoteCloneUrls[$remote->getCloneUrl()];
                $remoteid = $remote->getName();
                break;
            } else if ($source == $remote->getWebURL(true)) {
                $remoteid = $remote->getName();
                break;
            }
        }

        $vc = $this->repo->getVc();
        if (!$this->isLocalWebUrl($source)) {
            //only add remote homepage; we can calculate local ones ourselves
            $vc->getCommand('config')
                ->addArgument('remote.' . $remoteid . '.homepage')
                ->addArgument($source)
                ->execute();
        }
        if ($remoteTitle !== null) {
            $vc->getCommand('config')
                ->addArgument('remote.' . $remoteid . '.title')
                ->addArgument($remoteTitle)
                ->execute();
        }
        if ($remoteCloneUrl !== null) {
            $vc->getCommand('config')
                ->addArgument('remote.' . $remoteid . '.url')
                ->addArgument($remoteCloneUrl)
                ->execute();
        }
    }

    /**
     * Check if the given full URL is the URL of a local repository
     *
     * @return Repository
     */
    protected function isLocalWebUrl($url)
    {
        $base = Tools::fullUrl();
        if (substr($url, 0, strlen($base)) != $base) {
            //base does not match
            return false;
        }

        $remainder = substr($url, strlen($base));
        if (!is_numeric($remainder)) {
            return false;
        }
        try {
            $repo = new Repository();
            $repo->loadById($remainder);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }

    /**
     * Convert an array of git urls to local URLs if possible and serialize them
     * into a simple array.
     *
     * @param array $arGitUrls Array of array of urls. Main key is the title of
     *                         the URL array.
     *
     * @return array Key is the git clone URL, value the title of the remote
     */
    protected function localizeGitUrls($arGitUrls)
    {
        $pub = $pri = null;
        if (isset($GLOBALS['phorkie']['cfg']['git']['public'])) {
            $pub = $GLOBALS['phorkie']['cfg']['git']['public'];
        }
        if (isset($GLOBALS['phorkie']['cfg']['git']['private'])) {
            $pri = $GLOBALS['phorkie']['cfg']['git']['private'];
        }

        $arRemoteCloneUrls = array();
        foreach ($arGitUrls as $remoteTitle => $arUrls) {
            foreach ($arUrls as $remoteCloneUrl) {
                if ($pub !== null
                    && substr($remoteCloneUrl, 0, strlen($pub)) == $pub
                    && substr($remoteCloneUrl, -4) == '.git'
                ) {
                    $id = substr($remoteCloneUrl, strlen($pub), -4);
                    $repo = new Repository();
                    try {
                        $repo->loadById($id);
                        $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
                    } catch (Exception $e) {
                    }
                } else if ($pri !== null
                    && substr($remoteCloneUrl, 0, strlen($pri)) == $pri
                    && substr($remoteCloneUrl, -4) == '.git'
                ) {
                    $id = substr($remoteCloneUrl, strlen($pri), -4);
                    $repo = new Repository();
                    try {
                        $repo->loadById($id);
                        $arRemoteCloneUrls[$repo->gitDir] = $remoteTitle;
                    } catch (Exception $e) {
                    }
                } else {
                    $arRemoteCloneUrls[$remoteCloneUrl] = $remoteTitle;
                }
            }
        }
        return $arRemoteCloneUrls;
    }
}
?>