8 * Repository ID (number in repositories directory)
15 * Full path to the .git repository
22 * Full path to the work tree directory
29 * Revision of the repository that shall be shown
36 * Commit message of the last (or current) revision
44 * Load Repository data from GET-Request
48 * @throws Exception When something is wrong
50 public function loadFromRequest()
52 if (!isset($_GET['id'])) {
53 throw new Exception_Input('Paste ID missing');
55 if (!is_numeric($_GET['id'])) {
56 throw new Exception_Input('Paste ID not numeric');
58 if (isset($_GET['rev'])) {
59 $this->hash = $_GET['rev'];
62 $this->id = (int)$_GET['id'];
68 public function loadById($id)
70 if (!is_numeric($id)) {
71 throw new Exception_Input('Paste ID not numeric');
78 protected function loadDirs()
80 $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
81 if (!is_dir($gitDir)) {
82 throw new Exception_NotFound(
83 sprintf('Paste %d .git dir not found', $this->id)
86 $this->gitDir = $gitDir;
88 $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
89 if (!is_dir($workDir)) {
90 throw new Exception_NotFound(
91 sprintf('Paste %d work dir not found', $this->id)
94 $this->workDir = $workDir;
97 public function loadHash()
99 if ($this->hash !== null) {
103 $output = $this->getVc()->getCommand('log')
104 ->setOption('pretty', 'format:%H')
105 ->setOption('max-count', 1)
107 $output = trim($output);
108 if (strlen($output) !== 40) {
110 'Loading commit hash failed: ' . $output
113 $this->hash = $output;
117 * Populates $this->message
121 public function loadMessage()
123 $rev = (isset($this->hash)) ? $this->hash : 'HEAD';
124 $output = $this->getVc()->getCommand('log')
125 ->setOption('oneline')
129 $output = trim($output);
130 if (strpos($output, ' ') > 0) {
131 $output = substr($output, strpos($output, ' '), strlen($output));
132 $this->message = trim($output);
134 $this->message = "This commit message intentionally left blank.";
138 public function getVc()
140 return new \VersionControl_Git($this->gitDir);
144 * Loads the list of files in this repository
146 * @return File[] Array of file objects
148 public function getFiles()
150 $files = $this->getFilePaths();
152 foreach ($files as $name) {
153 $arFiles[] = new File($name, $this);
159 * Decodes unicode characters in git filenames
160 * They begin and end with double quote characters, and may contain
161 * backslash + 3 letter octal code numbers representing the character.
164 * > "t\303\244st.txt"
168 * On the shell, you can pipe them into "printf" and have them decoded.
170 * @param string Encoded git file name
172 * @return string Decoded file name
174 protected function decodeFileName($name)
176 $name = substr($name, 1, -1);
177 $name = str_replace('\"', '"', $name);
178 $name = preg_replace_callback(
181 return chr(octdec(substr($ar[0], 1)));
189 * Return array with all file paths in this repository
193 protected function getFilePaths()
195 if ($this->hash === null) {
200 $output = $this->getVc()->getCommand('ls-tree')
202 ->setOption('name-only')
205 $files = explode("\n", trim($output));
206 foreach ($files as &$file) {
207 if ($file{0} == '"') {
208 $file = $this->decodeFileName($file);
214 public function getFileByName($name, $bHasToExist = true)
216 $name = Tools::sanitizeFilename($name);
218 throw new Exception_Input('Empty file name given');
222 $files = $this->getFilePaths();
223 if (array_search($name, $files) === false) {
224 throw new Exception_Input('File does not exist');
227 return new File($name, $this);
230 public function hasFile($name)
233 $this->getFileByName($name);
234 } catch (Exception $e) {
241 * Permanently deletes the paste repository without any way to get
244 * @return boolean True if all went well, false if not
246 public function delete()
248 $db = new Database();
249 $db->getIndexer()->deleteRepo($this);
251 $bOk = Tools::recursiveDelete($this->workDir)
252 && Tools::recursiveDelete($this->gitDir);
254 $not = new Notificator();
260 public function getTitle()
262 $desc = $this->getDescription();
263 if (trim($desc) != '') {
267 return 'paste #' . $this->id;
270 public function getDescription()
272 if (!is_readable($this->gitDir . '/description')) {
275 return file_get_contents($this->gitDir . '/description');
278 public function setDescription($description)
280 file_put_contents($this->gitDir . '/description', $description);
284 * @return array Array with keys "email" and "name"
286 public function getOwner()
289 $name = $this->getVc()->getCommand('config')
290 ->addArgument('owner.name')->execute();
291 } catch (\VersionControl_Git_Exception $e) {
292 $name = $GLOBALS['phorkie']['auth']['anonymousName'];
295 $email = $this->getVc()->getCommand('config')
296 ->addArgument('owner.email')->execute();
297 } catch (\VersionControl_Git_Exception $e) {
298 $email = $GLOBALS['phorkie']['auth']['anonymousEmail'];
301 return array('name' => trim($name), 'email' => trim($email));
305 * Get a link to the repository
307 * @param string $type Link type. Supported are:
315 * @param string $option Additional link option, e.g. revision number
316 * @param boolean $full Return full URL or normal relative
320 public function getLink($type, $option = null, $full = false)
322 if ($type == 'edit') {
323 $link = $this->id . '/edit';
324 if ($option !== null) {
325 $link .= '/' . urlencode($option);
327 } else if ($type == 'display') {
329 } else if ($type == 'fork') {
330 $link = $this->id . '/fork';
331 } else if ($type == 'doap') {
332 $link = $this->id . '/doap';
333 } else if ($type == 'delete') {
334 $link = $this->id . '/delete';
335 } else if ($type == 'delete-confirm') {
336 $link = $this->id . '/delete/confirm';
337 } else if ($type == 'embed') {
338 $link = $this->id . '/embed';
339 } else if ($type == 'oembed-json') {
340 $link = 'oembed.php?format=json&url='
341 . urlencode($this->getLink('display', null, true));
342 } else if ($type == 'oembed-xml') {
343 $link = 'oembed.php?format=xml&url='
344 . urlencode($this->getLink('display', null, true));
345 } else if ($type == 'remotefork') {
346 return 'web+fork:' . $this->getLink('display', null, true);
347 } else if ($type == 'revision') {
348 $link = $this->id . '/rev/' . $option;
349 } else if ($type == 'linkback') {
350 $link = $this->id . '/linkback';
352 throw new Exception('Unknown link type');
356 $link = Tools::fullUrl($link);
361 public function getCloneURL($public = true)
363 $var = $public ? 'public' : 'private';
364 if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
365 return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '.git';
371 * Returns the history of the repository.
372 * We don't use VersionControl_Git's rev list fetcher since it does not
373 * give us separate email addresses and names, and it does not give us
374 * the amount of changed (added/deleted) lines.
376 * @return array Array of history objects
378 public function getHistory()
380 $output = $this->getVc()->getCommand('log')
381 ->setOption('pretty', 'format:commit %H%n%at%n%an%n%ae')
382 ->setOption('max-count', 10)
383 ->setOption('shortstat')
386 $arCommits = array();
387 $arOutput = explode("\n", $output);
388 $lines = count($arOutput);
390 while ($current < $lines) {
391 $commit = new Repository_Commit();
392 list($name,$commit->hash) = explode(' ', $arOutput[$current]);
393 if ($name !== 'commit') {
395 'Git log output format not as expected: ' . $arOutput[$current]
398 $commit->committerTime = $arOutput[$current + 1];
399 $commit->committerName = $arOutput[$current + 2];
400 $commit->committerEmail = $arOutput[$current + 3];
402 if (substr($arOutput[$current + 4], 0, 1) != ' ') {
403 //commit without changed lines
404 $arCommits[] = $commit;
409 $arLineParts = explode(' ', trim($arOutput[$current + 4]));
410 $commit->filesChanged = $arLineParts[0];
411 $commit->linesAdded = $arLineParts[3];
412 if (isset($arLineParts[5])) {
413 $commit->linesDeleted = $arLineParts[5];
418 $arCommits[] = $commit;
425 * @return Repository_ConnectionInfo
427 public function getConnectionInfo()
429 return new Repository_ConnectionInfo($this);