PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/classes/Inbox.php

https://github.com/Br3nda/statusnet-debian
PHP | 270 lines | 142 code | 42 blank | 86 comment | 18 complexity | 2d5448b68d95312f95c048fb278ad6a8 MD5 | raw file
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Data class for user location preferences
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Data
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  30. class Inbox extends Memcached_DataObject
  31. {
  32. const BOXCAR = 128;
  33. const MAX_NOTICES = 1024;
  34. ###START_AUTOCODE
  35. /* the code below is auto generated do not remove the above tag */
  36. public $__table = 'inbox'; // table name
  37. public $user_id; // int(4) primary_key not_null
  38. public $notice_ids; // blob
  39. /* Static get */
  40. function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Inbox',$k,$v); }
  41. /* the code above is auto generated do not remove the tag below */
  42. ###END_AUTOCODE
  43. function sequenceKey()
  44. {
  45. return array(false, false, false);
  46. }
  47. /**
  48. * Create a new inbox from existing Notice_inbox stuff
  49. */
  50. static function initialize($user_id)
  51. {
  52. $inbox = Inbox::fromNoticeInbox($user_id);
  53. unset($inbox->fake);
  54. $result = $inbox->insert();
  55. if (!$result) {
  56. common_log_db_error($inbox, 'INSERT', __FILE__);
  57. return null;
  58. }
  59. return $inbox;
  60. }
  61. static function fromNoticeInbox($user_id)
  62. {
  63. $ids = array();
  64. $ni = new Notice_inbox();
  65. $ni->user_id = $user_id;
  66. $ni->selectAdd();
  67. $ni->selectAdd('notice_id');
  68. $ni->orderBy('notice_id DESC');
  69. $ni->limit(0, self::MAX_NOTICES);
  70. if ($ni->find()) {
  71. while($ni->fetch()) {
  72. $ids[] = $ni->notice_id;
  73. }
  74. }
  75. $ni->free();
  76. unset($ni);
  77. $inbox = new Inbox();
  78. $inbox->user_id = $user_id;
  79. $inbox->pack($ids);
  80. $inbox->fake = true;
  81. return $inbox;
  82. }
  83. /**
  84. * Append the given notice to the given user's inbox.
  85. * Caching updates are managed for the inbox itself.
  86. *
  87. * If the notice is already in this inbox, the second
  88. * add will be silently dropped.
  89. *
  90. * @param int @user_id
  91. * @param int $notice_id
  92. * @return boolean success
  93. */
  94. static function insertNotice($user_id, $notice_id)
  95. {
  96. // Going straight to the DB rather than trusting our caching
  97. // during an update. Note: not using DB_DataObject::staticGet,
  98. // which is unsafe to use directly (in-process caching causes
  99. // memory leaks, which accumulate in queue processes).
  100. $inbox = new Inbox();
  101. if (!$inbox->get('user_id', $user_id)) {
  102. $inbox = Inbox::initialize($user_id);
  103. }
  104. if (empty($inbox)) {
  105. return false;
  106. }
  107. $ids = $inbox->unpack();
  108. if (in_array(intval($notice_id), $ids)) {
  109. // Already in there, we probably re-ran some inbox adds
  110. // due to an error. Skip the dupe silently.
  111. return true;
  112. }
  113. $result = $inbox->query(sprintf('UPDATE inbox '.
  114. 'set notice_ids = concat(cast(0x%08x as binary(4)), '.
  115. 'substr(notice_ids, 1, %d)) '.
  116. 'WHERE user_id = %d',
  117. $notice_id,
  118. 4 * (self::MAX_NOTICES - 1),
  119. $user_id));
  120. if ($result) {
  121. self::blow('inbox:user_id:%d', $user_id);
  122. }
  123. return $result;
  124. }
  125. static function bulkInsert($notice_id, $user_ids)
  126. {
  127. foreach ($user_ids as $user_id)
  128. {
  129. Inbox::insertNotice($user_id, $notice_id);
  130. }
  131. }
  132. function stream($user_id, $offset, $limit, $since_id, $max_id, $own=false)
  133. {
  134. $inbox = Inbox::staticGet('user_id', $user_id);
  135. if (empty($inbox)) {
  136. $inbox = Inbox::fromNoticeInbox($user_id);
  137. if (empty($inbox)) {
  138. return array();
  139. } else {
  140. $inbox->encache();
  141. }
  142. }
  143. $ids = $inbox->unpack();
  144. if (!empty($since_id)) {
  145. $newids = array();
  146. foreach ($ids as $id) {
  147. if ($id > $since_id) {
  148. $newids[] = $id;
  149. }
  150. }
  151. $ids = $newids;
  152. }
  153. if (!empty($max_id)) {
  154. $newids = array();
  155. foreach ($ids as $id) {
  156. if ($id <= $max_id) {
  157. $newids[] = $id;
  158. }
  159. }
  160. $ids = $newids;
  161. }
  162. $ids = array_slice($ids, $offset, $limit);
  163. return $ids;
  164. }
  165. /**
  166. * Wrapper for Inbox::stream() and Notice::getStreamByIds() returning
  167. * additional items up to the limit if we were short due to deleted
  168. * notices still being listed in the inbox.
  169. *
  170. * The fast path (when no items are deleted) should be just as fast; the
  171. * offset parameter is applied *before* lookups for maximum efficiency.
  172. *
  173. * This means offset-based paging may show duplicates, but similar behavior
  174. * already exists when new notices are posted between page views, so we
  175. * think people will be ok with this until id-based paging is introduced
  176. * to the user interface.
  177. *
  178. * @param int $user_id
  179. * @param int $offset skip past the most recent N notices (after since_id checks)
  180. * @param int $limit
  181. * @param mixed $since_id return only notices after but not including this id
  182. * @param mixed $max_id return only notices up to and including this id
  183. * @param mixed $own ignored?
  184. * @return array of Notice objects
  185. *
  186. * @todo consider repacking the inbox when this happens?
  187. * @fixme reimplement $own if we need it?
  188. */
  189. function streamNotices($user_id, $offset, $limit, $since_id, $max_id, $own=false)
  190. {
  191. $ids = self::stream($user_id, $offset, self::MAX_NOTICES, $since_id, $max_id, $own);
  192. // Do a bulk lookup for the first $limit items
  193. // Fast path when nothing's deleted.
  194. $firstChunk = array_slice($ids, 0, $limit);
  195. $notices = Notice::getStreamByIds($firstChunk);
  196. $wanted = count($firstChunk); // raw entry count in the inbox up to our $limit
  197. if ($notices->N >= $wanted) {
  198. return $notices;
  199. }
  200. // There were deleted notices, we'll need to look for more.
  201. assert($notices instanceof ArrayWrapper);
  202. $items = $notices->_items;
  203. $remainder = array_slice($ids, $limit);
  204. while (count($items) < $wanted && count($remainder) > 0) {
  205. $notice = Notice::staticGet(array_shift($remainder));
  206. if ($notice) {
  207. $items[] = $notice;
  208. } else {
  209. }
  210. }
  211. return new ArrayWrapper($items);
  212. }
  213. /**
  214. * Saves a list of integer notice_ids into a packed blob in this object.
  215. * @param array $ids list of integer notice_ids
  216. */
  217. protected function pack(array $ids)
  218. {
  219. $this->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids));
  220. }
  221. /**
  222. * @return array of integer notice_ids
  223. */
  224. protected function unpack()
  225. {
  226. return unpack('N*', $this->notice_ids);
  227. }
  228. }