update to latest services_libravatar release
[phorkie.git] / src / phorkie / Repository.php
1 <?php
2 namespace phorkie;
3
4
5 class Repository
6 {
7     /**
8      * Repository ID (number in repositories directory)
9      *
10      * @var integer
11      */
12     public $id;
13
14     /**
15      * Full path to the .git repository
16      *
17      * @var string
18      */
19     public $gitDir;
20
21     /**
22      * Full path to the work tree directory
23      *
24      * @var string
25      */
26     public $workDir;
27
28     /**
29      * Revision of the repository that shall be shown
30      *
31      * @var string
32      */
33     public $hash;
34
35
36
37     /**
38      * Load Repository data from GET-Request
39      *
40      * @return void
41      *
42      * @throws Exception When something is wrong
43      */
44     public function loadFromRequest()
45     {
46         if (!isset($_GET['id'])) {
47             throw new Exception_Input('Paste ID missing');
48         }
49         if (!is_numeric($_GET['id'])) {
50             throw new Exception_Input('Paste ID not numeric');
51         }
52         if (isset($_GET['rev'])) {
53             $this->hash = $_GET['rev'];
54         }
55
56         $this->id = (int)$_GET['id'];
57         $this->loadDirs();
58         $this->loadHash();
59     }
60
61     protected function loadDirs()
62     {
63         $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
64         if (!is_dir($gitDir)) {
65             throw new Exception_NotFound(
66                 sprintf('Paste %d .git dir not found', $this->id)
67             );
68         }
69         $this->gitDir = $gitDir;
70
71         $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
72         if (!is_dir($workDir)) {
73             throw new Exception_NotFound(
74                 sprintf('Paste %d work dir not found', $this->id)
75             );
76         }
77         $this->workDir = $workDir;
78     }
79
80     public function loadHash()
81     {
82         return;
83         if ($this->hash !== null) {
84             return;
85         }
86
87         $output = $this->getVc()->getCommand('log')
88             ->setOption('pretty', 'format:%H')
89             ->setOption('max-count', 1)
90             ->execute();
91         $output = trim($output);
92         if (strlen($output) !== 40) {
93             throw new Exception(
94                 'Loading commit hash failed: ' . $output
95             );
96         }
97         $this->hash = $output;
98     }
99
100     public function loadById($id)
101     {
102         if (!is_numeric($id)) {
103             throw new Exception_Input('Paste ID not numeric');
104         }
105         $this->id = (int)$id;
106         $this->loadDirs();
107         $this->loadHash();
108     }
109
110     public function getVc()
111     {
112         return new \VersionControl_Git($this->gitDir);
113     }
114
115     /**
116      * Loads the list of files in this repository
117      *
118      * @return File[] Array of files
119      */
120     public function getFiles()
121     {
122         $files = $this->getFilePaths();
123         $arFiles = array();
124         foreach ($files as $name) {
125             $arFiles[] = new File($name, $this);
126         }
127         return $arFiles;
128     }
129
130     protected function getFilePaths()
131     {
132         if ($this->hash === null) {
133             $hash = 'HEAD';
134         } else {
135             $hash = $this->hash;
136         }
137         $output = $this->getVc()->getCommand('ls-tree')
138             ->setOption('r')
139             ->setOption('name-only')
140             ->addArgument($hash)
141             ->execute();
142         return explode("\n", trim($output));
143     }
144
145     public function getFileByName($name, $bHasToExist = true)
146     {
147         $name = Tools::sanitizeFilename($name);
148         if ($name == '') {
149             throw new Exception_Input('Empty file name given');
150         }
151
152         if ($bHasToExist) {
153             $files = $this->getFilePaths();
154             if (array_search($name, $files) === false) {
155                 throw new Exception_Input('File does not exist');
156             }
157         }
158         return new File($name, $this);
159     }
160
161     public function hasFile($name)
162     {
163         try {
164             $this->getFileByName($name);
165         } catch (Exception $e) {
166             return false;
167         }
168         return true;
169     }
170
171     /**
172      * Permanently deletes the paste repository without any way to get
173      * it back.
174      *
175      * @return boolean True if all went well, false if not
176      */
177     public function delete()
178     {
179         return Tools::recursiveDelete($this->workDir)
180             && Tools::recursiveDelete($this->gitDir);
181     }
182
183     public function getDescription()
184     {
185         if (!is_readable($this->gitDir . '/description')) {
186             return null;
187         }
188         return file_get_contents($this->gitDir . '/description');
189     }
190
191     public function setDescription($description)
192     {
193         file_put_contents($this->gitDir . '/description', $description);
194     }
195
196     /**
197      * Get a link to the repository
198      *
199      * @param string $type Link type. Supported are:
200      *                     - "edit"
201      *                     - "delete"
202      *                     - "delete-confirm"
203      *                     - "display"
204      *                     - "fork"
205      *                     - "revision"
206      * @param string $option
207      *
208      * @return string
209      */
210     public function getLink($type, $option = null)
211     {
212         if ($type == 'edit') {
213             return '/' . $this->id . '/edit';
214         } else if ($type == 'display') {
215             return '/' . $this->id;
216         } else if ($type == 'fork') {
217             return '/' . $this->id . '/fork';
218         } else if ($type == 'delete') {
219             return '/' . $this->id . '/delete';
220         } else if ($type == 'delete-confirm') {
221             return '/' . $this->id . '/delete/confirm';
222         } else if ($type == 'revision') {
223             return '/' . $this->id . '/rev/' . $option;
224         }
225         throw new Exception('Unknown link type');
226     }
227
228     public function getCloneURL($public = true)
229     {
230         $var = $public ? 'public' : 'private';
231         if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
232             return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
233         }
234         return null;
235     }
236
237     /**
238      * Returns the history of the repository.
239      * We don't use VersionControl_Git's rev list fetcher since it does not
240      * give us separate email addresses and names, and it does not give us
241      * the amount of changed (added/deleted) lines.
242      *
243      * @return array Array of history objects
244      */
245     public function getHistory()
246     {
247         $output = $this->getVc()->getCommand('log')
248             ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
249             ->setOption('max-count', 10)
250             ->setOption('shortstat')
251             ->execute();
252
253         $arCommits = array();
254         $arOutput = explode("\n", $output);
255         $lines = count($arOutput);
256         $current = 0;
257         while ($current < $lines) {
258             $commit = new Repository_Commit();
259             list($name,$commit->hash) = explode(' ', $arOutput[$current]);
260             if ($name !== 'commit') {
261                 throw new Exception(
262                     'Git log output format not as expected: ' . $arOutput[$current]
263                 );
264             }
265             $commit->committerTime  = $arOutput[$current + 1];
266             $commit->committerName  = $arOutput[$current + 2];
267             $commit->committerEmail = $arOutput[$current + 3];
268
269             $arLineParts = explode(' ', trim($arOutput[$current + 4]));
270             $commit->filesChanged = $arLineParts[0];
271             $commit->linesAdded   = $arLineParts[3];
272             $commit->linesDeleted = $arLineParts[5];
273
274             $current += 6;
275
276             $arCommits[] = $commit;
277         }
278
279         return $arCommits;
280     }
281 }
282
283 ?>