web/scripts/update-ebook-database
Mike Colagrosso c8d5dfb11f Add properties that were previously omitted
PHP 8.3 (part of Ubuntu 24.04) outputs a deprecated warning for properties that
were not in the class definition. Error below. There were a few of these
missing from the `Ebook`, `GitCommit`, `CollectionMembership`, `EbookSource`,
and `Contributor` classes. Adding them doesn't change any functionality, but it
does make it clearer what properties a class has.

Some of these properties are never set for `Ebook` instances created from the
filesystem, i.e., `Created` and `Updated`, and some of them need to be manually
set to make `Ebook` instances from the filesystem and the database match, e.g.,
`GitCommitId`, `CollectionEbookId`, `EbookSourceId`, and `ContributorId`.
Making the `Ebook` instances from the filesystem and the database match each
other makes it easier to spot bugs in the future.

Previous errors with PHP 8.3:

```
PHP Deprecated:  Creation of dynamic property Ebook::$Created is deprecated in /standardebooks.org/web/lib/Traits/Accessor.php on line 42
PHP Deprecated:  Creation of dynamic property Ebook::$Updated is deprecated in /standardebooks.org/web/lib/Traits/Accessor.php on line 42
PHP Deprecated:  Creation of dynamic property GitCommit::$GitCommitId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$GitCommitId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$GitCommitId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$GitCommitId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$GitCommitId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property GitCommit::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property CollectionMembership::$CollectionEbookId is deprecated in /standardebooks.org/web/lib/Traits/Accessor.php on line 42
PHP Deprecated:  Creation of dynamic property CollectionMembership::$EbookId is deprecated in /standardebooks.org/web/lib/Traits/Accessor.php on line 42
PHP Deprecated:  Creation of dynamic property CollectionMembership::$CollectionId is deprecated in /standardebooks.org/web/lib/Traits/Accessor.php on line 42
PHP Deprecated:  Creation of dynamic property EbookSource::$EbookSourceId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property EbookSource::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property EbookSource::$EbookSourceId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property EbookSource::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property Contributor::$ContributorId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property Contributor::$EbookId is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
PHP Deprecated:  Creation of dynamic property Contributor::$SortOrder is deprecated in /standardebooks.org/web/lib/DbConnection.php on line 286
```
2024-11-04 13:16:56 -06:00

96 lines
2.9 KiB
PHP
Executable file

#!/usr/bin/php
<?
require_once('/standardebooks.org/web/lib/Core.php');
use function Safe\getopt;
$ignoredProperites = ['Created', 'Updated']; // Ebooks from the filesystem don't have these DB properties set.
function findObjectDifferences($fs, $db): array{
$diffs = [];
$fsReflection = new ReflectionClass($fs);
$dbReflection = new ReflectionClass($db);
foreach($fsReflection->getProperties() as $fsProperty){
if(in_array($fsProperty->getName(), $ignoredProperites)){
continue;
}
$dbProperty = $dbReflection->getProperty($fsProperty->getName());
try{
if($fsProperty->getName()[0] === '_'){
// Property starts with underscore, remove the underscore and call __get()
$propertyNameWithoutUnderscore = substr($fsProperty->getName(), 1);
if(is_array($fs->$propertyNameWithoutUnderscore) && is_array($db->$propertyNameWithoutUnderscore)){
foreach($fsProperty->getValue($fs) as $key => $value){
if(is_object($fsProperty->getValue($fs)[$key])){
$arrayDiff = findObjectDifferences($fsProperty->getValue($fs)[$key], $dbProperty->getValue($db)[$key]);
if(!empty($arrayDiff)){
$diffs[$fsProperty->getName()] = $arrayDiff;
}
}
else if($fsProperty->getValue($fs)[$key] != $dbProperty->getValue($db)[$key]){
$diffs[$fsProperty->getName()] = ["fs" => $fsProperty->getValue($fs), "db" => $dbProperty->getValue($db)];
}
}
}
else if($fs->$propertyNameWithoutUnderscore != $db->$propertyNameWithoutUnderscore){
$diffs[$fsProperty->getName()] = ["fs" => $fsProperty->getValue($fs), "db" => $dbProperty->getValue($db)];
}
}
else if($fsProperty->getValue($fs) != $dbProperty->getValue($db)){
$diffs[$fsProperty->getName()] = ["fs" => $fsProperty->getValue($fs), "db" => $dbProperty->getValue($db)];
}
}
catch(Error $e){
$diffs[$fsProperty->getName()] = ['missing'];
}
}
return $diffs;
}
$longopts = ['ebookWwwFilesystemPath:', 'verbose'];
$options = getopt('v', $longopts);
$ebookWwwFilesystemPath = $options['ebookWwwFilesystemPath'] ?? null;
$verbose = false;
if(isset($options['v']) || isset($options['verbose'])){
$verbose = true;
}
if($ebookWwwFilesystemPath === null){
print("Expected usage: update-ebook-database [-v,--verbose] --ebookWwwFilesystemPath <path>\n");
exit(1);
}
if($verbose){
print("ebookWwwFilesystemPath: $ebookWwwFilesystemPath\n");
}
$ebookFromFilesystem = Ebook::FromFilesystem($ebookWwwFilesystemPath);
if($verbose){
print("Title: $ebookFromFilesystem->Title\n");
print("Identifier: $ebookFromFilesystem->Identifier\n");
}
$ebookFromFilesystem->CreateOrUpdate();
$ebookFromDatabase = Ebook::GetByIdentifier($ebookFromFilesystem->Identifier);
$diffs = findObjectDifferences($ebookFromFilesystem, $ebookFromDatabase);
if(!empty($diffs)){
print("Error: Difference in Ebook on filesystem and in database. Diffs:\n");
print_r($diffs);
exit(1);
}
else{
if($verbose){
print("Ebook on filesystem and in database match.\n");
}
exit(0);
}