]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/Twitter_synch_status.php
Localisation updates from http://translatewiki.net.
[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 class Twitter_synch_status extends Memcached_DataObject
52 {
53     public $__table = 'twitter_synch_status'; // table name
54     public $foreign_id;                         // int(4)  primary_key not_null
55     public $timeline;                        // varchar(255)  primary_key not_null
56     public $last_id;                         // bigint not_null
57     public $created;                         // datetime not_null
58     public $modified;                        // datetime not_null
59
60     /**
61      * Get an instance by key
62      *
63      * @param string $k Key to use to lookup (usually 'foreign_id' for this class)
64      * @param mixed  $v Value to lookup
65      *
66      * @return Twitter_synch_status object found, or null for no hits
67      */
68     function staticGet($k, $v=null)
69     {
70         throw new Exception("Use pkeyGet() for this class.");
71     }
72
73     /**
74      * Get an instance by compound primary key
75      *
76      * @param array $kv key-value pair array
77      *
78      * @return Twitter_synch_status object found, or null for no hits
79      *
80      */
81     function pkeyGet($kv)
82     {
83         return Memcached_DataObject::pkeyGet('Twitter_synch_status', $kv);
84     }
85
86     /**
87      * return table definition for DB_DataObject
88      *
89      * DB_DataObject needs to know something about the table to manipulate
90      * instances. This method provides all the DB_DataObject needs to know.
91      *
92      * @return array array of column definitions
93      */
94     function table()
95     {
96         return array('foreign_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
97                      'timeline' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
98                      'last_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
99                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
100                      'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
101                      );
102     }
103
104     /**
105      * return key definitions for DB_DataObject
106      *
107      * DB_DataObject needs to know about keys that the table has, since it
108      * won't appear in StatusNet's own keys list. In most cases, this will
109      * simply reference your keyTypes() function.
110      *
111      * @return array list of key field names
112      */
113     function keys()
114     {
115         return array_keys($this->keyTypes());
116     }
117
118     /**
119      * return key definitions for Memcached_DataObject
120      *
121      * Our caching system uses the same key definitions, but uses a different
122      * method to get them. This key information is used to store and clear
123      * cached data, so be sure to list any key that will be used for static
124      * lookups.
125      *
126      * @return array associative array of key definitions, field name to type:
127      *         'K' for primary key: for compound keys, add an entry for each component;
128      *         'U' for unique keys: compound keys are not well supported here.
129      */
130     function keyTypes()
131     {
132         return array('foreign_id' => 'K',
133                      'timeline' => 'K');
134     }
135
136     /**
137      * Magic formula for non-autoincrementing integer primary keys
138      *
139      * If a table has a single integer column as its primary key, DB_DataObject
140      * assumes that the column is auto-incrementing and makes a sequence table
141      * to do this incrementation. Since we don't need this for our class, we
142      * overload this method and return the magic formula that DB_DataObject needs.
143      *
144      * @return array magic three-false array that stops auto-incrementing.
145      */
146     function sequenceKey()
147     {
148         return array(false, false, false);
149     }
150
151     static function getLastId($foreign_id, $timeline)
152     {
153         $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
154                                    'timeline' => $timeline));
155
156         if (empty($tss)) {
157             return null;
158         } else {
159             return $tss->last_id;
160         }
161     }
162
163     static function setLastId($foreign_id, $timeline, $last_id)
164     {
165         $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
166                                    'timeline' => $timeline));
167
168         if (empty($tss)) {
169             $tss = new Twitter_synch_status();
170
171             $tss->foreign_id = $foreign_id;
172             $tss->timeline   = $timeline;
173             $tss->last_id    = $last_id;
174             $tss->created    = common_sql_now();
175             $tss->modified   = $tss->created;
176
177             $tss->insert();
178
179             return true;
180         } else {
181             $orig = clone($tss);
182
183             $tss->last_id  = $last_id;
184             $tss->modified = common_sql_now();
185
186             $tss->update();
187
188             return true;
189         }
190     }
191 }