]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/Notice_to_status.php
Merge branch 'master' into 1.0.x
[quix0rs-gnu-social.git] / plugins / TwitterBridge / Notice_to_status.php
1 <?php
2 /**
3  * Data class for remembering notice-to-status mappings
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  * Data class for mapping notices to statuses
38  *
39  * Notices flow back and forth between Twitter and StatusNet. We use this
40  * table to remember which StatusNet notice corresponds to which Twitter
41  * status.
42  *
43  * Note that notice_id is unique only within a single database; if you
44  * want to share this data for some reason, get the notice's URI and use
45  * that instead, since it's universally unique.
46  *
47  * @category Action
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
51  * @link     http://status.net/
52  *
53  * @see      DB_DataObject
54  */
55
56 class Notice_to_status extends Memcached_DataObject
57 {
58     public $__table = 'notice_to_status'; // table name
59     public $notice_id;                    // int(4)  primary_key not_null
60     public $status_id;                    // int(4)
61     public $created;                      // datetime
62
63     /**
64      * Get an instance by key
65      *
66      * This is a utility method to get a single instance with a given key value.
67      *
68      * @param string $k Key to use to lookup
69      * @param mixed  $v Value to lookup
70      *
71      * @return Notice_to_status object found, or null for no hits
72      *
73      */
74     function staticGet($k, $v=null)
75     {
76         return Memcached_DataObject::staticGet('Notice_to_status', $k, $v);
77     }
78
79     /**
80      * return table definition for DB_DataObject
81      *
82      * DB_DataObject needs to know something about the table to manipulate
83      * instances. This method provides all the DB_DataObject needs to know.
84      *
85      * @return array array of column definitions
86      */
87     function table()
88     {
89         return array('notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
90                      'status_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
91                      'created'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
92     }
93
94     /**
95      * return key definitions for DB_DataObject
96      *
97      * DB_DataObject needs to know about keys that the table has, since it
98      * won't appear in StatusNet's own keys list. In most cases, this will
99      * simply reference your keyTypes() function.
100      *
101      * @return array list of key field names
102      */
103     function keys()
104     {
105         return array_keys($this->keyTypes());
106     }
107
108     /**
109      * return key definitions for Memcached_DataObject
110      *
111      * Our caching system uses the same key definitions, but uses a different
112      * method to get them. This key information is used to store and clear
113      * cached data, so be sure to list any key that will be used for static
114      * lookups.
115      *
116      * @return array associative array of key definitions, field name to type:
117      *         'K' for primary key: for compound keys, add an entry for each component;
118      *         'U' for unique keys: compound keys are not well supported here.
119      */
120     function keyTypes()
121     {
122         return array('notice_id' => 'K', 'status_id' => 'U');
123     }
124
125     /**
126      * Magic formula for non-autoincrementing integer primary keys
127      *
128      * If a table has a single integer column as its primary key, DB_DataObject
129      * assumes that the column is auto-incrementing and makes a sequence table
130      * to do this incrementation. Since we don't need this for our class, we
131      * overload this method and return the magic formula that DB_DataObject needs.
132      *
133      * @return array magic three-false array that stops auto-incrementing.
134      */
135     function sequenceKey()
136     {
137         return array(false, false, false);
138     }
139
140     /**
141      * Save a mapping between a notice and a status
142      * Warning: status_id values may not fit in 32-bit integers.
143      *
144      * @param integer $notice_id ID of the notice in StatusNet
145      * @param integer $status_id ID of the status in Twitter
146      *
147      * @return Notice_to_status new object for this value
148      */
149     static function saveNew($notice_id, $status_id)
150     {
151         if (empty($notice_id)) {
152             throw new Exception("Invalid notice_id $notice_id");
153         }
154         $n2s = Notice_to_status::staticGet('notice_id', $notice_id);
155
156         if (!empty($n2s)) {
157             return $n2s;
158         }
159
160         if (empty($status_id)) {
161             throw new Exception("Invalid status_id $status_id");
162         }
163         $n2s = Notice_to_status::staticGet('status_id', $status_id);
164
165         if (!empty($n2s)) {
166             return $n2s;
167         }
168
169         common_debug("Mapping notice {$notice_id} to Twitter status {$status_id}");
170
171         $n2s = new Notice_to_status();
172
173         $n2s->notice_id = $notice_id;
174         $n2s->status_id = $status_id;
175         $n2s->created   = common_sql_now();
176
177         $n2s->insert();
178
179         return $n2s;
180     }
181 }