Twitter_synch_status.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Store last-touched ID for various timelines
  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. * Store various timeline data
  35. *
  36. * We don't want to keep re-fetching the same statuses and direct messages from Twitter.
  37. * So, we store the last ID we see from a timeline, and store it. Next time
  38. * around, we use that ID in the since_id parameter.
  39. *
  40. * @category Action
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@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 Twitter_synch_status extends Managed_DataObject
  49. {
  50. public $__table = 'twitter_synch_status'; // table name
  51. public $foreign_id; // bigint primary_key not_null
  52. public $timeline; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
  53. public $last_id; // bigint not_null
  54. public $created; // datetime() not_null
  55. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  56. public static function schemaDef()
  57. {
  58. return array(
  59. 'fields' => array(
  60. 'foreign_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'Foreign message ID'),
  61. 'timeline' => array('type' => 'varchar', 'length' => 191, 'description' => 'timeline name'),
  62. 'last_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'last id fetched'),
  63. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  64. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  65. ),
  66. 'primary key' => array('foreign_id', 'timeline'),
  67. );
  68. }
  69. static function getLastId($foreign_id, $timeline)
  70. {
  71. $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
  72. 'timeline' => $timeline));
  73. if (empty($tss)) {
  74. return null;
  75. } else {
  76. return $tss->last_id;
  77. }
  78. }
  79. static function setLastId($foreign_id, $timeline, $last_id)
  80. {
  81. $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
  82. 'timeline' => $timeline));
  83. if (empty($tss)) {
  84. $tss = new Twitter_synch_status();
  85. $tss->foreign_id = $foreign_id;
  86. $tss->timeline = $timeline;
  87. $tss->last_id = $last_id;
  88. $tss->created = common_sql_now();
  89. $tss->modified = $tss->created;
  90. $tss->insert();
  91. return true;
  92. } else {
  93. $orig = clone($tss);
  94. $tss->last_id = $last_id;
  95. $tss->modified = common_sql_now();
  96. $tss->update();
  97. return true;
  98. }
  99. }
  100. }