listing all pastes works
authorChristian Weiske <cweiske@cweiske.de>
Tue, 27 Mar 2012 05:50:58 +0000 (07:50 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 27 Mar 2012 05:50:58 +0000 (07:50 +0200)
README.rst
data/templates/list.htm [new file with mode: 0644]
src/Phorkie/Repositories.php
src/Phorkie/Repository.php
www/.htaccess
www/list.php [new file with mode: 0644]

index b1c25228dd50b4df9428fd24cc31b5609531fecf..1a771822fb6ff1326d9d8576d193a277b51efdbf 100644 (file)
@@ -48,7 +48,6 @@ Install geshi
 TODO
 ====
 - edit
 TODO
 ====
 - edit
-- list all
 - search
 - OpenID-Login to get username+email as authorship information
 - sidebar: history
 - search
 - OpenID-Login to get username+email as authorship information
 - sidebar: history
diff --git a/data/templates/list.htm b/data/templates/list.htm
new file mode 100644 (file)
index 0000000..5142a7b
--- /dev/null
@@ -0,0 +1,25 @@
+{% extends "base.htm" %}
+{% block title %}List of all pastes{% endblock %}
+
+{% block content %}
+<ul class="list">
+{% for repo in repos %}
+ <li>
+  <h2><a href="{{repo.getLink('display')}}">{{repo.id}}</a></h2>
+  <p>{{repo.getDescription}}</p>
+ </li>
+{% endfor %}
+</ul>
+<ul class="pager">
+ {% if links.prev %}
+ <li class="prev">
+  <a href="{{links.prev}}">prev</a>
+ </li>
+ {% endif %}
+ {% if links.next %}
+ <li class="next">
+  <a href="{{links.next}}">next</a>
+ </li>
+ {% endif %}
+</ul>
+{% endblock %}
index 3afe8c5b01c510e138ca2a0309f1fede06a8a3ab..eeaec343736acb0e5497e36ddeefd0097637880e 100644 (file)
@@ -27,6 +27,29 @@ class Repositories
         return $r;
     }
 
         return $r;
     }
 
+    /**
+     * Get a list of repository objects
+     *
+     * @param integer $page    Page number, beginning with 0
+     * @param integer $perPage Number of repositories per page
+     *
+     * @return array Array of Repositories
+     */
+    public function getList($page = 0, $perPage = 10)
+    {
+        chdir($this->reposDir);
+        $dirs = glob('*', GLOB_ONLYDIR);
+        sort($dirs, SORT_NUMERIC);
+
+        $some = array_slice($dirs, $page * $perPage, $perPage);
+        $repos = array();
+        foreach ($some as $oneDir) {
+            $r = new Repository();
+            $r->loadById($oneDir);
+            $repos[] = $r;
+        }
+        return $repos;
+    }
 }
 
 ?>
 }
 
 ?>
index f23db7e17faea3f6d7569b27b1c7d9bd11da0fb1..aeccc72f826107a2465a4a9d4023490bc6b83175 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);
index bf82bc236851023fee38d3fe3e5a56bbc9a26e36..d053ca7cb6954c3f64af1a2a8c6535531b7525ca 100644 (file)
@@ -5,3 +5,5 @@ RewriteBase /
 RewriteRule ^([0-9]+)$ /display.php?id=$1
 RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2
 RewriteRule ^([0-9]+)/fork$ /fork.php?id=$1
 RewriteRule ^([0-9]+)$ /display.php?id=$1
 RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2
 RewriteRule ^([0-9]+)/fork$ /fork.php?id=$1
+RewriteRule ^list$ /list.php
+RewriteRule ^list/([0-9])+$ /list.php?page=$1
diff --git a/www/list.php b/www/list.php
new file mode 100644 (file)
index 0000000..a212322
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Fork a repository
+ */
+namespace Phorkie;
+require_once 'www-header.php';
+$rs = new Repositories();
+
+$page = 0;
+if (isset($_GET['page'])) {
+    if (!is_numeric($_GET['page'])) {
+        throw new Exception_Input('List page is not numeric');
+    }
+    $page = (int)$_GET['page'];
+}
+
+$perPage = 10;
+$repos = $rs->getList($page, $perPage);
+
+$links = array('prev' => null, 'next' => null);
+if ($page > 0) {
+    $links['prev'] = '/list/' . ($page - 1);
+    if ($page - 1 == 0) {
+        $links['prev'] = '/list';
+    }
+}
+if (count($repos) && count($repos) == $perPage) {
+    $links['next'] = '/list/' . ($page + 1);
+}
+
+render(
+    'list',
+    array(
+        'repos' => $repos,
+        'links' => $links,
+    )
+);
+?>