]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookSSO/classes/Notice_to_item.php
Merge branch '0.9.x' into facebook-upgrade
[quix0rs-gnu-social.git] / plugins / FacebookSSO / 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
52 class Notice_to_item extends Memcached_DataObject
53 {
54     public $__table = 'notice_to_item'; // table name
55     public $notice_id;                  // int(4)  primary_key not_null
56     public $item_id;                    // varchar(255) not null
57     public $created;                    // datetime
58
59     /**
60      * Get an instance by key
61      *
62      * This is a utility method to get a single instance with a given key value.
63      *
64      * @param string $k Key to use to lookup
65      * @param mixed  $v Value to lookup
66      *
67      * @return Notice_to_item object found, or null for no hits
68      *
69      */
70
71     function staticGet($k, $v=null)
72     {
73         return Memcached_DataObject::staticGet('Notice_to_item', $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(
88             'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
89             'item_id'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
90             'created'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
91         );
92     }
93
94     static function schemaDef()
95     {
96         return array(
97             new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
98             new ColumnDef('item_id', 'varchar', 255, false, 'UNI'),
99             new ColumnDef('created', 'datetime',  null, false)
100         );
101     }
102
103     /**
104      * return key definitions for DB_DataObject
105      *
106      * DB_DataObject needs to know about keys that the table has, since it
107      * won't appear in StatusNet's own keys list. In most cases, this will
108      * simply reference your keyTypes() function.
109      *
110      * @return array list of key field names
111      */
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
131     function keyTypes()
132     {
133         return array('notice_id' => 'K', 'item_id' => 'U');
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
147     function sequenceKey()
148     {
149         return array(false, false, false);
150     }
151
152     /**
153      * Save a mapping between a notice and a Facebook item
154      *
155      * @param integer $notice_id ID of the notice in StatusNet
156      * @param integer $item_id ID of the stream item on Facebook
157      *
158      * @return Notice_to_item new object for this value
159      */
160
161     static function saveNew($notice_id, $item_id)
162     {
163         $n2i = Notice_to_item::staticGet('notice_id', $notice_id);
164
165         if (!empty($n2i)) {
166             return $n2i;
167         }
168
169         $n2i = Notice_to_item::staticGet('item_id', $item_id);
170
171         if (!empty($n2i)) {
172             return $n2i;
173         }
174
175         common_debug(
176             "Mapping notice {$notice_id} to Facebook item {$item_id}",
177             __FILE__
178         );
179
180         $n2i = new Notice_to_item();
181
182         $n2i->notice_id = $notice_id;
183         $n2i->item_id   = $item_id;
184         $n2i->created   = common_sql_now();
185
186         $n2i->insert();
187
188         return $n2i;
189     }
190 }