Completed
Pull Request — patch_1-1-4 (#3202)
by Spuds
15:49
created

Daily_Maintenance   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 29.82%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 0
loc 102
ccs 17
cts 57
cp 0.2982
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D run() 0 93 14
1
<?php
2
3
/**
4
 * This class does daily cleaning up.
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 1.1
15
 *
16
 */
17
18
namespace ElkArte\sources\subs\ScheduledTask;
19
20
/**
21
 * Class Daily_Maintenance - This function does daily cleaning up:
22
 *
23
 * What it does:
24
 *
25
 * - decrements warning levels if it's enabled
26
 * - consolidate spider statistics
27
 * - fix MySQL version
28
 * - regenerate Diffie-Hellman keys for OpenID
29
 * - remove obsolete login history logs
30
 *
31
 * @package ScheduledTasks
32
 */
33
class Daily_Maintenance implements Scheduled_Task_Interface
34
{
35
	/**
36
	 * Our only method, runs the show
37
	 *
38
	 * @return bool
39
	 * @throws \Elk_Exception
40
	 */
41 1
	public function run()
42
	{
43 1
		global $modSettings;
44
45 1
		$db = database();
46
47
		// First clean out the cache.
48 1
		clean_cache('data');
49
50
		// If warning decrement is enabled and we have people who have not had a new warning in 24 hours, lower their warning level.
51 1
		list (,, $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']);
52 1
		if ($modSettings['warning_decrement'])
53 1
		{
54
			// Find every member who has a warning level...
55
			$request = $db->query('', '
56
				SELECT id_member, warning
57
				FROM {db_prefix}members
58
				WHERE warning > {int:no_warning}',
59
				array(
60
					'no_warning' => 0,
61
				)
62
			);
63
			$members = array();
64
			while ($row = $db->fetch_assoc($request))
65
				$members[$row['id_member']] = $row['warning'];
66
			$db->free_result($request);
67
68
			// Have some members to check?
69
			if (!empty($members))
70
			{
71
				// Find out when they were last warned.
72
				$request = $db->query('', '
73
					SELECT id_recipient, MAX(log_time) AS last_warning
74
					FROM {db_prefix}log_comments
75
					WHERE id_recipient IN ({array_int:member_list})
76
						AND comment_type = {string:warning}
77
					GROUP BY id_recipient',
78
					array(
79
						'member_list' => array_keys($members),
80
						'warning' => 'warning',
81
					)
82
				);
83
				$member_changes = array();
84
				while ($row = $db->fetch_assoc($request))
85
				{
86
					// More than 24 hours ago?
87
					if ($row['last_warning'] <= time() - 86400)
88
						$member_changes[] = array(
89
							'id' => $row['id_recipient'],
90
							'warning' => $members[$row['id_recipient']] >= $modSettings['warning_decrement'] ? $members[$row['id_recipient']] - $modSettings['warning_decrement'] : 0,
91
						);
92
				}
93
				$db->free_result($request);
94
95
				// Have some members to change?
96
				if (!empty($member_changes))
97
				{
98
					require_once(SUBSDIR . '/Members.subs.php');
99
					foreach ($member_changes as $change)
100
						updateMemberData($change['id'], array('warning' => $change['warning']));
101
				}
102
			}
103
		}
104
105
		// Do any spider stuff.
106 1
		if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
107 1
		{
108
			// We'll need this.
109
			require_once(SUBSDIR . '/SearchEngines.subs.php');
110
			consolidateSpiderStats();
111
		}
112
113
		// Regenerate the Diffie-Hellman keys if OpenID is enabled.
114 1
		if (!empty($modSettings['enableOpenID']))
115 1
		{
116
			require_once(SUBSDIR . '/OpenID.subs.php');
117
			$openID = new \OpenID();
118
			$openID->setup_DH(true);
119
		}
120 1
		elseif (!empty($modSettings['dh_keys']))
121
			removeSettings('dh_keys');
122
123
		// Clean up some old login history information.
124 1
		$db->query('', '
125
			DELETE FROM {db_prefix}member_logins
126 1
			WHERE time > {int:oldLogins}',
127
			array(
128 1
				'oldLogins' => !empty($modSettings['loginHistoryDays']) ? 60 * 60 * $modSettings['loginHistoryDays'] : 108000,
129 1
		));
130
131
		// Log we've done it...
132 1
		return true;
133
	}
134
}
135