Subs-Recent.php 4.1 KB

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