Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
PKPOAIDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.oai.OAI');
18 
19 class PKPOAIDAO extends DAO {
20 
22  var $oai;
23 
27  function PKPOAIDAO() {
28  parent::DAO();
29  }
30 
35  function setOAI(&$oai) {
36  $this->oai = $oai;
37  }
38 
39  //
40  // Records
41  //
42 
51  function getEarliestDatestamp($selectStatement, $setIds = array()) {
52  $params = $this->getOrderedRecordParams(null, $setIds);
53 
54  $result =& $this->retrieve(
55  $selectStatement . ' FROM mutex m ' .
56  $this->getRecordJoinClause(null, $setIds) . ' ' .
58  $params
59  );
60 
61  if (isset($result->fields[0])) {
62  $timestamp = strtotime($this->datetimeFromDB($result->fields[0]));
63  }
64  if (!isset($timestamp) || $timestamp == -1) {
65  $timestamp = 0;
66  }
67 
68  $result->Close();
69  unset($result);
70 
71  return $timestamp;
72  }
73 
82  function recordExists($dataObjectId, $setIds = array()) {
83  $params = $this->getOrderedRecordParams($dataObjectId, $setIds);
84 
85  $result =& $this->retrieve(
86  'SELECT COUNT(*)
87  FROM mutex m ' .
88  $this->getRecordJoinClause($dataObjectId, $setIds) . ' ' .
90  $params
91  );
92 
93  $returner = $result->fields[0] == 1;
94 
95  $result->Close();
96  unset($result);
97 
98  return $returner;
99  }
100 
109  function &getRecord($dataObjectId, $setIds = array()) {
110  $params = $this->getOrderedRecordParams($dataObjectId, $setIds);
111 
112  $result =& $this->retrieve(
113  $this->getRecordSelectStatement() . ' FROM mutex m ' .
114  $this->getRecordJoinClause($dataObjectId, $setIds) . ' ' .
116  $params
117  );
118 
119  $returner = null;
120  if ($result->RecordCount() != 0) {
121  $row =& $result->GetRowAssoc(false);
122  $returner =& $this->_returnRecordFromRow($row);
123  }
124 
125  $result->Close();
126  unset($result);
127 
128  return $returner;
129  }
130 
144  function &getRecords($setIds, $from, $until, $set, $offset, $limit, &$total) {
145  $records = array();
146 
147  $result =& $this->_getRecordsRecordSet($setIds, $from, $until, $set);
148 
149  $total = $result->RecordCount();
150 
151  $result->Move($offset);
152  for ($count = 0; $count < $limit && !$result->EOF; $count++) {
153  $row =& $result->GetRowAssoc(false);
154  $records[] =& $this->_returnRecordFromRow($row);
155  $result->moveNext();
156  }
157 
158  $result->Close();
159  unset($result);
160 
161  return $records;
162  }
163 
177  function &getIdentifiers($setIds, $from, $until, $set, $offset, $limit, &$total) {
178  $records = array();
179 
180  $result =& $this->_getRecordsRecordSet($setIds, $from, $until, $set);
181 
182  $total = $result->RecordCount();
183 
184  $result->Move($offset);
185  for ($count = 0; $count < $limit && !$result->EOF; $count++) {
186  $row =& $result->GetRowAssoc(false);
187  $records[] =& $this->_returnIdentifierFromRow($row);
188  $result->moveNext();
189  }
190 
191  $result->Close();
192  unset($result);
193 
194  return $records;
195  }
196 
197 
198  //
199  // Resumption tokens
200  //
204  function clearTokens() {
205  $this->update(
206  'DELETE FROM oai_resumption_tokens WHERE expire < ?', time()
207  );
208  }
209 
214  function &getToken($tokenId) {
215  $result =& $this->retrieve(
216  'SELECT * FROM oai_resumption_tokens WHERE token = ?',
217  array($tokenId)
218  );
219 
220  if ($result->RecordCount() == 0) {
221  $token = null;
222 
223  } else {
224  $row =& $result->getRowAssoc(false);
225  $token = new OAIResumptionToken($row['token'], $row['record_offset'], unserialize($row['params']), $row['expire']);
226  }
227 
228  $result->Close();
229  unset($result);
230 
231  return $token;
232  }
233 
239  function &insertToken(&$token) {
240  do {
241  // Generate unique token ID
242  $token->id = md5(uniqid(mt_rand(), true));
243  $result =& $this->retrieve(
244  'SELECT COUNT(*) FROM oai_resumption_tokens WHERE token = ?',
245  array($token->id)
246  );
247  $val = $result->fields[0];
248 
249  $result->Close();
250  unset($result);
251  } while($val != 0);
252 
253  $this->update(
254  'INSERT INTO oai_resumption_tokens (token, record_offset, params, expire)
255  VALUES
256  (?, ?, ?, ?)',
257  array($token->id, $token->offset, serialize($token->params), $token->expire)
258  );
259 
260  return $token;
261  }
262 
263 
264  //
265  // Protected methods.
266  //
277  function getOrderedRecordParams($dataObjectId = null, $setIds = array(), $set = null) {
278  $params = array();
279 
280  if (isset($dataObjectId)) {
281  $params[] = $dataObjectId;
282  }
283 
284  $notNullSetIds = array();
285  if (is_array($setIds) && !empty($setIds)) {
286  foreach($setIds as $id) {
287  // Avoid null values.
288  if (is_null($id)) continue;
289  $notNullSetIds[] = (int) $id;
290  $params[] = (int) $id;
291  }
292  }
293 
294  // Add the data object id again.
295  if (isset($dataObjectId)) {
296  $params[] = $dataObjectId;
297  }
298 
299  // Add the set specification, if any.
300  if (isset($set)) {
301  $params[] = $set;
302  }
303 
304  // Add the set ids again, so they can be used in the tombstone JOIN part of the sql too.
305  $params = array_merge($params, $notNullSetIds);
306 
307  return $params;
308  }
309 
320  assert(false);
321  }
322 
336  function getRecordJoinClause($dataObjectId = null, $setIds = array(), $set = null) {
337  assert(false);
338  }
339 
348  assert(false);
349  }
350 
361  function getDateRangeWhereClause($from, $until) {
362  assert(false);
363  }
364 
375  function &setOAIData($record, &$row, $isRecord) {
376  assert(false);
377  }
378 
379 
380  //
381  // Private helper methods.
382  //
388  function &_returnRecordFromRow(&$row) {
389  $record = new OAIRecord();
390  $record =& $this->_doCommonOAIFromRowOperations($record, $row);
391 
392  HookRegistry::call('OAIDAO::_returnRecordFromRow', array(&$record, &$row));
393 
394  return $record;
395  }
396 
402  function &_returnIdentifierFromRow(&$row) {
403  $record = new OAIIdentifier();
404  $record =& $this->_doCommonOAIFromRowOperations($record, $row);
405 
406  HookRegistry::call('OAIDAO::_returnIdentifierFromRow', array(&$record, &$row));
407 
408  return $record;
409  }
410 
417  function &_doCommonOAIFromRowOperations(&$record, &$row) {
418  $record->datestamp = OAIUtils::UTCDate(strtotime($this->datetimeFromDB($row['last_modified'])));
419 
420  if (isset($row['tombstone_id'])) {
421  $record->identifier = $row['oai_identifier'];
422  $record->sets = array($row['set_spec']);
423  $record->status = OAIRECORD_STATUS_DELETED;
424  } else {
425  $record->status = OAIRECORD_STATUS_ALIVE;
426  $record = $this->setOAIData($record, $row, is_a($record, 'OAIRecord'));
427  }
428 
429  return $record;
430  }
431 
441  function &_getRecordsRecordSet($setIds, $from, $until, $set) {
442  $params = $this->getOrderedRecordParams(null, $setIds, $set);
443 
444  $result =& $this->retrieve(
445  $this->getRecordSelectStatement() . ' FROM mutex m ' .
446  $this->getRecordJoinClause(null, $setIds, $set) . ' ' .
448  $this->getDateRangeWhereClause($from, $until),
449  $params
450  );
451 
452  return $result;
453  }
454 }
455 
456 ?>
& getToken($tokenId)
getOrderedRecordParams($dataObjectId=null, $setIds=array(), $set=null)
& setOAIData($record, &$row, $isRecord)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getDateRangeWhereClause($from, $until)
& _getRecordsRecordSet($setIds, $from, $until, $set)
Base class for DAO operations for the OAI interface.
getRecordJoinClause($dataObjectId=null, $setIds=array(), $set=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
recordExists($dataObjectId, $setIds=array())
& getRecord($dataObjectId, $setIds=array())
& _returnIdentifierFromRow(&$row)
& _doCommonOAIFromRowOperations(&$record, &$row)
getAccessibleRecordWhereClause()
& insertToken(&$token)
call($hookName, $args=null)
setOAI(&$oai)
& getRecords($setIds, $from, $until, $set, $offset, $limit, &$total)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
getRecordSelectStatement()
& _returnRecordFromRow(&$row)
getEarliestDatestamp($selectStatement, $setIds=array())
& getIdentifiers($setIds, $from, $until, $set, $offset, $limit, &$total)