Skip to content

Commit

Permalink
Released version 3.17.4.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berthereau authored and Daniel Berthereau committed Apr 25, 2022
1 parent 6b87e25 commit 430a9ee
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ protected function handleCheckSlug(Event $event, $resourceType): void
return;
}

$data['o:slug'] .= '_' . substr(str_replace(['+', '/'], '', base64_encode(random_bytes(20))), 0, 4);
$data['o:slug'] .= '_' . substr(str_replace(['+', '/', '='], ['', '', ''], base64_encode(random_bytes(128))), 0, 4);
$request->setContent($data);

$message = new Message('The slug "%s" is used or reserved. A random string has been automatically appended.', $slug); // @translate
Expand Down
2 changes: 1 addition & 1 deletion config/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ author_link = "https://gitlab.com/Daniel-KM"
module_link = "https://gitlab.com/Daniel-KM/omeka-s-module-CleanUrl"
support_link = "https://gitlab.com/Daniel-KM/omeka-s-module-CleanUrl/-/issues"
configurable = true
version = "3.17.3.3"
version = "3.17.4.3"
omeka_version_constraint = "^3.1.0"
119 changes: 113 additions & 6 deletions src/Generic/AbstractModule.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);
/*
* Copyright Daniel Berthereau, 2018-2021
* Copyright Daniel Berthereau, 2018-2022
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
Expand Down Expand Up @@ -81,7 +81,14 @@ public function install(ServiceLocatorInterface $services): void
);
throw new ModuleCannotInstallException((string) $message);
}
$this->execSqlFromFile($this->modulePath() . '/data/install/schema.sql');
$sqlFile = $this->modulePath() . '/data/install/schema.sql';
if (!$this->checkNewTablesFromFile($sqlFile)) {
$message = new Message(
$translator->translate('This module cannot install its tables, because they exist already. Try to remove them first.') // @translate
);
throw new ModuleCannotInstallException((string) $message);
}
$this->execSqlFromFile($sqlFile);
$this
->installAllResources()
->manageConfig('install')
Expand Down Expand Up @@ -269,27 +276,87 @@ protected function postUninstall(): void
{
}

/**
* Check if new tables can be installed and remove empty existing tables.
*
* If a new table exists and is empty, it is removed, because it is probably
* related to a broken installation.
*/
protected function checkNewTablesFromFile(string $filepath): bool
{
if (!file_exists($filepath) || !filesize($filepath) || !is_readable($filepath)) {
return true;
}

/** @var \Doctrine\DBAL\Connection $connection */
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');

// Get the list of all tables.
$tables = $connection->executeQuery('SHOW TABLES;')->fetchFirstColumn();

$dropTables = [];

// Use single statements for execution.
// See core commit #2689ce92f.
$sql = file_get_contents($filepath);
$sqls = array_filter(array_map('trim', explode(";\n", $sql)));
foreach ($sqls as $sql) {
if (mb_strtoupper(mb_substr($sql, 0, 13)) !== 'CREATE TABLE ') {
continue;
}
$table = trim(strtok(mb_substr($sql, 13), '('), "\"`' \n\r\t\v\0");
if (!in_array($table, $tables)) {
continue;
}
$result = $connection->executeQuery("SELECT * FROM `$table` LIMIT 1;")->fetchOne();
if ($result !== false) {
return false;
}
$dropTables[] = $table;
}

if (count($dropTables)) {
// No check: if a table cannot be removed, an exception will be
// thrown later.
foreach ($dropTables as $table) {
$connection->executeStatement("DROP TABLE `$table`;");
}

$translator = $services->get('MvcTranslator');
$message = new \Omeka\Stdlib\Message(
$translator->translate('The module removed tables "%s" from a previous broken install.'), // @translate
implode('", "', $dropTables)
);
$messenger = new \Omeka\Mvc\Controller\Plugin\Messenger();
$messenger->addWarning($message);
}

return true;
}

/**
* Execute a sql from a file.
*
* @param string $filepath
* @return int|null
*/
protected function execSqlFromFile($filepath)
protected function execSqlFromFile(string $filepath): ?int
{
if (!file_exists($filepath) || !filesize($filepath) || !is_readable($filepath)) {
return null;
}
$services = $this->getServiceLocator();

/** @var \Doctrine\DBAL\Connection $connection */
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
$sql = file_get_contents($filepath);

// Use single statements for execution.
// See core commit #2689ce92f.
$sql = file_get_contents($filepath);
$sqls = array_filter(array_map('trim', explode(";\n", $sql)));
foreach ($sqls as $sql) {
$result = $connection->exec($sql);
$result = $connection->executeStatement($sql);
}

return $result;
Expand Down Expand Up @@ -640,8 +707,48 @@ protected function checkDependencies(): bool
|| $this->areModulesActive($this->dependencies);
}

/**
* Check the version of a module and return a boolean or throw an exception.
*
* @throws \Omeka\Module\Exception\ModuleCannotInstallException
*/
protected function checkModuleAvailability(string $moduleName, ?string $version = null, bool $required = false, bool $exception = false): bool
{
$services = $this->getServiceLocator();
$module = $services->get('Omeka\ModuleManager')->getModule($moduleName);
if (!$module || !$this->isModuleActive($moduleName)) {
if (!$required) {
return true;
}
if (!$exception) {
return false;
}
// Else throw message below (required module with a version or not).
} elseif (!$version || version_compare($module->getIni('version') ?? '', $version, '>=')) {
return true;
} elseif (!$exception) {
return false;
}
$translator = $services->get('MvcTranslator');
if ($version) {
$message = new \Omeka\Stdlib\Message(
$translator->translate('This module requires the module "%1$s", version %2$s or above.'), // @translate
$moduleName, $version
);
} else {
$message = new \Omeka\Stdlib\Message(
$translator->translate('This module requires the module "%s".'), // @translate
$moduleName
);
}
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}

/**
* Check the version of a module.
*
* It is recommended to use checkModuleAvailability(), that manages the fact
* that the module may be required or not.
*/
protected function isModuleVersionAtLeast(string $module, string $version): bool
{
Expand Down
6 changes: 2 additions & 4 deletions src/Router/Http/CleanRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,7 @@ protected function getResourceIdentifierFromParams(array $params, string $identi
// The media representation doesn't have the position.
// $view->api() cannot set a response content.
return $this->entityManager
->getRepository(\Omeka\Entity\Media::class)
->find($resource->id())
->find(\Omeka\Entity\Media::class, $resource->id())
->getPosition();
default:
return null;
Expand All @@ -772,8 +771,7 @@ protected function mediaBelongsToItem(int $mediaId, int $itemId): bool
protected function mediaBelongsToItemSet(int $mediaId, int $itemSetId): bool
{
$media = $this->entityManager
->getRepository(\Omeka\Entity\Media::class)
->find($mediaId);
->find(\Omeka\Entity\Media::class, $mediaId);
return $this->itemBelongsToItemSet($media->getItem()->getId(), $itemSetId);
}

Expand Down
1 change: 1 addition & 0 deletions src/View/Helper/GetIdentifiersFromResourcesOfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function __invoke($resources, $resourceName = null)
}

// Create a temporary table when the number of resources is very big.
// TODO Use a cache table like module Reference.
$tempTable = count($resources) > self::CHUNK_RECORDS;
if ($tempTable) {
$query = 'DROP TABLE IF EXISTS `temp_resources`;';
Expand Down
23 changes: 11 additions & 12 deletions src/View/Helper/GetResourceTypeIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,27 @@ public function __invoke($resourceName, $encode = false, $skipPrefix = false): a
if ($lengthPrefix) {
if ($skipPrefix) {
$qb
->select([
// $qb->expr()->trim($qb->expr()->substring('value.text', $lengthPrefix + 1)),
'(TRIM(SUBSTR(value.value, ' . ($lengthPrefix + 1) . ')))',
]);
->select(
// $qb->expr()->trim($qb->expr()->substring('value.text', $lengthPrefix + 1))
'(TRIM(SUBSTR(value.value, ' . ($lengthPrefix + 1) . ')))'
);
} else {
$qb
->select([
'value.value',
]);
->select(
'value.value'
);
}
$qb
->andWhere('value.value LIKE :value_value')
->setParameter('value_value', $prefix . '%');
} else {
$qb
->select([
'value.value',
]);
->select(
'value.value'
);
}

$stmt = $this->connection->executeQuery($qb, $qb->getParameters());
$result = $stmt->fetchAll(\PDO::FETCH_COLUMN);
$result = $this->connection->executeQuery($qb, $qb->getParameters())->fetchFirstColumn();

if ($encode) {
$keepSlash = $this->options[$resourceName]['keep_slash'];
Expand Down

0 comments on commit 430a9ee

Please sign in to comment.