Notice_to_item.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Data class for storing notice-to-Facebook-item mappings
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Zach Copley <zach@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. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  33. /**
  34. * Data class for mapping notices to Facebook stream items
  35. *
  36. * Note that notice_id is unique only within a single database; if you
  37. * want to share this data for some reason, get the notice's URI and use
  38. * that instead, since it's universally unique.
  39. *
  40. * @category Action
  41. * @package StatusNet
  42. * @author Zach Copley <zach@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  44. * @link http://status.net/
  45. *
  46. * @see DB_DataObject
  47. */
  48. class Notice_to_item extends Managed_DataObject
  49. {
  50. public $__table = 'notice_to_item'; // table name
  51. public $notice_id; // int(4) primary_key not_null
  52. public $item_id; // varchar(191) not null not 255 because utf8mb4 takes more space
  53. public $created; // datetime
  54. /**
  55. * return table definition for DB_DataObject
  56. *
  57. * DB_DataObject needs to know something about the table to manipulate
  58. * instances. This method provides all the DB_DataObject needs to know.
  59. *
  60. * @return array array of column definitions
  61. */
  62. function table()
  63. {
  64. return array(
  65. 'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
  66. 'item_id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
  67. 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
  68. );
  69. }
  70. static function schemaDef()
  71. {
  72. return array(
  73. new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
  74. new ColumnDef('item_id', 'varchar', 191, false, 'UNI'),
  75. new ColumnDef('created', 'datetime', null, false)
  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. function keys()
  88. {
  89. return array_keys($this->keyTypes());
  90. }
  91. /**
  92. * return key definitions for Memcached_DataObject
  93. *
  94. * Our caching system uses the same key definitions, but uses a different
  95. * method to get them. This key information is used to store and clear
  96. * cached data, so be sure to list any key that will be used for static
  97. * lookups.
  98. *
  99. * @return array associative array of key definitions, field name to type:
  100. * 'K' for primary key: for compound keys, add an entry for each component;
  101. * 'U' for unique keys: compound keys are not well supported here.
  102. */
  103. function keyTypes()
  104. {
  105. return array('notice_id' => 'K', 'item_id' => 'U');
  106. }
  107. /**
  108. * Magic formula for non-autoincrementing integer primary keys
  109. *
  110. * If a table has a single integer column as its primary key, DB_DataObject
  111. * assumes that the column is auto-incrementing and makes a sequence table
  112. * to do this incrementation. Since we don't need this for our class, we
  113. * overload this method and return the magic formula that DB_DataObject needs.
  114. *
  115. * @return array magic three-false array that stops auto-incrementing.
  116. */
  117. function sequenceKey()
  118. {
  119. return array(false, false, false);
  120. }
  121. /**
  122. * Save a mapping between a notice and a Facebook item
  123. *
  124. * @param integer $notice_id ID of the notice in StatusNet
  125. * @param integer $item_id ID of the stream item on Facebook
  126. *
  127. * @return Notice_to_item new object for this value
  128. */
  129. static function saveNew($notice_id, $item_id)
  130. {
  131. $n2i = Notice_to_item::getKV('notice_id', $notice_id);
  132. if (!empty($n2i)) {
  133. return $n2i;
  134. }
  135. $n2i = Notice_to_item::getKV('item_id', $item_id);
  136. if (!empty($n2i)) {
  137. return $n2i;
  138. }
  139. common_debug(
  140. "Mapping notice {$notice_id} to Facebook item {$item_id}",
  141. __FILE__
  142. );
  143. $n2i = new Notice_to_item();
  144. $n2i->notice_id = $notice_id;
  145. $n2i->item_id = $item_id;
  146. $n2i->created = common_sql_now();
  147. $n2i->insert();
  148. return $n2i;
  149. }
  150. }