Subs-Recent.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file contains a couple of functions for the latests posts on forum.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2013 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Get the latest posts of a forum.
  18. *
  19. * @param array $latestPostOptions
  20. * @return array
  21. */
  22. function getLastPosts($latestPostOptions)
  23. {
  24. global $scripturl, $txt, $user_info, $modSettings, $smcFunc, $context;
  25. // Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessable...)
  26. // @todo SLOW This query is now slow, NEEDS to be fixed. Maybe break into two?
  27. $request = $smcFunc['db_query']('substring', '
  28. SELECT
  29. m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg,
  30. IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name,
  31. SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled
  32. FROM {db_prefix}messages AS m
  33. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  34. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  35. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  36. WHERE m.id_msg >= {int:likely_max_msg}' .
  37. (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  38. AND b.id_board != {int:recycle_board}' : '') . '
  39. AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  40. AND t.approved = {int:is_approved}
  41. AND m.approved = {int:is_approved}' : '') . '
  42. ORDER BY m.id_msg DESC
  43. LIMIT ' . $latestPostOptions['number_posts'],
  44. array(
  45. 'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']),
  46. 'recycle_board' => $modSettings['recycle_board'],
  47. 'is_approved' => 1,
  48. )
  49. );
  50. $posts = array();
  51. while ($row = $smcFunc['db_fetch_assoc']($request))
  52. {
  53. // Censor the subject and post for the preview ;).
  54. censorText($row['subject']);
  55. censorText($row['body']);
  56. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
  57. if ($smcFunc['strlen']($row['body']) > 128)
  58. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  59. // Build the array.
  60. $posts[] = array(
  61. 'board' => array(
  62. 'id' => $row['id_board'],
  63. 'name' => $row['board_name'],
  64. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  65. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
  66. ),
  67. 'topic' => $row['id_topic'],
  68. 'poster' => array(
  69. 'id' => $row['id_member'],
  70. 'name' => $row['poster_name'],
  71. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  72. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  73. ),
  74. 'subject' => $row['subject'],
  75. 'short_subject' => shorten_subject($row['subject'], 24),
  76. 'preview' => $row['body'],
  77. 'time' => timeformat($row['poster_time']),
  78. 'timestamp' => forum_time(true, $row['poster_time']),
  79. 'raw_timestamp' => $row['poster_time'],
  80. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
  81. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>'
  82. );
  83. }
  84. $smcFunc['db_free_result']($request);
  85. return $posts;
  86. }
  87. /**
  88. * Callback-function for the cache for getLastPosts().
  89. *
  90. * @param array $latestPostOptions
  91. */
  92. function cache_getLastPosts($latestPostOptions)
  93. {
  94. return array(
  95. 'data' => getLastPosts($latestPostOptions),
  96. 'expires' => time() + 60,
  97. 'post_retri_eval' => '
  98. foreach ($cache_block[\'data\'] as $k => $post)
  99. {
  100. $cache_block[\'data\'][$k][\'time\'] = timeformat($post[\'raw_timestamp\']);
  101. $cache_block[\'data\'][$k][\'timestamp\'] = forum_time(true, $post[\'raw_timestamp\']);
  102. }',
  103. );
  104. }
  105. ?>