Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 380bbd6

Browse files
author
Jamie Snape
committed
Migrate metadataextractor module settings to database
1 parent 72cca12 commit 380bbd6

File tree

12 files changed

+266
-207
lines changed

12 files changed

+266
-207
lines changed

modules/metadataextractor/Notification.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,29 @@
2121
/** Notification manager for the metadataextractor module */
2222
class Metadataextractor_Notification extends MIDAS_Notification
2323
{
24+
/** @var array */
2425
public $_moduleComponents = array('Extractor');
26+
27+
/** @var string */
2528
public $moduleName = 'metadataextractor';
2629

27-
/** init notification process */
30+
/** Initialize the notification process. */
2831
public function init()
2932
{
3033
$this->addTask(
31-
"TASK_METADATAEXTRACTOR_EXTRACT",
32-
'extractMetaData',
33-
"Extract Metadata. Parameters: Item, Revision"
34+
'TASK_METADATAEXTRACTOR_EXTRACT',
35+
'extractMetadata',
36+
'Extract Metadata. Parameters: Item, Revision'
3437
);
3538
$this->addEvent('EVENT_CORE_UPLOAD_FILE', 'TASK_METADATAEXTRACTOR_EXTRACT');
3639
}
3740

38-
/** get Config Tabs */
39-
public function extractMetaData($params)
41+
/**
42+
* Handle TASK_METADATAEXTRACTOR_EXTRACT.
43+
*
44+
* @param array $params parameters
45+
*/
46+
public function extractMetadata($params)
4047
{
4148
$this->ModuleComponent->Extractor->extract($params[1]);
4249
}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
; MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0.
2+
13
[global]
2-
; version of the module
3-
version = 1.0.0
4-
; full name
5-
fullname = MetaData Extractor
6-
; description
7-
description = Extract meta data from common files
8-
; category
9-
category = Filter
10-
; dependencies
4+
fullname = "Metadata Extractor"
5+
description = "Extract metadata from common files"
6+
category = "Metadata"
117
dependencies = scheduler
12-
13-
; hachoir-metadata command
14-
hachoir = "python hachoir-metadata"
8+
uuid = "92665a6e-d851-471c-8f96-5a5a5552dc78"
9+
version = "1.1.0"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
define('METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_KEY', 'hachoir_metadata_command');
22+
define('METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_DEFAULT_VALUE', 'hachoir-metadata');
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Admin controller for the metadataextractor module. */
22+
class Metadataextractor_AdminController extends Metadataextractor_AppController
23+
{
24+
/** @var array */
25+
public $_models = array('Setting');
26+
27+
/** Index action */
28+
public function indexAction()
29+
{
30+
$this->requireAdminPrivileges();
31+
32+
$this->view->pageTitle = 'Metadata Extractor Module Configuration';
33+
$form = new Metadataextractor_Form_Admin();
34+
35+
if ($this->getRequest()->isPost()) {
36+
$data = $this->getRequest()->getPost();
37+
38+
if ($form->isValid($data)) {
39+
$values = $form->getValues();
40+
41+
foreach ($values as $key => $value) {
42+
if ($value !== null) {
43+
$this->Setting->setConfig($key, $value, $this->moduleName);
44+
}
45+
}
46+
}
47+
48+
$form->populate($data);
49+
} else {
50+
$elements = $form->getElements();
51+
52+
foreach ($elements as $element) {
53+
$name = $element->getName();
54+
55+
if ($name !== 'csrf' && $name !== 'submit') {
56+
$value = $this->Setting->getValueByName($name, $this->moduleName);
57+
58+
if (!is_null($value)) {
59+
$form->setDefault($name, $value);
60+
}
61+
}
62+
}
63+
}
64+
65+
$this->view->form = $form;
66+
session_start();
67+
}
68+
}

modules/metadataextractor/controllers/ConfigController.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

modules/metadataextractor/controllers/components/ExtractorComponent.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
/** Extractor component for the metadataextractor module */
2222
class Metadataextractor_ExtractorComponent extends AppComponent
2323
{
24+
/** @var string */
25+
public $moduleName = 'metadataextractor';
26+
2427
/** extract metadata */
2528
public function extract($revision)
2629
{
27-
$itemRevisionModel = MidasLoader::loadModel("ItemRevision");
30+
/** @var ItemRevisionModel $itemRevisionModel */
31+
$itemRevisionModel = MidasLoader::loadModel('ItemRevision');
2832
$revision = $itemRevisionModel->load($revision['itemrevision_id']);
2933
if (!$revision) {
3034
return;
@@ -36,24 +40,26 @@ public function extract($revision)
3640
$bitstream = $bitstreams[0];
3741
$ext = strtolower(substr(strrchr($bitstream->getName(), '.'), 1));
3842

39-
$MetadataModel = MidasLoader::loadModel("Metadata");
43+
/** @var MetadataModel $metadataModel */
44+
$metadataModel = MidasLoader::loadModel('Metadata');
4045
if ($ext == 'pdf') {
4146
$pdf = Zend_Pdf::load($bitstream->getFullPath());
4247
foreach ($pdf->properties as $name => $property) {
4348
$name = strtolower($name);
4449
try {
45-
$metadataDao = $MetadataModel->getMetadata(MIDAS_METADATA_TEXT, 'misc', $name);
50+
$metadataDao = $metadataModel->getMetadata(MIDAS_METADATA_TEXT, 'misc', $name);
4651
if (!$metadataDao) {
47-
$MetadataModel->addMetadata(MIDAS_METADATA_TEXT, 'misc', $name, '');
52+
$metadataModel->addMetadata(MIDAS_METADATA_TEXT, 'misc', $name, '');
4853
}
49-
$MetadataModel->addMetadataValue($revision, MIDAS_METADATA_TEXT, 'misc', $name, $property);
54+
$metadataModel->addMetadataValue($revision, MIDAS_METADATA_TEXT, 'misc', $name, $property);
5055
} catch (Zend_Exception $exc) {
5156
echo $exc->getMessage();
5257
}
5358
}
5459
} else {
55-
$modulesConfig = Zend_Registry::get('configsModules');
56-
$command = $modulesConfig['metadataextractor']->hachoir;
60+
/** @var SettingModel $settingModel */
61+
$settingModel = MidasLoader::loadModel('Setting');
62+
$command = $settingModel->getValueByName(METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_KEY, $this->moduleName);
5763
exec(str_replace("'", '"', $command).' "'.$bitstream->getFullPath().'"', $output);
5864

5965
if (!isset($output[0]) || $output[0] != "Metadata:") {
@@ -66,17 +72,15 @@ public function extract($revision)
6672
$name = strtolower(substr($out, 0, $pos));
6773
$value = substr($out, $pos + 2);
6874
try {
69-
$metadataDao = $MetadataModel->getMetadata(MIDAS_METADATA_TEXT, 'misc', $name);
75+
$metadataDao = $metadataModel->getMetadata(MIDAS_METADATA_TEXT, 'misc', $name);
7076
if (!$metadataDao) {
71-
$MetadataModel->addMetadata(MIDAS_METADATA_TEXT, 'misc', $name, '');
77+
$metadataModel->addMetadata(MIDAS_METADATA_TEXT, 'misc', $name, '');
7278
}
73-
$MetadataModel->addMetadataValue($revision, MIDAS_METADATA_TEXT, 'misc', $name, $value);
79+
$metadataModel->addMetadataValue($revision, MIDAS_METADATA_TEXT, 'misc', $name, $value);
7480
} catch (Zend_Exception $exc) {
7581
echo $exc->getMessage();
7682
}
7783
}
7884
}
79-
80-
return;
8185
}
8286
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
require_once BASE_PATH.'/modules/metadataextractor/constant/module.php';
22+
23+
/** Install the metadataextractor module. */
24+
class Metadataextractor_InstallScript extends MIDASModuleInstallScript
25+
{
26+
/** @var string */
27+
public $moduleName = 'metadataextractor';
28+
29+
/** Post database install. */
30+
public function postInstall()
31+
{
32+
/** @var SettingModel $settingModel */
33+
$settingModel = MidasLoader::loadModel('Setting');
34+
$settingModel->setConfig(METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_KEY, METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_DEFAULT_VALUE, $this->moduleName);
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Upgrade the metadataextractor module to version 1.1.0. */
22+
class Metadataextractor_Upgrade_1_1_0 extends MIDASUpgrade
23+
{
24+
/** @var string */
25+
public $moduleName = 'metadataextractor';
26+
27+
/** Post database upgrade. */
28+
public function postUpgrade()
29+
{
30+
/** @var SettingModel $settingModel */
31+
$settingModel = MidasLoader::loadModel('Setting');
32+
$configPath = LOCAL_CONFIGS_PATH.DIRECTORY_SEPARATOR.$this->moduleName.'.local.ini';
33+
34+
if (file_exists($configPath)) {
35+
$config = new Zend_Config_Ini($configPath, 'global');
36+
$settingModel->setConfig(METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_KEY, $config->get('hachoir', METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_DEFAULT_VALUE), $this->moduleName);
37+
38+
$config = new Zend_Config_Ini($configPath, null, true);
39+
unset($config->global->hachoir);
40+
41+
$writer = new Zend_Config_Writer_Ini();
42+
$writer->setConfig($config);
43+
$writer->setFilename($configPath);
44+
$writer->write();
45+
} else {
46+
$settingModel->setConfig(METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_KEY, METADATAEXTRACTOR_HACHOIR_METADATA_COMMAND_DEFAULT_VALUE, $this->moduleName);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)