1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Models; |
5
|
|
|
|
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
use SilverStripe\Security\PermissionProvider; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class \Firesphere\SolrSearch\Models\SolrError |
14
|
|
|
* |
15
|
|
|
* @package Firesphere\SolrSearch\Models |
16
|
|
|
* @property string $Timestamp |
17
|
|
|
* @property string $Index |
18
|
|
|
* @property string $Type |
19
|
|
|
* @property string $Level |
20
|
|
|
* @property string $Message |
21
|
|
|
*/ |
22
|
|
|
class SolrLog extends DataObject implements PermissionProvider |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Used to give the Gridfield rows a corresponding colour |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $row_color = [ |
29
|
|
|
'ERROR' => 'alert alert-danger', |
30
|
|
|
'WARN' => 'alert alert-warning', |
31
|
|
|
'INFO' => 'alert alert-info', |
32
|
|
|
]; |
33
|
|
|
/** |
34
|
|
|
* @var string Database table name |
35
|
|
|
*/ |
36
|
|
|
private static $table_name = 'SolrLog'; |
37
|
|
|
/** |
38
|
|
|
* @var array Database columns |
39
|
|
|
*/ |
40
|
|
|
private static $db = [ |
41
|
|
|
'Timestamp' => 'Datetime', |
42
|
|
|
'Index' => 'Varchar(255)', |
43
|
|
|
'Type' => 'Enum("Config,Index,Query")', |
44
|
|
|
'Level' => 'Varchar(10)', |
45
|
|
|
'Message' => 'Text', |
46
|
|
|
]; |
47
|
|
|
/** |
48
|
|
|
* @var array Summary fields |
49
|
|
|
*/ |
50
|
|
|
private static $summary_fields = [ |
|
|
|
|
51
|
|
|
'Timestamp', |
52
|
|
|
'Index', |
53
|
|
|
'Type', |
54
|
|
|
'Level' |
55
|
|
|
]; |
56
|
|
|
/** |
57
|
|
|
* @var array Searchable fields |
58
|
|
|
*/ |
59
|
|
|
private static $searchable_fields = [ |
60
|
|
|
'Created', |
61
|
|
|
'Timestamp', |
62
|
|
|
'Index', |
63
|
|
|
'Type', |
64
|
|
|
'Level' |
65
|
|
|
]; |
66
|
|
|
/** |
67
|
|
|
* @var array Timestamp is indexed |
68
|
|
|
*/ |
69
|
|
|
private static $indexes = [ |
70
|
|
|
'Timestamp' => true, |
71
|
|
|
]; |
72
|
|
|
/** |
73
|
|
|
* @var string Default sort |
74
|
|
|
*/ |
75
|
|
|
private static $default_sort = 'Timestamp DESC'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Convert the Timestamp to a DBDatetime for compatibility |
79
|
|
|
*/ |
80
|
35 |
|
public function onBeforeWrite() |
81
|
|
|
{ |
82
|
35 |
|
parent::onBeforeWrite(); |
83
|
|
|
|
84
|
35 |
|
$this->Timestamp = DBDatetime::create()->setValue(strtotime($this->Timestamp)); |
85
|
35 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the first line of this log item error |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
public function getLastErrorLine() |
92
|
|
|
{ |
93
|
1 |
|
$lines = explode(PHP_EOL, $this->Message); |
94
|
|
|
|
95
|
1 |
|
return $lines[0]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Not creatable by users |
100
|
|
|
* |
101
|
|
|
* @param null|Member $member |
102
|
|
|
* @param array $context |
103
|
|
|
* @return bool|mixed |
104
|
|
|
*/ |
105
|
1 |
|
public function canCreate($member = null, $context = []) |
106
|
|
|
{ |
107
|
1 |
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Not editable by users |
112
|
|
|
* |
113
|
|
|
* @param null|Member $member |
114
|
|
|
* @return bool|mixed |
115
|
|
|
*/ |
116
|
1 |
|
public function canEdit($member = null) |
117
|
|
|
{ |
118
|
1 |
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Member has view access? |
123
|
|
|
* |
124
|
|
|
* @param null|Member $member |
125
|
|
|
* @return bool|mixed |
126
|
|
|
*/ |
127
|
36 |
|
public function canView($member = null) |
128
|
|
|
{ |
129
|
36 |
|
return parent::canView($member); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Only deleteable by admins or when in dev mode to clean up |
134
|
|
|
* |
135
|
|
|
* @param null|Member $member |
136
|
|
|
* @return bool|mixed |
137
|
|
|
*/ |
138
|
1 |
|
public function canDelete($member = null) |
139
|
|
|
{ |
140
|
1 |
|
if ($member) { |
141
|
1 |
|
return $member->inGroup('administrators') || Director::isDev(); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return parent::canDelete($member) || Director::isDev(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the extra classes to colour the gridfield rows |
149
|
|
|
* @return mixed|string |
150
|
|
|
*/ |
151
|
1 |
|
public function getExtraClass() |
152
|
|
|
{ |
153
|
1 |
|
$classMap = static::$row_color; |
154
|
|
|
|
155
|
1 |
|
return $classMap[$this->Level] ?? 'alert alert-info'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return a map of permission codes to add to the dropdown shown in the Security section of the CMS. |
161
|
|
|
* array( |
162
|
|
|
* 'VIEW_SITE' => 'View the site', |
163
|
|
|
* ); |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
1 |
|
public function providePermissions() |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
'DELETE_LOG' => [ |
170
|
1 |
|
'name' => _t(self::class . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Solr logs'), |
171
|
1 |
|
'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'), |
172
|
1 |
|
'help' => _t( |
173
|
1 |
|
self::class . '.PERMISSION_DELETE_HELP', |
174
|
1 |
|
'Permission required to delete existing Solr logs.' |
175
|
|
|
) |
176
|
|
|
], |
177
|
|
|
'VIEW_LOG' => [ |
178
|
1 |
|
'name' => _t(self::class . '.PERMISSION_VIEW_DESCRIPTION', 'View Solr logs'), |
179
|
1 |
|
'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'), |
180
|
1 |
|
'help' => _t( |
181
|
1 |
|
self::class . '.PERMISSION_VIEW_HELP', |
182
|
1 |
|
'Permission required to view existing Solr logs.' |
183
|
|
|
) |
184
|
|
|
], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|