]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/classes/Yammer_notice_stub.php
Updating all Memcached_DataObject extended classes to Managed_DataObject
[quix0rs-gnu-social.git] / plugins / YammerImport / classes / Yammer_notice_stub.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  * Temporary storage for imported Yammer messages between fetching and saving
36  * as local notices.
37  *
38  * The Yammer API only allows us to page down from the most recent items; in
39  * order to start saving the oldest notices first, we have to pull them all
40  * down in reverse chronological order, then go back over them from oldest to
41  * newest and actually save them into our notice table.
42  */
43 class Yammer_notice_stub extends Managed_DataObject
44 {
45     public $__table = 'yammer_notice_stub'; // table name
46     public $id;                             // int  primary_key not_null
47     public $json_data;                      // text
48     public $created;                        // datetime
49
50     /**
51      * Return schema definition to set this table up in onCheckSchema
52      */
53     static function schemaDef()
54     {
55         return array(new ColumnDef('id', 'bigint', null,
56                                    false, 'PRI'),
57                      new ColumnDef('json_data', 'text', null,
58                                    false),
59                      new ColumnDef('created', 'datetime', null,
60                                    false));
61     }
62
63     /**
64      * return table definition for DB_DataObject
65      *
66      * DB_DataObject needs to know something about the table to manipulate
67      * instances. This method provides all the DB_DataObject needs to know.
68      *
69      * @return array array of column definitions
70      */
71     function table()
72     {
73         return array('id'           => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
74                      'json_data'    => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
75                      'created'      => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
76     }
77
78     /**
79      * return key definitions for DB_DataObject
80      *
81      * DB_DataObject needs to know about keys that the table has, since it
82      * won't appear in StatusNet's own keys list. In most cases, this will
83      * simply reference your keyTypes() function.
84      *
85      * @return array list of key field names
86      */
87
88     function keys()
89     {
90         return array_keys($this->keyTypes());
91     }
92
93     /**
94      * return key definitions for Memcached_DataObject
95      *
96      * Our caching system uses the same key definitions, but uses a different
97      * method to get them. This key information is used to store and clear
98      * cached data, so be sure to list any key that will be used for static
99      * lookups.
100      *
101      * @return array associative array of key definitions, field name to type:
102      *         'K' for primary key: for compound keys, add an entry for each component;
103      *         'U' for unique keys: compound keys are not well supported here.
104      */
105     function keyTypes()
106     {
107         return array('id' => 'K');
108     }
109
110     /**
111      * Magic formula for non-autoincrementing integer primary keys
112      *
113      * If a table has a single integer column as its primary key, DB_DataObject
114      * assumes that the column is auto-incrementing and makes a sequence table
115      * to do this incrementation. Since we don't need this for our class, we
116      * overload this method and return the magic formula that DB_DataObject needs.
117      *
118      * @return array magic three-false array that stops auto-incrementing.
119      */
120     function sequenceKey()
121     {
122         return array(false, false, false);
123     }
124
125     /**
126      * Decode the stored data structure.
127      *
128      * @return mixed
129      */
130     public function getData()
131     {
132         return json_decode($this->json_data, true);
133     }
134
135     /**
136      * Save the native Yammer API representation of a message for the pending
137      * import. Since they come in in reverse chronological order, we need to
138      * record them all as stubs and then go through from the beginning and
139      * save them as native notices, or we'll lose ordering and threading
140      * data.
141      *
142      * @param integer $orig_id ID of the notice on Yammer
143      * @param array $data the message record fetched out of Yammer API returnd data
144      *
145      * @return Yammer_notice_stub new object for this value
146      */
147
148     static function record($orig_id, $data)
149     {
150         common_debug("Recording Yammer message stub {$orig_id} for pending import...");
151
152         $stub = new Yammer_notice_stub();
153
154         $stub->id = $orig_id;
155         $stub->json_data = json_encode($data);
156         $stub->created = common_sql_now();
157
158         $stub->insert();
159
160         return $stub;
161     }
162 }