summaryrefslogtreecommitdiff
path: root/src/Bridge.php
blob: 3b3ed51d2d609c44cc9e9ea5b1ae8fcee4aec676 (plain)
1
2
3
4
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

require_once(__DIR__ . '/BridgeConf.php');
require_once(__DIR__ . '/Util.php');

class Bridge {
  protected $db;

  public function __construct($db = FALSE) {
    $this->db = $db ? $db :
      new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB, DB_USER, DB_PASS);

    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }

  public function getPostId($messageId) {
    throw_if_null($messageId);

    $sql = 'SELECT post_id FROM posts ' .
           'WHERE message_id = ' . $this->db->quote($messageId);

    $row = $this->get_exactly_one_row($sql);
    return $row ? $row['post_id'] : false;
  }

  public function getMessageId($postId) {
    throw_if_null($postId);

    $sql = 'SELECT p1.message_id FROM posts AS p1 ' .
           'LEFT OUTER JOIN posts AS p2 ON (' .
              'p1.post_id = p2.post_id AND ' .
              'p1.edit_id < p2.edit_id' .
           ') WHERE p1.post_id = ' . $postId . ' AND ' .
           'p2.post_id IS NULL';

    $row = $this->get_exactly_one_row($sql);
    return $row ? $row['message_id'] : false;
  }

  public function setPostId($messageId, $postId) {
    throw_if_null($messageId);
    throw_if_null($postId);

    $sql = 'UPDATE posts SET ' .
           'post_id = ' . $postId . ' ' . 
           'WHERE message_id = ' . $this->db->quote($messageId);

    $count = $this->db->exec($sql);

    if ($count != 1) {
      throw new Exception('Failed to set post id: ' . $messageId);
    }
  }

  public function getDefaultForumId($list) {
    throw_if_null($list);

    $sql = 'SELECT forum_id FROM forums ' .
           'WHERE list_name = ' . $this->db->quote($list);

    $row = $this->get_exactly_one_row($sql);
    return $row ? $row['forum_id'] : false;
  }

  public function getLists($forumId) {
    throw_if_null($forumId);

    $sql = 'SELECT list_name FROM lists ' .
           'WHERE forum_id = ' . $forumId;

    $result = $this->db->query($sql);

    $rows = $result->fetchAll(PDO::FETCH_COLUMN);
    $result->closeCursor();
    return $rows;
  }

  public function reserveEditId($postId) {
    throw_if_null($postId);

    $sql = 'INSERT INTO posts (post_id) VALUES (' . $postId . ')';

    $count = $this->db->exec($sql);
    if ($count != 1) {
      throw new Exception('Failed to register post id: ' . $postId);
    }

    return $this->db->lastInsertId();
  }

  public function registerByEditId($editId, $messageId, $inReplyTo) {
    throw_if_null($messageId);

    $sql = 'UPDATE posts SET ' .
              'message_id = ' . $this->db->quote($messageId) . ', ' .
              'in_reply_to = ' . $this->quote($inReplyTo) . ' ' .
           'WHERE edit_id = ' . $editId;

    $count = $this->db->exec($sql);
    return $count == 1;
  }

  public function registerByMessageId($messageId, $inReplyTo) {
    throw_if_null($messageId);

    $sql = 'INSERT IGNORE INTO posts ' .
           '(message_id, in_reply_to) ' .
           'VALUES (' .
              $this->db->quote($messageId) . ', ' .
              $this->quote($inReplyTo) .
           ')';

    $count = $this->db->exec($sql);
    return $count == 1 ? $this->db->lastInsertId() : false;
  }

  public function unregisterMessage($editId) {
    throw_if_null($editId);

    $sql = 'DELETE FROM posts WHERE edit_id = ' . $editId;

    $count = $this->db->exec($sql);

    if ($count != 1) {
      throw new Exception('Failed to delete edit id: ' . $editId);
    }
  }

  public function removePost($postId) {
    throw_if_null($postId);

    $sql = 'DELETE FROM posts WHERE post_id = ' . $postId;
    
    $count = $this->db->exec($sql);

# FIXME: throwing an exception prevents us from deleting old posts
    if ($count < 1) {
      throw new Exception('Failed to delete post id: ' . $postId);
    }
  }

  protected function get_exactly_one_row($sql) {
    $result = $this->db->query($sql);

    $rows = $result->fetchAll(PDO::FETCH_ASSOC);
    $result->closeCursor();
    
    switch (count($rows)) {
    case 0:
      return false;

    case 1:
      return $rows[0];

    default:
      throw new Exception("Too many rows returned: $sql");
    }
  }

  protected function quote($arg) {
    return $arg === null ? 'NULL' : $this->db->quote($arg);
  }
}

?>