Skip to content

Commit a4e3fa3

Browse files
Daniel BerthereauDaniel Berthereau
authored andcommitted
Updated for Common.
1 parent 4c92ae0 commit a4e3fa3

6 files changed

Lines changed: 138 additions & 151 deletions

File tree

Module.php

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_once dirname(__DIR__) . '/Common/TraitModule.php';
77
}
88

9+
use Common\Stdlib\PsrMessage;
910
use Common\TraitModule;
1011
use DataTypeGeometry\DataType\Geography;
1112
use DataTypeGeometry\Form\BatchEditFieldset;
@@ -22,13 +23,12 @@
2223
use Laminas\View\Renderer\PhpRenderer;
2324
use LongitudeOne\Spatial\PHP\Types\Geography\GeographyInterface;
2425
use Omeka\Module\AbstractModule;
25-
use Omeka\Stdlib\Message;
2626

2727
/**
2828
* Data type Geometry
2929
*
3030
* Adds a data type Geometry to properties of resources and allows to manage
31-
* values in Omeka or an an external database.
31+
* values in Omeka or an external database.
3232
*
3333
* @copyright Daniel Berthereau, 2018-2024
3434
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
@@ -44,10 +44,30 @@ public function init(ModuleManager $moduleManager)
4444
require_once __DIR__ . '/vendor/autoload.php';
4545
}
4646

47+
protected function preInstall(): void
48+
{
49+
$services = $this->getServiceLocator();
50+
$translate = $services->get('ControllerPluginManager')->get('translate');
51+
52+
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.60')) {
53+
$message = new \Omeka\Stdlib\Message(
54+
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
55+
'Common', '3.4.60'
56+
);
57+
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
58+
}
59+
}
60+
61+
/**
62+
* This method overrides TraitModule because the sql file depends on the
63+
* engine used for sql.
64+
*/
4765
public function install(ServiceLocatorInterface $services): void
4866
{
4967
$this->setServiceLocator($services);
50-
$messenger = $services->get('ControllerPluginManager')->get('messenger');
68+
69+
$this->initTranslations();
70+
$this->preInstall();
5171

5272
// In case of upgrade of a recent version of Cartography, the database
5373
// may exist.
@@ -60,28 +80,50 @@ public function install(ServiceLocatorInterface $services): void
6080
$this->execSqlFromFile($this->modulePath() . '/data/install/uninstall-cartography.sql');
6181
}
6282

83+
$databaseVersion = $this->getDatabaseVersion();
84+
$useMyIsam = $databaseVersion->requireMyIsamToSupportGeometry();
85+
86+
$filepath = $useMyIsam
87+
? $this->modulePath() . '/data/install/schema-myisam.sql'
88+
: $this->modulePath() . '/data/install/schema.sql';
89+
$this->execSqlFromFile($filepath);
90+
91+
$this->postInstall();
92+
}
93+
94+
protected function getDatabaseVersion(): \DataTypeGeometry\View\Helper\DatabaseVersion
95+
{
96+
$services = $this->getServiceLocator();
97+
6398
// The module is not available during install.
6499
require_once __DIR__ . '/src/View/Helper/DatabaseVersion.php';
65100
require_once __DIR__ . '/src/Service/ViewHelper/DatabaseVersionFactory.php';
66101

67102
/** @var \DataTypeGeometry\View\Helper\DatabaseVersion $databaseVersion */
68103
// $databaseVersion = $services->get('ViewHelperManager')->get('databaseVersion');
69104
$databaseVersion = new \DataTypeGeometry\Service\ViewHelper\DatabaseVersionFactory;
70-
$databaseVersion = $databaseVersion($services, 'databaseVersion', []);
105+
106+
return $databaseVersion($services, 'databaseVersion', []);
107+
}
108+
109+
protected function postInstall(): void
110+
{
111+
$services = $this->getServiceLocator();
112+
$messenger = $services->get('ControllerPluginManager')->get('messenger');
113+
$databaseVersion = $this->getDatabaseVersion();
71114

72115
if (!$databaseVersion->supportGeographicSearch()) {
73-
$messenger->addWarning('Your database does not support advanced spatial search. See the minimum requirements in readme.'); // @translate
116+
$messenger->addWarning(
117+
'Your database does not support advanced spatial search. See the minimum requirements in readme.' // @translate
118+
);
74119
}
75120

76121
$useMyIsam = $databaseVersion->requireMyIsamToSupportGeometry();
77122
if ($useMyIsam) {
78-
$messenger->addWarning('Your database does not support modern spatial indexing. It has no impact in common cases. See the minimum requirements in readme.'); // @translate
123+
$messenger->addWarning(
124+
'Your database does not support modern spatial indexing. It has no impact in common cases. See the minimum requirements in readme.' // @translate
125+
);
79126
}
80-
81-
$filepath = $useMyIsam
82-
? $this->modulePath() . '/data/install/schema-myisam.sql'
83-
: $this->modulePath() . '/data/install/schema.sql';
84-
$this->execSqlFromFile($filepath);
85127
}
86128

87129
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
@@ -254,13 +296,15 @@ public function handleConfigForm(AbstractController $controller)
254296

255297
$dispatcher = $services->get(\Omeka\Job\Dispatcher::class);
256298
$job = $dispatcher->dispatch(IndexGeometries::class, $params);
257-
$message = new Message(
258-
'Processing in the background (%sjob #%d%s)', // @translate
259-
sprintf('<a href="%s">',
260-
htmlspecialchars($controller->url()->fromRoute('admin/id', ['controller' => 'job', 'id' => $job->getId()]))
261-
),
262-
$job->getId(),
263-
'</a>'
299+
$message = new PsrMessage(
300+
'Processing in the background ({link}job #{job_id}{link_end})', // @translate
301+
[
302+
'link' => sprintf('<a href="%s">',
303+
htmlspecialchars($controller->url()->fromRoute('admin/id', ['controller' => 'job', 'id' => $job->getId()]))
304+
),
305+
'job_id' => $job->getId(),
306+
'link_end' => '</a>',
307+
]
264308
);
265309
$message->setEscapeHtml(false);
266310
$controller->messenger()->addSuccess($message);
@@ -460,8 +504,8 @@ public function handleResourceBatchUpdatePreprocess(Event $event): void
460504
/** @see \DataTypeGeometry\View\Helper\DatabaseVersion::supportRegexpExt() */
461505
&& !$services->get('ViewHelperManager')->get('databaseVersion')->supportRegexpExt()
462506
) {
463-
$message = new Message('Your database does not support the function `regexp_substr`. Upgrade it to MariaDB 10.0.5 or MySQL 8.0.'); // @translate
464-
$logger->err($message);
507+
$message = new PsrMessage('Your database does not support the function `regexp_substr`. Upgrade it to MariaDB 10.0.5 or MySQL 8.0.'); // @translate
508+
$logger->err($message->getMessage());
465509
$messenger->addError($message);
466510
unset($data['geometry']);
467511
$event->setParam('data', $data);
@@ -476,17 +520,17 @@ public function handleResourceBatchUpdatePreprocess(Event $event): void
476520
}
477521

478522
if (empty($post['geometry']['from_properties'])) {
479-
$message = new Message('No source property set for conversion of geometric or geographic data.'); // @translate
480-
$logger->err($message);
523+
$message = new PsrMessage('No source property set for conversion of geometric or geographic data.'); // @translate
524+
$logger->err($message->getMessage());
481525
$messenger->addError($message);
482526
unset($data['geometry']);
483527
$event->setParam('data', $data);
484528
return;
485529
} elseif (!in_array('all', $post['geometry']['from_properties'])
486530
&& !$easyMeta->propertyIds($post['geometry']['from_properties']
487531
)) {
488-
$message = new Message('Invalid source properties set for conversion of geometric or geographic data.'); // @translate
489-
$logger->err($message);
532+
$message = new PsrMessage('Invalid source properties set for conversion of geometric or geographic data.'); // @translate
533+
$logger->err($message->getMessage());
490534
$messenger->addError($message);
491535
unset($data['geometry']);
492536
$event->setParam('data', $data);
@@ -496,8 +540,8 @@ public function handleResourceBatchUpdatePreprocess(Event $event): void
496540
if (in_array($manage, ['sync', 'features_to_coordinates'])
497541
&& !empty($post['geometry']['to_property'])
498542
) {
499-
$message = new Message('A destination property is needed to convert geometric or geographic data.'); // @translate
500-
$logger->err($message);
543+
$message = new PsrMessage('A destination property is needed to convert geometric or geographic data.'); // @translate
544+
$logger->err($message->getMessage());
501545
$messenger->addError($message);
502546
unset($data['geometry']);
503547
$event->setParam('data', $data);
@@ -507,8 +551,8 @@ public function handleResourceBatchUpdatePreprocess(Event $event): void
507551
if (!empty($post['geometry']['to_property'])) {
508552
$to = $easyMeta->propertyId($post['geometry']['to_property']);
509553
if (!$to) {
510-
$message = new Message('Invalid destination property set for conversion of geometric or geographic data.'); // @translate
511-
$logger->err($message);
554+
$message = new PsrMessage('Invalid destination property set for conversion of geometric or geographic data.'); // @translate
555+
$logger->err($message->getMessage());
512556
$messenger->addError($message);
513557
unset($data['geometry']);
514558
$event->setParam('data', $data);

data/scripts/upgrade.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DataTypeGeometry;
44

5-
use Omeka\Stdlib\Message;
5+
use Common\Stdlib\PsrMessage;
66

77
/**
88
* @var Module $this
@@ -19,10 +19,19 @@
1919
$plugins = $services->get('ControllerPluginManager');
2020
$api = $plugins->get('api');
2121
$settings = $services->get('Omeka\Settings');
22+
$translate = $plugins->get('translate');
2223
$connection = $services->get('Omeka\Connection');
2324
$messenger = $plugins->get('messenger');
2425
$entityManager = $services->get('Omeka\EntityManager');
2526

27+
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.60')) {
28+
$message = new \Omeka\Stdlib\Message(
29+
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
30+
'Common', '3.4.60'
31+
);
32+
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
33+
}
34+
2635
if (version_compare($oldVersion, '3.0.1', '<')) {
2736
// This is a full reinstall.
2837
$this->install($services);
@@ -34,7 +43,7 @@
3443
SQL;
3544
$connection->executeStatement($sql);
3645

37-
$message = new Message(
46+
$message = new PsrMessage(
3847
'You should reindex your geometries in the config of this module.' // @translate
3948
);
4049
$messenger->addWarning($message);
@@ -51,11 +60,11 @@
5160

5261
$settings->delete('datatypegeometry_buttons');
5362

54-
$message = new Message(
63+
$message = new PsrMessage(
5564
'A new datatype has been added to manage geographic coordinates (latitude/longitude). It can manage be used as a source for the markers for the module Mapping too. A batch edit process is added to convert them.' // @translate
5665
);
5766
$messenger->addSuccess($message);
58-
$message = new Message(
67+
$message = new PsrMessage(
5968
'The data types of this module are no longer automatically appended to resource forms. They should be added to selected properties via a template.' // @translate
6069
);
6170
$messenger->addWarning($message);
@@ -96,25 +105,25 @@
96105
try {
97106
$connection->executeStatement($sql);
98107
} catch (\Exception $e) {
99-
$message = new Message(
108+
$message = new PsrMessage(
100109
'Your database is not compatible with geographic search: only flat geometry is supported.' // @translate
101110
);
102111
$messenger->addWarning($message);
103112
}
104113

105-
$message = new Message(
114+
$message = new PsrMessage(
106115
'Datatype names were simplified: "geometry", "geography", "geography:coordinates".' // @translate
107116
);
108117
$messenger->addWarning($message);
109118

110-
$message = new Message(
119+
$message = new PsrMessage(
111120
'Two new datatypes have been added to manage geometries: x/y coordinates ("geometry:coordinates") and position from top left ("geometry:position").' // @translate
112121
);
113122
$messenger->addSuccess($message);
114123
}
115124

116125
if (version_compare($oldVersion, '3.4.4', '<')) {
117-
$message = new Message(
126+
$message = new PsrMessage(
118127
'WARNING: the value representation has been normalized in the api to follow the opengis specifications for geography:coordinates, geometry:coordinates and geometry:position. The rdf value is now always a string, no more an array. Check compatibility with your external tools if needed.' // @translate
119128
);
120129
$messenger->addWarning($message);

src/DataType/QueryGeometryTrait.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ protected function joinGeo(AbstractEntityAdapter $adapter, QueryBuilder $qb, $qu
298298
$property = $query['geo'][0]['property'] ?? null;
299299
$expr = $qb->expr();
300300
if ($property) {
301-
$propertyId = $this->getPropertyId($adapter, $property);
301+
$services = $adapter->getServiceLocator();
302+
$propertyId = $services->get('EasyMeta')->propertyId($adapter, $property);
302303
$qb
303304
->leftJoin(
304305
$dataTypeClass,
@@ -320,40 +321,4 @@ protected function joinGeo(AbstractEntityAdapter $adapter, QueryBuilder $qb, $qu
320321
}
321322
return $alias;
322323
}
323-
324-
/**
325-
* Get a property id from a property term or an integer.
326-
*
327-
* @param AbstractEntityAdapter $adapter
328-
* @param string|int property
329-
* @return int
330-
*/
331-
protected function getPropertyId(AbstractEntityAdapter $adapter, $property): int
332-
{
333-
if (empty($property)) {
334-
return 0;
335-
}
336-
if (is_numeric($property)) {
337-
return (int) $property;
338-
}
339-
if (!preg_match('/^[a-z0-9-_]+:[a-z0-9-_]+$/i', $property)) {
340-
return 0;
341-
}
342-
[$prefix, $localName] = explode(':', $property);
343-
$dql = <<<'DQL'
344-
SELECT p.id
345-
FROM Omeka\Entity\Property p
346-
JOIN p.vocabulary v
347-
WHERE p.localName = :localName
348-
AND v.prefix = :prefix
349-
DQL;
350-
return (int) $adapter
351-
->getEntityManager()
352-
->createQuery($dql)
353-
->setParameters(new ArrayCollection([
354-
new Parameter('localName', $localName, ParameterType::STRING),
355-
new Parameter('prefix', $prefix, ParameterType::STRING),
356-
]))
357-
->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);
358-
}
359324
}

0 commit comments

Comments
 (0)