]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/classes/Yammer_common.php
Fix mising require_once (now required here because of rearranging)
[quix0rs-gnu-social.git] / plugins / YammerImport / classes / Yammer_common.php
1 <?php
2 /**
3  * Data class for remembering Yammer import mappings
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Brion Vibber <brion@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 /**
35  * Common base class for the Yammer import mappings for users, groups, and notices.
36  *
37  * Child classes must override these static methods, since we need to run
38  * on PHP 5.2.x which has no late static binding:
39  * - staticGet (as our other classes)
40  * - schemaDef (call self::doSchemaDef)
41  * - record (call self::doRecord)
42  */
43
44 class Yammer_common extends Memcached_DataObject
45 {
46     public $__table = 'yammer_XXXX'; // table name
47     public $__field = 'XXXX_id';     // field name to save into
48     public $id;                      // int  primary_key not_null
49     public $user_id;                 // int(4)
50     public $created;                 // datetime
51
52     /**
53      * @fixme add a 'references' thing for the foreign key when we support that
54      */
55     protected static function doSchemaDef($field)
56     {
57         return array(new ColumnDef('id', 'bigint', null,
58                                    false, 'PRI'),
59                      new ColumnDef($field, 'integer', null,
60                                    false, 'UNI'),
61                      new ColumnDef('created', 'datetime', null,
62                                    false));
63     }
64
65     /**
66      * return table definition for DB_DataObject
67      *
68      * DB_DataObject needs to know something about the table to manipulate
69      * instances. This method provides all the DB_DataObject needs to know.
70      *
71      * @return array array of column definitions
72      */
73
74     function table()
75     {
76         return array('id'           => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
77                      $this->__field => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
78                      'created'      => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
79     }
80
81     /**
82      * return key definitions for DB_DataObject
83      *
84      * DB_DataObject needs to know about keys that the table has, since it
85      * won't appear in StatusNet's own keys list. In most cases, this will
86      * simply reference your keyTypes() function.
87      *
88      * @return array list of key field names
89      */
90
91     function keys()
92     {
93         return array_keys($this->keyTypes());
94     }
95
96     /**
97      * return key definitions for Memcached_DataObject
98      *
99      * Our caching system uses the same key definitions, but uses a different
100      * method to get them. This key information is used to store and clear
101      * cached data, so be sure to list any key that will be used for static
102      * lookups.
103      *
104      * @return array associative array of key definitions, field name to type:
105      *         'K' for primary key: for compound keys, add an entry for each component;
106      *         'U' for unique keys: compound keys are not well supported here.
107      */
108
109     function keyTypes()
110     {
111         return array('id' => 'K', $this->__field => 'U');
112     }
113
114     /**
115      * Magic formula for non-autoincrementing integer primary keys
116      *
117      * If a table has a single integer column as its primary key, DB_DataObject
118      * assumes that the column is auto-incrementing and makes a sequence table
119      * to do this incrementation. Since we don't need this for our class, we
120      * overload this method and return the magic formula that DB_DataObject needs.
121      *
122      * @return array magic three-false array that stops auto-incrementing.
123      */
124
125     function sequenceKey()
126     {
127         return array(false, false, false);
128     }
129
130     /**
131      * Save a mapping between a remote Yammer and local imported user.
132      *
133      * @param integer $user_id ID of the status in StatusNet
134      * @param integer $orig_id ID of the notice in Yammer
135      *
136      * @return Yammer_common new object for this value
137      */
138
139     protected static function doRecord($class, $field, $orig_id, $local_id)
140     {
141         $map = parent::staticGet($class, 'id', $orig_id);
142
143         if (!empty($map)) {
144             return $map;
145         }
146
147         $map = parent::staticGet($class, $field, $local_id);
148
149         if (!empty($map)) {
150             return $map;
151         }
152
153         common_debug("Mapping Yammer $field {$orig_id} to local $field {$local_id}");
154
155         $map = new $class();
156
157         $map->id = $orig_id;
158         $map->$field = $local_id;
159         $map->created = common_sql_now();
160
161         $map->insert();
162
163         return $map;
164     }
165 }