|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
|
7
|
|
|
* @package Controller |
|
8
|
|
|
* @subpackage Jobs |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Aimeos\Controller\Jobs\Admin\Log; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Admin log controller. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Controller |
|
19
|
|
|
* @subpackage Jobs |
|
20
|
|
|
*/ |
|
21
|
|
|
class Standard |
|
22
|
|
|
extends \Aimeos\Controller\Jobs\Base |
|
23
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
|
24
|
|
|
{ |
|
25
|
|
|
/** controller/jobs/admin/log/name |
|
26
|
|
|
* Class name of the used admin log scheduler controller implementation |
|
27
|
|
|
* |
|
28
|
|
|
* Each default log controller can be replace by an alternative imlementation. |
|
29
|
|
|
* To use this implementation, you have to set the last part of the class |
|
30
|
|
|
* name as configuration value so the controller factory knows which class it |
|
31
|
|
|
* has to instantiate. |
|
32
|
|
|
* |
|
33
|
|
|
* For example, if the name of the default class is |
|
34
|
|
|
* |
|
35
|
|
|
* \Aimeos\Controller\Jobs\Admin\Log\Standard |
|
36
|
|
|
* |
|
37
|
|
|
* and you want to replace it with your own version named |
|
38
|
|
|
* |
|
39
|
|
|
* \Aimeos\Controller\Jobs\Admin\Log\Mylog |
|
40
|
|
|
* |
|
41
|
|
|
* then you have to set the this configuration option: |
|
42
|
|
|
* |
|
43
|
|
|
* controller/jobs/admin/log/name = Mylog |
|
44
|
|
|
* |
|
45
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
|
46
|
|
|
* so take care that the configuration value is exactly named like the last |
|
47
|
|
|
* part of the class name. |
|
48
|
|
|
* |
|
49
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
|
50
|
|
|
* characters are possible! You should always start the last part of the class |
|
51
|
|
|
* name with an upper case character and continue only with lower case characters |
|
52
|
|
|
* or numbers. Avoid chamel case names like "MyLog"! |
|
53
|
|
|
* |
|
54
|
|
|
* @param string Last part of the class name |
|
55
|
|
|
* @since 2014.09 |
|
56
|
|
|
*/ |
|
57
|
|
|
|
|
58
|
|
|
/** controller/jobs/admin/log/decorators/excludes |
|
59
|
|
|
* Excludes decorators added by the "common" option from the admin log controllers |
|
60
|
|
|
* |
|
61
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
62
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
63
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
64
|
|
|
* modify what is returned to the caller. |
|
65
|
|
|
* |
|
66
|
|
|
* This option allows you to remove a decorator added via |
|
67
|
|
|
* "controller/jobs/common/decorators/default" before they are wrapped |
|
68
|
|
|
* around the job controller. |
|
69
|
|
|
* |
|
70
|
|
|
* controller/jobs/admin/log/decorators/excludes = array( 'decorator1' ) |
|
71
|
|
|
* |
|
72
|
|
|
* This would remove the decorator named "decorator1" from the list of |
|
73
|
|
|
* common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via |
|
74
|
|
|
* "controller/jobs/common/decorators/default" to this job controller. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array List of decorator names |
|
77
|
|
|
* @since 2015.09 |
|
78
|
|
|
* @see controller/jobs/common/decorators/default |
|
79
|
|
|
* @see controller/jobs/admin/log/decorators/global |
|
80
|
|
|
* @see controller/jobs/admin/log/decorators/local |
|
81
|
|
|
*/ |
|
82
|
|
|
|
|
83
|
|
|
/** controller/jobs/admin/log/decorators/global |
|
84
|
|
|
* Adds a list of globally available decorators only to the admin log controllers |
|
85
|
|
|
* |
|
86
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
87
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
88
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
89
|
|
|
* modify what is returned to the caller. |
|
90
|
|
|
* |
|
91
|
|
|
* This option allows you to wrap global decorators |
|
92
|
|
|
* ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller. |
|
93
|
|
|
* |
|
94
|
|
|
* controller/jobs/admin/log/decorators/global = array( 'decorator1' ) |
|
95
|
|
|
* |
|
96
|
|
|
* This would add the decorator named "decorator1" defined by |
|
97
|
|
|
* "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array List of decorator names |
|
100
|
|
|
* @since 2015.09 |
|
101
|
|
|
* @see controller/jobs/common/decorators/default |
|
102
|
|
|
* @see controller/jobs/admin/log/decorators/excludes |
|
103
|
|
|
* @see controller/jobs/admin/log/decorators/local |
|
104
|
|
|
*/ |
|
105
|
|
|
|
|
106
|
|
|
/** controller/jobs/admin/log/decorators/local |
|
107
|
|
|
* Adds a list of local decorators only to the admin log controllers |
|
108
|
|
|
* |
|
109
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
110
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
111
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
112
|
|
|
* modify what is returned to the caller. |
|
113
|
|
|
* |
|
114
|
|
|
* This option allows you to wrap local decorators |
|
115
|
|
|
* ("\Aimeos\Controller\Jobs\Admin\Log\Decorator\*") around this job controller. |
|
116
|
|
|
* |
|
117
|
|
|
* controller/jobs/admin/log/decorators/local = array( 'decorator2' ) |
|
118
|
|
|
* |
|
119
|
|
|
* This would add the decorator named "decorator2" defined by |
|
120
|
|
|
* "\Aimeos\Controller\Jobs\Admin\Log\Decorator\Decorator2" only to this job |
|
121
|
|
|
* controller. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array List of decorator names |
|
124
|
|
|
* @since 2015.09 |
|
125
|
|
|
* @see controller/jobs/common/decorators/default |
|
126
|
|
|
* @see controller/jobs/admin/log/decorators/excludes |
|
127
|
|
|
* @see controller/jobs/admin/log/decorators/global |
|
128
|
|
|
*/ |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the localized name of the job. |
|
133
|
|
|
* |
|
134
|
|
|
* @return string Name of the job |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getName() : string |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->context()->translate( 'controller/jobs', 'Log cleanup' ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the localized description of the job. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string Description of the job |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getDescription() : string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->context()->translate( 'controller/jobs', 'Removes the old log entries from the database and archives them (optional)' ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Executes the job. |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
|
157
|
|
|
*/ |
|
158
|
|
|
public function run() |
|
159
|
|
|
{ |
|
160
|
|
|
$fh = $this->tempfile(); |
|
161
|
|
|
$context = $this->context(); |
|
162
|
|
|
$fs = $context->fs( 'fs-admin' ); |
|
163
|
|
|
|
|
164
|
|
|
$manager = \Aimeos\MAdmin::create( $context, 'log' ); |
|
165
|
|
|
$filter = $manager->filter()->add( 'log.timestamp', '<=', $this->timestamp() )->order( 'log.timestamp' ); |
|
166
|
|
|
$cursor = $manager->cursor( $filter->slice( 0, 1000 ) ); |
|
167
|
|
|
|
|
168
|
|
|
while( $items = $manager->iterate( $cursor ) ) |
|
169
|
|
|
{ |
|
170
|
|
|
foreach( $items as $id => $item ) |
|
171
|
|
|
{ |
|
172
|
|
|
if( fputcsv( $fh, $item->toArray(), ',', '"', '' ) === false ) { |
|
173
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( 'Unable to write log data to temporary file' ); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$manager->delete( $items ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
rewind( $fh ); |
|
181
|
|
|
$fs->writes( $this->path(), $fh ); |
|
182
|
|
|
fclose( $fh ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the path where the export file should be stored |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function path() : string |
|
190
|
|
|
{ |
|
191
|
|
|
/** controller/jobs/admin/log/path |
|
192
|
|
|
* Path to a writable directory where the log archive files should be stored |
|
193
|
|
|
* |
|
194
|
|
|
* During normal operation, a lot of data can be logged, not only for |
|
195
|
|
|
* errors that have occured. By default, these data is written into the |
|
196
|
|
|
* log database and its size will grow if old log entries are not |
|
197
|
|
|
* removed. There's a job controller available that can delete old log |
|
198
|
|
|
* entries and save the old log entries to the given relative path. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string Relative file system path in the fs-admin filesystem |
|
201
|
|
|
* @since 2014.09 |
|
202
|
|
|
* @see controller/jobs/admin/log/limit-days |
|
203
|
|
|
*/ |
|
204
|
|
|
$path = $this->context()->config()->get( 'controller/jobs/admin/log/path', 'logs' ); |
|
205
|
|
|
|
|
206
|
|
|
return $path . '/aimeos_' . date( 'Y-m-d' ) . '.log'; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Returns a file handle for a temporary file |
|
212
|
|
|
* |
|
213
|
|
|
* @return resource Temporary file handle |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function tempfile() |
|
216
|
|
|
{ |
|
217
|
|
|
if( ( $fh = tmpfile() ) === false ) { |
|
218
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( 'Unable to create temporary file' ); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $fh; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Returns the timestamp until the logs entries should be moved |
|
227
|
|
|
* |
|
228
|
|
|
* @return string Timestamp in "YYYY-MM-DD HH:mm:ss" format |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function timestamp() : string |
|
231
|
|
|
{ |
|
232
|
|
|
/** controller/jobs/admin/log/limit-days |
|
233
|
|
|
* Only remove log entries that were created berore the configured number of days |
|
234
|
|
|
* |
|
235
|
|
|
* This option specifies the number of days log entries will be kept in |
|
236
|
|
|
* the database. Afterwards, they will be removed and archived. |
|
237
|
|
|
* |
|
238
|
|
|
* @param integer Number of days |
|
239
|
|
|
* @since 2014.09 |
|
240
|
|
|
* @see controller/jobs/admin/log/path |
|
241
|
|
|
*/ |
|
242
|
|
|
$limit = $this->context()->config()->get( 'controller/jobs/admin/log/limit-days', 30 ); |
|
243
|
|
|
return date( 'Y-m-d H:i:s', time() - $limit * 86400 ); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|