summaryrefslogtreecommitdiff
path: root/src/Bridge.php
blob: 81f80b64c4af0bcf1db4c73f6619996c95466d1d (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
<?php

require_once(__DIR__ . '/BridgeConf.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) {
    $sql = 'SELECT post_id FROM posts ' .
           'WHERE message_id = ' . $this->db->quote($messageId);

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

    $rows = $result->fetchAll(PDO::FETCH_ASSOC);
    $result->closeCursor();

    switch (count($rows)) {
    case 0:
      trigger_error("Unknown message id: $messageId", E_USER_ERROR);
      break;

    case 1:
      return $rows[0]['post_id'];

    default:
      trigger_error("Too many rows returned: $messageId", E_USER_ERROR);
      break;
    }   
  }

  public function getMessageId($postId) {
    $sql = 'SELECT message_id FROM posts ' .
           'WHERE post_id = ' . $this->db->quote($postId);

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

    $rows = $result->fetchAll(PDO::FETCH_ASSOC);
    $result->closeCursor();
    
    switch (count($rows)) {
    case 0:
      trigger_error("Unknown post id: $postId", E_USER_ERROR);
      break;

    case 1:
      return $rows[0]['message_id'];

    default:
      trigger_error("Too many rows returned: $postId", E_USER_ERROR);
      break;
    }    
  }

  public function registerMessage($msg, $parentId) {
    $sql = 'INSERT INTO posts ' .
           '(post_id, message_id, in_reply_to, refs) ' .
           'VALUES (' . $msg->getPostId()                       . ', '
                      . $this->db->quote($msg->getMessageId())  . ', '
                      . $this->db->quote()                      . ', '
                      . $this->db->quote($msg->getReferences()) . ')'; 

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

    if ($count != 1) {
      trigger_error(
        'Failed to register message: ' . $msg->getMessageId(), E_USER_ERROR
      );
    }
  }

  public function getDefaultForumId($list) {
    $sql = 'SELECT forum_id FROM bridge ' .
           'WHERE list_name = ' . $this->db->quote($list);

    $result = $this->db->query($sql);
    if (!$result) {
      trigger_error("Unknown list name: $list", E_USER_ERROR);
    }

    // FIXME: what to do if more than one row is returned?
    $row = $result->fetch(PDO::FETCH_ASSOC);
    $result->closeCursor();

    return $row['forum_id'];
  }
}

?>