/
Harvester.inc.php
189 lines (152 loc) · 4.71 KB
/
Harvester.inc.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* @file Harvester.inc.php
*
* Copyright (c) 2005-2012 Alec Smecher and John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package harvester
* @class Harvester
*
* Generic harvester
*
*/
// $Id$
define('DUBLIN_CORE_METADATA_PREFIX', 'oai_dc');
class Harvester {
/** @var $errors array */
var $errors;
/** @var $recordDao object */
var $recordDao;
/** @var $sortOrderDao object */
var $sortOrderDao;
/** @var $fieldDao object */
var $fieldDao;
/** @var $archive object */
var $archive;
function Harvester($archive) {
$this->errors = array();
$this->archive =& $archive;
$this->recordDao =& DAORegistry::getDAO('RecordDAO');
$this->fieldDao =& DAORegistry::getDAO('FieldDAO');
$this->sortOrderDao =& DAORegistry::getDAO('SortOrderDAO');
}
/**
* Return an array of error messages.
*/
function getErrors() {
return $this->errors;
}
/**
* Determine whether or not the harvester is in an error condition.
* @return boolean true == OK; false == error (see getErrors)
*/
function getStatus() {
return empty($this->errors);
}
/**
* Get the archive object.
*/
function &getArchive() {
return $this->archive;
}
/**
* Add an error to the current list.
* @param $error string
*/
function addError($error) {
array_push($this->errors, $error);
}
function &getSchema() {
fatalError('ABSTRACT CLASS');
}
/**
* Index the record sort indexes for a specific sort order.
* @param $record object
* @param $sortOrder object
*/
function indexRecordSortingForSortOrder(&$record, &$sortOrder) {
$schemaPlugin =& $this->getSchemaPlugin();
$schema =& $this->getSchema();
$schemaId = $schema->getSchemaId();
$recordId = $record->getRecordId();
$sortOrderId = $sortOrder->getSortOrderId();
$sortOrderType = $sortOrder->getType();
$fields =& $this->fieldDao->getFieldsBySortOrder($sortOrder->getSortOrderId());
while ($field =& $fields->next()) {
if ($field->getSchemaId() == $schemaId) {
$fieldName = $field->getName();
$fieldValue = $schemaPlugin->getFieldValue($record, $fieldName, $sortOrderType);
if ($fieldValue !== null) switch ($sortOrderType) {
case SORT_ORDER_TYPE_STRING:
$this->sortOrderDao->insertString($recordId, $sortOrderId, $fieldValue);
break;
case SORT_ORDER_TYPE_NUMBER:
$this->sortOrderDao->insertNumber($recordId, $sortOrderId, $fieldValue);
break;
case SORT_ORDER_TYPE_DATE:
$this->sortOrderDao->insertDate($recordId, $sortOrderId, $fieldValue);
break;
}
}
unset($field);
}
}
/**
* Index the record sort indexes for all sort orders.
* @param $record object
*/
function indexRecordSorting(&$record) {
// Deal with sort order indexing
$sortOrders =& $this->sortOrderDao->getSortOrders();
while ($sortOrder =& $sortOrders->next()) {
$this->indexRecordSortingForSortOrder($record, $sortOrder);
unset($sortOrder);
}
}
/**
* Delete record sorting for a record.
*/
function deleteRecordSorting(&$record) {
$this->sortOrderDao->flushRecordIndex($record->getRecordId());
}
function &getRecordByIdentifier($identifier) {
$returner =& $this->recordDao->getRecordByIdentifier($identifier);
return $returner;
}
function _deleteRecordByIdentifier($identifier) {
$record =& $this->getRecordByIdentifier($identifier);
if (!$record) return false;
$this->recordDao->deleteRecordByIdentifier($identifier);
HookRegistry::call('Harvester::deleteRecord', array(&$record));
return true;
}
function _updateRecord(&$record, &$contents) {
$schemaPlugin =& $this->getSchemaPlugin();
$record->setContents($contents);
$record->setParsedContents($schemaPlugin->parseContents($contents));
$this->recordDao->updateRecord($record);
$this->deleteRecordSorting($record);
$this->indexRecordSorting($record);
HookRegistry::call('Harvester::updateRecord', array(&$record));
return true;
}
function _insertRecord($identifier, &$contents) {
$schemaPlugin =& $this->getSchemaPlugin();
$schema =& $this->getSchema();
$schemaId = $schema->getSchemaId();
$record = new Record();
$record->setSchemaId($schemaId);
$record->setArchiveId($this->archive->getArchiveId());
$record->setContents($contents);
HookRegistry::call('Harvester::preprocessRecord', array(&$record, &$this->archive, &$schema));
$record->setParsedContents($schemaPlugin->parseContents($record->getContents()));
$record->setIdentifier($identifier);
$this->recordDao->insertRecord($record);
$this->indexRecordSorting($record);
HookRegistry::call('Harvester::insertRecord', array(&$record, &$this->archive, &$schema));
HookRegistry::call('Harvester::postprocessRecord', array(&$record, &$this->archive, &$schema));
return true;
}
}
?>