Yammer_notice_stub.php 5.4 KB

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