0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
User.php
Go to the documentation of this file.
1 <?
24 require_once 'HypertableConnection.php';
25 require_once 'TweetTable.php';
26 
27 class User
28 {
29  public function getId() {
30  return ($this->_id);
31  }
32 
33  public function setId($id) {
34  $this->_id=$id;
35  }
36 
37  public function follow($other) {
38  $id=$this->_id;
39  HypertableConnection::insert('user', $id, "following:$other",
40  '');
41  HypertableConnection::insert('user', $id, 'following_count',
42  1);
43  HypertableConnection::insert('user', $other, 'followers:'.$id,
44  '');
45  HypertableConnection::insert('user', $other, 'followers_count',
46  1);
47 
48  // obtain the timestamps
49  $result=HypertableConnection::query("SELECT CELLS * FROM user WHERE ".
50  "(CELL = '$id','following:$other' ".
51  "OR CELL = '$other','followers:$id')");
52  $following_timestamp=$result->cells[0]->key->timestamp;
53  $followers_timestamp=$result->cells[1]->key->timestamp;
54 
55  // insert into the histories with the timestamps
56  HypertableConnection::insert('user', $id, 'following_history', $other,
57  HypertableConnection::format_timestamp_ns($following_timestamp));
58  HypertableConnection::insert('user', $other, 'followers_history', $id,
59  HypertableConnection::format_timestamp_ns($followers_timestamp));
60  }
61 
62  public function unfollow($other) {
63  // obtain the timestamps
64  $result=HypertableConnection::query("SELECT CELLS * FROM user WHERE ".
65  "(CELL = '$id','following:$other' ".
66  "OR CELL = '$other','followers:$id')");
67  $following_timestamp=$result->cells[0]->key->timestamp;
68  $followers_timestamp=$result->cells[1]->key->timestamp;
69 
70  // DELETE following_history and followers_history
71  HypertableConnection::delete('user', $this->_id, 'following_history',
72  $following_timestamp);
73  HypertableConnection::delete('user', $other, 'followers_history',
74  $followers_timestamp);
75 
76  // DELETE following and followers
77  HypertableConnection::delete('user', $this->_id, "following:$other");
78  HypertableConnection::delete('user', $other, 'followers:'.$this->_id);
79 
80  // decrement the counters
81  HypertableConnection::insert('user', $this->_id, 'following_count', '-1');
82  HypertableConnection::insert('user', $other, 'followers_count', '-1');
83  }
84 
85  public function getFollowingCount() {
86  $result=HypertableConnection::query("SELECT following_count FROM ".
87  "user WHERE ROW='".$this->_id."'");
88  if (!$result || count($result->cells)==0)
89  return 0;
90  return $result->cells[0]->value;
91  }
92 
93  public function getFollowing($cutoff_time, $limit) {
94  $a=array();
95  $id=$this->_id;
96  if ($cutoff_time)
97  $t="AND TIMESTAMP < '".
99  else
100  $t='';
101  $result=HypertableConnection::query("SELECT following_history FROM user ".
102  "WHERE ROW='$id' $t CELL_LIMIT $limit");
103  if (!$result or !count($result->cells))
104  return $a;
105  foreach ($result->cells as $cell) {
106  array_push($a, $cell->value);
107  }
108  $this->_cutoff=$result->cells[count($result->cells)-1]->key->timestamp;
109  return $a;
110  }
111 
112  public function isFollowing($other) {
113  $id=$this->_id;
114  $result=HypertableConnection::query("SELECT CELLS following:$other FROM ".
115  "user WHERE ROW='$id'");
116  if (!$result)
117  return null;
118  return count($result->cells);
119  }
120 
121  public function getFollowersCount() {
122  $result=HypertableConnection::query("SELECT followers_count FROM ".
123  "user WHERE ROW='".$this->_id."'");
124  if (!$result || count($result->cells)==0)
125  return 0;
126  return $result->cells[0]->value;
127  }
128 
129  public function getFollowers($cutoff_time, $limit) {
130  $a=array();
131  $id=$this->_id;
132  if ($cutoff_time)
133  $t="AND TIMESTAMP < '".
135  else
136  $t='';
137  $result=HypertableConnection::query("SELECT followers_history FROM user ".
138  "WHERE ROW='$id' $t CELL_LIMIT $limit");
139  if (!$result or !count($result->cells))
140  return $a;
141  foreach ($result->cells as $cell) {
142  array_push($a, $cell->value);
143  }
144  $this->_cutoff=$result->cells[count($result->cells)-1]->key->timestamp;
145  return $a;
146  }
147 
148  public function sendTweet($tweet) {
149  $tid=$tweet->getId();
150  HypertableConnection::insert('user', $this->_id, "my_stream",
151  $tid);
152  HypertableConnection::insert('user', $this->_id, 'my_stream_count',
153  1);
154 
155  // store in follow_stream, otherwise the user's own tweets would not
156  // be displayed in the timeline
157  HypertableConnection::insert('user', $this->_id, 'follow_stream',
158  $tid);
159 
160  // get followers
161  $result=HypertableConnection::query("SELECT followers FROM ".
162  "user WHERE ROW='".$this->_id."'");
163 
164  // foreach follower: store tweet id in their "follow-stream"
165  foreach ($result->cells as $cell) {
166  $qualifier=$cell->key->column_qualifier;
167  HypertableConnection::insert('user', $qualifier, 'follow_stream',
168  $tid);
169  HypertableConnection::insert('user', $qualifier, 'follow_stream_count',
170  '+1');
171  }
172  }
173 
174  public function getMyStreamCount() {
175  $result=HypertableConnection::query("SELECT my_stream_count FROM ".
176  "user WHERE ROW='".$this->_id."'");
177  if (!$result || !count($result->cells))
178  return 0;
179  return $result->cells[0]->value;
180  }
181 
182  public function getMyStream($cutoff_time, $limit) {
183  $a=array();
184  $id=$this->_id;
185  if ($cutoff_time)
186  $t="AND TIMESTAMP < '".
188  else
189  $t='';
190  $result=HypertableConnection::query("SELECT my_stream FROM user ".
191  "WHERE ROW='$id' $t CELL_LIMIT $limit");
192  if (!$result or !count($result->cells))
193  return $a;
194  foreach ($result->cells as $cell) {
195  array_push($a, TweetTable::load($cell->value));
196  }
197  $this->_cutoff=$result->cells[count($result->cells)-1]->key->timestamp;
198  return $a;
199  }
200 
201  public function getFollowStreamCount() {
202  $result=HypertableConnection::query("SELECT follow_stream_count FROM ".
203  "user WHERE ROW='".$this->_id."'");
204  if (!$result || !count($result->cells))
205  return 0;
206  return $result->cells[0]->value;
207  }
208 
209  public function getFollowStream($cutoff_time, $limit) {
210  $a=array();
211  $id=$this->_id;
212  if ($cutoff_time)
213  $t="AND TIMESTAMP < '".
215  else
216  $t='';
217  $result=HypertableConnection::query("SELECT follow_stream FROM user ".
218  "WHERE ROW='$id' $t CELL_LIMIT $limit");
219  if (!$result or !count($result->cells))
220  return $a;
221  foreach ($result->cells as $cell) {
222  array_push($a, TweetTable::load($cell->value));
223  }
224  $this->_cutoff=$result->cells[count($result->cells)-1]->key->timestamp;
225  return $a;
226  }
227 
228  public function getCutoffTime() {
229  return $this->_cutoff;
230  }
231 
232  protected $_id;
233  protected $_cutoff;
234 }
235 
236 ?>
static insert($table, $row, $column, $value, $timestamp=0)
getMyStream($cutoff_time, $limit)
Definition: User.php:182
static delete($table, $row, $column, $timestamp=0)
isFollowing($other)
Definition: User.php:112
getCutoffTime()
Definition: User.php:228
getMyStreamCount()
Definition: User.php:174
getFollowStream($cutoff_time, $limit)
Definition: User.php:209
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: User.php:27
getFollowers($cutoff_time, $limit)
Definition: User.php:129
$_id
Definition: User.php:232
getFollowingCount()
Definition: User.php:85
getFollowersCount()
Definition: User.php:121
sendTweet($tweet)
Definition: User.php:148
getFollowStreamCount()
Definition: User.php:201
getId()
Definition: User.php:29
follow($other)
Definition: User.php:37
unfollow($other)
Definition: User.php:62
getFollowing($cutoff_time, $limit)
Definition: User.php:93
$_cutoff
Definition: User.php:233
setId($id)
Definition: User.php:33