do not overwrite variable
[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      * Load Repository data from GET-Request
30      *
31      * @return void
32      *
33      * @throws Exception When something is wrong
34      */
35     public function loadFromRequest()
36     {
37         if (!isset($_GET['id'])) {
38             throw new Exception_Input('Paste ID missing');
39         }
40         if (!is_numeric($_GET['id'])) {
41             throw new Exception_Input('Paste ID not numeric');
42         }
43         $this->id = (int)$_GET['id'];
44         $this->loadDirs();
45     }
46
47     protected function loadDirs()
48     {
49         $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
50         if (!is_dir($gitDir)) {
51             throw new Exception_NotFound(
52                 sprintf('Paste %d .git dir not found', $this->id)
53             );
54         }
55         $this->gitDir = $gitDir;
56
57         $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
58         if (!is_dir($workDir)) {
59             throw new Exception_NotFound(
60                 sprintf('Paste %d work dir not found', $this->id)
61             );
62         }
63         $this->workDir = $workDir;
64     }
65
66     public function loadById($id)
67     {
68         if (!is_numeric($id)) {
69             throw new Exception_Input('Paste ID not numeric');
70         }
71         $this->id = (int)$id;
72         $this->loadDirs();
73     }
74
75     public function getVc()
76     {
77         return new \VersionControl_Git($this->gitDir);
78     }
79
80     /**
81      * Loads the list of files in this repository
82      *
83      * @return File[] Array of files
84      */
85     public function getFiles()
86     {
87         $files = glob($this->workDir . '/*');
88         $arFiles = array();
89         foreach ($files as $path) {
90             $arFiles[] = new File($path, $this);
91         }
92         return $arFiles;
93     }
94
95     public function getFileByName($name, $bHasToExist = true)
96     {
97         $base = basename($name);
98         if ($base != $name) {
99             throw new Exception('No directories supported for now');
100         }
101         if ($name == '') {
102             throw new Exception_Input('Empty file name given');
103         }
104         $path = $this->workDir . '/' . $base;
105         if ($bHasToExist && !is_readable($path)) {
106             throw new Exception_Input('File does not exist');
107         }
108         return new File($path, $this);
109     }
110
111     public function hasFile($name)
112     {
113         try {
114             $this->getFileByName($name);
115         } catch (Exception $e) {
116             return false;
117         }
118         return true;
119     }
120
121     /**
122      * Permanently deletes the paste repository without any way to get
123      * it back.
124      *
125      * @return boolean True if all went well, false if not
126      */
127     public function delete()
128     {
129         return Tools::recursiveDelete($this->workDir)
130             && Tools::recursiveDelete($this->gitDir);
131     }
132
133     public function getDescription()
134     {
135         if (!is_readable($this->gitDir . '/description')) {
136             return null;
137         }
138         return file_get_contents($this->gitDir . '/description');
139     }
140
141     public function setDescription($description)
142     {
143         file_put_contents($this->gitDir . '/description', $description);
144     }
145
146     /**
147      * Get a link to the repository
148      *
149      * @param string $type Link type. Supported are:
150      *                     - "edit"
151      *                     - "display"
152      *                     - "fork"
153      *
154      * @return string
155      */
156     public function getLink($type)
157     {
158         if ($type == 'edit') {
159             return '/' . $this->id . '/edit';
160         } else if ($type == 'display') {
161             return '/' . $this->id;
162         } else if ($type == 'fork') {
163             return '/' . $this->id . '/fork';
164         } else if ($type == 'delete') {
165             return '/' . $this->id . '/delete';
166         } else if ($type == 'delete-confirm') {
167             return '/' . $this->id . '/delete/confirm';
168         }
169         throw new Exception('Unknown link type');
170     }
171
172     public function getCloneURL($public = true)
173     {
174         $var = $public ? 'public' : 'private';
175         if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
176             return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '/.git';
177         }
178         return null;
179     }
180 }
181
182 ?>