aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/adm/admin_search.php
blob: 70f4d752a2f0bbdcd0c366f0cd849a734febb494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/***************************************************************************
 *                             admin_search.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id$
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

if (!empty($setmodules))
{
	if (!$auth->acl_get('a_search'))
	{
		return;
	}

	$module['DB']['SEARCH_INDEX'] = basename(__FILE__) . $SID;

	return;
}

define('IN_PHPBB', 1);
// Include files
$phpbb_root_path = '../';
require($phpbb_root_path . 'extension.inc');
require('pagestart.' . $phpEx);
include($phpbb_root_path . 'includes/functions_posting.'.$phpEx);

// Check permissions
if (!$auth->acl_get('a_search'))
{
	trigger_error($user->lang['NO_ADMIN']);
}

// Start indexing
if (isset($_POST['start']) || isset($_GET['batchstart']))
{
	$batchsize = 200; // Process this many posts per batch
	$batchstart = (!isset($_GET['batchstart'])) ? $row['min_post_id'] : $_GET['batchstart'];
	$batchcount = (!isset($_GET['batchcount'])) ? 1 : $_GET['batchcount'];
	$loopcount = 0;
	$batchend = $batchstart + $batchsize;

	// Search re-indexing is tough on the server ... so we'll check the load
	// each loop and if we're on a 1min load of 3 or more we'll re-load the page
	// and try again. No idea how well this will work in practice so we'll see ...
	if (file_exists('/proc/loadavg'))
	{
		if ($load = @file('/proc/loadavg'))
		{
			list($load) = explode(' ', $load[0]);

			if ($load > 3)
			{
				redirect("admin_search.$phpEx$SID&batchstart=$batchstart&batchcount=$batch_count");
			}
		}
	}

	// Try and load stopword and synonym files
	$stopword_array = array();
	$synonym_array = array();

	$dir = opendir($phpbb_root_path . 'language/');
	while ($file = readdir($dir))
	{
		if (preg_match('#^lang_#', $file) && !is_file($phpbb_root_path . 'language/' . $file) && !is_link($phpbb_root_path . 'language/' . $file))
		{
			unset($tmp_array);
			$tmp_array = @file($phpbb_root_path . 'language/' . $file . '/search_stopwords.txt');
			if (is_array($tmp_array))
			{
				$stopword_array = array_unique(array_merge($stopword_array, $tmp_array));
			}

			unset($tmp_array);
			$tmp_array = @file($phpbb_root_path . 'language/' . $file . '/search_synonyms.txt');
			if (is_array($tmp_array))
			{
				$synonym_array = array_unique(array_merge($synonym_array, $tmp_array));
			}
		}
	}

	closedir($dir);

	if (!isset($_GET['batchstart']))
	{
		// Take board offline
		set_config('board_disable', 1);

		// Empty existing tables
		$db->sql_query("TRUNCATE " . SEARCH_TABLE);
		$db->sql_query("TRUNCATE " . SEARCH_WORD_TABLE);
		$db->sql_query("TRUNCATE " . SEARCH_MATCH_TABLE);
	}

	// Fetch a batch of posts_text entries
	$sql = "SELECT COUNT(*) AS total, MAX(post_id) AS max_post_id, MIN(post_id) AS min_post_id
		FROM " . POSTS_TEXT_TABLE;
	$result = $db->sql_query($sql);

	$row = $db->sql_fetchrow($result);
	$totalposts = $row['total'];
	$max_post_id = $row['max_post_id'];

	$db->sql_freeresult($result);

	$sql = "SELECT *
		FROM " . POSTS_TEXT_TABLE . "
		WHERE post_id
			BETWEEN $batchstart
				AND $batchend";
	$result = $db->sql_query($sql);

	if ($row = $db->sql_fetchrow($result))
	{
		do
		{
			$post_id = $row['post_id'];

			$search_raw_words = array();
			$search_raw_words['text'] = split_words(clean_words('post', $row['post_text'], $stopword_array, $synonym_array));
			$search_raw_words['title'] = split_words(clean_words('post', $row['post_subject'], $stopword_array, $synonym_array));

			$word = array();
			$word_insert_sql = array();
			foreach ($search_raw_words as $word_in => $search_matches)
			{
				$word_insert_sql[$word_in] = '';
				if (!empty($search_matches))
				{
					for ($i = 0; $i < count($search_matches); $i++)
					{
						$search_matches[$i] = trim($search_matches[$i]);

						if ($search_matches[$i] != '')
						{
							$word[] = $search_matches[$i];
							$word_insert_sql[$word_in] .= ($word_insert_sql[$word_in] != '') ? ", '" . $search_matches[$i] . "'" : "'" . $search_matches[$i] . "'";
						}
					}
				}
			}

			if (count($word))
			{
				$word_text_sql = '';
				$word = array_unique($word);

				for($i = 0; $i < count($word); $i++)
				{
					$word_text_sql .= (($word_text_sql != '') ? ', ' : '') . "'" . $word[$i] . "'";
				}

				$check_words = array();
				switch(SQL_LAYER)
				{
					case 'postgresql':
					case 'msaccess':
					case 'mssql-odbc':
					case 'oracle':
					case 'db2':
						$sql = "SELECT word_id, word_text
							FROM " . SEARCH_WORD_TABLE . "
							WHERE word_text IN ($word_text_sql)";
						$result = $db->sql_query($sql);

						while ($row = $db->sql_fetchrow($result))
						{
							$check_words[$row['word_text']] = $row['word_id'];
						}
						break;
				}

				$value_sql = '';
				$match_word = array();
				for ($i = 0; $i < count($word); $i++)
				{
					$new_match = true;
					if (isset($check_words[$word[$i]]))
					{
						$new_match = false;
					}

					if ($new_match)
					{
						switch(SQL_LAYER)
						{
							case 'mysql':
							case 'mysql4':
								$value_sql .= (($value_sql != '') ? ', ' : '') . '(\'' . $word[$i] . '\')';
								break;
							case 'mssql':
								$value_sql .= (($value_sql != '') ? ' UNION ALL ' : '') . "SELECT '" . $word[$i] . "'";
								break;
							default:
								$sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text)
									VALUES ('" . $word[$i] . "')";
								$db->sql_query($sql);
								break;
						}
					}
				}

				if ($value_sql != '')
				{
					switch (SQL_LAYER)
					{
						case 'mysql':
						case 'mysql4':
							$sql = "INSERT IGNORE INTO " . SEARCH_WORD_TABLE . " (word_text)
								VALUES $value_sql";
							break;
						case 'mssql':
							$sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text)
								$value_sql";
							break;
					}

					$db->sql_query($sql);
				}
			}

			foreach ($word_insert_sql as $word_in => $match_sql)
			{
				$title_match = ($word_in == 'title') ? 1 : 0;

				if ($match_sql != '')
				{
					$sql = "INSERT INTO " . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match)
						SELECT $post_id, word_id, $title_match
							FROM " . SEARCH_WORD_TABLE . "
							WHERE word_text IN ($match_sql)";
					$db->sql_query($sql);
				}
			}

		}
		while ($row = $db->sql_fetchrow($result));
	}

	$db->sql_freeresult($result);

	// Remove common words after the first 2 batches and after every 4th batch after that.
	if ($batchcount % 4 == 3)
	{
//		remove_common('global', $config['common_search']);
	}

	$batchcount++;

	if (($batchstart + $batchsize) < $max_post_id)
	{
		redirect("Location: admin_search.$phpEx$SID&batchstart=" . ($batchstart + $batchsize) . "&batchcount=$batch_count");
	}
	else
	{
		set_config('board_disable', 0);
		page_header($user->lang['SEARCH_INDEX']);

?>

<h1><?php echo $user->lang['SEARCH_INDEX']; ?></h1>

<p><?php echo $user->lang['SEARCH_INDEX_COMPLETE']; ?></p>

<?php

		page_footer();

	}

	exit;

}
else if (isset($_POST['cancel']))
{
	set_config('board_disable', 0);
	page_header($user->lang['SEARCH_INDEX']);

?>

<h1><?php echo $user->lang['SEARCH_INDEX']; ?></h1>

<p><?php echo $user->lang['SEARCH_INDEX_CANCEL']; ?></p>

<?php

	page_footer();

}
else
{
	page_header($user->lang['Search_index']);

?>

<h1><?php echo $user->lang['SEARCH_INDEX']; ?></h1>

<p><?php echo $user->lang['SEARCH_INDEX_EXPLAIN']; ?></p>

<form method="post" action="<?php echo "admin_search.$phpEx$SID"; ?>"><table cellspacing="1" cellpadding="4" border="0" align="center" bgcolor="#98AAB1">
	<tr>
		<td class="cat" height="28" align="center">&nbsp;<input type="submit" name="start" value="<?php echo $user->lang['START']; ?>" class="mainoption" /> &nbsp; <input type="submit" name="cancel" value="<?php echo $user->lang['CANCEL']; ?>" class="mainoption" />&nbsp;</td>
	</tr>
</table></form>

<?php

	page_footer();

}

?>