Skip to content

Commit

Permalink
better feedback in database update process (18 class), better phpdoc …
Browse files Browse the repository at this point in the history
…in settings class, monolog updated
  • Loading branch information
jcvignoli committed Nov 14, 2024
1 parent 0167eea commit a0478b6
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"vimeo/psalm": "@stable",
"wapmorgan/php-deprecation-detector": "@stable",
"codeception/module-webdriver": "*",
"php-stubs/wp-cli-stubs": "^2.10"
"php-stubs/wp-cli-stubs": "@stable"
},
"require": {
"php": "8.0",
Expand Down
6 changes: 3 additions & 3 deletions dist/class/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function lumiere_select_type_search (): array {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_admin_tablename() by Settings::get_admin_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_ADMIN_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_admin_tablename(): string {
return get_option( 'imdbAdminOptions' ) !== false ? 'imdbAdminOptions' : self::LUMIERE_ADMIN_OPTIONS;
Expand All @@ -667,7 +667,7 @@ public static function get_compat_admin_tablename(): string {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_data_tablename() by Settings::get_data_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_DATA_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_data_tablename(): string {
return get_option( 'imdbWidgetOptions' ) !== false ? 'imdbWidgetOptions' : self::LUMIERE_DATA_OPTIONS;
Expand All @@ -679,7 +679,7 @@ public static function get_compat_data_tablename(): string {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_cache_tablename() by Settings::get_cache_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_CACHE_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_cache_tablename(): string {
return get_option( 'imdbCacheOptions' ) !== false ? 'imdbCacheOptions' : self::LUMIERE_CACHE_OPTIONS;
Expand Down
6 changes: 6 additions & 0 deletions dist/class/updates/18.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ protected function lumiere_run_local_update(): void {

if ( $execute_sql ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Successfully renamed table ' . $old_admin_table . ' to ' . $new_admin_table );
} elseif ( $execute_sql === 0 && count( get_option( $new_admin_table ) ) > 0 ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Update row ' . $new_admin_table . ' not needed, row already exists.' );
} else {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Failed to rename table ' . $old_admin_table . ' to ' . $new_admin_table . ' Last DB error: ' . $wpdb->last_error );
}
Expand All @@ -100,6 +102,8 @@ protected function lumiere_run_local_update(): void {

if ( $execute_sql ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Successfully renamed table ' . $old_data_table . ' to ' . $new_data_table );
} elseif ( $execute_sql === 0 && count( get_option( $new_data_table ) ) > 0 ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Update row ' . $new_data_table . ' not needed, row already exists.' );
} else {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Failed to rename table ' . $old_data_table . ' to ' . $new_data_table . ' Last DB error: ' . $wpdb->last_error );

Expand All @@ -114,6 +118,8 @@ protected function lumiere_run_local_update(): void {

if ( $execute_sql ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Successfully renamed table ' . $old_cache_table . ' to ' . $new_cache_table );
} elseif ( $execute_sql === 0 && count( get_option( $new_cache_table ) ) > 0 ) {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Update row ' . $new_cache_table . ' not needed, row already exists.' );
} else {
$this->logger->log()->info( '[Lumiere][updateVersion' . self::LUMIERE_NUMBER_UPDATE . '] Failed to rename table ' . $old_cache_table . ' to ' . $new_cache_table . ' Last DB error: ' . $wpdb->last_error );
}
Expand Down
14 changes: 7 additions & 7 deletions dist/vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@
},
{
"name": "monolog/monolog",
"version": "2.9.3",
"version_normalized": "2.9.3.0",
"version": "2.10.0",
"version_normalized": "2.10.0.0",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/monolog.git",
"reference": "a30bfe2e142720dfa990d0a7e573997f5d884215"
"reference": "5cf826f2991858b54d5c3809bee745560a1042a7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/a30bfe2e142720dfa990d0a7e573997f5d884215",
"reference": "a30bfe2e142720dfa990d0a7e573997f5d884215",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/5cf826f2991858b54d5c3809bee745560a1042a7",
"reference": "5cf826f2991858b54d5c3809bee745560a1042a7",
"shasum": ""
},
"require": {
Expand Down Expand Up @@ -139,7 +139,7 @@
"rollbar/rollbar": "Allow sending log messages to Rollbar",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server"
},
"time": "2024-04-12T20:52:51+00:00",
"time": "2024-11-12T12:43:37+00:00",
"type": "library",
"extra": {
"branch-alias": {
Expand Down Expand Up @@ -172,7 +172,7 @@
],
"support": {
"issues": "https://github.com/Seldaek/monolog/issues",
"source": "https://github.com/Seldaek/monolog/tree/2.9.3"
"source": "https://github.com/Seldaek/monolog/tree/2.10.0"
},
"funding": [
{
Expand Down
10 changes: 5 additions & 5 deletions dist/vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'jcvignoli/lumiere-movies',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'ba1848e88fc763d17e8af3f2584f9f95ca114fd4',
'reference' => '0167eea6c19172882128517b6e932040dc8fe80e',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -24,16 +24,16 @@
'jcvignoli/lumiere-movies' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'ba1848e88fc763d17e8af3f2584f9f95ca114fd4',
'reference' => '0167eea6c19172882128517b6e932040dc8fe80e',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev_requirement' => false,
),
'monolog/monolog' => array(
'pretty_version' => '2.9.3',
'version' => '2.9.3.0',
'reference' => 'a30bfe2e142720dfa990d0a7e573997f5d884215',
'pretty_version' => '2.10.0',
'version' => '2.10.0.0',
'reference' => '5cf826f2991858b54d5c3809bee745560a1042a7',
'type' => 'library',
'install_path' => __DIR__ . '/../monolog/monolog',
'aliases' => array(),
Expand Down
8 changes: 8 additions & 0 deletions dist/vendor/monolog/monolog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 2.10.0 (2024-11-12)

* Added `$fileOpenMode` to `StreamHandler` to define a custom fopen mode to open the log file (#1913)
* Fixed `StreamHandler` handling of write failures so that it now closes/reopens the stream and retries the write once before failing (#1882)
* Fixed `StreamHandler` error handler causing issues if a stream handler triggers an error (#1866)
* Fixed `JsonFormatter` handling of incomplete classes (#1834)
* Fixed `RotatingFileHandler` bug where rotation could sometimes not happen correctly (#1905)

### 2.9.3 (2024-04-12)

* Fixed PHP 8.4 deprecation warnings (#1874)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ protected function normalize($data, int $depth = 0)
return $data;
}

if (\get_class($data) === '__PHP_Incomplete_Class') {
return new \ArrayObject($data);
}

if (method_exists($data, '__toString')) {
return $data->__toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ protected function bulkSend(array $records): void
*/
protected function createExceptionFromResponses($responses): Throwable
{
// @phpstan-ignore offsetAccess.nonOffsetAccessible
foreach ($responses['items'] ?? [] as $item) {
if (isset($item['index']['error'])) {
return $this->createExceptionFromError($item['index']['error']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,22 @@ public function setFilenameFormat(string $filenameFormat, string $dateFormat): s
*/
protected function write(array $record): void
{
// on the first record written, if the log is new, we should rotate (once per day)
// on the first record written, if the log is new, we rotate (once per day) after the log has been written so that the new file exists
if (null === $this->mustRotate) {
$this->mustRotate = null === $this->url || !file_exists($this->url);
}

// if the next rotation is expired, then we rotate immediately
if ($this->nextRotation <= $record['datetime']) {
$this->mustRotate = true;
$this->close();
$this->close(); // triggers rotation
}

parent::write($record);

if ($this->mustRotate) {
$this->close(); // triggers rotation
}
}

/**
Expand All @@ -134,6 +139,8 @@ protected function rotate(): void
$this->url = $this->getTimedFilename();
$this->nextRotation = new \DateTimeImmutable('tomorrow');

$this->mustRotate = false;

// skip GC of old logs if files are unlimited
if (0 === $this->maxFiles) {
return;
Expand Down Expand Up @@ -166,8 +173,6 @@ protected function rotate(): void
restore_error_handler();
}
}

$this->mustRotate = false;
}

protected function getTimedFilename(): string
Expand Down
44 changes: 38 additions & 6 deletions dist/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ class StreamHandler extends AbstractProcessingHandler
protected $filePermission;
/** @var bool */
protected $useLocking;
/** @var string */
protected $fileOpenMode;
/** @var true|null */
private $dirCreated = null;
/** @var bool */
private $retrying = false;

/**
* @param resource|string $stream If a missing path can't be created, an UnexpectedValueException will be thrown on first write
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param bool $useLocking Try to lock log file before doing any writes
* @param string $fileOpenMode The fopen() mode used when opening a file, if $stream is a file path
*
* @throws \InvalidArgumentException If stream is not a resource or string
*/
public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)
public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false, $fileOpenMode = 'a')
{
parent::__construct($level, $bubble);

Expand All @@ -78,6 +83,7 @@ public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true
throw new \InvalidArgumentException('A stream must either be a resource or a string.');
}

$this->fileOpenMode = $fileOpenMode;
$this->filePermission = $filePermission;
$this->useLocking = $useLocking;
}
Expand Down Expand Up @@ -134,9 +140,11 @@ protected function write(array $record): void
}
$this->createDir($url);
$this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']);
set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
try {
$stream = fopen($url, 'a');
$stream = fopen($url, $this->fileOpenMode);
if ($this->filePermission !== null) {
@chmod($url, $this->filePermission);
}
Expand All @@ -162,8 +170,30 @@ protected function write(array $record): void
flock($stream, LOCK_EX);
}

$this->streamWrite($stream, $record);
$this->errorMessage = null;
set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
try {
$this->streamWrite($stream, $record);
} finally {
restore_error_handler();
}
if ($this->errorMessage !== null) {
$error = $this->errorMessage;
// close the resource if possible to reopen it, and retry the failed write
if (!$this->retrying && $this->url !== null && $this->url !== 'php://memory') {
$this->retrying = true;
$this->close();
$this->write($record);

return;
}

throw new \UnexpectedValueException('Writing to the log file failed: '.$error . Utils::getRecordMessageForException($record));
}

$this->retrying = false;
if ($this->useLocking) {
flock($stream, LOCK_UN);
}
Expand All @@ -183,7 +213,7 @@ protected function streamWrite($stream, array $record): void

private function customErrorHandler(int $code, string $msg): bool
{
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
$this->errorMessage = preg_replace('{^(fopen|mkdir|fwrite)\(.*?\): }', '', $msg);

return true;
}
Expand Down Expand Up @@ -212,7 +242,9 @@ private function createDir(string $url): void
$dir = $this->getDirFromStream($url);
if (null !== $dir && !is_dir($dir)) {
$this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']);
set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
$status = mkdir($dir, 0777, true);
restore_error_handler();
if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {
Expand Down
7 changes: 4 additions & 3 deletions dist/vendor/monolog/monolog/src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Logger implements LoggerInterface, ResettableInterface
private $logDepth = 0;

/**
* @var \WeakMap<\Fiber, int>|null Keeps track of depth inside fibers to prevent infinite logging loops
* @var \WeakMap<\Fiber<mixed, mixed, mixed, mixed>, int> Keeps track of depth inside fibers to prevent infinite logging loops
*/
private $fiberLogDepth;

Expand Down Expand Up @@ -197,7 +197,7 @@ public function __construct(string $name, array $handlers = [], array $processor

if (\PHP_VERSION_ID >= 80100) {
// Local variable for phpstan, see https://github.com/phpstan/phpstan/issues/6732#issuecomment-1111118412
/** @var \WeakMap<\Fiber, int> $fiberLogDepth */
/** @var \WeakMap<\Fiber<mixed, mixed, mixed, mixed>, int> $fiberLogDepth */
$fiberLogDepth = new \WeakMap();
$this->fiberLogDepth = $fiberLogDepth;
}
Expand Down Expand Up @@ -345,6 +345,7 @@ public function addRecord(int $level, string $message, array $context = [], ?Dat

if ($this->detectCycles) {
if (\PHP_VERSION_ID >= 80100 && $fiber = \Fiber::getCurrent()) {
// @phpstan-ignore offsetAssign.dimType
$this->fiberLogDepth[$fiber] = $this->fiberLogDepth[$fiber] ?? 0;
$logDepth = ++$this->fiberLogDepth[$fiber];
} else {
Expand Down Expand Up @@ -753,7 +754,7 @@ public function __unserialize(array $data): void

if (\PHP_VERSION_ID >= 80100) {
// Local variable for phpstan, see https://github.com/phpstan/phpstan/issues/6732#issuecomment-1111118412
/** @var \WeakMap<\Fiber, int> $fiberLogDepth */
/** @var \WeakMap<\Fiber<mixed, mixed, mixed, mixed>, int> $fiberLogDepth */
$fiberLogDepth = new \WeakMap();
$this->fiberLogDepth = $fiberLogDepth;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/vendor/monolog/monolog/src/Monolog/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static function expandIniShorthandBytes($val)
}

$val = (int) $match['val'];
switch (strtolower($match['unit'] ?? '')) {
switch (strtolower($match['unit'])) {
case 'g':
$val *= 1024;
case 'm':
Expand Down
6 changes: 3 additions & 3 deletions src/class/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function lumiere_select_type_search (): array {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_admin_tablename() by Settings::get_admin_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_ADMIN_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_admin_tablename(): string {
return get_option( 'imdbAdminOptions' ) !== false ? 'imdbAdminOptions' : self::LUMIERE_ADMIN_OPTIONS;
Expand All @@ -667,7 +667,7 @@ public static function get_compat_admin_tablename(): string {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_data_tablename() by Settings::get_data_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_DATA_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_data_tablename(): string {
return get_option( 'imdbWidgetOptions' ) !== false ? 'imdbWidgetOptions' : self::LUMIERE_DATA_OPTIONS;
Expand All @@ -679,7 +679,7 @@ public static function get_compat_data_tablename(): string {
* @return string
* @since 4.2.1
*
* @TODO Should be removed at a later stage and renamed Settings::get_compat_cache_tablename() by Settings::get_cache_tablename() everywhere
* @TODO get_option() should be removed at a later stage and return self::LUMIERE_CACHE_OPTIONS only, method renamed to get_data_tablename()
*/
public static function get_compat_cache_tablename(): string {
return get_option( 'imdbCacheOptions' ) !== false ? 'imdbCacheOptions' : self::LUMIERE_CACHE_OPTIONS;
Expand Down
Loading

0 comments on commit a0478b6

Please sign in to comment.