Completed
Pull Request — development (#3050)
by John
23:37
created

Weekly_Maintenance::run()   F

Complexity

Conditions 13
Paths 385

Size

Total Lines 187
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 13.4002

Importance

Changes 0
Metric Value
cc 13
eloc 75
nc 385
nop 0
dl 0
loc 187
ccs 52
cts 60
cp 0.8667
crap 13.4002
rs 3.7737
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Weekly maintenance tasks
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 2.0 dev
15
 *
16
 */
17
18
namespace ElkArte\sources\subs\ScheduledTask;
19
20
/**
21
 * Class Weekly_Maintenance - Weekly maintenance tasks
22
 *
23
 * What it does:
24
 *
25
 * - remove empty or temporary settings
26
 * - prune logs
27
 * - obsolete paid subscriptions
28
 * - clear sessions table
29
 *
30
 * @package ScheduledTasks
31
 */
32
class Weekly_Maintenance implements Scheduled_Task_Interface
33
{
34
	/**
35
	 * Runs the weekly maintenance tasks to keep the forum running smooth as silk
36
	 *
37
	 * @return bool
38
	 */
39 1
	public function run()
40
	{
41 1
		global $modSettings;
42
43 1
		$db = database();
44
45
		// Delete some settings that needn't be set if they are otherwise empty.
46
		$emptySettings = array(
47 1
			'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group',
48
			'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email',
49
			'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online',
50
		);
51
52 1
		$db->query('', '
53
			DELETE FROM {db_prefix}settings
54
			WHERE variable IN ({array_string:setting_list})
55
				AND (value = {string:zero_value} OR value = {string:blank_value})',
56
			array(
57 1
				'zero_value' => '0',
58 1
				'blank_value' => '',
59 1
				'setting_list' => $emptySettings,
60
			)
61
		);
62
63
		// Some settings we never want to keep - they are just there for temporary purposes.
64
		$deleteAnywaySettings = array(
65 1
			'attachment_full_notified',
66
		);
67
68 1
		removeSettings($deleteAnywaySettings);
69
70
		// Ok should we prune the logs?
71 1
		if (!empty($modSettings['pruningOptions']))
72
		{
73 1
			if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false)
74 1
				list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']);
75
76 1
			if (!empty($modSettings['pruneErrorLog']))
77
			{
78
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
79 1
				$t = time() - $modSettings['pruneErrorLog'] * 86400;
80
81 1
				$db->query('', '
82
					DELETE FROM {db_prefix}log_errors
83
					WHERE log_time < {int:log_time}',
84
					array(
85 1
						'log_time' => $t,
86
					)
87
				);
88
			}
89
90 1
			if (!empty($modSettings['pruneModLog']))
91
			{
92
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
93 1
				$t = time() - $modSettings['pruneModLog'] * 86400;
94
95 1
				$db->query('', '
96
					DELETE FROM {db_prefix}log_actions
97
					WHERE log_time < {int:log_time}
98
						AND id_log = {int:moderation_log}',
99
					array(
100 1
						'log_time' => $t,
101 1
						'moderation_log' => 1,
102
					)
103
				);
104
			}
105
106 1
			if (!empty($modSettings['pruneBanLog']))
107
			{
108
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
109 1
				$t = time() - $modSettings['pruneBanLog'] * 86400;
110
111 1
				$db->query('', '
112
					DELETE FROM {db_prefix}log_banned
113
					WHERE log_time < {int:log_time}',
114
					array(
115 1
						'log_time' => $t,
116
					)
117
				);
118
			}
119
120 1
			if (!empty($modSettings['pruneBadbehaviorLog']))
121
			{
122
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
123
				$t = time() - $modSettings['pruneBadbehaviorLog'] * 86400;
124
125
				$db->query('', '
126
					DELETE FROM {db_prefix}log_badbehavior
127
					WHERE log_time < {int:log_time}',
128
					array(
129
						'log_time' => $t,
130
					)
131
				);
132
			}
133
134 1
			if (!empty($modSettings['pruneReportLog']))
135
			{
136
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
137 1
				$t = time() - $modSettings['pruneReportLog'] * 86400;
138
139
				// This one is more complex then the other logs.  First we need to figure out which reports are too old.
140 1
				$reports = array();
141 1
				$result = $db->query('', '
142
					SELECT id_report
143
					FROM {db_prefix}log_reported
144
					WHERE time_started < {int:time_started}
145
						AND closed = {int:closed}',
146
					array(
147 1
						'time_started' => $t,
148 1
						'closed' => 1,
149
					)
150
				);
151 1
				while ($row = $db->fetch_row($result))
152
					$reports[] = $row[0];
153 1
				$db->free_result($result);
154
155 1
				if (!empty($reports))
156
				{
157
					// Now delete the reports...
158
					$db->query('', '
159
						DELETE FROM {db_prefix}log_reported
160
						WHERE id_report IN ({array_int:report_list})',
161
						array(
162
							'report_list' => $reports,
163
						)
164
					);
165
					// And delete the comments for those reports...
166
					$db->query('', '
167
						DELETE FROM {db_prefix}log_reported_comments
168
						WHERE id_report IN ({array_int:report_list})',
169
						array(
170
							'report_list' => $reports,
171
						)
172
					);
173
				}
174
			}
175
176 1
			if (!empty($modSettings['pruneScheduledTaskLog']))
177
			{
178
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
179 1
				$t = time() - $modSettings['pruneScheduledTaskLog'] * 86400;
180
181 1
				$db->query('', '
182
					DELETE FROM {db_prefix}log_scheduled_tasks
183
					WHERE time_run < {int:time_run}',
184
					array(
185 1
						'time_run' => $t,
186
					)
187
				);
188
			}
189
190 1
			if (!empty($modSettings['pruneSpiderHitLog']))
191
			{
192
				// Figure out when our cutoff time is.  1 day = 86400 seconds.
193 1
				$t = time() - $modSettings['pruneSpiderHitLog'] * 86400;
194
195 1
				require_once(SUBSDIR . '/SearchEngines.subs.php');
196 1
				removeSpiderOldLogs($t);
197
			}
198
		}
199
200
		// Get rid of any paid subscriptions that were never actioned.
201 1
		$db->query('', '
202
			DELETE FROM {db_prefix}log_subscribed
203
			WHERE end_time = {int:no_end_time}
204
				AND status = {int:not_active}
205
				AND start_time < {int:start_time}
206
				AND payments_pending < {int:payments_pending}',
207
			array(
208 1
				'no_end_time' => 0,
209 1
				'not_active' => 0,
210 1
				'start_time' => time() - 60,
211 1
				'payments_pending' => 1,
212
			)
213
		);
214
215
		// Some OS's don't seem to clean out their sessions.
216 1
		$db->query('', '
217
			DELETE FROM {db_prefix}sessions
218
			WHERE last_update < {int:last_update}',
219
			array(
220 1
				'last_update' => time() - 86400,
221
			)
222
		);
223
224 1
		return true;
225
	}
226
}
227