acp_pmsearch_module::init_search()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 14
ccs 0
cts 11
cp 0
crap 12
rs 10
1
<?php
2
/**
3
*
4
* @package Anavaro.com PM Search
5
* @copyright (c) 2013 Lucifer
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
/**
10
* @ignore
11
*/
12
namespace anavaro\pmsearch\acp;
13
14
/**
15
* @package acp
16
*/
17
class acp_pmsearch_module
18
{
19
	private $search_helper;
20
	var $state;
21
	var $search;
22
	var $max_post_id;
23
	var $batch_size = 200;
24
	var $u_action;
25
	function main($id, $mode)
26
	{
27
		global $config, $user, $table_prefix, $db, $template, $request, $phpbb_root_path, $phpbb_log, $phpbb_admin_path, $phpEx;
28
		global $phpbb_container;
29
30
		$this->search_helper = $phpbb_container->get('anavaro.pmsearch.search.helper');
31
32
		switch($mode)
33
		{
34
			default:
35
				//Let's see indexing
36
				$pm_index = $request->variable('pm_index', 0);
37
38
				if ($pm_index == '1')
39
				{
40
					$config->set('pmsearch_pm_index', 0);
41
				}
42
				else if ($pm_index == '2')
43
				{
44
					$config->set('pmsearch_pm_index', 1);
45
				}
46
47
				//Do we want users to be able to search?
48
				$pm_search = $request->variable('pm_search', 0);
49
50
				if ($pm_search == '1')
51
				{
52
					$config->set('pmsearch_search', 0);
53
				}
54
				else if ($pm_index == '2')
55
				{
56
					$config->set('pmsearch_search', 1);
57
				}
58
				$this->tpl_name		= 'acp_pmsearch';
59
				$this->page_title	= 'PMSEARCH_ADMIN';
60
61
				$template->assign_var('PM_INDEX', $config['pmsearch_pm_index']);
62
				$template->assign_var('PM_SEARCH', $config['pmsearch_search']);
63
				$template->assign_var('U_ACTION', append_sid("index.php?i=".$id."&mode=".$mode));
64
65
				if($config['pmsearch_pm_index'])
66
				{
67
					$action_index = $request->variable('action_index', '');
68
					$this->state = explode(',', $config['search_pm_indexing_state']);
69
					if (isset($_POST['cancel']))
70
					{
71
						$action = '';
72
						$this->state = array();
73
						$this->save_state();
74
					}
75
					if ($action_index)
76
					{
77
						switch ($action_index)
78
						{
79
							case 'delete':
80
								$this->state[1] = 'delete';
81
							break;
82
83
							case 'create':
84
								$this->state[1] = 'create';
85
							break;
86
87
							default:
88
								trigger_error('NO_ACTION', E_USER_ERROR);
89
							break;
90
						}
91
						if (empty($this->state[0]))
92
						{
93
							$this->state[0] = $request->variable('target_index', 'normal');
94
						}
95
						$this->search = null;
96
						$error = false;
97
						$search_types = $this->get_search_types();
98
						if ($this->search_helper->init_search($search_types[0], $this->search, $error))
99
						{
100
							trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
101
						}
102
						$name = $this->search->get_name();
103
						$action_index = &$this->state[1];
104
						$this->max_post_id = $this->get_max_post_id();
105
106
						$post_counter = (isset($this->state[2])) ? $this->state[2] : 0;
107
						$this->state[2] = &$post_counter;
108
						$this->save_state();
109
						switch ($action_index)
110
						{
111
							case 'delete':
112
								if (method_exists($this->search, 'delete_index'))
113
								{
114
									$this->state = array('');
115
									$this->save_state();
116
									// pass a reference to myself so the $search object can make use of save_state() and attributes
117
									if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
118
									{
119
										$this->state = array('');
120
										$this->save_state();
121
										add_log('admin', 'LOG_PMSEARCH_INDEX_REMOVED', $name);
0 ignored issues
show
Bug introduced by
The function add_log was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
										/** @scrutinizer ignore-call */ 
122
          add_log('admin', 'LOG_PMSEARCH_INDEX_REMOVED', $name);
Loading history...
122
										trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
123
									}
124
								}
125
							break;
126
							case 'create':
127
								//$this->var_display($this->state);
128
								$starttime = explode(' ', microtime());
129
								$starttime = $starttime[1] + $starttime[0];
130
								$row_count = 0;
131
132
								while (still_on_time() && $post_counter <= $this->max_post_id)
0 ignored issues
show
Bug introduced by
The function still_on_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
								while (/** @scrutinizer ignore-call */ still_on_time() && $post_counter <= $this->max_post_id)
Loading history...
133
								{
134
									$sql = 'SELECT msg_id, message_subject, message_text, author_id
135
										FROM ' . PRIVMSGS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant anavaro\pmsearch\acp\PRIVMSGS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
136
										WHERE msg_id >= ' . (int) ($post_counter + 1) . '
137
											AND msg_id <= ' . (int) ($post_counter + $this->batch_size);
138
									$result = $db->sql_query($sql);
139
140
									$buffer = $db->sql_buffer_nested_transactions();
141
142
									if ($buffer)
143
									{
144
										$rows = $db->sql_fetchrowset($result);
145
										$rows[] = false; // indicate end of array for while loop below
146
147
										$db->sql_freeresult($result);
148
									}
149
150
									$i = 0;
151
									while ($row = ($buffer ? $rows[$i++] : $db->sql_fetchrow($result)))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
152
									{
153
										//	Indexing
154
										$this->search->index('post', (int) $row['msg_id'], $row['message_text'], $row['message_subject'], (int) $row['author_id']);
155
										$row_count++;
156
									}
157
									if (!$buffer)
158
									{
159
										$db->sql_freeresult($result);
160
									}
161
									//$this->var_display($this->state[2]);
162
									$post_counter += $this->batch_size;
163
164
									// pretend the number of posts was as big as the number of ids we indexed so far
165
									// just an estimation as it includes deleted posts
166
									$num_posts = $config['num_posts'];
167
									$config['num_posts'] = min($config['num_posts'], $post_counter);
168
									$this->search->tidy();
169
									$config['num_posts'] = $num_posts;
170
171
									if ($post_counter <= $this->max_post_id)
172
									{
173
										$mtime = explode(' ', microtime());
174
										$totaltime = $mtime[0] + $mtime[1] - $starttime;
175
										$rows_per_second = $row_count / $totaltime;
176
										$this->state[3] = 'continue';
177
										$this->save_state();
178
										meta_refresh(1, append_sid($this->u_action . '&amp;action_index=create&amp;skip_rows=' . $post_counter));
0 ignored issues
show
Bug introduced by
The function meta_refresh was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
										/** @scrutinizer ignore-call */ 
179
          meta_refresh(1, append_sid($this->u_action . '&amp;action_index=create&amp;skip_rows=' . $post_counter));
Loading history...
179
										trigger_error($user->lang('SEARCH_INDEX_CREATE_REDIRECT', (int) $row_count, $post_counter) . $user->lang('SEARCH_INDEX_CREATE_REDIRECT_RATE', $rows_per_second));
180
									}
181
									else
182
									{
183
										$this->state = array('');
184
										add_log('admin', 'LOG_PMSEARCH_INDEX_CREATED', $name);
185
									}
186
								}
187
							break;
188
						}
189
					}
190
					$search_types = $this->get_search_types();
191
192
					$search = null;
193
					$error = false;
194
					$search_options = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $search_options is dead and can be removed.
Loading history...
195
196
					if ($this->search_helper->init_search($search_types[0], $search, $error) || !method_exists($search, 'index_created'))
197
					{
198
						break;
199
					}
200
201
					//Let's build normal
202
					$name = $search->get_name();
203
204
					$data = array();
205
					if (method_exists($search, 'index_stats'))
206
					{
207
						$data = $search->index_stats();
208
					}
209
210
					$statistics = array();
211
					foreach ($data as $statistic => $value)
212
					{
213
						$n = sizeof($statistics);
214
						if ($n && sizeof($statistics[$n - 1]) < 3)
215
						{
216
							$statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
217
						}
218
						else
219
						{
220
							$statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
221
						}
222
					}
223
					$template->assign_block_vars('normal', array(
224
						'L_NAME'			=> $name,
225
						'NAME'				=> $name,
226
						'DISSALOW'			=> ($this->state[0] == 'black' && $this->state[3] == 'continue') ? 1 : 0,
227
						'S_INDEXED'			=> (bool) $search->index_created(),
0 ignored issues
show
Bug introduced by
The method index_created() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
						'S_INDEXED'			=> (bool) $search->/** @scrutinizer ignore-call */ index_created(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
228
						'S_STATS'			=> (bool) sizeof($statistics),
229
						'STATISTIC_1'	=> $statistics[0]['statistic_1'],
230
						'VALUE_1'		=> $statistics[0]['value_1'],
231
						'STATISTIC_2'	=> (isset($statistics[0]['statistic_2'])) ? $statistics[0]['statistic_2'] : '',
232
						'VALUE_2'		=> (isset($statistics[0]['value_2'])) ? $statistics[0]['value_2'] : '',
233
						'CONTINUE'		=> ($this->state[0] == 'normal' && (isset($this->state[3]) && $this->state[3] == 'continue')) ? 1 : 0,
234
						'CONTINUE_URL'	=> append_sid($this->u_action . '&amp;action_index=create')
235
						)
236
					);
237
					//end building normal
238
				}
239
			break;
240
		}
241
	}
242
	function get_search_types()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
243
	{
244
		global $phpbb_root_path, $phpEx, $phpbb_extension_manager;
245
246
		$finder = $phpbb_extension_manager->get_finder();
247
248
		return $finder
249
			->extension_suffix('_backend1')
250
			->extension_directory('')
251
			->core_path('ext/anavaro/pmsearch/search/')
252
			->get_classes();
253
	}
254
	function save_state($state = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
255
	{
256
		global $config;
257
		if ($state)
258
		{
259
			$this->state = $state;
260
		}
261
262
		ksort($this->state);
263
264
		$config->set('search_pm_indexing_state', implode(',', $this->state), true);
265
	}
266
267
	function get_max_post_id()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
268
	{
269
		global $db;
270
271
		$sql = 'SELECT MAX(msg_id) as max_post_id
272
			FROM '. PRIVMSGS_TABLE;
0 ignored issues
show
Bug introduced by
The constant anavaro\pmsearch\acp\PRIVMSGS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
273
		$result = $db->sql_query($sql);
274
		$max_post_id = (int) $db->sql_fetchfield('max_post_id');
275
		$db->sql_freeresult($result);
276
277
		return $max_post_id;
278
	}
279
	/**
280
	* Initialises a search backend object
281
	*
282
	* @return false if no error occurred else an error message
283
	*/
284
	function init_search($type, &$search, &$error)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
285
	{
286
		global $phpbb_root_path, $phpEx, $user, $auth, $config, $db, $table_prefix;
287
288
		if (!class_exists($type) || !method_exists($type, 'keyword_search'))
289
		{
290
			$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
291
			return $error;
292
		}
293
294
		$error = false;
295
		$search = new $type($auth, $config, $db, $user, $table_prefix, $phpbb_root_path, $phpEx);
296
297
		return $error;
298
	}
299
}
300