-
Notifications
You must be signed in to change notification settings - Fork 2
/
strawberry_runners.install
116 lines (110 loc) · 4.04 KB
/
strawberry_runners.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?PHP
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Database\Database;
/**
* Update 8901
*
* Installs strawberry_runners_postprocessor config entity for saving post processor specs.
*
* @see 'https://www.drupal.org/node/3034742'
*/
function strawberry_runners_update_8901() {
\Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([
'id' => 'strawberry_runners_postprocessor',
'label' => new TranslatableMarkup('Strawberry Runners Post Processor Configuration'),
'config_prefix' => 'strawberry_runners_postprocessor',
'admin_permission' => 'administer site configuration',
'entity_keys' => [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
'active' => 'active',
'weight' => 'weight',
],
'config_export' => [
'id',
'label',
'uuid',
'weight',
'pluginconfig',
'pluginid',
'active',
'parent',
'depth'
],
]));
}
/**
* Update 8902 - Create runneritem_entity.
*/
function strawberry_runners_update_8902() {
$schema = Database::getConnection()->schema();
if (!$schema->tableExists('runneritem_entity')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->installEntityType(\Drupal::entityTypeManager()->getDefinition('runneritem_entity'));
}
else {
return 'runner item entity already exists';
}
}
/**
* Update 8903 - Remove runneritem_entity.
*/
function strawberry_runners_update_8903() {
if (\Drupal::entityTypeManager()->getDefinition('runneritem_entity', FALSE)) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->uninstallEntityType(\Drupal::entityTypeManager()->getDefinition('runneritem_entity'));
return 'runner item entity removed. Not needed in 2021';
}
else {
return 'runner item entity was not present, so no need to uninstall';
}
}
/**
* Update 8904 - Add configs for pdfalto executable and language on ocr strawberry_runner post-processor.
*/
function strawberry_runners_update_8904() {
// Get the path to the pdfalto executable if it exists.
$pdfalto_executable = `which pdfalto`;
if ($pdfalto_executable) {
// Remove trailing newline.
$pdfalto_executable = trim($pdfalto_executable ?? '');
$plugin_config_defaults = [
'path_pdfalto' => $pdfalto_executable,
'arguments_pdfalto' => '%file',
'language_key' => 'language_iso639_3',
'language_default' => 'eng',
'arguments_tesseract' => '%file stdout -l %language hocr',
'datafolder_tesseract' => '/usr/share/tessdata',
];
$msgs = [];
// Load all strawberry_runners_postprocessor configurations and add the pdfalto settings to the ocr postprocessors if needed.
$configNames = \Drupal::service('config.storage')->listAll('strawberry_runners.strawberry_runners_postprocessor');
foreach($configNames as $configName) {
$config = \Drupal::configFactory()->getEditable($configName);
// Is it an ocr postprocessor?
if($config && $config->get('pluginid') == 'ocr') {
$plugin_config = $config->get('pluginconfig');
if($plugin_config) {
// Just clobber whatever settings they may have had. Should we be more nuanced?
$plugin_config = array_replace($plugin_config, $plugin_config_defaults);
$config->set('pluginconfig', $plugin_config);
$config->save();
$msgs[] = "'" . $configName . "' configuration updated";
}
}
}
if (!empty($msgs)) {
return implode("\n", $msgs);
}
else {
return "Could not find ocr postprocessor configurations to update. You will need to manually check and update your ocr postprocessor settings";
}
}
else {
return "Could not find the pdfalto executable in your installation. No update made.\nTo use pdfalto in ocr, first ensure that the pdfalto executable is installed and then manually edit your ocr postprocessor(s) to add the path to this executable.";
}
}