Status_network_tag.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, 2010 StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) { exit(1); }
  20. class Status_network_tag extends Safe_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'status_network_tag'; // table name
  25. public $site_id; // int(4) primary_key not_null
  26. public $tag; // varchar(64) primary_key not_null
  27. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  28. function __construct()
  29. {
  30. global $config;
  31. global $_DB_DATAOBJECT;
  32. $sn = new Status_network();
  33. $sn->_connect();
  34. $config['db']['table_'. $this->tableName()] = $sn->_database;
  35. $this->_connect();
  36. }
  37. /* the code above is auto generated do not remove the tag below */
  38. ###END_AUTOCODE
  39. /* Static get */
  40. static function getKV($k,$v=null)
  41. {
  42. // TODO: This probably has to be converted to a non-static call
  43. $i = DB_DataObject::staticGet('Status_network_tag',$k,$v);
  44. // Don't use local process cache; if we're fetching multiple
  45. // times it's because we're reloading it in a long-running
  46. // process; we need a fresh copy!
  47. global $_DB_DATAOBJECT;
  48. unset($_DB_DATAOBJECT['CACHE']['status_network_tag']);
  49. return $i;
  50. }
  51. static function pkeyGet($kv)
  52. {
  53. return Memcached_DataObject::pkeyGetClass('Status_network_tag', $kv);
  54. }
  55. /**
  56. * Fetch the (possibly cached) tag entries for the given site id.
  57. * Uses status_network's cache settings.
  58. *
  59. * @param string $site_id
  60. * @return array of strings
  61. */
  62. static function getTags($site_id)
  63. {
  64. $key = 'status_network_tags:' . $site_id;
  65. if (Status_network::$cache) {
  66. $packed = Status_network::$cache->get($key);
  67. if (is_string($packed)) {
  68. if ($packed == '') {
  69. return array();
  70. } else {
  71. return explode('|', $packed);
  72. }
  73. }
  74. }
  75. $result = array();
  76. $tags = new Status_network_tag();
  77. $tags->site_id = $site_id;
  78. if ($tags->find()) {
  79. while ($tags->fetch()) {
  80. $result[] = $tags->tag;
  81. }
  82. }
  83. if (Status_network::$cache) {
  84. $packed = implode('|', $result);
  85. Status_network::$cache->set($key, $packed, 0, 3600);
  86. }
  87. return $result;
  88. }
  89. /**
  90. * Drop the cached tag entries for this site.
  91. * Needed after inserting/deleting a tag entry.
  92. */
  93. function decache()
  94. {
  95. $key = 'status_network_tags:' . $this->site_id;
  96. if (Status_network::$cache || Status_network::$cacheInitialized) {
  97. // FIXME: this was causing errors, so I'm hiding them.
  98. // I'm a big chicken and lazy.
  99. @Status_network::$cache->delete($key);
  100. }
  101. }
  102. function insert()
  103. {
  104. $ret = parent::insert();
  105. $this->decache();
  106. return $ret;
  107. }
  108. function delete($useWhere=false)
  109. {
  110. $this->decache();
  111. return parent::delete($useWhere);
  112. }
  113. static function withTag($tag)
  114. {
  115. $snt = new Status_network_tag();
  116. $snt->tag = $tag;
  117. $snt->find();
  118. return $snt;
  119. }
  120. }