PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/oai/ocs/OAIDAO.inc.php

https://github.com/mcrider/ocs
PHP | 505 lines | 303 code | 73 blank | 129 comment | 33 complexity | 1ba4754c2b392025eed9d771e0c93968 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file OAIDAO.inc.php
  4. *
  5. * Copyright (c) 2000-2010 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class OAIDAO
  9. * @ingroup oai_ocs
  10. * @see OAI
  11. *
  12. * @brief DAO operations for the OCS OAI interface.
  13. */
  14. //$Id$
  15. import('oai.OAI');
  16. class OAIDAO extends DAO {
  17. /** @var $oai ConferenceOAI parent OAI object */
  18. var $oai;
  19. /** Helper DAOs */
  20. var $conferenceDao;
  21. var $trackDao;
  22. var $publishedPaperDao;
  23. var $paperGalleyDao;
  24. var $authorDao;
  25. var $suppFileDao;
  26. var $conferenceSettingsDao;
  27. var $conferenceCache;
  28. var $schedConfCache;
  29. var $trackCache;
  30. /**
  31. * Constructor.
  32. */
  33. function OAIDAO() {
  34. parent::DAO();
  35. $this->conferenceDao =& DAORegistry::getDAO('ConferenceDAO');
  36. $this->schedConfDao =& DAORegistry::getDAO('SchedConfDAO');
  37. $this->trackDao =& DAORegistry::getDAO('TrackDAO');
  38. $this->publishedPaperDao =& DAORegistry::getDAO('PublishedPaperDAO');
  39. $this->paperGalleyDao =& DAORegistry::getDAO('PaperGalleyDAO');
  40. $this->authorDao =& DAORegistry::getDAO('AuthorDAO');
  41. $this->suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
  42. $this->conferenceSettingsDao =& DAORegistry::getDAO('ConferenceSettingsDAO');
  43. $this->conferenceCache = array();
  44. $this->schedConfCache = array();
  45. $this->trackCache = array();
  46. }
  47. /**
  48. * Set parent OAI object.
  49. * @param ConferenceOAI
  50. */
  51. function setOAI(&$oai) {
  52. $this->oai = $oai;
  53. }
  54. //
  55. // Records
  56. //
  57. /**
  58. * Return the *nix timestamp of the earliest published paper.
  59. * @param $conferenceId int optional
  60. * @return int
  61. */
  62. function getEarliestDatestamp($conferenceId = null) {
  63. $result =& $this->retrieve(
  64. 'SELECT MIN(p.last_modified)
  65. FROM papers p,
  66. published_papers pp '
  67. . (isset($conferenceId) ? ' LEFT JOIN sched_confs sc ON (pp.sched_conf_id = sc.sched_conf_id) ' : '') .
  68. 'WHERE p.paper_id = pp.paper_id'
  69. . (isset($conferenceId) ? ' AND sc.conference_id = ?' : ''),
  70. isset($conferenceId) ? array((int) $conferenceId) : false
  71. );
  72. if (isset($result->fields[0])) {
  73. $timestamp = strtotime($this->datetimeFromDB($result->fields[0]));
  74. }
  75. if (!isset($timestamp) || $timestamp == -1) {
  76. $timestamp = 0;
  77. }
  78. $result->Close();
  79. unset($result);
  80. return $timestamp;
  81. }
  82. /**
  83. * Check if an paper ID specifies a published paper.
  84. * @param $paperId int
  85. * @param $conferenceId int optional
  86. * @return boolean
  87. */
  88. function recordExists($paperId, $conferenceId = null) {
  89. $result =& $this->retrieve(
  90. 'SELECT COUNT(*)
  91. FROM published_papers pp'
  92. . (isset($conferenceId) ? ', sched_confs s' : '')
  93. . ' WHERE pp.paper_id = ?'
  94. . (isset($conferenceId) ? ' AND s.conference_id = ? AND pp.sched_conf_id = s.sched_conf_id' : ''),
  95. isset($conferenceId) ? array($paperId, $conferenceId) : $paperId
  96. );
  97. $returner = $result->fields[0] == 1;
  98. $result->Close();
  99. unset($result);
  100. return $returner;
  101. }
  102. /**
  103. * Return OAI record for specified paper.
  104. * @param $paperId int
  105. * @param $conferenceId int optional
  106. * @return OAIRecord
  107. */
  108. function &getRecord($paperId, $conferenceId = null) {
  109. $result =& $this->retrieve(
  110. 'SELECT pp.*, p.*,
  111. c.path AS conference_path,
  112. c.conference_id AS conference_id,
  113. s.path AS sched_conf_path
  114. FROM published_papers pp, conferences c, sched_confs s, papers p
  115. LEFT JOIN tracks t ON t.track_id = p.track_id
  116. WHERE pp.paper_id = p.paper_id AND
  117. c.conference_id = s.conference_id AND
  118. s.sched_conf_id = p.sched_conf_id
  119. AND pp.paper_id = ?'
  120. . (isset($conferenceId) ? ' AND c.conference_id = ?' : ''),
  121. isset($conferenceId) ? array((int) $paperId, (int) $conferenceId) : array((int) $paperId)
  122. );
  123. $returner = null;
  124. if ($result->RecordCount() != 0) {
  125. $row =& $result->GetRowAssoc(false);
  126. $returner =& $this->_returnRecordFromRow($row);
  127. }
  128. $result->Close();
  129. unset($result);
  130. return $returner;
  131. }
  132. /**
  133. * Return set of OAI records matching specified parameters.
  134. * @param $conferenceId int
  135. * @param $trackId int
  136. * @parma $from int timestamp
  137. * @parma $until int timestamp
  138. * @param $offset int
  139. * @param $limit int
  140. * @param $total int
  141. * @return array OAIRecord
  142. */
  143. function &getRecords($conferenceId, $trackId, $from, $until, $offset, $limit, &$total) {
  144. $records = array();
  145. $params = array();
  146. if (isset($conferenceId)) {
  147. array_push($params, (int) $conferenceId);
  148. }
  149. if (isset($trackId)) {
  150. array_push($params, (int) $trackId);
  151. }
  152. $result =& $this->retrieve(
  153. 'SELECT pp.*, p.*,
  154. c.path AS conference_path,
  155. c.conference_id AS conference_id,
  156. s.path AS sched_conf_path
  157. FROM published_papers pp,
  158. conferences c,
  159. sched_confs s,
  160. papers p
  161. LEFT JOIN tracks t ON t.track_id = p.track_id
  162. WHERE pp.paper_id = p.paper_id AND
  163. p.sched_conf_id = s.sched_conf_id AND
  164. s.conference_id = c.conference_id'
  165. . (isset($conferenceId) ? ' AND c.conference_id = ?' : '')
  166. . (isset($trackId) ? ' AND p.track_id = ?' : '')
  167. . (isset($from) ? ' AND p.last_modified >= ' . $this->datetimeToDB($from) : '')
  168. . (isset($until) ? ' AND p.last_modified <= ' . $this->datetimeToDB($until) : ''),
  169. $params
  170. );
  171. $total = $result->RecordCount();
  172. $result->Move($offset);
  173. for ($count = 0; $count < $limit && !$result->EOF; $count++) {
  174. $row =& $result->GetRowAssoc(false);
  175. $records[] =& $this->_returnRecordFromRow($row);
  176. $result->moveNext();
  177. }
  178. $result->Close();
  179. unset($result);
  180. return $records;
  181. }
  182. /**
  183. * Return set of OAI identifiers matching specified parameters.
  184. * @param $conferenceId int
  185. * @param $trackId int
  186. * @parma $from int timestamp
  187. * @parma $until int timestamp
  188. * @param $offset int
  189. * @param $limit int
  190. * @param $total int
  191. * @return array OAIIdentifier
  192. */
  193. function &getIdentifiers($conferenceId, $trackId, $from, $until, $offset, $limit, &$total) {
  194. $records = array();
  195. $params = array();
  196. if (isset($conferenceId)) {
  197. array_push($params, (int) $conferenceId);
  198. }
  199. if (isset($trackId)) {
  200. array_push($params, (int) $trackId);
  201. }
  202. $result =& $this->retrieve(
  203. 'SELECT pp.paper_id,
  204. p.last_modified,
  205. c.path AS conference_path,
  206. c.conference_id,
  207. s.path AS sched_conf_path,
  208. s.sched_conf_id,
  209. p.track_id
  210. FROM published_papers pp,
  211. conferences c,
  212. sched_confs s,
  213. papers p
  214. LEFT JOIN tracks t ON t.track_id = p.track_id
  215. WHERE pp.paper_id = p.paper_id AND
  216. p.sched_conf_id = s.sched_conf_id AND
  217. s.conference_id = c.conference_id'
  218. . (isset($conferenceId) ? ' AND c.conference_id = ?' : '')
  219. . (isset($trackId) ? ' AND p.track_id = ?' : '')
  220. . (isset($from) ? ' AND p.last_modified >= ' . $this->datetimeToDB($from) : '')
  221. . (isset($until) ? ' AND p.last_modified <= ' . $this->datetimeToDB($until) : ''),
  222. $params
  223. );
  224. $total = $result->RecordCount();
  225. $result->Move($offset);
  226. for ($count = 0; $count < $limit && !$result->EOF; $count++) {
  227. $row =& $result->GetRowAssoc(false);
  228. $records[] =& $this->_returnIdentifierFromRow($row);
  229. $result->moveNext();
  230. }
  231. $result->Close();
  232. unset($result);
  233. return $records;
  234. }
  235. function stripAssocArray($values) {
  236. foreach (array_keys($values) as $key) {
  237. $values[$key] = strip_tags($values[$key]);
  238. }
  239. return $values;
  240. }
  241. /**
  242. * Cached function to get a conference
  243. * @param $conferenceId int
  244. * @return object
  245. */
  246. function &getConference($conferenceId) {
  247. if (!isset($this->conferenceCache[$conferenceId])) {
  248. $this->conferenceCache[$conferenceId] =& $this->conferenceDao->getConference($conferenceId);
  249. }
  250. return $this->conferenceCache[$conferenceId];
  251. }
  252. /**
  253. * Cached function to get a schedConf
  254. * @param $schedConfId int
  255. * @return object
  256. */
  257. function &getSchedConf($schedConfId) {
  258. if (!isset($this->schedConfCache[$schedConfId])) {
  259. $this->schedConfCache[$schedConfId] =& $this->schedConfDao->getSchedConf($schedConfId);
  260. }
  261. return $this->schedConfCache[$schedConfId];
  262. }
  263. /**
  264. * Cached function to get a track
  265. * @param $trackId int
  266. * @return object
  267. */
  268. function &getTrack($trackId) {
  269. if (!isset($this->trackCache[$trackId])) {
  270. $this->trackCache[$trackId] =& $this->trackDao->getTrack($trackId);
  271. }
  272. return $this->trackCache[$trackId];
  273. }
  274. /**
  275. * Return OAIRecord object from database row.
  276. * @param $row array
  277. * @return OAIRecord
  278. */
  279. function &_returnRecordFromRow(&$row) {
  280. $record = new OAIRecord();
  281. $paperId = $row['paper_id'];
  282. /* if ($this->conferenceSettingsDao->getSetting($row['conference_id'], 'enablePublicPaperId')) {
  283. if (!empty($row['public_paper_id'])) {
  284. $paperId = $row['public_paper_id'];
  285. }
  286. } */
  287. $paper =& $this->publishedPaperDao->getPublishedPaperByPaperId($paperId);
  288. $conference =& $this->getConference($row['conference_id']);
  289. $schedConf =& $this->getSchedConf($row['sched_conf_id']);
  290. $track =& $this->getTrack($row['track_id']);
  291. $galleys =& $this->paperGalleyDao->getGalleysByPaper($paperId);
  292. $record->setData('paper', $paper);
  293. $record->setData('conference', $conference);
  294. $record->setData('schedConf', $schedConf);
  295. $record->setData('track', $track);
  296. $record->setData('galleys', $galleys);
  297. // FIXME Use public ID in OAI identifier?
  298. $record->identifier = $this->oai->paperIdToIdentifier($row['paper_id']);
  299. $record->datestamp = OAIUtils::UTCDate(strtotime($this->datetimeFromDB($row['last_modified'])));
  300. $record->sets = array($conference->getPath() . ':' . $track->getLocalizedAbbrev());
  301. return $record;
  302. }
  303. /**
  304. * Return OAIIdentifier object from database row.
  305. * @param $row array
  306. * @return OAIIdentifier
  307. */
  308. function &_returnIdentifierFromRow(&$row) {
  309. $record = new OAIRecord();
  310. $conference =& $this->getConference($row['conference_id']);
  311. $schedConf =& $this->getSchedConf($row['sched_conf_id']);
  312. $track =& $this->getTrack($row['track_id']);
  313. $record->identifier = $this->oai->paperIdToIdentifier($row['paper_id']);
  314. $record->datestamp = OAIUtils::UTCDate(strtotime($this->datetimeFromDB($row['last_modified'])));
  315. $record->sets = array($conference->getPath() . ':' . $track->getLocalizedAbbrev());
  316. return $record;
  317. }
  318. //
  319. // Resumption tokens
  320. //
  321. /**
  322. * Clear stale resumption tokens.
  323. */
  324. function clearTokens() {
  325. $this->update(
  326. 'DELETE FROM oai_resumption_tokens WHERE expire < ?', time()
  327. );
  328. }
  329. /**
  330. * Retrieve a resumption token.
  331. * @return OAIResumptionToken
  332. */
  333. function &getToken($tokenId) {
  334. $result =& $this->retrieve(
  335. 'SELECT * FROM oai_resumption_tokens WHERE token = ?',
  336. array($tokenId)
  337. );
  338. if ($result->RecordCount() == 0) {
  339. $token = null;
  340. } else {
  341. $row =& $result->getRowAssoc(false);
  342. $token = new OAIResumptionToken($row['token'], $row['record_offset'], unserialize($row['params']), $row['expire']);
  343. }
  344. $result->Close();
  345. unset($result);
  346. return $token;
  347. }
  348. /**
  349. * Insert an OAI resumption token, generating a new ID.
  350. * @param $token OAIResumptionToken
  351. * @return OAIResumptionToken
  352. */
  353. function &insertToken(&$token) {
  354. do {
  355. // Generate unique token ID
  356. $token->id = md5(uniqid(mt_rand(), true));
  357. $result =& $this->retrieve(
  358. 'SELECT COUNT(*) FROM oai_resumption_tokens WHERE token = ?',
  359. array($token->id)
  360. );
  361. $val = $result->fields[0];
  362. $result->Close();
  363. unset($result);
  364. } while($val != 0);
  365. $this->update(
  366. 'INSERT INTO oai_resumption_tokens (token, record_offset, params, expire)
  367. VALUES
  368. (?, ?, ?, ?)',
  369. array($token->id, $token->offset, serialize($token->params), $token->expire)
  370. );
  371. return $token;
  372. }
  373. //
  374. // Sets
  375. //
  376. /**
  377. * Return hierarchy of OAI sets (conferences plus conference tracks).
  378. * @param $conferenceId int
  379. * @param $offset int
  380. * @param $total int
  381. * @return array OAISet
  382. */
  383. function &getConferenceSets($conferenceId, $offset, &$total) {
  384. if (isset($conferenceId)) {
  385. $conferences = array($this->conferenceDao->getConference($conferenceId));
  386. } else {
  387. $conferences =& $this->conferenceDao->getConferences();
  388. $conferences =& $conferences->toArray();
  389. }
  390. // FIXME Set descriptions
  391. $sets = array();
  392. foreach ($conferences as $conference) {
  393. $title = $conference->getConferenceTitle();
  394. $abbrev = $conference->getPath();
  395. array_push($sets, new OAISet($abbrev, $title, ''));
  396. $tracks =& $this->trackDao->getConferenceTracks($conference->getId());
  397. foreach ($tracks->toArray() as $track) {
  398. array_push($sets, new OAISet($abbrev . ':' . $track->getLocalizedAbbrev(), $track->getTrackTitle(), ''));
  399. }
  400. }
  401. if ($offset != 0) {
  402. $sets = array_slice($sets, $offset);
  403. }
  404. return $sets;
  405. }
  406. /**
  407. * Return the conference ID and track ID corresponding to a conference/track pairing.
  408. * @param $conferenceSpec string
  409. * @param $trackSpec string
  410. * @param $restrictConferenceId int
  411. * @return array (int, int)
  412. */
  413. function getSetConferenceTrackId($conferenceSpec, $trackSpec, $restrictConferenceId = null) {
  414. $conferenceId = null;
  415. $conference =& $this->conferenceDao->getConferenceByPath($conferenceSpec);
  416. if (!isset($conference) || (isset($restrictConferenceId) && $conference->getId() != $restrictConferenceId)) {
  417. return array(0, 0);
  418. }
  419. $conferenceId = $conference->getId();
  420. $trackId = null;
  421. if (isset($trackSpec)) {
  422. $track =& $this->trackDao->getTrackByAbbrev($trackSpec, $conference->getId());
  423. if (isset($track)) {
  424. $trackId = $track->getId();
  425. } else {
  426. $trackId = 0;
  427. }
  428. }
  429. return array($conferenceId, $trackId);
  430. }
  431. }
  432. ?>