paste deletion is possible now
[phorkie.git] / src / Phorkie / Repository.php
index f23db7e17faea3f6d7569b27b1c7d9bd11da0fb1..bcaf3e1cd7f2686c5d7e45f144f4e8b6b160a19d 100644 (file)
@@ -42,6 +42,20 @@ class Repository
         $this->repoDir = $repoDir;
     }
 
         $this->repoDir = $repoDir;
     }
 
+    public function loadById($id)
+    {
+        if (!is_numeric($id)) {
+            throw new Exception_Input('Paste ID not numeric');
+        }
+        $this->id = (int)$id;
+
+        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
+        if (!is_dir($repoDir)) {
+            throw new Exception_NotFound('Paste not found');
+        }
+        $this->repoDir = $repoDir;
+    }
+
     public function getVc()
     {
         return new \VersionControl_Git($this->repoDir);
     public function getVc()
     {
         return new \VersionControl_Git($this->repoDir);
@@ -62,19 +76,43 @@ class Repository
         return $arFiles;
     }
 
         return $arFiles;
     }
 
-    public function getFileByName($name)
+    public function getFileByName($name, $bHasToExist = true)
     {
         $base = basename($name);
         if ($base != $name) {
             throw new Exception('No directories supported for now');
         }
     {
         $base = basename($name);
         if ($base != $name) {
             throw new Exception('No directories supported for now');
         }
+        if ($name == '') {
+            throw new Exception_Input('Empty file name given');
+        }
         $path = $this->repoDir . '/' . $base;
         $path = $this->repoDir . '/' . $base;
-        if (!is_readable($path)) {
+        if ($bHasToExist && !is_readable($path)) {
             throw new Exception_Input('File does not exist');
         }
         return new File($path, $this);
     }
 
             throw new Exception_Input('File does not exist');
         }
         return new File($path, $this);
     }
 
+    public function hasFile($name)
+    {
+        try {
+            $this->getFileByName($name);
+        } catch (Exception $e) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Permanently deletes the paste repository without any way to get
+     * it back.
+     *
+     * @return boolean True if all went well, false if not
+     */
+    public function delete()
+    {
+        return Tools::recursiveDelete($this->repoDir);
+    }
+
     public function getDescription()
     {
         if (!is_readable($this->repoDir . '/.git/description')) {
     public function getDescription()
     {
         if (!is_readable($this->repoDir . '/.git/description')) {
@@ -83,6 +121,11 @@ class Repository
         return file_get_contents($this->repoDir . '/.git/description');
     }
 
         return file_get_contents($this->repoDir . '/.git/description');
     }
 
+    public function setDescription($description)
+    {
+        file_put_contents($this->repoDir . '/.git/description', $description);
+    }
+
     /**
      * Get a link to the repository
      *
     /**
      * Get a link to the repository
      *
@@ -101,8 +144,12 @@ class Repository
             return '/' . $this->id;
         } else if ($type == 'fork') {
             return '/' . $this->id . '/fork';
             return '/' . $this->id;
         } else if ($type == 'fork') {
             return '/' . $this->id . '/fork';
+        } else if ($type == 'delete') {
+            return '/' . $this->id . '/delete';
+        } else if ($type == 'delete-confirm') {
+            return '/' . $this->id . '/delete/confirm';
         }
         }
-        throw new Exception('Unknown type');
+        throw new Exception('Unknown link type');
     }
 
 }
     }
 
 }