]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/Twitter_synch_status.php
Correct DB_DataObject constant in Twitter_synch_status
[quix0rs-gnu-social.git] / plugins / TwitterBridge / Twitter_synch_status.php
1 <?php
2 /**
3  * Store last-touched ID for various timelines
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2010, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Store various timeline data
38  *
39  * We don't want to keep re-fetching the same statuses and direct messages from Twitter.
40  * So, we store the last ID we see from a timeline, and store it. Next time
41  * around, we use that ID in the since_id parameter.
42  *
43  * @category Action
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47  * @link     http://status.net/
48  *
49  * @see      DB_DataObject
50  */
51
52 class Twitter_synch_status extends Memcached_DataObject
53 {
54     public $__table = 'twitter_synch_status'; // table name
55     public $user_id;                         // int(4)  primary_key not_null
56     public $timeline;                        // varchar(255)  primary_key not_null
57     public $last_id;                         // bigint not_null
58     public $created;                         // datetime not_null
59     public $modified;                        // datetime not_null
60
61     /**
62      * Get an instance by key
63      *
64      * @param string $k Key to use to lookup (usually 'user_id' for this class)
65      * @param mixed  $v Value to lookup
66      *
67      * @return User_greeting_count object found, or null for no hits
68      *
69      */
70
71     function staticGet($k, $v=null)
72     {
73         return Memcached_DataObject::staticGet('Twitter_synch_status', $k, $v);
74     }
75
76     /**
77      * return table definition for DB_DataObject
78      *
79      * DB_DataObject needs to know something about the table to manipulate
80      * instances. This method provides all the DB_DataObject needs to know.
81      *
82      * @return array array of column definitions
83      */
84
85     function table()
86     {
87         return array('user_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
88                      'timeline' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
89                      'last_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
90                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
91                      'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
92                      );
93     }
94
95     /**
96      * return key definitions for DB_DataObject
97      *
98      * DB_DataObject needs to know about keys that the table has, since it
99      * won't appear in StatusNet's own keys list. In most cases, this will
100      * simply reference your keyTypes() function.
101      *
102      * @return array list of key field names
103      */
104
105     function keys()
106     {
107         return array_keys($this->keyTypes());
108     }
109
110     /**
111      * return key definitions for Memcached_DataObject
112      *
113      * Our caching system uses the same key definitions, but uses a different
114      * method to get them. This key information is used to store and clear
115      * cached data, so be sure to list any key that will be used for static
116      * lookups.
117      *
118      * @return array associative array of key definitions, field name to type:
119      *         'K' for primary key: for compound keys, add an entry for each component;
120      *         'U' for unique keys: compound keys are not well supported here.
121      */
122
123     function keyTypes()
124     {
125         return array('user_id' => 'K',
126                      'timeline' => 'K');
127     }
128
129     /**
130      * Magic formula for non-autoincrementing integer primary keys
131      *
132      * If a table has a single integer column as its primary key, DB_DataObject
133      * assumes that the column is auto-incrementing and makes a sequence table
134      * to do this incrementation. Since we don't need this for our class, we
135      * overload this method and return the magic formula that DB_DataObject needs.
136      *
137      * @return array magic three-false array that stops auto-incrementing.
138      */
139
140     function sequenceKey()
141     {
142         return array(false, false, false);
143     }
144
145     static function getLastId($user_id, $timeline)
146     {
147         $tss = self::staticGet(array('user_id' => $user_id,
148                                      'timeline' => $timeline));
149
150         if (empty($tss)) {
151             return null;
152         } else {
153             return $tss->last_id;
154         }
155     }
156
157     static function setLastId($user_id, $timeline, $last_id)
158     {
159         $tss = self::staticGet(array('user_id' => $user_id,
160                                      'timeline' => $timeline));
161
162         if (empty($tss)) {
163
164             $tss = new Twitter_synch_status();
165
166             $tss->user_id  = $user_id;
167             $tss->timeline = $timeline;
168             $tss->last_id  = $last_id;
169             $tss->created  = common_sql_now();
170             $tss->modified = $tss->created;
171
172             $tss->insert();
173
174             return true;
175
176         } else {
177
178             $orig = clone($tss);
179
180             $tss->last_id  = $last_id;
181             $tss->modified = common_sql_now();
182
183             $tss->update();
184
185             return true;
186         }
187     }
188 }