Initial implementation of comments with doctrine.
authorEdward Z. Yang <edwardzyang@thewritingpot.com>
Mon, 27 Apr 2009 21:12:08 +0000 (27 17:12 -0400)
committerEdward Z. Yang <edwardzyang@thewritingpot.com>
Mon, 27 Apr 2009 21:16:02 +0000 (27 17:16 -0400)
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
.gitignore
XHTMLCompiler.php
common.php
config.default.php
functions.php
generate.php [new file with mode: 0644]
models/Comment.php [new file with mode: 0644]
models/generated/BaseComment.php [new file with mode: 0644]
schema.yml [new file with mode: 0644]

index eb6fad4..63e91df 100644 (file)
@@ -17,3 +17,7 @@
 # Cache/Testing
 /cache/svn/log/*.ser
 /tests/tmp/*
+
+# Doctrine
+data.db
+test.php
index 6631cfe..8717ee9 100644 (file)
@@ -58,7 +58,7 @@ class XHTMLCompiler
     protected $configKeys = array('allowed_dirs', 'directory_index',
         'indexed_dirs', 'web_path', 'web_domain', 'viewvc_url',
         'svn_headurl_munge', 'debug', 'error_xsl', 'admin_email', 'from_email',
-        'error_log', 'error_mute', 'configdoc', 'smtp_transport');
+        'error_log', 'error_mute', 'configdoc', 'smtp_transport', 'dsn');
     protected $config = array();
     protected $filterManager;
     
index 8f4358a..c361231 100644 (file)
@@ -33,11 +33,8 @@ require_once 'XHTMLCompiler/TextFilter.php';
 require_once 'XHTMLCompiler/DOMFilter.php';
 require_once 'XHTMLCompiler/DOMFilter/NewsBase.php';
 
-// load external libraries that we *need*
+// needed for error reporting
 require_once 'external/swiftmailer/lib/swift_required.php';
-require_once 'external/phpgit/library/Git.php';
-require_once 'external/htmlpurifier/library/HTMLPurifier.auto.php';
-require_once 'external/htmlpurifier/extras/HTMLPurifierExtras.auto.php';
 
 set_exception_handler('xhtmlcompiler_exception_handler');
 $xc = XHTMLCompiler::getInstance(); // invoke the super-object
@@ -47,3 +44,17 @@ if($xc->getConf('debug')) {
     restore_error_handler();
     ini_set('display_errors', true);
 }
+
+require_once 'external/phpgit/library/Git.php';
+require_once 'external/htmlpurifier/library/HTMLPurifier.auto.php';
+require_once 'external/htmlpurifier/extras/HTMLPurifierExtras.auto.php';
+
+// load Doctrine
+require_once 'external/doctrine/lib/Doctrine.php';
+spl_autoload_register(array('Doctrine', 'autoload'));
+
+Doctrine::loadModels('xhtml-compiler/models/generated');
+Doctrine::loadModels('xhtml-compiler/models');
+
+Doctrine_Manager::connection($xc->getConf('dsn'));
+
index 05f250d..4752674 100644 (file)
@@ -108,3 +108,9 @@ $configdoc = 'http://htmlpurifier.org/live/configdoc/plain.html#%s';
 // default should "just work".
 $smtp_transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t');
 
+// ** Data source name
+// See http://www.doctrine-project.org/documentation/manual/1_1/en/introduction-to-connections
+// for information on the syntax. By default, we'll dump everything into
+// an sqlite database, which is not the best in terms of scalability.
+$dsn = 'sqlite:///' . dirname(__FILE__) . '/data.db';
+
index 0ac01d5..7d869d5 100644 (file)
@@ -1,6 +1,13 @@
 <?php
 
 /**
+ * Returns true if in cli mode
+ */
+function is_cli() {
+    return php_sapi_name() === 'cli';
+}
+
+/**
  * Convenience function that outputs an HTTP status code
  * @param $code integer HTTP status code
  * @return string HTTP status code's name
@@ -84,7 +91,12 @@ function is_created_by_us($page) {
 function xhtmlcompiler_exception_handler(Exception $e) {
     $xc  = XHTMLCompiler::getInstance();
     $php = XHTMLCompiler::getPHPWrapper();
-    
+
+    if (is_cli()) {
+        $php->paint((string) $e);
+        return;
+    }
+
     $error_xsl = $xc->getConf('error_xsl');
     
     // extract information out of exception
diff --git a/generate.php b/generate.php
new file mode 100644 (file)
index 0000000..d64bd76
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+require_once 'common.php';
+
+if (!is_cli()) throw new XHTMLCompiler_Exception(403, "Cannot perform database generation from web");
+
+chdir('xhtml-compiler');
+
+Doctrine::dropDatabases();
+Doctrine::createDatabases();
+Doctrine::generateModelsFromYaml('schema.yml', 'models');
+Doctrine::createTablesFromModels('models');
+
diff --git a/models/Comment.php b/models/Comment.php
new file mode 100644 (file)
index 0000000..bb4a7c3
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * Comment
+ * 
+ * This class has been auto-generated by the Doctrine ORM Framework
+ * 
+ * @package    ##PACKAGE##
+ * @subpackage ##SUBPACKAGE##
+ * @author     ##NAME## <##EMAIL##>
+ * @version    SVN: $Id$
+ */
+class Comment extends BaseComment
+{
+
+}
\ No newline at end of file
diff --git a/models/generated/BaseComment.php b/models/generated/BaseComment.php
new file mode 100644 (file)
index 0000000..6c8f551
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * BaseComment
+ * 
+ * This class has been auto-generated by the Doctrine ORM Framework
+ * 
+ * @property integer $id
+ * @property string $username
+ * @property timestamp $time
+ * @property string $ip
+ * @property integer $parent_id
+ * @property clob $content
+ * @property Comment $Parent
+ * @property Doctrine_Collection $Comment
+ * 
+ * @package    ##PACKAGE##
+ * @subpackage ##SUBPACKAGE##
+ * @author     ##NAME## <##EMAIL##>
+ * @version    SVN: $Id$
+ */
+abstract class BaseComment extends Doctrine_Record
+{
+    public function setTableDefinition()
+    {
+        $this->setTableName('comment');
+        $this->hasColumn('id', 'integer', null, array('type' => 'integer', 'primary' => true, 'autoincrement' => true));
+        $this->hasColumn('username', 'string', null, array('type' => 'string'));
+        $this->hasColumn('time', 'timestamp', null, array('type' => 'timestamp'));
+        $this->hasColumn('ip', 'string', null, array('type' => 'string'));
+        $this->hasColumn('parent_id', 'integer', null, array('type' => 'integer'));
+        $this->hasColumn('content', 'clob', null, array('type' => 'clob'));
+    }
+
+    public function setUp()
+    {
+        $this->hasOne('Comment as Parent', array('local' => 'parent_id',
+                                                 'foreign' => 'id'));
+
+        $this->hasMany('Comment', array('local' => 'id',
+                                        'foreign' => 'parent_id'));
+    }
+}
\ No newline at end of file
diff --git a/schema.yml b/schema.yml
new file mode 100644 (file)
index 0000000..eb47c1d
--- /dev/null
@@ -0,0 +1,18 @@
+---
+
+Comment:
+  columns:
+    id:
+        type: integer
+        primary: true
+        autoincrement: true
+    username: string
+    time: timestamp
+    ip: string
+    parent_id: integer
+    content: clob
+  relations:
+    Parent:
+      class: Comment
+      local: parent_id
+      foreign: id