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