SearchSub.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Data class to store local search subscriptions
  4. *
  5. * PHP version 5
  6. *
  7. * @category SearchSubPlugin
  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) 2011, 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. * For storing the search subscriptions
  34. *
  35. * @category PollPlugin
  36. * @package StatusNet
  37. * @author Brion Vibber <brion@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  39. * @link http://status.net/
  40. *
  41. * @see DB_DataObject
  42. */
  43. class SearchSub extends Managed_DataObject
  44. {
  45. public $__table = 'searchsub'; // table name
  46. public $search; // text
  47. public $profile_id; // int -> profile.id
  48. public $created; // datetime
  49. /**
  50. * The One True Thingy that must be defined and declared.
  51. */
  52. public static function schemaDef()
  53. {
  54. return array(
  55. 'description' => 'SearchSubPlugin search subscription records',
  56. 'fields' => array(
  57. 'search' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash search associated with this subscription'),
  58. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
  59. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  60. ),
  61. 'primary key' => array('search', 'profile_id'),
  62. 'foreign keys' => array(
  63. 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  64. ),
  65. 'indexes' => array(
  66. 'searchsub_created_idx' => array('created'),
  67. 'searchsub_profile_id_tag_idx' => array('profile_id', 'search'),
  68. ),
  69. );
  70. }
  71. /**
  72. * Start a search subscription!
  73. *
  74. * @param profile $profile subscriber
  75. * @param string $search subscribee
  76. * @return SearchSub
  77. */
  78. static function start(Profile $profile, $search)
  79. {
  80. $ts = new SearchSub();
  81. $ts->search = $search;
  82. $ts->profile_id = $profile->id;
  83. $ts->created = common_sql_now();
  84. $ts->insert();
  85. self::blow('searchsub:by_profile:%d', $profile->id);
  86. return $ts;
  87. }
  88. /**
  89. * End a search subscription!
  90. *
  91. * @param profile $profile subscriber
  92. * @param string $search subscribee
  93. */
  94. static function cancel(Profile $profile, $search)
  95. {
  96. $ts = SearchSub::pkeyGet(array('search' => $search,
  97. 'profile_id' => $profile->id));
  98. if ($ts) {
  99. $ts->delete();
  100. self::blow('searchsub:by_profile:%d', $profile->id);
  101. }
  102. }
  103. static function forProfile(Profile $profile)
  104. {
  105. $searches = array();
  106. $keypart = sprintf('searchsub:by_profile:%d', $profile->id);
  107. $searchstring = self::cacheGet($keypart);
  108. if ($searchstring !== false) {
  109. if (!empty($searchstring)) {
  110. $searches = explode(',', $searchstring);
  111. }
  112. } else {
  113. $searchsub = new SearchSub();
  114. $searchsub->profile_id = $profile->id;
  115. $searchsub->selectAdd();
  116. $searchsub->selectAdd('search');
  117. if ($searchsub->find()) {
  118. $searches = $searchsub->fetchAll('search');
  119. }
  120. self::cacheSet($keypart, implode(',', $searches));
  121. }
  122. return $searches;
  123. }
  124. }