Open Journal Systems  3.3.0
PKPOAIDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.oai.OAI');
18 
19 abstract class PKPOAIDAO extends DAO {
20 
22  var $oai;
23 
24 
29  function setOAI($oai) {
30  $this->oai = $oai;
31  }
32 
33  //
34  // Resumption tokens
35  //
39  function clearTokens() {
40  $this->update(
41  'DELETE FROM oai_resumption_tokens WHERE expire < ?', time()
42  );
43  }
44 
50  function getToken($tokenId) {
51  $result = $this->retrieve(
52  'SELECT * FROM oai_resumption_tokens WHERE token = ?',
53  array($tokenId)
54  );
55 
56  if ($result->RecordCount() == 0) {
57  $token = null;
58 
59  } else {
60  $row = $result->getRowAssoc(false);
61  $token = new OAIResumptionToken($row['token'], $row['record_offset'], unserialize($row['params']), $row['expire']);
62  }
63 
64  $result->Close();
65  return $token;
66  }
67 
73  function insertToken($token) {
74  do {
75  // Generate unique token ID
76  $token->id = md5(uniqid(mt_rand(), true));
77  $result = $this->retrieve(
78  'SELECT COUNT(*) FROM oai_resumption_tokens WHERE token = ?',
79  array($token->id)
80  );
81  $val = $result->fields[0];
82 
83  $result->Close();
84  } while($val != 0);
85 
86  $this->update(
87  'INSERT INTO oai_resumption_tokens (token, record_offset, params, expire)
88  VALUES
89  (?, ?, ?, ?)',
90  array($token->id, $token->offset, serialize($token->params), $token->expire)
91  );
92 
93  return $token;
94  }
95 
96 
105  function recordExists($dataObjectId, $setIds = array()) {
106  return $this->getRecord($dataObjectId, $setIds)?true:false;
107  }
108 
117  function getRecord($dataObjectId, $setIds = array()) {
118  $result = $this->_getRecordsRecordSet($setIds, null, null, null, $dataObjectId);
119  if ($result->RecordCount() != 0) {
120  $row = $result->GetRowAssoc(false);
121  $returner = $this->_returnRecordFromRow($row);
122  } else $returner = null;
123  $result->Close();
124  return $returner;
125  }
126 
140  function getRecords($setIds, $from, $until, $set, $offset, $limit, &$total) {
141  $result = $this->_getRecordsRecordSet($setIds, $from, $until, $set);
142  $total = $result->RecordCount();
143 
144  $records = array();
145  $result->Move($offset);
146  for ($count = 0; $count < $limit && !$result->EOF; $count++) {
147  $row = $result->GetRowAssoc(false);
148  $records[] = $this->_returnRecordFromRow($row);
149  $result->MoveNext();
150  }
151  $result->Close();
152  return $records;
153  }
154 
168  function getIdentifiers($setIds, $from, $until, $set, $offset, $limit, &$total) {
169  $result = $this->_getRecordsRecordSet($setIds, $from, $until, $set);
170  $total = $result->RecordCount();
171 
172  $records = array();
173  $result->Move($offset);
174  for ($count = 0; $count < $limit && !$result->EOF; $count++) {
175  $row = $result->GetRowAssoc(false);
176  $records[] = $this->_returnIdentifierFromRow($row);
177  $result->MoveNext();
178  }
179  $result->Close();
180  return $records;
181  }
182 
190  function getEarliestDatestamp($setIds = array()) {
191  $result = $this->_getRecordsRecordSet($setIds, null, null, null, null, 'last_modified ASC');
192  if ($result->RecordCount() != 0) {
193  $row = $result->GetRowAssoc(false);
194  $record = $this->_returnRecordFromRow($row);
195  $datestamp = OAIUtils::UTCtoTimestamp($record->datestamp);
196  } else {
197  $datestamp = 0;
198  }
199  $result->Close();
200  return $datestamp;
201  }
202 
203 
204  //
205  // Private helper methods.
206  //
212  function _returnRecordFromRow($row) {
213  $record = new OAIRecord();
214  $record = $this->_doCommonOAIFromRowOperations($record, $row);
215 
216  HookRegistry::call('OAIDAO::_returnRecordFromRow', array(&$record, &$row));
217 
218  return $record;
219  }
220 
226  function _returnIdentifierFromRow($row) {
227  $record = new OAIIdentifier();
228  $record = $this->_doCommonOAIFromRowOperations($record, $row);
229 
230  HookRegistry::call('OAIDAO::_returnIdentifierFromRow', array(&$record, &$row));
231 
232  return $record;
233  }
234 
241  function _doCommonOAIFromRowOperations($record, $row) {
242  $record->datestamp = OAIUtils::UTCDate(strtotime($this->datetimeFromDB($row['last_modified'])));
243 
244  if (isset($row['tombstone_id'])) {
245  $record->identifier = $row['oai_identifier'];
246  $record->sets = array($row['set_spec']);
247  $record->status = OAIRECORD_STATUS_DELETED;
248  } else {
249  $record->status = OAIRECORD_STATUS_ALIVE;
250  $record = $this->setOAIData($record, $row, is_a($record, 'OAIRecord'));
251  }
252 
253  return $record;
254  }
255 
267  abstract function _getRecordsRecordSet($setIds, $from, $until, $set, $submissionId = null, $orderBy = 'journal_id, submission_id');
268 }
269 
270 
PKPOAIDAO\_doCommonOAIFromRowOperations
_doCommonOAIFromRowOperations($record, $row)
Definition: PKPOAIDAO.inc.php:244
PKPOAIDAO
Base class for DAO operations for the OAI interface.
Definition: PKPOAIDAO.inc.php:19
PKPOAIDAO\getRecords
getRecords($setIds, $from, $until, $set, $offset, $limit, &$total)
Definition: PKPOAIDAO.inc.php:143
PKPOAIDAO\$oai
$oai
Definition: PKPOAIDAO.inc.php:25
OAIRecord
Definition: OAIStruct.inc.php:301
PKPOAIDAO\insertToken
insertToken($token)
Definition: PKPOAIDAO.inc.php:76
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
PKPOAIDAO\getIdentifiers
getIdentifiers($setIds, $from, $until, $set, $offset, $limit, &$total)
Definition: PKPOAIDAO.inc.php:171
PKPOAIDAO\getEarliestDatestamp
getEarliestDatestamp($setIds=array())
Definition: PKPOAIDAO.inc.php:193
PKPOAIDAO\clearTokens
clearTokens()
Definition: PKPOAIDAO.inc.php:42
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
OAIUtils\UTCDate
static UTCDate($timestamp=0, $includeTime=true)
Definition: OAIUtils.inc.php:26
OAIUtils\UTCtoTimestamp
static UTCtoTimestamp($date, $requiredGranularity=null)
Definition: OAIUtils.inc.php:48
PKPOAIDAO\setOAI
setOAI($oai)
Definition: PKPOAIDAO.inc.php:32
PKPOAIDAO\_getRecordsRecordSet
_getRecordsRecordSet($setIds, $from, $until, $set, $submissionId=null, $orderBy='journal_id, submission_id')
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
OAIResumptionToken
Definition: OAIStruct.inc.php:140
PKPOAIDAO\recordExists
recordExists($dataObjectId, $setIds=array())
Definition: PKPOAIDAO.inc.php:108
PKPOAIDAO\getRecord
getRecord($dataObjectId, $setIds=array())
Definition: PKPOAIDAO.inc.php:120
PKPOAIDAO\_returnRecordFromRow
_returnRecordFromRow($row)
Definition: PKPOAIDAO.inc.php:215
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
PKPOAIDAO\getToken
getToken($tokenId)
Definition: PKPOAIDAO.inc.php:53
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
PKPOAIDAO\_returnIdentifierFromRow
_returnIdentifierFromRow($row)
Definition: PKPOAIDAO.inc.php:229
OAIIdentifier
Definition: OAIStruct.inc.php:270