public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
* Prettified some QueryBuilder code.
* Store user comments in their session. [#199 state:resolved]
vito (author)
Mon Sep 01 11:22:48 -0700 2008
commit  0a83e618df461db5a1dc6aedf397251ee67fcbf7
tree    88fe6c2ba8c44a2998291b924865107f3573566d
parent  79592f602d99c0782f4cdbcc834728df075026cc
...
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
93
94
95
 
 
 
96
97
98
...
115
116
117
118
119
120
121
122
 
 
 
123
124
125
...
148
149
150
151
 
152
153
154
...
231
232
233
 
234
235
 
236
237
238
...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
 
 
 
 
 
 
 
 
 
 
 
 
256
257
258
...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
...
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
82
83
84
85
86
 
 
87
88
89
90
91
92
...
109
110
111
 
 
 
 
 
112
113
114
115
116
117
...
140
141
142
 
143
144
145
146
...
223
224
225
226
227
228
229
230
231
232
...
234
235
236
 
 
 
 
 
 
 
 
 
 
 
 
 
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
0
@@ -5,6 +5,47 @@
0
    */
0
   class QueryBuilder {
0
     /**
0
+     * Function: build_insert
0
+     * Creates a full insert query.
0
+     */
0
+    public static function build_insert($table, $data) {
0
+      return "INSERT INTO __$table\n".
0
+                   self::build_insert_header($data)."\n".
0
+                   "VALUES\n".
0
+                   self::build_insert_values($data);
0
+    }
0
+
0
+    /**
0
+     * Function: build_replace
0
+     * Creates a full replace query.
0
+     */
0
+    public static function build_replace($table, $data) {
0
+      return "REPLACE INTO __$table\n".
0
+                   self::build_insert_header($data)."\n".
0
+                   "VALUES\n".
0
+                   self::build_insert_values($data);
0
+    }
0
+
0
+    /**
0
+     * Function: build_update
0
+     * Creates a full update query.
0
+     */
0
+    public static function build_update($table, $conds, $data, &$params = array()) {
0
+      return "UPDATE __$table\n".
0
+                   "SET ".self::build_update_values($data, $params)."\n".
0
+                   ($conds ? "WHERE ".self::build_where($conds, $table, $params) : "");
0
+    }
0
+
0
+    /**
0
+     * Function: build_delete
0
+     * Creates a full delete query.
0
+     */
0
+    public static function build_delete($table, $conds, &$params = array()) {
0
+      return "DELETE FROM __$table\n".
0
+                   ($conds ? "WHERE ".self::build_where($conds, $table, $params) : "");
0
+    }
0
+
0
+    /**
0
      * Function: build_update_values
0
      * Creates an update data part.
0
      */
0
@@ -35,64 +76,17 @@
0
     }
0
 
0
     /**
0
-     * Function: build_insert
0
-     * Creates a full insert query.
0
-     */
0
-    public static function build_insert($table, $data) {
0
-      return "
0
-        INSERT INTO __$table
0
-        ".self::build_insert_header($data)."
0
-        VALUES
0
-        ".self::build_insert_values($data)."
0
-      ";
0
-    }
0
-
0
-    /**
0
-     * Function: build_replace
0
-     * Creates a full replace query.
0
-     */
0
-    public static function build_replace($table, $data) {
0
-      return "
0
-        REPLACE INTO __$table
0
-        ".self::build_insert_header($data)."
0
-        VALUES
0
-        ".self::build_insert_values($data)."
0
-      ";
0
-    }
0
-
0
-    /**
0
-     * Function: build_update
0
-     * Creates a full update query.
0
-     */
0
-    public static function build_update($table, $conds, $data, &$params = array()) {
0
-      return "
0
-        UPDATE __$table
0
-        SET ".self::build_update_values($data, $params)."
0
-        ".($conds ? "WHERE ".self::build_where($conds, $table, $params) : "")."
0
-      ";
0
-    }
0
-
0
-    /**
0
-     * Function: build_delete
0
-     * Creates a full delete query.
0
-     */
0
-    public static function build_delete($table, $conds, &$params = array()) {
0
-      return "
0
-        DELETE FROM __$table
0
-        ".($conds ? "WHERE ".self::build_where($conds, $table, $params) : "")."
0
-      ";
0
-    }
0
-
0
-    /**
0
      * Function: build_limits
0
      * Creates a LIMIT part for a query.
0
      */
0
     public static function build_limits($offset, $limit) {
0
       if ($limit === null)
0
         return "";
0
+
0
       if ($offset !== null)
0
-        return "LIMIT $offset, $limit";
0
-      return "LIMIT $limit";
0
+        return "LIMIT ".$offset.", ".$limit;
0
+
0
+      return "LIMIT ".$limit;
0
     }
0
 
0
     /**
0
@@ -115,11 +109,9 @@
0
      * Creates a SELECT COUNT(1) query.
0
      */
0
     public static function build_count($tables, $conds, &$params = array()) {
0
-      $query = "
0
-        SELECT COUNT(1) AS count
0
-        FROM ".self::build_from($tables);
0
-      $query.= "\n\t\t\t\t".($conds ? "WHERE ".self::build_where($conds, $tables, $params) : "");
0
-      return $query;
0
+      return "SELECT COUNT(1) AS count\n".
0
+             "FROM ".self::build_from($tables)."\n".
0
+             ($conds ? "WHERE ".self::build_where($conds, $tables, $params) : "");
0
     }
0
 
0
     /**
0
@@ -148,7 +140,7 @@
0
 
0
       $conditions = self::build_conditions($conds, $params, $tables);
0
 
0
-      return implode(" AND ", array_filter($conditions));
0
+      return (empty($conditions)) ? "" : "(".implode(") AND (", array_filter($conditions)).")";
0
     }
0
 
0
     /**
0
@@ -231,8 +223,10 @@
0
 
0
     public static function build_in($vals) {
0
       $return = array();
0
+
0
       foreach ($vals as $val)
0
         $return[] = SQL::current()->escape($val);
0
+
0
       return "(".join(",", $return).")";
0
     }
0
 
0
@@ -240,19 +234,18 @@
0
      * Function: build_select
0
      * Creates a full SELECT query.
0
      */
0
-    public static function build_select($tables, $fields, $conds, $order = null, $limit = null, $offset = null, $group = null, $left_join = null, &$params = array()) {
0
-      $query = "
0
-        SELECT ".self::build_select_header($fields, $tables)."
0
-        FROM ".self::build_from($tables);
0
-      if (isset($left_join))
0
-        foreach ($left_join as $join)
0
-          $query.= "\n\t\t\t\tLEFT JOIN __".$join["table"]." ON ".self::build_where($join["where"], $join["table"], $params);
0
-      $query.= "
0
-        ".($conds ? "WHERE ".self::build_where($conds, $tables, $params) : "")."
0
-        ".($group ? "GROUP BY ".self::build_group($group, $tables) : "")."
0
-        ".($order ? "ORDER BY ".self::build_order($order, $tables) : "")."
0
-        ".self::build_limits($offset, $limit)."
0
-      ";
0
+    public static function build_select($tables, $fields, $conds, $order = null, $limit = null, $offset = null, $group = null, $left_join = array(), &$params = array()) {
0
+      $query = "SELECT ".self::build_select_header($fields, $tables)."\n".
0
+               "FROM ".self::build_from($tables)."\n";
0
+
0
+      foreach ($left_join as $join)
0
+        $query.= "LEFT JOIN __".$join["table"]." ON ".self::build_where($join["where"], $join["table"], $params)."\n";
0
+
0
+      $query.= ($conds ? "WHERE ".self::build_where($conds, $tables, $params) : "")."\n".
0
+               ($group ? "GROUP BY ".self::build_group($group, $tables) : "")."\n".
0
+               ($order ? "ORDER BY ".self::build_order($order, $tables) : "")."\n".
0
+               self::build_limits($offset, $limit);
0
+
0
       return $query;
0
     }
0
 
...
192
193
194
 
195
196
197
...
211
212
213
 
214
215
 
216
217
218
...
224
225
226
 
227
228
229
...
237
238
239
 
240
241
242
...
251
252
253
 
254
255
256
...
264
265
266
 
267
268
269
...
192
193
194
195
196
197
198
...
212
213
214
215
216
 
217
218
219
220
...
226
227
228
229
230
231
232
...
240
241
242
243
244
245
246
...
255
256
257
258
259
260
261
...
269
270
271
272
273
274
275
0
@@ -192,6 +192,7 @@
0
      *     $tables - An array (or string) of tables to count results on.
0
      *     $conds - An array (or string) of conditions to match.
0
      *     $params - An associative array of parameters used in the query.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
     public function count($tables, $conds = null, $params = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_count($tables, $conds, $params), $params, $throw_exceptions)->fetchColumn();
0
@@ -211,8 +212,9 @@
0
      *     $offset - Offset for the select statement.
0
      *     $group - GROUP BY statement. Can be an array.
0
      *     $left_join - An array of additional LEFT JOINs.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
-    public function select($tables, $fields = "*", $conds = null, $order = null, $params = array(), $limit = null, $offset = null, $group = null, $left_join = null, $throw_exceptions = false) {
0
+    public function select($tables, $fields = "*", $conds = null, $order = null, $params = array(), $limit = null, $offset = null, $group = null, $left_join = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_select($tables, $fields, $conds, $order, $limit, $offset, $group, $left_join, $params), $params, $throw_exceptions);
0
     }
0
 
0
@@ -224,6 +226,7 @@
0
      *     $table - Table to insert to.
0
      *     $data - An associative array of data to insert.
0
      *     $params - An associative array of parameters used in the query.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
     public function insert($table, $data, $params = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_insert($table, $data), $params, $throw_exceptions);
0
@@ -237,6 +240,7 @@
0
      *     $table - Table to insert to.
0
      *     $data - An associative array of data to insert.
0
      *     $params - An associative array of parameters used in the query.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
     public function replace($table, $data, $params = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_replace($table, $data), $params, $throw_exceptions);
0
@@ -251,6 +255,7 @@
0
      *     $conds - Rows to update.
0
      *     $data - An associative array of data to update.
0
      *     $params - An associative array of parameters used in the query.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
     public function update($table, $conds, $data, $params = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_update($table, $conds, $data, $params), $params, $throw_exceptions);
0
@@ -264,6 +269,7 @@
0
      *     $table - Table to delete from.
0
      *     $conds - Rows to delete..
0
      *     $params - An associative array of parameters used in the query.
0
+     *     $throw_exceptions - Should exceptions be thrown on error?
0
      */
0
     public function delete($table, $conds, $params = array(), $throw_exceptions = false) {
0
       return $this->query(QueryBuilder::build_delete($table, $conds, $params), $params, $throw_exceptions);
...
23
24
25
26
 
27
28
29
...
23
24
25
 
26
27
28
29
0
@@ -23,7 +23,7 @@
0
 
0
   # Constant: DEBUG
0
   # Should Chyrp use debugging processes?
0
-  define('DEBUG', false);
0
+  define('DEBUG', true);
0
 
0
   # Fallback all these definitions.
0
   if (!defined('JAVASCRIPT')) define('JAVASCRIPT', false);
...
430
431
432
433
 
434
435
436
437
438
439
440
441
442
443
444
 
 
 
 
 
 
 
445
446
 
447
448
449
 
450
451
452
...
627
628
629
630
 
631
632
633
634
635
636
637
638
639
640
 
 
 
 
 
 
641
642
 
643
644
645
646
647
 
 
 
648
649
650
...
671
672
673
674
675
676
677
678
679
680
681
682
 
 
 
 
 
 
683
684
 
685
686
687
...
742
743
744
 
 
 
 
 
 
 
745
...
430
431
432
 
433
434
 
 
 
 
 
 
 
 
 
 
435
436
437
438
439
440
441
442
 
443
444
 
 
445
446
447
448
...
623
624
625
 
626
627
 
 
 
 
 
 
 
 
 
628
629
630
631
632
633
634
 
635
636
 
 
 
 
637
638
639
640
641
642
...
663
664
665
 
 
 
 
 
 
 
 
 
666
667
668
669
670
671
672
 
673
674
675
676
...
731
732
733
734
735
736
737
738
739
740
741
0
@@ -430,23 +430,19 @@
0
           if ($post->latest_comment > $_POST['last_comment']) {
0
             $new_comments = $sql->select("comments",
0
                                          "id",
0
-                                         array("post_id = :post_id",
0
+                                         array("post_id" => $_POST['post_id'],
0
                                                "id > :last_comment",
0
-                                               "status != 'spam'",
0
-                                               "(status != 'denied' OR (
0
-                                                    status = 'denied' AND (
0
-                                                        (
0
-                                                            author_ip != 0 AND
0
-                                                            author_ip = :current_ip
0
-                                                        ) OR (
0
-                                                            user_id != 0 AND
0
-                                                            user_id = :visitor_id
0
-                                                        )
0
+                                               "status not" => "spam",
0
+                                               "status != 'denied' OR (
0
+                                                    (
0
+                                                        user_id != 0 AND
0
+                                                        user_id = :visitor_id
0
+                                                    ) OR (
0
+                                                        id IN ".self::visitor_comments()."
0
                                                     )
0
-                                                ))"),
0
+                                                )"),
0
                                          "created_at ASC",
0
-                                         array(":post_id" => $_POST['post_id'],
0
-                                               ":last_comment" => $_POST['last_comment'],
0
+                                         array(":last_comment" => $_POST['last_comment'],
0
                                                ":current_ip" => ip2long($_SERVER['REMOTE_ADDR']),
0
                                                ":visitor_id" => $visitor->id
0
                                          ));
0
@@ -627,24 +623,20 @@
0
       if (isset($route->action) and $route->action == "view") {
0
         $get_comments = $sql->select("comments", # table
0
                                      "*", # fields
0
-                                     array("post_id = :post_id",
0
+                                     array("post_id" => $post->id,
0
                                            "status != 'spam'",
0
-                                           "(status != 'denied' OR (
0
-                                                status = 'denied' AND (
0
-                                                    (
0
-                                                        author_ip != 0 AND
0
-                                                        author_ip = :current_ip
0
-                                                    ) OR (
0
-                                                        user_id != 0 AND
0
-                                                        user_id = :visitor_id
0
-                                                    )
0
+                                           "status != 'denied' OR (
0
+                                                (
0
+                                                    user_id != 0 AND
0
+                                                    user_id = :visitor_id
0
+                                                ) OR (
0
+                                                    id IN ".self::visitor_comments()."
0
                                                 )
0
-                                            ))"),
0
+                                            )"),
0
                                      "created_at ASC", # order
0
-                                     array(
0
-                                         ":post_id" => $post->id,
0
-                                         ":current_ip" => ip2long($_SERVER['REMOTE_ADDR']),
0
-                                         ":visitor_id" => $visitor->id
0
+                                     array(":post_id" => $post->id,
0
+                                           ":current_ip" => ip2long($_SERVER['REMOTE_ADDR']),
0
+                                           ":visitor_id" => $visitor->id
0
                                      ));
0
 
0
         $post->comments = array();
0
@@ -671,17 +663,14 @@
0
       $options["left_join"][] = array("table" => "comments",
0
                                       "where" => array("post_id = posts.id",
0
                                                        "status != 'spam'",
0
-                                                       "(status != 'denied' OR (
0
-                                                            status = 'denied' AND (
0
-                                                                (
0
-                                                                    author_ip != 0 AND
0
-                                                                    author_ip = :current_ip
0
-                                                                ) OR (
0
-                                                                    user_id != 0 AND
0
-                                                                    user_id = :visitor_id
0
-                                                                )
0
+                                                       "status != 'denied' OR (
0
+                                                            (
0
+                                                                user_id != 0 AND
0
+                                                                user_id = :visitor_id
0
+                                                            ) OR (
0
+                                                                id IN ".self::visitor_comments()."
0
                                                             )
0
-                                                        ))"));
0
+                                                        )"));
0
 
0
       $options["params"][":current_ip"] = ip2long($_SERVER['REMOTE_ADDR']);
0
       $options["params"][":visitor_id"] = Visitor::current()->id;
0
@@ -742,4 +731,11 @@
0
       header("HTTP/1.1 301 Moved Permanently");
0
       redirect("comments_feed/");
0
     }
0
+
0
+    static function visitor_comments() {
0
+      if (empty($_SESSION['comments']))
0
+        return "(0)";
0
+      else
0
+        return QueryBuilder::build_in($_SESSION['comments']);
0
+    }
0
   }
...
107
108
109
 
 
 
110
111
112
...
126
127
128
 
 
 
129
130
131
...
107
108
109
110
111
112
113
114
115
...
129
130
131
132
133
134
135
136
137
0
@@ -107,6 +107,9 @@
0
                                $post,
0
                                $visitor->id);
0
 
0
+          fallback($_SESSION['comments'], array());
0
+          $_SESSION['comments'][] = $comment->id;
0
+
0
           if (isset($_POST['ajax']))
0
             exit("{ comment_id: ".$comment->id." }");
0
 
0
@@ -126,6 +129,9 @@
0
                            $post,
0
                            $visitor->id);
0
 
0
+        fallback($_SESSION['comments'], array());
0
+        $_SESSION['comments'][] = $comment->id;
0
+
0
         if (isset($_POST['ajax']))
0
           exit("{ comment_id: ".$comment->id." }");
0
 

Comments