Notice_to_status.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Data class for remembering notice-to-status mappings
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@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 statuses
  35. *
  36. * Notices flow back and forth between Twitter and StatusNet. We use this
  37. * table to remember which StatusNet notice corresponds to which Twitter
  38. * status.
  39. *
  40. * Note that notice_id is unique only within a single database; if you
  41. * want to share this data for some reason, get the notice's URI and use
  42. * that instead, since it's universally unique.
  43. *
  44. * @category Action
  45. * @package StatusNet
  46. * @author Evan Prodromou <evan@status.net>
  47. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  48. * @link http://status.net/
  49. *
  50. * @see DB_DataObject
  51. */
  52. class Notice_to_status extends Managed_DataObject
  53. {
  54. public $__table = 'notice_to_status'; // table name
  55. public $notice_id; // int(4) primary_key not_null
  56. public $status_id; // bigint not_null
  57. public $created; // datetime() not_null
  58. public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
  59. public static function schemaDef()
  60. {
  61. return array(
  62. 'fields' => array(
  63. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'local notice id'),
  64. 'status_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'twitter status id'),
  65. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  66. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  67. ),
  68. 'primary key' => array('notice_id'),
  69. 'unique keys' => array(
  70. 'status_id_key' => array('status_id'),
  71. ),
  72. 'foreign keys' => array(
  73. 'notice_to_status_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  74. ),
  75. );
  76. }
  77. /**
  78. * Save a mapping between a notice and a status
  79. * Warning: status_id values may not fit in 32-bit integers.
  80. *
  81. * @param integer $notice_id ID of the notice in StatusNet
  82. * @param integer $status_id ID of the status in Twitter
  83. *
  84. * @return Notice_to_status new object for this value
  85. */
  86. static function saveNew($notice_id, $status_id)
  87. {
  88. if (empty($notice_id)) {
  89. throw new Exception("Invalid notice_id $notice_id");
  90. }
  91. $n2s = Notice_to_status::getKV('notice_id', $notice_id);
  92. if (!empty($n2s)) {
  93. return $n2s;
  94. }
  95. if (empty($status_id)) {
  96. throw new Exception("Invalid status_id $status_id");
  97. }
  98. $n2s = Notice_to_status::getKV('status_id', $status_id);
  99. if (!empty($n2s)) {
  100. return $n2s;
  101. }
  102. common_debug("Mapping notice {$notice_id} to Twitter status {$status_id}");
  103. $n2s = new Notice_to_status();
  104. $n2s->notice_id = $notice_id;
  105. $n2s->status_id = $status_id;
  106. $n2s->created = common_sql_now();
  107. $n2s->insert();
  108. return $n2s;
  109. }
  110. }