Yammer_common.php 5.3 KB

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