RestApi
1 month ago
LogHandler.php
10 months ago
LogListingRepository.php
1 month ago
LogRepository.php
2 weeks ago
LoggerFactory.php
11 months ago
LogsDownload.php
2 weeks ago
PluginVersionProcessor.php
3 years ago
index.php
3 years ago
LogRepository.php
320 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Logging; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\Repository; |
| 9 | use MailPoet\Entities\LogEntity; |
| 10 | use MailPoet\Entities\NewsletterEntity; |
| 11 | use MailPoet\InvalidStateException; |
| 12 | use MailPoet\Util\Helpers; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 15 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 16 | |
| 17 | /** |
| 18 | * @extends Repository<LogEntity> |
| 19 | */ |
| 20 | class LogRepository extends Repository { |
| 21 | public function saveLog(LogEntity $log): void { |
| 22 | // Save log entity using DBAL to avoid calling "flush()" on the entity manager. |
| 23 | // Calling "flush()" can have unintended side effects, such as saving unwanted |
| 24 | // changes or trying to save entities that were detached from the entity manager. |
| 25 | $this->entityManager->getConnection()->insert( |
| 26 | $this->entityManager->getClassMetadata(LogEntity::class)->getTableName(), |
| 27 | [ |
| 28 | 'name' => $log->getName(), |
| 29 | 'level' => $log->getLevel(), |
| 30 | 'message' => $log->getMessage(), |
| 31 | 'raw_message' => $log->getRawMessage(), |
| 32 | 'context' => json_encode($log->getContext()), |
| 33 | 'created_at' => ( |
| 34 | $log->getCreatedAt() ?? Carbon::now()->millisecond(0) |
| 35 | )->format('Y-m-d H:i:s'), |
| 36 | ], |
| 37 | ); |
| 38 | |
| 39 | // sync the changes with the entity manager |
| 40 | if ($this->entityManager->isOpen()) { |
| 41 | $lastInsertId = (int)$this->entityManager->getConnection()->lastInsertId(); |
| 42 | $log->setId($lastInsertId); |
| 43 | $this->entityManager->getUnitOfWork()->registerManaged($log, ['id' => $log->getId()], []); |
| 44 | $this->entityManager->refresh($log); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param \DateTimeInterface|null $dateFrom |
| 50 | * @param \DateTimeInterface|null $dateTo |
| 51 | * @param string|null $search |
| 52 | * @param string $offset |
| 53 | * @param string $limit |
| 54 | * @return LogEntity[] |
| 55 | */ |
| 56 | public function getLogs( |
| 57 | ?\DateTimeInterface $dateFrom = null, |
| 58 | ?\DateTimeInterface $dateTo = null, |
| 59 | ?string $search = null, |
| 60 | ?string $offset = null, |
| 61 | ?string $limit = null |
| 62 | ): array { |
| 63 | $query = $this->doctrineRepository->createQueryBuilder('l') |
| 64 | ->select('l'); |
| 65 | |
| 66 | if ($dateFrom instanceof \DateTimeInterface) { |
| 67 | $query |
| 68 | ->andWhere('l.createdAt >= :dateFrom') |
| 69 | ->setParameter('dateFrom', $dateFrom->format('Y-m-d 00:00:00')); |
| 70 | } |
| 71 | if ($dateTo instanceof \DateTimeInterface) { |
| 72 | $query |
| 73 | ->andWhere('l.createdAt <= :dateTo') |
| 74 | ->setParameter('dateTo', $dateTo->format('Y-m-d 23:59:59')); |
| 75 | } |
| 76 | if ($search) { |
| 77 | $search = Helpers::escapeSearch($search); |
| 78 | $query |
| 79 | ->andWhere('l.name LIKE :search or l.message LIKE :search') |
| 80 | ->setParameter('search', "%$search%"); |
| 81 | } |
| 82 | |
| 83 | $query->orderBy('l.createdAt', 'desc'); |
| 84 | if ($offset !== null) { |
| 85 | $query->setFirstResult((int)$offset); |
| 86 | } |
| 87 | if ($limit === null) { |
| 88 | $query->setMaxResults(500); |
| 89 | } else { |
| 90 | $query->setMaxResults((int)$limit); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | return $query->getQuery()->getResult(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Distinct log names (sources), used to populate the listing's name filter. |
| 99 | * |
| 100 | * @return string[] |
| 101 | */ |
| 102 | public function getDistinctNames(): array { |
| 103 | $rows = $this->entityManager->createQueryBuilder() |
| 104 | ->select('DISTINCT l.name') |
| 105 | ->from(LogEntity::class, 'l') |
| 106 | ->where('l.name IS NOT NULL') |
| 107 | ->andWhere("l.name != ''") |
| 108 | ->orderBy('l.name', 'asc') |
| 109 | ->getQuery() |
| 110 | ->getSingleColumnResult(); |
| 111 | |
| 112 | $names = []; |
| 113 | foreach ($rows as $row) { |
| 114 | if (is_string($row)) { |
| 115 | $names[] = $row; |
| 116 | } |
| 117 | } |
| 118 | return $names; |
| 119 | } |
| 120 | |
| 121 | public function purgeOldLogs(int $daysToKeepLogs, int $limit = 1000): int { |
| 122 | $logsTable = $this->entityManager->getClassMetadata(LogEntity::class)->getTableName(); |
| 123 | $result = $this->entityManager->getConnection()->executeStatement( |
| 124 | " |
| 125 | DELETE FROM `{$logsTable}` |
| 126 | WHERE `created_at` < :date |
| 127 | ORDER BY `created_at` ASC, `id` ASC |
| 128 | LIMIT :limit |
| 129 | ", |
| 130 | [ |
| 131 | 'date' => Carbon::now()->subDays($daysToKeepLogs)->toDateTimeString(), |
| 132 | 'limit' => $limit, |
| 133 | ], |
| 134 | [ |
| 135 | 'date' => ParameterType::STRING, |
| 136 | 'limit' => ParameterType::INTEGER, |
| 137 | ] |
| 138 | ); |
| 139 | |
| 140 | return (int)$result; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Delete logs matching the listing's filter shape (`from`/`to`/`name`/`level`) |
| 145 | * and free-text search, so a deletion removes exactly what the filtered |
| 146 | * listing shows. Deletes in batches to keep each statement bounded on large |
| 147 | * log tables, mirroring purgeOldLogs(). |
| 148 | * |
| 149 | * @param array{from?: string, to?: string, name?: string[], level?: int[]} $filter |
| 150 | */ |
| 151 | public function deleteLogs(array $filter, ?string $search = null, int $batchSize = 1000): int { |
| 152 | $logsTable = $this->entityManager->getClassMetadata(LogEntity::class)->getTableName(); |
| 153 | [$where, $parameters, $types] = $this->buildFilterSql($filter, $search); |
| 154 | $parameters['batch_limit'] = $batchSize; |
| 155 | $types['batch_limit'] = ParameterType::INTEGER; |
| 156 | |
| 157 | $sql = "DELETE FROM `{$logsTable}`{$where} ORDER BY `created_at` ASC, `id` ASC LIMIT :batch_limit"; |
| 158 | $connection = $this->entityManager->getConnection(); |
| 159 | |
| 160 | $deleted = 0; |
| 161 | do { |
| 162 | $affected = (int)$connection->executeStatement($sql, $parameters, $types); |
| 163 | $deleted += $affected; |
| 164 | } while ($affected === $batchSize); |
| 165 | |
| 166 | return $deleted; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Fetch logs matching the listing's filter shape (`from`/`to`/`name`/`level`) |
| 171 | * and free-text search for export, so a download contains exactly the rows the |
| 172 | * filtered listing shows. Mirrors deleteLogs()'s WHERE clause and is capped by |
| 173 | * $limit. |
| 174 | * |
| 175 | * Yields rows in batches via keyset pagination on (`created_at`, `id`) rather |
| 176 | * than one big result set. MailPoet's WPDB-backed Doctrine driver buffers an |
| 177 | * entire result in memory (see Doctrine\WPDB\Result), so a single |
| 178 | * `LIMIT 50000` query would load every row at once and can exhaust PHP's |
| 179 | * memory on large installs. Paging keeps peak memory at one batch. |
| 180 | * |
| 181 | * @param array{from?: string, to?: string, name?: string[], level?: int[]} $filter |
| 182 | * @return iterable<int, array{created_at: string, name: string|null, message: string|null}> |
| 183 | */ |
| 184 | public function getLogsForExport(array $filter, ?string $search = null, int $limit = 50000, int $batchSize = 1000): iterable { |
| 185 | $logsTable = $this->entityManager->getClassMetadata(LogEntity::class)->getTableName(); |
| 186 | [$where, $baseParameters, $baseTypes] = $this->buildFilterSql($filter, $search); |
| 187 | |
| 188 | $batchSize = $batchSize > 0 ? $batchSize : 1000; |
| 189 | $remaining = $limit; |
| 190 | $lastCreatedAt = null; |
| 191 | $lastId = null; |
| 192 | |
| 193 | while ($remaining > 0) { |
| 194 | $batch = (int)min($batchSize, $remaining); |
| 195 | $parameters = $baseParameters; |
| 196 | $types = $baseTypes; |
| 197 | $conditions = $where; |
| 198 | |
| 199 | if ($lastCreatedAt !== null) { |
| 200 | // Keyset cursor: continue strictly after the last row in the same |
| 201 | // (`created_at` DESC, `id` DESC) order. Stable even while new rows are |
| 202 | // inserted at the top of the table during the export. |
| 203 | $keyset = '(`created_at` < :ks_created_at OR (`created_at` = :ks_created_at AND `id` < :ks_id))'; |
| 204 | $conditions = $conditions === '' ? ' WHERE ' . $keyset : $conditions . ' AND ' . $keyset; |
| 205 | $parameters['ks_created_at'] = $lastCreatedAt; |
| 206 | $parameters['ks_id'] = $lastId; |
| 207 | $types['ks_created_at'] = ParameterType::STRING; |
| 208 | $types['ks_id'] = ParameterType::INTEGER; |
| 209 | } |
| 210 | |
| 211 | $parameters['batch_limit'] = $batch; |
| 212 | $types['batch_limit'] = ParameterType::INTEGER; |
| 213 | |
| 214 | $sql = "SELECT `id`, `created_at`, `name`, `message` FROM `{$logsTable}`{$conditions} ORDER BY `created_at` DESC, `id` DESC LIMIT :batch_limit"; |
| 215 | |
| 216 | $rows = $this->entityManager->getConnection() |
| 217 | ->executeQuery($sql, $parameters, $types) |
| 218 | ->fetchAllAssociative(); |
| 219 | |
| 220 | if ($rows === []) { |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | foreach ($rows as $row) { |
| 225 | $lastCreatedAt = $this->castToNullableString($row['created_at']); |
| 226 | $lastId = (int)$this->castToNullableString($row['id']); |
| 227 | yield [ |
| 228 | 'created_at' => $this->castToNullableString($row['created_at']) ?? '', |
| 229 | 'name' => $this->castToNullableString($row['name']), |
| 230 | 'message' => $this->castToNullableString($row['message']), |
| 231 | ]; |
| 232 | $remaining--; |
| 233 | } |
| 234 | |
| 235 | // A short page means the table is exhausted; stop before an empty query. |
| 236 | if (count($rows) < $batch) { |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @param mixed $value |
| 244 | */ |
| 245 | private function castToNullableString($value): ?string { |
| 246 | return is_scalar($value) ? (string)$value : null; |
| 247 | } |
| 248 | |
| 249 | public function getRawMessagesForNewsletter(NewsletterEntity $newsletter, string $topic): array { |
| 250 | return $this->entityManager->createQueryBuilder() |
| 251 | ->select('DISTINCT logs.rawMessage message') |
| 252 | ->from(LogEntity::class, 'logs') |
| 253 | ->where('logs.name = :topic') |
| 254 | ->andWhere('logs.context LIKE :context') |
| 255 | ->orderBy('logs.createdAt') |
| 256 | ->setParameter('context', json_encode(['newsletter_id' => $newsletter->getId()])) |
| 257 | ->setParameter('topic', $topic) |
| 258 | ->getQuery() |
| 259 | ->getSingleColumnResult(); |
| 260 | } |
| 261 | |
| 262 | public function persist($entity): void { |
| 263 | throw new InvalidStateException('Use saveLog() instead to avoid unintended side effects'); |
| 264 | } |
| 265 | |
| 266 | public function flush(): void { |
| 267 | throw new InvalidStateException('Use saveLog() instead to avoid unintended side effects'); |
| 268 | } |
| 269 | |
| 270 | protected function getEntityClassName() { |
| 271 | return LogEntity::class; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Build the WHERE clause shared by log deletion. Day boundaries |
| 276 | * (`00:00:00`–`23:59:59`) and the literal LOCATE() search match |
| 277 | * LogListingRepository so deleting honours the same rows the listing shows. |
| 278 | * |
| 279 | * @param array{from?: string, to?: string, name?: string[], level?: int[]} $filter |
| 280 | * @return array{0: string, 1: array<string, mixed>, 2: array<string, int>} |
| 281 | */ |
| 282 | private function buildFilterSql(array $filter, ?string $search): array { |
| 283 | $conditions = []; |
| 284 | $parameters = []; |
| 285 | $types = []; |
| 286 | |
| 287 | if (!empty($filter['from'])) { |
| 288 | $conditions[] = '`created_at` >= :date_from'; |
| 289 | $parameters['date_from'] = $filter['from'] . ' 00:00:00'; |
| 290 | $types['date_from'] = ParameterType::STRING; |
| 291 | } |
| 292 | if (!empty($filter['to'])) { |
| 293 | $conditions[] = '`created_at` <= :date_to'; |
| 294 | $parameters['date_to'] = $filter['to'] . ' 23:59:59'; |
| 295 | $types['date_to'] = ParameterType::STRING; |
| 296 | } |
| 297 | if (!empty($filter['name'])) { |
| 298 | $conditions[] = '`name` IN (:names)'; |
| 299 | $parameters['names'] = array_values($filter['name']); |
| 300 | $types['names'] = ArrayParameterType::STRING; |
| 301 | } |
| 302 | if (!empty($filter['level'])) { |
| 303 | $conditions[] = '`level` IN (:levels)'; |
| 304 | $parameters['levels'] = array_values($filter['level']); |
| 305 | $types['levels'] = ArrayParameterType::INTEGER; |
| 306 | } |
| 307 | if ($search !== null && trim($search) !== '') { |
| 308 | $conditions[] = '(LOCATE(:search, `name`) > 0 OR LOCATE(:search, `message`) > 0)'; |
| 309 | $parameters['search'] = trim($search); |
| 310 | $types['search'] = ParameterType::STRING; |
| 311 | } |
| 312 | |
| 313 | return [ |
| 314 | $conditions === [] ? '' : ' WHERE ' . implode(' AND ', $conditions), |
| 315 | $parameters, |
| 316 | $types, |
| 317 | ]; |
| 318 | } |
| 319 | } |
| 320 |