00001 <?php
00002
00016
00017
00018
00019 import('oai.OAI');
00020 import('issue.Issue');
00021
00022 class OAIDAO extends DAO {
00024 var $oai;
00025
00027 var $journalDao;
00028 var $sectionDao;
00029 var $articleDao;
00030 var $issueDao;
00031 var $authorDao;
00032 var $suppFileDao;
00033 var $journalSettingsDao;
00034
00035 var $journalCache;
00036 var $sectionCache;
00037
00041 function OAIDAO() {
00042 parent::DAO();
00043 $this->journalDao =& DAORegistry::getDAO('JournalDAO');
00044 $this->sectionDao =& DAORegistry::getDAO('SectionDAO');
00045 $this->articleDao =& DAORegistry::getDAO('ArticleDAO');
00046 $this->issueDao =& DAORegistry::getDAO('IssueDAO');
00047 $this->authorDao =& DAORegistry::getDAO('AuthorDAO');
00048 $this->suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
00049 $this->journalSettingsDao =& DAORegistry::getDAO('JournalSettingsDAO');
00050
00051 $this->journalCache = array();
00052 $this->sectionCache = array();
00053 }
00054
00059 function setOAI(&$oai) {
00060 $this->oai = $oai;
00061 }
00062
00063
00064
00065
00066
00072 function getEarliestDatestamp($journalId = null) {
00073 $result =& $this->retrieve(
00074 'SELECT MIN(pa.date_published)
00075 FROM published_articles pa, issues i
00076 WHERE pa.issue_id = i.issue_id AND i.published = 1'
00077 . (isset($journalId) ? ' AND i.journal_id = ?' : ''),
00078
00079 isset($journalId) ? $journalId : false
00080 );
00081
00082 if (isset($result->fields[0])) {
00083 $timestamp = strtotime($this->datetimeFromDB($result->fields[0]));
00084 }
00085 if (!isset($timestamp) || $timestamp == -1) {
00086 $timestamp = 0;
00087 }
00088
00089 $result->Close();
00090 unset($result);
00091
00092 return $timestamp;
00093 }
00094
00101 function recordExists($articleId, $journalId = null) {
00102 $result =& $this->retrieve(
00103 'SELECT COUNT(*)
00104 FROM published_articles pa, issues i
00105 WHERE pa.issue_id = i.issue_id AND i.published = 1 AND pa.article_id = ?'
00106 . (isset($journalId) ? ' AND i.journal_id = ?' : ''),
00107
00108 isset($journalId) ? array($articleId, $journalId) : $articleId
00109 );
00110
00111 $returner = $result->fields[0] == 1;
00112
00113 $result->Close();
00114 unset($result);
00115
00116 return $returner;
00117 }
00118
00125 function &getRecord($articleId, $journalId = null) {
00126 $result =& $this->retrieve(
00127 'SELECT pa.*,
00128 a.article_id,
00129 i.issue_id,
00130 j.journal_id,
00131 s.section_id
00132 FROM published_articles pa,
00133 issues i,
00134 journals j,
00135 articles a,
00136 sections s
00137 WHERE pa.article_id = a.article_id
00138 AND s.section_id = a.section_id
00139 AND j.journal_id = a.journal_id
00140 AND pa.issue_id = i.issue_id
00141 AND i.published = 1
00142 AND pa.article_id = ?'
00143 . (isset($journalId) ? ' AND a.journal_id = ?' : ''),
00144 isset($journalId) ? array($articleId, $journalId) : $articleId
00145 );
00146
00147 $returner = null;
00148 if ($result->RecordCount() != 0) {
00149 $row =& $result->GetRowAssoc(false);
00150 $returner =& $this->_returnRecordFromRow($row);
00151 }
00152
00153 $result->Close();
00154 unset($result);
00155
00156 return $returner;
00157 }
00158
00170 function &getRecords($journalId, $sectionId, $from, $until, $offset, $limit, &$total) {
00171 $records = array();
00172
00173 $params = array();
00174 if (isset($journalId)) {
00175 array_push($params, $journalId);
00176 }
00177 if (isset($sectionId)) {
00178 array_push($params, $sectionId);
00179 }
00180 $result =& $this->retrieve(
00181 'SELECT pa.*,
00182 a.article_id,
00183 j.journal_id,
00184 s.section_id,
00185 i.issue_id
00186 FROM published_articles pa,
00187 issues i,
00188 journals j,
00189 articles a,
00190 sections s
00191 WHERE pa.article_id = a.article_id
00192 AND s.section_id = a.section_id
00193 AND j.journal_id = a.journal_id
00194 AND pa.issue_id = i.issue_id
00195 AND i.published = 1'
00196 . (isset($journalId) ? ' AND a.journal_id = ?' : '')
00197 . (isset($sectionId) ? ' AND a.section_id = ?' : '')
00198 . (isset($from) ? ' AND pa.date_published >= ' . $this->datetimeToDB($from) : '')
00199 . (isset($until) ? ' AND pa.date_published <= ' . $this->datetimeToDB($until) : '')
00200 . ' ORDER BY journal_id',
00201 $params
00202 );
00203
00204 $total = $result->RecordCount();
00205
00206 $result->Move($offset);
00207 for ($count = 0; $count < $limit && !$result->EOF; $count++) {
00208 $row =& $result->GetRowAssoc(false);
00209 $records[] =& $this->_returnRecordFromRow($row);
00210 $result->moveNext();
00211 }
00212
00213 $result->Close();
00214 unset($result);
00215
00216 return $records;
00217 }
00218
00230 function &getIdentifiers($journalId, $sectionId, $from, $until, $offset, $limit, &$total) {
00231 $records = array();
00232
00233 $params = array();
00234 if (isset($journalId)) {
00235 array_push($params, $journalId);
00236 }
00237 if (isset($sectionId)) {
00238 array_push($params, $sectionId);
00239 }
00240 $result =& $this->retrieve(
00241 'SELECT pa.article_id,
00242 pa.date_published,
00243 j.journal_id,
00244 s.section_id
00245 FROM published_articles pa,
00246 issues i,
00247 journals j,
00248 articles a,
00249 sections s
00250 WHERE pa.article_id = a.article_id
00251 AND s.section_id = a.section_id
00252 AND j.journal_id = a.journal_id
00253 AND pa.issue_id = i.issue_id AND i.published = 1'
00254 . (isset($journalId) ? ' AND a.journal_id = ?' : '')
00255 . (isset($sectionId) ? ' AND a.section_id = ?' : '')
00256 . (isset($from) ? ' AND pa.date_published >= ' . $this->datetimeToDB($from) : '')
00257 . (isset($until) ? ' AND pa.date_published <= ' . $this->datetimeToDB($until) : '')
00258 . ' ORDER BY journal_id',
00259 $params
00260 );
00261
00262 $total = $result->RecordCount();
00263
00264 $result->Move($offset);
00265 for ($count = 0; $count < $limit && !$result->EOF; $count++) {
00266 $row =& $result->GetRowAssoc(false);
00267 $records[] =& $this->_returnIdentifierFromRow($row);
00268 $result->moveNext();
00269 }
00270
00271 $result->Close();
00272 unset($result);
00273
00274 return $records;
00275 }
00276
00277 function stripAssocArray($values) {
00278 foreach (array_keys($values) as $key) {
00279 $values[$key] = strip_tags($values[$key]);
00280 }
00281 return $values;
00282 }
00283
00289 function &getJournal($journalId) {
00290 if (!isset($this->journalCache[$journalId])) {
00291 $this->journalCache[$journalId] =& $this->journalDao->getJournal($journalId);
00292 }
00293 return $this->journalCache[$journalId];
00294 }
00295
00301 function &getIssue($issueId) {
00302 if (!isset($this->issueCache[$issueId])) {
00303 $this->issueCache[$issueId] =& $this->issueDao->getIssueById($issueId);
00304 }
00305 return $this->issueCache[$issueId];
00306 }
00307
00313 function &getSection($sectionId) {
00314 if (!isset($this->sectionCache[$sectionId])) {
00315 $this->sectionCache[$sectionId] =& $this->sectionDao->getSection($sectionId);
00316 }
00317 return $this->sectionCache[$sectionId];
00318 }
00319
00320
00326 function &_returnRecordFromRow(&$row) {
00327 $record =& new OAIRecord();
00328
00329 $articleId = $row['article_id'];
00330 if ($this->journalSettingsDao->getSetting($row['journal_id'], 'enablePublicArticleId')) {
00331 if (!empty($row['public_article_id'])) {
00332 $articleId = $row['public_article_id'];
00333 }
00334 }
00335
00336 $article =& $this->articleDao->getArticle($articleId);
00337 $journal =& $this->getJournal($row['journal_id']);
00338 $section =& $this->getSection($row['section_id']);
00339 $issue =& $this->getIssue($row['issue_id']);
00340
00341
00342
00343 $record->identifier = $this->oai->articleIdToIdentifier($row['article_id']);
00344 $record->datestamp = $this->oai->UTCDate(strtotime($this->datetimeFromDB($row['date_published'])));
00345 $record->sets = array($journal->getPath() . ':' . $section->getSectionAbbrev());
00346
00347 $record->url = Request::url($journal->getPath(), 'article', 'view', array($articleId));
00348
00349 $record->titles = $this->stripAssocArray((array) $article->getTitle(null));
00350
00351 $record->subjects = array_merge_recursive(
00352 $this->stripAssocArray((array) $article->getDiscipline(null)),
00353 $this->stripAssocArray((array) $article->getSubject(null)),
00354 $this->stripAssocArray((array) $article->getSubjectClass(null))
00355 );
00356 $record->descriptions = $this->stripAssocArray((array) $article->getAbstract(null));
00357 $record->publishers = $this->stripAssocArray((array) $journal->getTitle(null));
00358 $record->contributors = $this->stripAssocArray((array) $article->getSponsor(null));
00359 $record->date = date('Y-m-d', strtotime($issue->getDatePublished()));
00360 $types = $this->stripAssocArray((array) $section->getIdentifyType(null));
00361 $record->types = empty($types)?array(Locale::getLocale() => Locale::translate('rt.metadata.pkp.peerReviewed')):$types;
00362 $record->format = array();
00363
00364 $record->sources = $this->stripAssocArray((array) $journal->getTitle(null));
00365 foreach ($record->sources as $key => $source) {
00366 $record->sources[$key] .= '; ' . $this->_formatIssueId($row);
00367 }
00368
00369 $record->language = strip_tags($article->getLanguage());
00370 $record->relation = array();
00371 $record->coverage = array_merge_recursive(
00372 $this->stripAssocArray((array) $article->getCoverageGeo(null)),
00373 $this->stripAssocArray((array) $article->getCoverageChron(null)),
00374 $this->stripAssocArray((array) $article->getCoverageSample(null))
00375 );
00376
00377 $record->rights = (array) $this->journalSettingsDao->getSetting($row['journal_id'], 'copyrightNotice');
00378 $record->pages = $article->getPages();
00379
00380
00381 $publisherInstitution = (array) $journal->getSetting('publisherInstitution');
00382 if (!empty($publisherInstitution)) {
00383 $record->publishers = $publisherInstitution;
00384 }
00385
00386
00387 $authors = $this->authorDao->getAuthorsByArticle($row['article_id']);
00388 $record->creator = array();
00389 for ($i = 0, $num = count($authors); $i < $num; $i++) {
00390 $authorName = $authors[$i]->getFullName();
00391 $affiliation = $authors[$i]->getAffiliation();
00392 if (!empty($affiliation)) {
00393 $authorName .= '; ' . $affiliation;
00394 }
00395 $record->creator[] = $authorName;
00396 }
00397
00398
00399 $result =& $this->retrieve(
00400 'SELECT DISTINCT(f.file_type) FROM article_galleys g, article_files f WHERE g.file_id = f.file_id AND g.article_id = ?',
00401 $row['article_id']
00402 );
00403 while (!$result->EOF) {
00404 $record->format[] = $result->fields[0];
00405 $result->MoveNext();
00406 }
00407
00408 $result->Close();
00409 unset($result);
00410
00411
00412 $suppFiles =& $this->suppFileDao->getSuppFilesByArticle($row['article_id']);
00413 for ($i = 0, $num = count($suppFiles); $i < $num; $i++) {
00414
00415 $record->relation[] = Request::url($journal->getPath(), 'article', 'download', array($articleId, $suppFiles[$i]->getFileId()));
00416 }
00417
00418 $record->primaryLocale = $journal->getPrimaryLocale();
00419
00420 return $record;
00421 }
00422
00428 function &_returnIdentifierFromRow(&$row) {
00429 $journal =& $this->getJournal($row['journal_id']);
00430 $section =& $this->getSection($row['section_id']);
00431
00432 $record =& new OAIRecord();
00433
00434 $record->identifier = $this->oai->articleIdToIdentifier($row['article_id']);
00435 $record->datestamp = $this->oai->UTCDate(strtotime($this->datetimeFromDB($row['date_published'])));
00436 $record->sets = array($journal->getPath() . ':' . $section->getSectionAbbrev());
00437
00438 return $record;
00439 }
00440
00441
00442 function _formatIssueId(&$row) {
00443 $issue =& $this->getIssue($row['issue_id']);
00444 return $issue->getIssueIdentification();
00445 }
00446
00447
00448
00449
00450
00454 function clearTokens() {
00455 $this->update(
00456 'DELETE FROM oai_resumption_tokens WHERE expire < ?', time()
00457 );
00458 }
00459
00464 function &getToken($tokenId) {
00465 $result =& $this->retrieve(
00466 'SELECT * FROM oai_resumption_tokens WHERE token = ?', $tokenId
00467 );
00468
00469 if ($result->RecordCount() == 0) {
00470 $token = null;
00471
00472 } else {
00473 $row =& $result->getRowAssoc(false);
00474 $token =& new OAIResumptionToken($row['token'], $row['record_offset'], unserialize($row['params']), $row['expire']);
00475 }
00476
00477 $result->Close();
00478 unset($result);
00479
00480 return $token;
00481 }
00482
00488 function &insertToken(&$token) {
00489 do {
00490
00491 $token->id = md5(uniqid(mt_rand(), true));
00492 $result =& $this->retrieve(
00493 'SELECT COUNT(*) FROM oai_resumption_tokens WHERE token = ?',
00494 $token->id
00495 );
00496 $val = $result->fields[0];
00497
00498 $result->Close();
00499 unset($result);
00500 } while($val != 0);
00501
00502 $this->update(
00503 'INSERT INTO oai_resumption_tokens (token, record_offset, params, expire)
00504 VALUES
00505 (?, ?, ?, ?)',
00506 array($token->id, $token->offset, serialize($token->params), $token->expire)
00507 );
00508
00509 return $token;
00510 }
00511
00512
00513
00514
00515
00523 function &getJournalSets($journalId, $offset, &$total) {
00524 if (isset($journalId)) {
00525 $journals = array($this->journalDao->getJournal($journalId));
00526 } else {
00527 $journals =& $this->journalDao->getJournals();
00528 $journals =& $journals->toArray();
00529 }
00530
00531
00532 $sets = array();
00533 foreach ($journals as $journal) {
00534 $title = $journal->getJournalTitle();
00535 $abbrev = $journal->getPath();
00536 array_push($sets, new OAISet($abbrev, $title, ''));
00537
00538 $sections =& $this->sectionDao->getJournalSections($journal->getJournalId());
00539 foreach ($sections->toArray() as $section) {
00540 array_push($sets, new OAISet($abbrev . ':' . $section->getSectionAbbrev(), $section->getSectionTitle(), ''));
00541 }
00542 }
00543
00544 if ($offset != 0) {
00545 $sets = array_slice($sets, $offset);
00546 }
00547
00548 return $sets;
00549 }
00550
00558 function getSetJournalSectionId($journalSpec, $sectionSpec, $restrictJournalId = null) {
00559 $journalId = null;
00560
00561 $journal =& $this->journalDao->getJournalByPath($journalSpec);
00562 if (!isset($journal) || (isset($restrictJournalId) && $journal->getJournalId() != $restrictJournalId)) {
00563 return array(0, 0);
00564 }
00565
00566 $journalId = $journal->getJournalId();
00567 $sectionId = null;
00568
00569 if (isset($sectionSpec)) {
00570 $section =& $this->sectionDao->getSectionByAbbrev($sectionSpec, $journal->getJournalId());
00571 if (isset($section)) {
00572 $sectionId = $section->getSectionId();
00573 } else {
00574 $sectionId = 0;
00575 }
00576 }
00577
00578 return array($journalId, $sectionId);
00579 }
00580 }
00581
00582 ?>