User_greeting_count.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Data class for counting greetings
  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) 2009, 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 counting greetings
  35. *
  36. * We use the DB_DataObject framework for data classes in StatusNet. Each
  37. * table maps to a particular data class, making it easier to manipulate
  38. * data.
  39. *
  40. * Data classes should extend Memcached_DataObject, the (slightly misnamed)
  41. * extension of DB_DataObject that provides caching, internationalization,
  42. * and other bits of good functionality to StatusNet-specific data classes.
  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 User_greeting_count extends Managed_DataObject
  53. {
  54. public $__table = 'user_greeting_count'; // table name
  55. public $user_id; // int(4) primary_key not_null
  56. public $greeting_count; // int(4)
  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. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user id'),
  64. 'greeting_count' => array('type' => 'int', 'not null' => true, 'description' => 'the greeting count'),
  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('user_id'),
  69. 'foreign keys' => array(
  70. 'user_greeting_count_user_id_fkey' => array('user', array('user_id' => 'id')),
  71. ),
  72. );
  73. }
  74. /**
  75. * Increment a user's greeting count and return instance
  76. *
  77. * This method handles the ins and outs of creating a new greeting_count for a
  78. * user or fetching the existing greeting count and incrementing its value.
  79. *
  80. * @param integer $user_id ID of the user to get a count for
  81. *
  82. * @return User_greeting_count instance for this user, with count already incremented.
  83. */
  84. static function inc($user_id)
  85. {
  86. $gc = User_greeting_count::getKV('user_id', $user_id);
  87. if (empty($gc)) {
  88. $gc = new User_greeting_count();
  89. $gc->user_id = $user_id;
  90. $gc->greeting_count = 1;
  91. $result = $gc->insert();
  92. if (!$result) {
  93. // TRANS: Exception thrown when the user greeting count could not be saved in the database.
  94. // TRANS: %d is a user ID (number).
  95. throw new Exception(sprintf(_m('Could not save new greeting count for %d.'),
  96. $user_id));
  97. }
  98. } else {
  99. $orig = clone($gc);
  100. $gc->greeting_count++;
  101. $result = $gc->update($orig);
  102. if (!$result) {
  103. // TRANS: Exception thrown when the user greeting count could not be saved in the database.
  104. // TRANS: %d is a user ID (number).
  105. throw new Exception(sprintf(_m('Could not increment greeting count for %d.'),
  106. $user_id));
  107. }
  108. }
  109. return $gc;
  110. }
  111. }