favoritedslice.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * List of popular notices
  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 Public
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. class FavoritedSliceAction extends FavoritedAction
  34. {
  35. private $includeUsers = array(), $excludeUsers = array();
  36. /**
  37. * Take arguments for running
  38. *
  39. * @param array $args $_REQUEST args
  40. *
  41. * @return boolean success flag
  42. *
  43. * @todo move queries from showContent() to here
  44. */
  45. function prepare($args)
  46. {
  47. parent::prepare($args);
  48. $this->slice = $this->arg('slice', 'default');
  49. $data = array();
  50. if (Event::handle('SlicedFavoritesGetSettings', array($this->slice, &$data))) {
  51. // TRANS: Client exception.
  52. throw new ClientException(_m('Unknown favorites slice.'));
  53. }
  54. if (isset($data['include'])) {
  55. $this->includeUsers = $data['include'];
  56. }
  57. if (isset($data['exclude'])) {
  58. $this->excludeUsers = $data['exclude'];
  59. }
  60. return true;
  61. }
  62. /**
  63. * Content area
  64. *
  65. * Shows the list of popular notices
  66. *
  67. * @return void
  68. */
  69. function showContent()
  70. {
  71. $slice = $this->sliceWhereClause();
  72. if (!$slice) {
  73. return parent::showContent();
  74. }
  75. $weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
  76. $cutoff = sprintf("fave.modified > '%s'",
  77. common_sql_date(time() - common_config('popular', 'cutoff')));
  78. $qry = 'SELECT notice.*, '.
  79. $weightexpr . ' as weight ' .
  80. 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
  81. "WHERE $cutoff AND $slice " .
  82. 'GROUP BY id,profile_id,uri,content,rendered,url,created,notice.modified,reply_to,is_local,source,notice.conversation ' .
  83. 'ORDER BY weight DESC';
  84. $offset = ($this->page - 1) * NOTICES_PER_PAGE;
  85. $limit = NOTICES_PER_PAGE + 1;
  86. if (common_config('db', 'type') == 'pgsql') {
  87. $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
  88. } else {
  89. $qry .= ' LIMIT ' . $offset . ', ' . $limit;
  90. }
  91. $notice = Memcached_DataObject::cachedQuery('Notice',
  92. $qry,
  93. 600);
  94. $nl = new NoticeList($notice, $this);
  95. $cnt = $nl->show();
  96. if ($cnt == 0) {
  97. $this->showEmptyList();
  98. }
  99. $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
  100. $this->page, 'favorited');
  101. }
  102. private function sliceWhereClause()
  103. {
  104. $include = $this->nicknamesToIds($this->includeUsers);
  105. $exclude = $this->nicknamesToIds($this->excludeUsers);
  106. if (count($include) == 1) {
  107. return "profile_id = " . intval($include[0]);
  108. } else if (count($include) > 1) {
  109. return "profile_id IN (" . implode(',', $include) . ")";
  110. } else if (count($exclude) == 1) {
  111. return "profile_id != " . intval($exclude[0]);
  112. } else if (count($exclude) > 1) {
  113. return "profile_id NOT IN (" . implode(',', $exclude) . ")";
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. *
  120. * @param array $nicks array of user nicknames
  121. * @return array of profile/user IDs
  122. */
  123. private function nicknamesToIds($nicks)
  124. {
  125. $ids = array();
  126. foreach ($nicks as $nick) {
  127. // not the most efficient way for a big list!
  128. $user = User::getKV('nickname', $nick);
  129. if ($user) {
  130. $ids[] = intval($user->id);
  131. }
  132. }
  133. return $ids;
  134. }
  135. }