1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2022 |
7
|
|
|
* @package MAdmin |
8
|
|
|
* @subpackage Log |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MAdmin\Log\Manager; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default log manager implementation. |
17
|
|
|
* |
18
|
|
|
* @package MAdmin |
19
|
|
|
* @subpackage Log |
20
|
|
|
*/ |
21
|
|
|
class Standard |
22
|
|
|
extends \Aimeos\MAdmin\Common\Manager\Base |
23
|
|
|
implements \Aimeos\MAdmin\Log\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface |
24
|
|
|
{ |
25
|
|
|
/** madmin/log/manager/name |
26
|
|
|
* Class name of the used log manager implementation |
27
|
|
|
* |
28
|
|
|
* Each default manager 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 manager factory knows which class it |
31
|
|
|
* has to instantiate. |
32
|
|
|
* |
33
|
|
|
* For example, if the name of the default class is |
34
|
|
|
* |
35
|
|
|
* \Aimeos\MShop\Log\Manager\Standard |
36
|
|
|
* |
37
|
|
|
* and you want to replace it with your own version named |
38
|
|
|
* |
39
|
|
|
* \Aimeos\MShop\Log\Manager\Mymanager |
40
|
|
|
* |
41
|
|
|
* then you have to set the this configuration option: |
42
|
|
|
* |
43
|
|
|
* madmin/log/manager/name = Mymanager |
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 "MyManager"! |
53
|
|
|
* |
54
|
|
|
* @param string Last part of the class name |
55
|
|
|
* @since 2014.03 |
56
|
|
|
* @category Developer |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
/** madmin/log/manager/decorators/excludes |
60
|
|
|
* Excludes decorators added by the "common" option from the log manager |
61
|
|
|
* |
62
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
63
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
64
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
65
|
|
|
* modify what is returned to the caller. |
66
|
|
|
* |
67
|
|
|
* This option allows you to remove a decorator added via |
68
|
|
|
* "madmin/common/manager/decorators/default" before they are wrapped |
69
|
|
|
* around the log manager. |
70
|
|
|
* |
71
|
|
|
* madmin/log/manager/decorators/excludes = array( 'decorator1' ) |
72
|
|
|
* |
73
|
|
|
* This would remove the decorator named "decorator1" from the list of |
74
|
|
|
* common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via |
75
|
|
|
* "madmin/common/manager/decorators/default" for the log manager. |
76
|
|
|
* |
77
|
|
|
* @param array List of decorator names |
78
|
|
|
* @since 2014.03 |
79
|
|
|
* @category Developer |
80
|
|
|
* @see madmin/common/manager/decorators/default |
81
|
|
|
* @see madmin/log/manager/decorators/global |
82
|
|
|
* @see madmin/log/manager/decorators/local |
83
|
|
|
*/ |
84
|
|
|
|
85
|
|
|
/** madmin/log/manager/decorators/global |
86
|
|
|
* Adds a list of globally available decorators only to the log manager |
87
|
|
|
* |
88
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
89
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
90
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
91
|
|
|
* modify what is returned to the caller. |
92
|
|
|
* |
93
|
|
|
* This option allows you to wrap global decorators |
94
|
|
|
* ("\Aimeos\MShop\Common\Manager\Decorator\*") around the log manager. |
95
|
|
|
* |
96
|
|
|
* madmin/log/manager/decorators/global = array( 'decorator1' ) |
97
|
|
|
* |
98
|
|
|
* This would add the decorator named "decorator1" defined by |
99
|
|
|
* "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the log controller. |
100
|
|
|
* |
101
|
|
|
* @param array List of decorator names |
102
|
|
|
* @since 2014.03 |
103
|
|
|
* @category Developer |
104
|
|
|
* @see madmin/common/manager/decorators/default |
105
|
|
|
* @see madmin/log/manager/decorators/excludes |
106
|
|
|
* @see madmin/log/manager/decorators/local |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
/** madmin/log/manager/decorators/local |
110
|
|
|
* Adds a list of local decorators only to the log manager |
111
|
|
|
* |
112
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
113
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
114
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
115
|
|
|
* modify what is returned to the caller. |
116
|
|
|
* |
117
|
|
|
* This option allows you to wrap local decorators |
118
|
|
|
* ("\Aimeos\MShop\Common\Manager\Decorator\*") around the log manager. |
119
|
|
|
* |
120
|
|
|
* madmin/log/manager/decorators/local = array( 'decorator2' ) |
121
|
|
|
* |
122
|
|
|
* This would add the decorator named "decorator2" defined by |
123
|
|
|
* "\Aimeos\MShop\Common\Manager\Decorator\Decorator2" only to the log |
124
|
|
|
* controller. |
125
|
|
|
* |
126
|
|
|
* @param array List of decorator names |
127
|
|
|
* @since 2014.03 |
128
|
|
|
* @category Developer |
129
|
|
|
* @see madmin/common/manager/decorators/default |
130
|
|
|
* @see madmin/log/manager/decorators/excludes |
131
|
|
|
* @see madmin/log/manager/decorators/global |
132
|
|
|
*/ |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
use \Aimeos\Base\Logger\Traits; |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
private $loglevel; |
139
|
|
|
private $requestid; |
140
|
|
|
|
141
|
|
|
private $searchConfig = array( |
142
|
|
|
'log.id' => array( |
143
|
|
|
'code' => 'log.id', |
144
|
|
|
'internalcode' => 'malog."id"', |
145
|
|
|
'label' => 'Log ID', |
146
|
|
|
'type' => 'integer', |
147
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
148
|
|
|
), |
149
|
|
|
'log.siteid' => array( |
150
|
|
|
'code' => 'log.siteid', |
151
|
|
|
'internalcode' => 'malog."siteid"', |
152
|
|
|
'label' => 'Log site ID', |
153
|
|
|
'type' => 'string', |
154
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
155
|
|
|
'public' => false, |
156
|
|
|
), |
157
|
|
|
'log.message' => array( |
158
|
|
|
'code' => 'log.message', |
159
|
|
|
'internalcode' => 'malog."message"', |
160
|
|
|
'label' => 'Log message', |
161
|
|
|
'type' => 'string', |
162
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
163
|
|
|
), |
164
|
|
|
'log.facility' => array( |
165
|
|
|
'code' => 'log.facility', |
166
|
|
|
'internalcode' => 'malog."facility"', |
167
|
|
|
'label' => 'Log facility', |
168
|
|
|
'type' => 'string', |
169
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
170
|
|
|
), |
171
|
|
|
'log.priority' => array( |
172
|
|
|
'code' => 'log.priority', |
173
|
|
|
'internalcode' => 'malog."priority"', |
174
|
|
|
'label' => 'Log priority', |
175
|
|
|
'type' => 'integer', |
176
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
177
|
|
|
), |
178
|
|
|
'log.timestamp' => array( |
179
|
|
|
'code' => 'log.timestamp', |
180
|
|
|
'internalcode' => 'malog."timestamp"', |
181
|
|
|
'label' => 'Log create date/time', |
182
|
|
|
'type' => 'datetime', |
183
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
184
|
|
|
), |
185
|
|
|
'log.request' => array( |
186
|
|
|
'code' => 'log.request', |
187
|
|
|
'internalcode' => 'malog."request"', |
188
|
|
|
'label' => 'Log request', |
189
|
|
|
'type' => 'string', |
190
|
|
|
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Creates the log manager that will use the given context object. |
197
|
|
|
* |
198
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object with required objects |
199
|
|
|
*/ |
200
|
|
|
public function __construct( \Aimeos\MShop\ContextIface $context ) |
201
|
|
|
{ |
202
|
|
|
parent::__construct( $context ); |
203
|
|
|
$this->setResourceName( 'db-log' ); |
204
|
|
|
|
205
|
|
|
$config = $context->config(); |
206
|
|
|
|
207
|
|
|
/** madmin/log/manager/loglevel |
208
|
|
|
* Sets the severity level for messages to be written to the log |
209
|
|
|
* |
210
|
|
|
* Manager, provider and other active components write messages about |
211
|
|
|
* problems, informational and debug output to the logs. The messages |
212
|
|
|
* that are actually written to the logs can be limited with the |
213
|
|
|
* "loglevel" configuration. |
214
|
|
|
* |
215
|
|
|
* Available log levels are: |
216
|
|
|
* * Emergency (0): system is unusable |
217
|
|
|
* * Alert (1): action must be taken immediately |
218
|
|
|
* * Critical (2): critical conditions |
219
|
|
|
* * Error (3): error conditions |
220
|
|
|
* * Warning (4): warning conditions |
221
|
|
|
* * Notice (5): normal but significant condition |
222
|
|
|
* * Informational (6): informational messages |
223
|
|
|
* * Debug (7): debug messages |
224
|
|
|
* |
225
|
|
|
* The "loglevel" configuration option defines the severity of messages |
226
|
|
|
* that will be written to the logs, e.g. a log level of "3" (error) |
227
|
|
|
* will allow all messages with an associated level of three and below |
228
|
|
|
* (error, critical, alert and emergency) to be written to the storage. |
229
|
|
|
* Messages with other log levels (warning, notice, informational and |
230
|
|
|
* debug) would be discarded and won't be written to the storage. |
231
|
|
|
* |
232
|
|
|
* The higher the log level, the more messages will be written to the |
233
|
|
|
* storage. Keep in mind that a higher volume of messages will slow |
234
|
|
|
* down the system and the debug log level shouldn't be used in |
235
|
|
|
* production environments with a high number of visitors! |
236
|
|
|
* |
237
|
|
|
* @param int Log level number |
238
|
|
|
* @since 2014.03 |
239
|
|
|
* @category Developer |
240
|
|
|
* @category User |
241
|
|
|
*/ |
242
|
|
|
$this->loglevel = $config->get( 'madmin/log/manager/loglevel', \Aimeos\Base\Logger\Iface::NOTICE ); |
243
|
|
|
$this->requestid = md5( php_uname( 'n' ) . getmypid() . date( 'Y-m-d H:i:s' ) ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Removes old entries from the storage. |
249
|
|
|
* |
250
|
|
|
* @param iterable $siteids List of IDs for sites whose entries should be deleted |
251
|
|
|
* @return \Aimeos\MAdmin\Log\Manager\Iface Manager object for chaining method calls |
252
|
|
|
*/ |
253
|
|
|
public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
254
|
|
|
{ |
255
|
|
|
$path = 'madmin/log/manager/submanagers'; |
256
|
|
|
foreach( $this->context()->config()->get( $path, [] ) as $domain ) { |
257
|
|
|
$this->object()->getSubManager( $domain )->clear( $siteids ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this->clearBase( $siteids, 'madmin/log/manager/delete' ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Creates a new empty item instance |
266
|
|
|
* |
267
|
|
|
* @param array $values Values the item should be initialized with |
268
|
|
|
* @return \Aimeos\MAdmin\Log\Item\Iface New log item object |
269
|
|
|
*/ |
270
|
|
|
public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
271
|
|
|
{ |
272
|
|
|
try { |
273
|
|
|
$values['log.siteid'] = $values['log.siteid'] ?? $this->context()->locale()->getSiteId(); |
274
|
|
|
} catch( \Exception $e ) {} // if no locale item is available |
275
|
|
|
|
276
|
|
|
return $this->createItemBase( $values ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Removes multiple items. |
282
|
|
|
* |
283
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items |
284
|
|
|
* @return \Aimeos\MAdmin\Log\Manager\Iface Manager object for chaining method calls |
285
|
|
|
*/ |
286
|
|
|
public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface |
287
|
|
|
{ |
288
|
|
|
/** madmin/log/manager/delete/mysql |
289
|
|
|
* Deletes the items matched by the given IDs from the database |
290
|
|
|
* |
291
|
|
|
* @see madmin/log/manager/delete/ansi |
292
|
|
|
*/ |
293
|
|
|
|
294
|
|
|
/** madmin/log/manager/delete/ansi |
295
|
|
|
* Deletes the items matched by the given IDs from the database |
296
|
|
|
* |
297
|
|
|
* Removes the records specified by the given IDs from the log database. |
298
|
|
|
* The records must be from the site that is configured via the |
299
|
|
|
* context item. |
300
|
|
|
* |
301
|
|
|
* The ":cond" placeholder is replaced by the name of the ID column and |
302
|
|
|
* the given ID or list of IDs while the site ID is bound to the question |
303
|
|
|
* mark. |
304
|
|
|
* |
305
|
|
|
* The SQL statement should conform to the ANSI standard to be |
306
|
|
|
* compatible with most relational database systems. This also |
307
|
|
|
* includes using double quotes for table and column names. |
308
|
|
|
* |
309
|
|
|
* @param string SQL statement for deleting items |
310
|
|
|
* @since 2014.03 |
311
|
|
|
* @category Developer |
312
|
|
|
* @see madmin/log/manager/insert/ansi |
313
|
|
|
* @see madmin/log/manager/update/ansi |
314
|
|
|
* @see madmin/log/manager/newid/ansi |
315
|
|
|
* @see madmin/log/manager/search/ansi |
316
|
|
|
* @see madmin/log/manager/count/ansi |
317
|
|
|
*/ |
318
|
|
|
$path = 'madmin/log/manager/delete'; |
319
|
|
|
|
320
|
|
|
return $this->deleteItemsBase( $itemIds, $path ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Creates the log object for the given log id. |
326
|
|
|
* |
327
|
|
|
* @param string $id Log ID to fetch log object for |
328
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
329
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
330
|
|
|
* @return \Aimeos\MAdmin\Log\Item\Iface Returns the log item of the given id |
331
|
|
|
* @throws \Aimeos\MAdmin\Log\Exception If item couldn't be found |
332
|
|
|
*/ |
333
|
|
|
public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
334
|
|
|
{ |
335
|
|
|
$criteria = $this->object()->filter( $default ); |
336
|
|
|
$expr = [ |
337
|
|
|
$criteria->compare( '==', 'log.id', $id ), |
338
|
|
|
$criteria->getConditions() |
339
|
|
|
]; |
340
|
|
|
$criteria->setConditions( $criteria->and( $expr ) ); |
341
|
|
|
|
342
|
|
|
if( ( $item = $this->object()->search( $criteria, $ref )->first() ) ) { |
343
|
|
|
return $item; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$msg = $this->context()->translate( 'mshop', 'Log entry with ID "%1$s" not found' ); |
347
|
|
|
throw new \Aimeos\MAdmin\Log\Exception( sprintf( $msg, $id ) ); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Returns the available manager types |
353
|
|
|
* |
354
|
|
|
* @param bool $withsub Return also the resource type of sub-managers if true |
355
|
|
|
* @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
356
|
|
|
*/ |
357
|
|
|
public function getResourceType( bool $withsub = true ) : array |
358
|
|
|
{ |
359
|
|
|
$path = 'madmin/log/manager/submanagers'; |
360
|
|
|
return $this->getResourceTypeBase( 'log', $path, [], $withsub ); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Returns the attributes that can be used for searching. |
366
|
|
|
* |
367
|
|
|
* @param bool $withsub Return also attributes of sub-managers if true |
368
|
|
|
* @return \Aimeos\Base\Criteria\Attribute\Iface[] Returns a list of search attributes |
369
|
|
|
*/ |
370
|
|
|
public function getSearchAttributes( bool $withsub = true ) : array |
371
|
|
|
{ |
372
|
|
|
/** madmin/log/manager/submanagers |
373
|
|
|
* List of manager names that can be instantiated by the log manager |
374
|
|
|
* |
375
|
|
|
* Managers provide a generic interface to the underlying storage. |
376
|
|
|
* Each manager has or can have sub-managers caring about particular |
377
|
|
|
* aspects. Each of these sub-managers can be instantiated by its |
378
|
|
|
* parent manager using the getSubManager() method. |
379
|
|
|
* |
380
|
|
|
* The search keys from sub-managers can be normally used in the |
381
|
|
|
* manager as well. It allows you to search for items of the manager |
382
|
|
|
* using the search keys of the sub-managers to further limit the |
383
|
|
|
* retrieved list of items. |
384
|
|
|
* |
385
|
|
|
* @param array List of sub-manager names |
386
|
|
|
* @since 2014.03 |
387
|
|
|
* @category Developer |
388
|
|
|
*/ |
389
|
|
|
$path = 'madmin/log/manager/submanagers'; |
390
|
|
|
|
391
|
|
|
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub ); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Returns a new manager for log extensions |
397
|
|
|
* |
398
|
|
|
* @param string $manager Name of the sub manager type in lower case |
399
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
400
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc. |
401
|
|
|
*/ |
402
|
|
|
public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
403
|
|
|
{ |
404
|
|
|
return $this->getSubManagerBase( 'log', $manager, $name ); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Writes a message to the configured log facility. |
410
|
|
|
* |
411
|
|
|
* @param string|array|object $message Message text that should be written to the log facility |
412
|
|
|
* @param int $priority Priority of the message for filtering |
413
|
|
|
* @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog) |
414
|
|
|
* @return \Aimeos\Base\Logger\Iface Logger object for method chaining |
415
|
|
|
*/ |
416
|
|
|
public function log( $message, int $priority = \Aimeos\Base\Logger\Iface::ERR, string $facility = 'message' ) : \Aimeos\Base\Logger\Iface |
417
|
|
|
{ |
418
|
|
|
if( $priority <= $this->loglevel ) |
419
|
|
|
{ |
420
|
|
|
if( !is_scalar( $message ) ) { |
421
|
|
|
$message = json_encode( $message ); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
$item = $this->object()->create(); |
425
|
|
|
|
426
|
|
|
$item->setFacility( $facility ); |
427
|
|
|
$item->setPriority( $priority ); |
428
|
|
|
$item->setMessage( $message ); |
429
|
|
|
$item->setRequest( $this->requestid ); |
430
|
|
|
|
431
|
|
|
$this->object()->save( $item ); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
return $this; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Adds a new log to the storage. |
440
|
|
|
* |
441
|
|
|
* @param \Aimeos\MAdmin\Log\Item\Iface $item Log item that should be saved to the storage |
442
|
|
|
* @param bool $fetch True if the new ID should be returned in the item |
443
|
|
|
* @return \Aimeos\MAdmin\Log\Item\Iface Updated item including the generated ID |
444
|
|
|
*/ |
445
|
|
|
protected function saveItem( \Aimeos\MAdmin\Log\Item\Iface $item, bool $fetch = true ) : \Aimeos\MAdmin\Log\Item\Iface |
446
|
|
|
{ |
447
|
|
|
if( !$item->isModified() ) { |
448
|
|
|
return $item; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
try { |
452
|
|
|
$siteid = $this->context()->locale()->getSiteId(); |
453
|
|
|
} catch( \Exception $e ) { |
454
|
|
|
$siteid = ''; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
$id = $item->getId(); |
458
|
|
|
$columns = $this->object()->getSaveAttributes(); |
459
|
|
|
$conn = $this->context()->db( $this->getResourceName(), true ); |
460
|
|
|
|
461
|
|
|
if( $id === null ) |
462
|
|
|
{ |
463
|
|
|
/** madmin/log/manager/insert/mysql |
464
|
|
|
* Inserts a new log record into the database table |
465
|
|
|
* |
466
|
|
|
* @see madmin/log/manager/insert/ansi |
467
|
|
|
*/ |
468
|
|
|
|
469
|
|
|
/** madmin/log/manager/insert/ansi |
470
|
|
|
* Inserts a new log record into the database table |
471
|
|
|
* |
472
|
|
|
* Items with no ID yet (i.e. the ID is NULL) will be created in |
473
|
|
|
* the database and the newly created ID retrieved afterwards |
474
|
|
|
* using the "newid" SQL statement. |
475
|
|
|
* |
476
|
|
|
* The SQL statement must be a string suitable for being used as |
477
|
|
|
* prepared statement. It must include question marks for binding |
478
|
|
|
* the values from the log item to the statement before they are |
479
|
|
|
* sent to the database server. The number of question marks must |
480
|
|
|
* be the same as the number of columns listed in the INSERT |
481
|
|
|
* statement. The order of the columns must correspond to the |
482
|
|
|
* order in the save() method, so the correct values are |
483
|
|
|
* bound to the columns. |
484
|
|
|
* |
485
|
|
|
* The SQL statement should conform to the ANSI standard to be |
486
|
|
|
* compatible with most relational database systems. This also |
487
|
|
|
* includes using double quotes for table and column names. |
488
|
|
|
* |
489
|
|
|
* @param string SQL statement for inserting records |
490
|
|
|
* @since 2014.03 |
491
|
|
|
* @category Developer |
492
|
|
|
* @see madmin/log/manager/update/ansi |
493
|
|
|
* @see madmin/log/manager/newid/ansi |
494
|
|
|
* @see madmin/log/manager/delete/ansi |
495
|
|
|
* @see madmin/log/manager/search/ansi |
496
|
|
|
* @see madmin/log/manager/count/ansi |
497
|
|
|
*/ |
498
|
|
|
$path = 'madmin/log/manager/insert'; |
499
|
|
|
$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) ); |
|
|
|
|
500
|
|
|
} |
501
|
|
|
else |
502
|
|
|
{ |
503
|
|
|
/** madmin/log/manager/update/mysql |
504
|
|
|
* Updates an existing log record in the database |
505
|
|
|
* |
506
|
|
|
* @see madmin/log/manager/update/ansi |
507
|
|
|
*/ |
508
|
|
|
|
509
|
|
|
/** madmin/log/manager/update/ansi |
510
|
|
|
* Updates an existing log record in the database |
511
|
|
|
* |
512
|
|
|
* Items which already have an ID (i.e. the ID is not NULL) will |
513
|
|
|
* be updated in the database. |
514
|
|
|
* |
515
|
|
|
* The SQL statement must be a string suitable for being used as |
516
|
|
|
* prepared statement. It must include question marks for binding |
517
|
|
|
* the values from the log item to the statement before they are |
518
|
|
|
* sent to the database server. The order of the columns must |
519
|
|
|
* correspond to the order in the save() method, so the |
520
|
|
|
* correct values are bound to the columns. |
521
|
|
|
* |
522
|
|
|
* The SQL statement should conform to the ANSI standard to be |
523
|
|
|
* compatible with most relational database systems. This also |
524
|
|
|
* includes using double quotes for table and column names. |
525
|
|
|
* |
526
|
|
|
* @param string SQL statement for updating records |
527
|
|
|
* @since 2014.03 |
528
|
|
|
* @category Developer |
529
|
|
|
* @see madmin/log/manager/insert/ansi |
530
|
|
|
* @see madmin/log/manager/newid/ansi |
531
|
|
|
* @see madmin/log/manager/delete/ansi |
532
|
|
|
* @see madmin/log/manager/search/ansi |
533
|
|
|
* @see madmin/log/manager/count/ansi |
534
|
|
|
*/ |
535
|
|
|
$path = 'madmin/log/manager/update'; |
536
|
|
|
$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false ); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
$idx = 1; |
540
|
|
|
$stmt = $this->getCachedStatement( $conn, $path, $sql ); |
541
|
|
|
|
542
|
|
|
foreach( $columns as $name => $entry ) { |
543
|
|
|
$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() ); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
|
547
|
|
|
$stmt->bind( $idx++, $item->getFacility() ); |
548
|
|
|
$stmt->bind( $idx++, date( 'Y-m-d H:i:s' ) ); |
549
|
|
|
$stmt->bind( $idx++, $item->getPriority(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
550
|
|
|
$stmt->bind( $idx++, $item->getMessage() ); |
551
|
|
|
$stmt->bind( $idx++, $item->getRequest() ); |
552
|
|
|
$stmt->bind( $idx++, $siteid ); |
553
|
|
|
|
554
|
|
|
if( $item->getId() !== null ) { |
555
|
|
|
$stmt->bind( $idx++, $item->getId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
$stmt->execute()->finish(); |
559
|
|
|
|
560
|
|
|
if( $id === null && $fetch === true ) |
561
|
|
|
{ |
562
|
|
|
/** madmin/log/manager/newid/mysql |
563
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
564
|
|
|
* |
565
|
|
|
* @see madmin/log/manager/newid/ansi |
566
|
|
|
*/ |
567
|
|
|
|
568
|
|
|
/** madmin/log/manager/newid/ansi |
569
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
570
|
|
|
* |
571
|
|
|
* As soon as a new record is inserted into the database table, |
572
|
|
|
* the database server generates a new and unique identifier for |
573
|
|
|
* that record. This ID can be used for retrieving, updating and |
574
|
|
|
* deleting that specific record from the table again. |
575
|
|
|
* |
576
|
|
|
* For MySQL: |
577
|
|
|
* SELECT LAST_INSERT_ID() |
578
|
|
|
* For PostgreSQL: |
579
|
|
|
* SELECT currval('seq_malog_id') |
580
|
|
|
* For SQL Server: |
581
|
|
|
* SELECT SCOPE_IDENTITY() |
582
|
|
|
* For Oracle: |
583
|
|
|
* SELECT "seq_malog_id".CURRVAL FROM DUAL |
584
|
|
|
* |
585
|
|
|
* There's no way to retrive the new ID by a SQL statements that |
586
|
|
|
* fits for most database servers as they implement their own |
587
|
|
|
* specific way. |
588
|
|
|
* |
589
|
|
|
* @param string SQL statement for retrieving the last inserted record ID |
590
|
|
|
* @since 2014.03 |
591
|
|
|
* @category Developer |
592
|
|
|
* @see madmin/log/manager/insert/ansi |
593
|
|
|
* @see madmin/log/manager/update/ansi |
594
|
|
|
* @see madmin/log/manager/delete/ansi |
595
|
|
|
* @see madmin/log/manager/search/ansi |
596
|
|
|
* @see madmin/log/manager/count/ansi |
597
|
|
|
*/ |
598
|
|
|
$id = $this->newId( $conn, 'madmin/log/manager/newid' ); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
$conn->close(); |
602
|
|
|
|
603
|
|
|
return $item->setId( $id ); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Search for log entries based on the given criteria. |
609
|
|
|
* |
610
|
|
|
* @param \Aimeos\Base\Criteria\Iface $search Search object containing the conditions |
611
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
612
|
|
|
* @param int &$total Number of items that are available in total |
613
|
|
|
* @return \Aimeos\Map List of items implementing Aimeos\MAdmin\Log\Item\Iface with ids as keys |
614
|
|
|
*/ |
615
|
|
|
public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map |
616
|
|
|
{ |
617
|
|
|
$items = []; |
618
|
|
|
$context = $this->context(); |
619
|
|
|
$conn = $context->db( $this->getResourceName() ); |
620
|
|
|
|
621
|
|
|
$required = array( 'log' ); |
622
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE; |
623
|
|
|
|
624
|
|
|
/** madmin/log/manager/search/mysql |
625
|
|
|
* Retrieves the records matched by the given criteria in the database |
626
|
|
|
* |
627
|
|
|
* @see madmin/log/manager/search/ansi |
628
|
|
|
*/ |
629
|
|
|
|
630
|
|
|
/** madmin/log/manager/search/ansi |
631
|
|
|
* Retrieves the records matched by the given criteria in the database |
632
|
|
|
* |
633
|
|
|
* Fetches the records matched by the given criteria from the log |
634
|
|
|
* database. The records must be from one of the sites that are |
635
|
|
|
* configured via the context item. If the current site is part of |
636
|
|
|
* a tree of sites, the SELECT statement can retrieve all records |
637
|
|
|
* from the current site and the complete sub-tree of sites. |
638
|
|
|
* |
639
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
640
|
|
|
* their tables must be joined in the SQL context. This is done by |
641
|
|
|
* using the "internaldeps" property from the definition of the ID |
642
|
|
|
* column of the sub-managers. These internal dependencies specify |
643
|
|
|
* the JOIN between the tables and the used columns for joining. The |
644
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
645
|
|
|
* the sub-managers. |
646
|
|
|
* |
647
|
|
|
* To limit the records matched, conditions can be added to the given |
648
|
|
|
* criteria object. It can contain comparisons like column names that |
649
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
650
|
|
|
* operators. The resulting string of SQL conditions replaces the |
651
|
|
|
* ":cond" placeholder before the statement is sent to the database |
652
|
|
|
* server. |
653
|
|
|
* |
654
|
|
|
* If the records that are retrieved should be ordered by one or more |
655
|
|
|
* columns, the generated string of column / sort direction pairs |
656
|
|
|
* replaces the ":order" placeholder. In case no ordering is required, |
657
|
|
|
* the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/" |
658
|
|
|
* markers is removed to speed up retrieving the records. Columns of |
659
|
|
|
* sub-managers can also be used for ordering the result set but then |
660
|
|
|
* no index can be used. |
661
|
|
|
* |
662
|
|
|
* The number of returned records can be limited and can start at any |
663
|
|
|
* number between the begining and the end of the result set. For that |
664
|
|
|
* the ":size" and ":start" placeholders are replaced by the |
665
|
|
|
* corresponding values from the criteria object. The default values |
666
|
|
|
* are 0 for the start and 100 for the size value. |
667
|
|
|
* |
668
|
|
|
* The SQL statement should conform to the ANSI standard to be |
669
|
|
|
* compatible with most relational database systems. This also |
670
|
|
|
* includes using double quotes for table and column names. |
671
|
|
|
* |
672
|
|
|
* @param string SQL statement for searching items |
673
|
|
|
* @since 2014.03 |
674
|
|
|
* @category Developer |
675
|
|
|
* @see madmin/log/manager/insert/ansi |
676
|
|
|
* @see madmin/log/manager/update/ansi |
677
|
|
|
* @see madmin/log/manager/newid/ansi |
678
|
|
|
* @see madmin/log/manager/delete/ansi |
679
|
|
|
* @see madmin/log/manager/count/ansi |
680
|
|
|
*/ |
681
|
|
|
$cfgPathSearch = 'madmin/log/manager/search'; |
682
|
|
|
|
683
|
|
|
/** madmin/log/manager/count/mysql |
684
|
|
|
* Counts the number of records matched by the given criteria in the database |
685
|
|
|
* |
686
|
|
|
* @see madmin/log/manager/count/ansi |
687
|
|
|
*/ |
688
|
|
|
|
689
|
|
|
/** madmin/log/manager/count/ansi |
690
|
|
|
* Counts the number of records matched by the given criteria in the database |
691
|
|
|
* |
692
|
|
|
* Counts all records matched by the given criteria from the log |
693
|
|
|
* database. The records must be from one of the sites that are |
694
|
|
|
* configured via the context item. If the current site is part of |
695
|
|
|
* a tree of sites, the statement can count all records from the |
696
|
|
|
* current site and the complete sub-tree of sites. |
697
|
|
|
* |
698
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
699
|
|
|
* their tables must be joined in the SQL context. This is done by |
700
|
|
|
* using the "internaldeps" property from the definition of the ID |
701
|
|
|
* column of the sub-managers. These internal dependencies specify |
702
|
|
|
* the JOIN between the tables and the used columns for joining. The |
703
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
704
|
|
|
* the sub-managers. |
705
|
|
|
* |
706
|
|
|
* To limit the records matched, conditions can be added to the given |
707
|
|
|
* criteria object. It can contain comparisons like column names that |
708
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
709
|
|
|
* operators. The resulting string of SQL conditions replaces the |
710
|
|
|
* ":cond" placeholder before the statement is sent to the database |
711
|
|
|
* server. |
712
|
|
|
* |
713
|
|
|
* Both, the strings for ":joins" and for ":cond" are the same as for |
714
|
|
|
* the "search" SQL statement. |
715
|
|
|
* |
716
|
|
|
* Contrary to the "search" statement, it doesn't return any records |
717
|
|
|
* but instead the number of records that have been found. As counting |
718
|
|
|
* thousands of records can be a long running task, the maximum number |
719
|
|
|
* of counted records is limited for performance reasons. |
720
|
|
|
* |
721
|
|
|
* The SQL statement should conform to the ANSI standard to be |
722
|
|
|
* compatible with most relational database systems. This also |
723
|
|
|
* includes using double quotes for table and column names. |
724
|
|
|
* |
725
|
|
|
* @param string SQL statement for counting items |
726
|
|
|
* @since 2014.03 |
727
|
|
|
* @category Developer |
728
|
|
|
* @see madmin/log/manager/insert/ansi |
729
|
|
|
* @see madmin/log/manager/update/ansi |
730
|
|
|
* @see madmin/log/manager/newid/ansi |
731
|
|
|
* @see madmin/log/manager/delete/ansi |
732
|
|
|
* @see madmin/log/manager/search/ansi |
733
|
|
|
*/ |
734
|
|
|
$cfgPathCount = 'madmin/log/manager/count'; |
735
|
|
|
|
736
|
|
|
$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
737
|
|
|
|
738
|
|
|
while( ( $row = $results->fetch() ) !== null ) |
739
|
|
|
{ |
740
|
|
|
if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) { |
741
|
|
|
$items[$row['log.id']] = $item; |
742
|
|
|
} |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
return map( $items ); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Create new admin log item object initialized with given parameters. |
751
|
|
|
* |
752
|
|
|
* @param array $values Associative list of key/value pairs of a job |
753
|
|
|
* @return \Aimeos\MAdmin\Log\Item\Iface New log item |
754
|
|
|
*/ |
755
|
|
|
protected function createItemBase( array $values = [] ) : \Aimeos\MAdmin\Log\Item\Iface |
756
|
|
|
{ |
757
|
|
|
return new \Aimeos\MAdmin\Log\Item\Standard( $values ); |
758
|
|
|
} |
759
|
|
|
} |
760
|
|
|
|