User_greeting_count.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class for counting greetings
  18. *
  19. * @package GNU social
  20. * @author Brion Vibber <brionv@status.net>
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * We use the DB_DataObject framework for data classes in GNU social. Each
  28. * table maps to a particular data class, making it easier to manipulate
  29. * data.
  30. *
  31. * Data classes should extend Memcached_DataObject, the (slightly misnamed)
  32. * extension of DB_DataObject that provides caching, internationalization,
  33. * and other bits of good functionality to StatusNet-specific data classes.
  34. *
  35. * @category Action
  36. * @package GNU social
  37. * @author Evan Prodromou <evan@status.net>
  38. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. *
  41. * @see DB_DataObject
  42. */
  43. class User_greeting_count extends Managed_DataObject
  44. {
  45. public $__table = 'user_greeting_count'; // table name
  46. public $user_id; // int(4) primary_key not_null
  47. public $greeting_count; // int(4)
  48. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  49. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  50. public static function schemaDef()
  51. {
  52. return [
  53. 'fields' => [
  54. 'user_id' => ['type' => 'int', 'not null' => true, 'description' => 'user id'],
  55. 'greeting_count' => ['type' => 'int', 'not null' => true, 'description' => 'the greeting count'],
  56. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'],
  57. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  58. ],
  59. 'primary key' => ['user_id'],
  60. 'foreign keys' => [
  61. 'user_greeting_count_user_id_fkey' => ['user', ['user_id' => 'id']],
  62. ],
  63. ];
  64. }
  65. /**
  66. * Increment a user's greeting count and return instance
  67. *
  68. * This method handles the ins and outs of creating a new greeting_count for a
  69. * user or fetching the existing greeting count and incrementing its value.
  70. *
  71. * @param integer $user_id ID of the user to get a count for
  72. *
  73. * @return User_greeting_count instance for this user, with count already incremented.
  74. * @throws Exception
  75. */
  76. public static function inc($user_id)
  77. {
  78. $gc = User_greeting_count::getKV('user_id', $user_id);
  79. if (empty($gc)) {
  80. $gc = new User_greeting_count();
  81. $gc->user_id = $user_id;
  82. $gc->greeting_count = 1;
  83. $result = $gc->insert();
  84. if (!$result) {
  85. // TRANS: Exception thrown when the user greeting count could not be saved in the database.
  86. // TRANS: %d is a user ID (number).
  87. throw new Exception(sprintf(
  88. _m('Could not save new greeting count for %d.'),
  89. $user_id
  90. ));
  91. }
  92. } else {
  93. $orig = clone($gc);
  94. ++$gc->greeting_count;
  95. $result = $gc->update($orig);
  96. if (!$result) {
  97. // TRANS: Exception thrown when the user greeting count could not be saved in the database.
  98. // TRANS: %d is a user ID (number).
  99. throw new Exception(sprintf(
  100. _m('Could not increment greeting count for %d.'),
  101. $user_id
  102. ));
  103. }
  104. }
  105. return $gc;
  106. }
  107. }