]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookBridge/classes/Notice_to_item.php
a7cf5a13aa20cac519fe61fd462c6b0d565c9752
[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 Managed_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      * return table definition for DB_DataObject
60      *
61      * DB_DataObject needs to know something about the table to manipulate
62      * instances. This method provides all the DB_DataObject needs to know.
63      *
64      * @return array array of column definitions
65      */
66     function table()
67     {
68         return array(
69             'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
70             'item_id'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
71             'created'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
72         );
73     }
74
75     static function schemaDef()
76     {
77         return array(
78             new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
79             new ColumnDef('item_id', 'varchar', 255, false, 'UNI'),
80             new ColumnDef('created', 'datetime',  null, false)
81         );
82     }
83
84     /**
85      * return key definitions for DB_DataObject
86      *
87      * DB_DataObject needs to know about keys that the table has, since it
88      * won't appear in StatusNet's own keys list. In most cases, this will
89      * simply reference your keyTypes() function.
90      *
91      * @return array list of key field names
92      */
93     function keys()
94     {
95         return array_keys($this->keyTypes());
96     }
97
98     /**
99      * return key definitions for Memcached_DataObject
100      *
101      * Our caching system uses the same key definitions, but uses a different
102      * method to get them. This key information is used to store and clear
103      * cached data, so be sure to list any key that will be used for static
104      * lookups.
105      *
106      * @return array associative array of key definitions, field name to type:
107      *         'K' for primary key: for compound keys, add an entry for each component;
108      *         'U' for unique keys: compound keys are not well supported here.
109      */
110     function keyTypes()
111     {
112         return array('notice_id' => 'K', 'item_id' => 'U');
113     }
114
115     /**
116      * Magic formula for non-autoincrementing integer primary keys
117      *
118      * If a table has a single integer column as its primary key, DB_DataObject
119      * assumes that the column is auto-incrementing and makes a sequence table
120      * to do this incrementation. Since we don't need this for our class, we
121      * overload this method and return the magic formula that DB_DataObject needs.
122      *
123      * @return array magic three-false array that stops auto-incrementing.
124      */
125     function sequenceKey()
126     {
127         return array(false, false, false);
128     }
129
130     /**
131      * Save a mapping between a notice and a Facebook item
132      *
133      * @param integer $notice_id ID of the notice in StatusNet
134      * @param integer $item_id ID of the stream item on Facebook
135      *
136      * @return Notice_to_item new object for this value
137      */
138     static function saveNew($notice_id, $item_id)
139     {
140         $n2i = Notice_to_item::getKV('notice_id', $notice_id);
141
142         if (!empty($n2i)) {
143             return $n2i;
144         }
145
146         $n2i = Notice_to_item::getKV('item_id', $item_id);
147
148         if (!empty($n2i)) {
149             return $n2i;
150         }
151
152         common_debug(
153             "Mapping notice {$notice_id} to Facebook item {$item_id}",
154             __FILE__
155         );
156
157         $n2i = new Notice_to_item();
158
159         $n2i->notice_id = $notice_id;
160         $n2i->item_id   = $item_id;
161         $n2i->created   = common_sql_now();
162
163         $n2i->insert();
164
165         return $n2i;
166     }
167 }