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