PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/reindex.php

https://github.com/karpenoktem/punbb
PHP | 267 lines | 195 code | 49 blank | 23 comment | 20 complexity | c248ca1293b08adc96260cdb9694fe27 MD5 | raw file
  1. <?php
  2. /**
  3. * Search index rebuilding script.
  4. *
  5. * Allows administrators to rebuild the index used to search the posts and topics.
  6. *
  7. * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
  8. * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  9. * @package PunBB
  10. */
  11. if (!defined('FORUM_ROOT'))
  12. define('FORUM_ROOT', '../');
  13. // Tell common.php that we don't want output buffering
  14. define('FORUM_DISABLE_BUFFERING', 1);
  15. require FORUM_ROOT.'include/common.php';
  16. require FORUM_ROOT.'include/common_admin.php';
  17. ($hook = get_hook('ari_start')) ? eval($hook) : null;
  18. if ($forum_user['g_id'] != FORUM_ADMIN)
  19. message($lang_common['No permission']);
  20. // Load the admin.php language file
  21. require FORUM_ROOT.'lang/'.$forum_user['language'].'/admin_common.php';
  22. require FORUM_ROOT.'lang/'.$forum_user['language'].'/admin_reindex.php';
  23. if (isset($_GET['i_per_page']) && isset($_GET['i_start_at']))
  24. {
  25. $per_page = intval($_GET['i_per_page']);
  26. $start_at = intval($_GET['i_start_at']);
  27. if ($per_page < 1 || $start_at < 1)
  28. message($lang_common['Bad request']);
  29. // We validate the CSRF token. If it's set in POST and we're at this point, the token is valid.
  30. // If it's in GET, we need to make sure it's valid.
  31. if (!isset($_POST['csrf_token']) && (!isset($_GET['csrf_token']) || $_GET['csrf_token'] !== generate_form_token('reindex'.$forum_user['id'])))
  32. csrf_confirm_form();
  33. ($hook = get_hook('ari_cycle_start')) ? eval($hook) : null;
  34. @set_time_limit(0);
  35. // If this is the first cycle of posts we empty the search index before we proceed
  36. if (isset($_GET['i_empty_index']))
  37. {
  38. $query = array(
  39. 'DELETE' => 'search_matches'
  40. );
  41. ($hook = get_hook('ari_cycle_qr_empty_search_matches')) ? eval($hook) : null;
  42. $forum_db->query_build($query) or error(__FILE__, __LINE__);
  43. $query = array(
  44. 'DELETE' => 'search_words'
  45. );
  46. ($hook = get_hook('ari_cycle_qr_empty_search_words')) ? eval($hook) : null;
  47. $forum_db->query_build($query) or error(__FILE__, __LINE__);
  48. // Reset the sequence for the search words (not needed for SQLite)
  49. switch ($db_type)
  50. {
  51. case 'mysql':
  52. case 'mysql_innodb':
  53. case 'mysqli':
  54. case 'mysqli_innodb':
  55. $result = $forum_db->query('ALTER TABLE '.$forum_db->prefix.'search_words auto_increment=1') or error(__FILE__, __LINE__);
  56. break;
  57. case 'pgsql';
  58. $result = $forum_db->query('SELECT setval(\''.$forum_db->prefix.'search_words_id_seq\', 1, false)') or error(__FILE__, __LINE__);
  59. }
  60. }
  61. // Setup breadcrumbs
  62. $forum_page['crumbs'] = array(
  63. array($forum_config['o_board_title'], forum_link($forum_url['index'])),
  64. array($lang_admin_common['Forum administration'], forum_link($forum_url['admin_index'])),
  65. array($lang_admin_common['Management'], forum_link($forum_url['admin_reports'])),
  66. $lang_admin_reindex['Rebuilding index title']
  67. );
  68. ?>
  69. <!DOCTYPE html>
  70. <html lang="<?php $lang_common['lang_identifier'] ?>" dir="<?php echo $lang_common['lang_direction'] ?>">
  71. <head>
  72. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  73. <title><?php echo generate_crumbs(true) ?></title>
  74. <style type="text/css">
  75. body {
  76. font: 68.75% Verdana, Arial, Helvetica, sans-serif;
  77. color: #333333;
  78. background-color: #FFFFFF
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <p><?php echo $lang_admin_reindex['Rebuilding index'] ?></p>
  84. <?php
  85. if (!defined('FORUM_SEARCH_IDX_FUNCTIONS_LOADED'))
  86. require FORUM_ROOT.'include/search_idx.php';
  87. // Fetch posts to process
  88. $query = array(
  89. 'SELECT' => 'p.id, p.message, t.id, t.subject, t.first_post_id',
  90. 'FROM' => 'posts AS p',
  91. 'JOINS' => array(
  92. array(
  93. 'INNER JOIN' => 'topics AS t',
  94. 'ON' => 't.id=p.topic_id'
  95. )
  96. ),
  97. 'WHERE' => 'p.id >= '.$start_at,
  98. 'ORDER BY' => 'p.id',
  99. 'LIMIT' => $per_page
  100. );
  101. ($hook = get_hook('ari_cycle_qr_fetch_posts')) ? eval($hook) : null;
  102. $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
  103. $post_id = 0;
  104. echo '<p>';
  105. while ($cur_post = $forum_db->fetch_row($result))
  106. {
  107. echo sprintf($lang_admin_reindex['Processing post'], $cur_post[0], $cur_post[2]).'<br />'."\n";
  108. if ($cur_post[0] == $cur_post[4]) // This is the "topic post" so we have to index the subject as well
  109. update_search_index('post', $cur_post[0], $cur_post[1], $cur_post[3]);
  110. else
  111. update_search_index('post', $cur_post[0], $cur_post[1]);
  112. $post_id = $cur_post[0];
  113. }
  114. echo '</p>';
  115. // Check if there is more work to do
  116. $query = array(
  117. 'SELECT' => 'p.id',
  118. 'FROM' => 'posts AS p',
  119. 'WHERE' => 'p.id > '.$post_id,
  120. 'ORDER BY' => 'p.id',
  121. 'LIMIT' => '1'
  122. );
  123. ($hook = get_hook('ari_cycle_qr_find_next_post')) ? eval($hook) : null;
  124. $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
  125. $next_posts_to_proced = $forum_db->result($result);
  126. $query_str = '';
  127. if (!is_null($next_posts_to_proced) && $next_posts_to_proced !== false)
  128. {
  129. $query_str = '?i_per_page='.$per_page.'&i_start_at='.$next_posts_to_proced.'&csrf_token='.generate_form_token('reindex'.$forum_user['id']);
  130. }
  131. ($hook = get_hook('ari_cycle_end')) ? eval($hook) : null;
  132. $forum_db->end_transaction();
  133. $forum_db->close();
  134. exit('<script type="text/javascript">window.location="'.forum_link($forum_url['admin_reindex']).$query_str.'"</script><br />'.$lang_admin_reindex['Javascript redirect'].' <a href="'.forum_link($forum_url['admin_reindex']).$query_str.'">'.$lang_admin_reindex['Click to continue'].'</a>.');
  135. }
  136. // Get the first post ID from the db
  137. $query = array(
  138. 'SELECT' => 'p.id',
  139. 'FROM' => 'posts AS p',
  140. 'ORDER BY' => 'p.id',
  141. 'LIMIT' => '1'
  142. );
  143. ($hook = get_hook('ari_qr_find_lowest_post_id')) ? eval($hook) : null;
  144. $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
  145. $first_id = $forum_db->result($result);
  146. if (is_null($first_id) || $first_id === false)
  147. {
  148. unset($first_id);
  149. }
  150. // Setup form
  151. $forum_page['group_count'] = $forum_page['item_count'] = $forum_page['fld_count'] = 0;
  152. // Setup breadcrumbs
  153. $forum_page['crumbs'] = array(
  154. array($forum_config['o_board_title'], forum_link($forum_url['index'])),
  155. array($lang_admin_common['Forum administration'], forum_link($forum_url['admin_index'])),
  156. array($lang_admin_common['Management'], forum_link($forum_url['admin_reports'])),
  157. array($lang_admin_common['Rebuild index'], forum_link($forum_url['admin_reindex']))
  158. );
  159. ($hook = get_hook('ari_pre_header_load')) ? eval($hook) : null;
  160. define('FORUM_PAGE_SECTION', 'management');
  161. define('FORUM_PAGE', 'admin-reindex');
  162. require FORUM_ROOT.'header.php';
  163. // START SUBST - <!-- forum_main -->
  164. ob_start();
  165. ($hook = get_hook('ari_main_output_start')) ? eval($hook) : null;
  166. ?>
  167. <div class="main-subhead">
  168. <h2 class="hn"><span><?php echo $lang_admin_reindex['Reindex heading'] ?></span></h2>
  169. </div>
  170. <div class="main-content main-frm">
  171. <div class="ct-box warn-box">
  172. <p><?php echo $lang_admin_reindex['Reindex info'] ?></p>
  173. <p class="important"><?php echo $lang_admin_reindex['Reindex warning'] ?></p>
  174. <p class="warn"><?php echo $lang_admin_reindex['Empty index warning'] ?></p>
  175. </div>
  176. <form class="frm-form" method="get" accept-charset="utf-8" action="<?php echo forum_link($forum_url['admin_reindex']) ?>">
  177. <div class="hidden">
  178. <input type="hidden" name="csrf_token" value="<?php echo generate_form_token('reindex'.$forum_user['id']) ?>" />
  179. </div>
  180. <?php ($hook = get_hook('ari_pre_rebuild_fieldset')) ? eval($hook) : null; ?>
  181. <fieldset class="frm-group group<?php echo ++$forum_page['group_count'] ?>">
  182. <legend class="group-legend"><span><?php echo $lang_admin_reindex['Rebuild index legend'] ?></span></legend>
  183. <?php ($hook = get_hook('ari_pre_rebuild_per_page')) ? eval($hook) : null; ?>
  184. <div class="sf-set set<?php echo ++$forum_page['item_count'] ?>">
  185. <div class="sf-box text">
  186. <label for="fld<?php echo ++$forum_page['fld_count'] ?>"><span><?php echo $lang_admin_reindex['Posts per cycle'] ?></span> <small><?php echo $lang_admin_reindex['Posts per cycle info'] ?></small></label><br />
  187. <span class="fld-input"><input type="number" id="fld<?php echo $forum_page['fld_count'] ?>" name="i_per_page" size="7" maxlength="7" value="100" /></span>
  188. </div>
  189. </div>
  190. <?php ($hook = get_hook('ari_pre_rebuild_start_post')) ? eval($hook) : null; ?>
  191. <div class="sf-set set<?php echo ++$forum_page['item_count'] ?>">
  192. <div class="sf-box text">
  193. <label for="fld<?php echo ++$forum_page['fld_count'] ?>"><span class="fld-label"><?php echo $lang_admin_reindex['Starting post'] ?></span> <small><?php echo $lang_admin_reindex['Starting post info'] ?></small></label><br />
  194. <span class="fld-input"><input type="number" id="fld<?php echo $forum_page['fld_count'] ?>" name="i_start_at" size="7" maxlength="7" value="<?php echo (isset($first_id)) ? $first_id : 0 ?>" /></span>
  195. </div>
  196. </div>
  197. <?php ($hook = get_hook('ari_pre_rebuild_empty_index')) ? eval($hook) : null; ?>
  198. <div class="sf-set set<?php echo ++$forum_page['item_count'] ?>">
  199. <div class="sf-box checkbox">
  200. <span class="fld-input"><input type="checkbox" id="fld<?php echo ++$forum_page['fld_count'] ?>" name="i_empty_index" value="1" checked="checked" /></span>
  201. <label for="fld<?php echo $forum_page['fld_count'] ?>"><?php echo $lang_admin_reindex['Empty index'] ?></label>
  202. </div>
  203. </div>
  204. <?php ($hook = get_hook('ari_pre_rebuild_fieldset_end')) ? eval($hook) : null; ?>
  205. </fieldset>
  206. <?php ($hook = get_hook('ari_rebuild_fieldset_end')) ? eval($hook) : null; ?>
  207. <div class="frm-buttons">
  208. <span class="submit primary"><input type="submit" name="rebuild_index" value="<?php echo $lang_admin_reindex['Rebuild index'] ?>" /></span>
  209. </div>
  210. </form>
  211. </div>
  212. <?php
  213. ($hook = get_hook('ari_end')) ? eval($hook) : null;
  214. $tpl_temp = forum_trim(ob_get_contents());
  215. $tpl_main = str_replace('<!-- forum_main -->', $tpl_temp, $tpl_main);
  216. ob_end_clean();
  217. // END SUBST - <!-- forum_main -->
  218. require FORUM_ROOT.'footer.php';