Made some text changes.
[fluxbb:fluxbb.git] / upload / admin_maintenance.php
1 <?php
2 /***********************************************************************
3
4   Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
5
6   This file is part of PunBB.
7
8   PunBB is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PunBB is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21   MA  02111-1307  USA
22
23 ************************************************************************/
24
25
26 // Tell header.php to use the admin template
27 define('PUN_ADMIN_CONSOLE', 1);
28 // Tell common.php that we don't want output buffering
29 define('PUN_DISABLE_BUFFERING', 1);
30
31 define('PUN_ROOT', './');
32 require PUN_ROOT.'include/common.php';
33 require PUN_ROOT.'include/common_admin.php';
34
35
36 if ($pun_user['g_id'] != PUN_ADMIN)
37         message($lang_common['No permission']);
38
39
40 if (isset($_GET['i_per_page']) && isset($_GET['i_start_at']))
41 {
42         $per_page = intval($_GET['i_per_page']);
43         $start_at = intval($_GET['i_start_at']);
44         if ($per_page < 1 || $start_at < 1)
45                 message($lang_common['Bad request']);
46
47         @set_time_limit(0);
48
49         // If this is the first cycle of posts we empty the search index before we proceed
50         if (isset($_GET['i_empty_index']))
51         {
52                 // This is the only potentially "dangerous" thing we can do here, so we check the referer
53                 confirm_referrer('admin_maintenance.php');
54
55                 $truncate_sql = ($db_type != 'sqlite' && $db_type != 'pgsql') ? 'TRUNCATE TABLE ' : 'DELETE FROM ';
56                 $db->query($truncate_sql.$db->prefix.'search_matches') or error('Unable to empty search index match table', __FILE__, __LINE__, $db->error());
57                 $db->query($truncate_sql.$db->prefix.'search_words') or error('Unable to empty search index words table', __FILE__, __LINE__, $db->error());
58
59                 // Reset the sequence for the search words (not needed for SQLite)
60                 switch ($db_type)
61                 {
62                         case 'mysql':
63                         case 'mysqli':
64                         case 'mysql_innodb':
65                         case 'mysqli_innodb':
66                                 $result = $db->query('ALTER TABLE '.$db->prefix.'search_words auto_increment=1') or error('Unable to update table auto_increment', __FILE__, __LINE__, $db->error());
67                                 break;
68
69                         case 'pgsql';
70                                 $result = $db->query('SELECT setval(\''.$db->prefix.'search_words_id_seq\', 1, false)') or error('Unable to update sequence', __FILE__, __LINE__, $db->error());
71                 }
72         }
73
74 ?>
75 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
76
77 <html>
78 <head>
79 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
80 <title><?php echo pun_htmlspecialchars($pun_config['o_board_title']) ?> / Rebuilding search index &hellip;</title>
81 <style type="text/css">
82 body {
83         font: 10px Verdana, Arial, Helvetica, sans-serif;
84         color: #333333;
85         background-color: #FFFFFF
86 }
87 </style>
88 </head>
89 <body>
90
91 Rebuilding index &hellip; This might be a good time to put on some coffee :-)<br /><br />
92
93 <?php
94
95         require PUN_ROOT.'include/search_idx.php';
96
97         // Fetch posts to process
98         $result = $db->query('SELECT id FROM '.$db->prefix.'topics WHERE id>='.$start_at.' ORDER BY id LIMIT '.$per_page) or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
99         $topics = array();
100         while ($cur_topic = $db->fetch_row($result))
101                 $topics[] = $cur_topic[0];
102
103         $result = $db->query('SELECT topic_id, id, message FROM '.$db->prefix.'posts WHERE topic_id IN ('.implode(',', $topics).') ORDER BY topic_id') or error('Unable to fetch topic/post info', __FILE__, __LINE__, $db->error());
104
105         $cur_topic = 0;
106         while ($cur_post = $db->fetch_row($result))
107         {
108                 if ($cur_post[0] <> $cur_topic)
109                 {
110                         // Fetch subject and ID of first post in topic
111                         $result2 = $db->query('SELECT p.id, t.subject, MIN(p.posted) AS first FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.id='.$cur_post[0].' GROUP BY p.id, t.subject ORDER BY first LIMIT 1') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
112                         list($first_post, $subject) = $db->fetch_row($result2);
113
114                         $cur_topic = $cur_post[0];
115                 }
116
117                 echo 'Processing post <strong>'.$cur_post[1].'</strong> in topic <strong>'.$cur_post[0].'</strong><br />'."\n";
118
119                 if ($cur_post[1] == $first_post)        // This is the "topic post" so we have to index the subject as well
120                         update_search_index('post', $cur_post[1], $cur_post[2], $subject);
121                 else
122                         update_search_index('post', $cur_post[1], $cur_post[2]);
123         }
124
125         // Check if there is more work to do
126         $result = $db->query('SELECT id FROM '.$db->prefix.'topics WHERE id>'.$cur_topic.' ORDER BY id ASC LIMIT 1') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
127
128         $query_str = ($db->num_rows($result)) ? '?i_per_page='.$per_page.'&i_start_at='.$db->result($result) : '';
129
130         $db->end_transaction();
131         $db->close();
132
133         exit('<script type="text/javascript">window.location="admin_maintenance.php'.$query_str.'"</script><br />JavaScript redirect unsuccessful. Click <a href="admin_maintenance.php'.$query_str.'">here</a> to continue.');
134 }
135
136
137 // Get the first post ID from the db
138 $result = $db->query('SELECT id FROM '.$db->prefix.'topics ORDER BY id LIMIT 1') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
139 if ($db->num_rows($result))
140         $first_id = $db->result($result);
141
142 $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / Admin / Maintenance';
143 require PUN_ROOT.'header.php';
144
145 generate_admin_menu('maintenance');
146
147 ?>
148         <div class="blockform">
149                 <h2><span>Forum Maintenance</span></h2>
150                 <div class="box">
151                         <form method="get" action="admin_maintenance.php">
152                                 <div class="inform">
153                                         <fieldset>
154                                                 <legend>Rebuild search index</legend>
155                                                 <div class="infldset">
156                                                         <p>If you've added, edited or removed posts manually in the database or if you're having problems searching, you should rebuild the search index. For best performance you should put the forum in maintenance mode during rebuilding. <strong>Rebuilding the search index can take a long time and will increase server load during the rebuild process!</strong></p>
157                                                         <table class="aligntop" cellspacing="0">
158                                                                 <tr>
159                                                                         <th scope="row">Topics per cycle</th>
160                                                                         <td>
161                                                                                 <input type="text" name="i_per_page" size="7" maxlength="7" value="100" tabindex="1" />
162                                                                                 <span>The number of topics to process per pageview. E.g. if you were to enter 100, one hundred topics would be processed and then the page would refresh. This is to prevent the script from timing out during the rebuild process.</span>
163                                                                         </td>
164                                                                 </tr>
165                                                                 <tr>
166                                                                         <th scope="row">Starting topic ID</th>
167                                                                         <td>
168                                                                                 <input type="text" name="i_start_at" size="7" maxlength="7" value="<?php echo (isset($first_id)) ? $first_id : 0 ?>" tabindex="2" />
169                                                                                 <span>The topic ID to start rebuilding at. The default value is the first available ID in the database. Normally you wouldn't want to change this.</span>
170                                                                         </td>
171                                                                 </tr>
172                                                                 <tr>
173                                                                         <th scope="row">Empty index</th>
174                                                                         <td class="inputadmin">
175                                                                                 <span><input type="checkbox" name="i_empty_index" value="1" tabindex="3" checked="checked" />&nbsp;&nbsp;Select this if you want the search index to be emptied before rebuilding (see below).</span>
176                                                                         </td>
177                                                                 </tr>
178                                                         </table>
179                                                         <p class="topspace">Once the process has completed you will be redirected back to this page. It is highly recommended that you have JavaScript enabled in your browser during rebuilding (for automatic redirect when a cycle has completed). If you are forced to abort the rebuild process, make a note of the last processed topic ID and enter that ID+1 in "Starting Topic ID" when/if you want to continue ("Empty index" must not be selected).</p>
180                                                         <div class="fsetsubmit"><input type="submit" name="rebuild_index" value="Rebuild index" tabindex="4" /></div>
181                                                 </div>
182                                         </fieldset>
183                                 </div>
184                         </form>
185                 </div>
186         </div>
187         <div class="clearer"></div>
188 </div>
189 <?php
190
191 require PUN_ROOT.'footer.php';