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

/ojs/ojs-2.3.2-1/classes/article/ArticleFileDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 398 lines | 262 code | 50 blank | 86 comment | 35 complexity | 1777a0397be4794a419f2ac8e3402ff9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/articleArticleFileDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class ArticleFileDAO
  9. * @ingroup article
  10. * @see ArticleFile
  11. *
  12. * @brief Operations for retrieving and modifying ArticleFile objects.
  13. */
  14. // $Id$
  15. import('article.ArticleFile');
  16. define('INLINEABLE_TYPES_FILE', Config::getVar('general', 'registry_dir') . DIRECTORY_SEPARATOR . 'inlineTypes.txt');
  17. class ArticleFileDAO extends DAO {
  18. /**
  19. * Array of MIME types that can be displayed inline in a browser
  20. */
  21. var $inlineableTypes;
  22. /**
  23. * Retrieve an article by ID.
  24. * @param $fileId int
  25. * @param $revision int optional, if omitted latest revision is used
  26. * @param $articleId int optional
  27. * @return ArticleFile
  28. */
  29. function &getArticleFile($fileId, $revision = null, $articleId = null) {
  30. if ($fileId === null) {
  31. $returner = null;
  32. return $returner;
  33. }
  34. if ($revision == null) {
  35. if ($articleId != null) {
  36. $result =& $this->retrieveLimit(
  37. 'SELECT a.* FROM article_files a WHERE file_id = ? AND article_id = ? ORDER BY revision DESC',
  38. array($fileId, $articleId),
  39. 1
  40. );
  41. } else {
  42. $result =& $this->retrieveLimit(
  43. 'SELECT a.* FROM article_files a WHERE file_id = ? ORDER BY revision DESC',
  44. $fileId,
  45. 1
  46. );
  47. }
  48. } else {
  49. if ($articleId != null) {
  50. $result =& $this->retrieve(
  51. 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision = ? AND article_id = ?',
  52. array($fileId, $revision, $articleId)
  53. );
  54. } else {
  55. $result =& $this->retrieve(
  56. 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision = ?',
  57. array($fileId, $revision)
  58. );
  59. }
  60. }
  61. $returner = null;
  62. if (isset($result) && $result->RecordCount() != 0) {
  63. $returner =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
  64. }
  65. $result->Close();
  66. unset($result);
  67. return $returner;
  68. }
  69. /**
  70. * Retrieve all revisions of an article file.
  71. * @param $articleId int
  72. * @return ArticleFile
  73. */
  74. function &getArticleFileRevisions($fileId, $round = null) {
  75. if ($fileId === null) {
  76. $returner = null;
  77. return $returner;
  78. }
  79. $articleFiles = array();
  80. // FIXME If "round" is review-specific, it shouldn't be in this table
  81. if ($round == null) {
  82. $result =& $this->retrieve(
  83. 'SELECT a.* FROM article_files a WHERE file_id = ? ORDER BY revision',
  84. $fileId
  85. );
  86. } else {
  87. $result =& $this->retrieve(
  88. 'SELECT a.* FROM article_files a WHERE file_id = ? AND round = ? ORDER BY revision',
  89. array($fileId, $round)
  90. );
  91. }
  92. while (!$result->EOF) {
  93. $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
  94. $result->moveNext();
  95. }
  96. $result->Close();
  97. unset($result);
  98. return $articleFiles;
  99. }
  100. /**
  101. * Retrieve revisions of an article file in a range.
  102. * @param $articleId int
  103. * @return ArticleFile
  104. */
  105. function &getArticleFileRevisionsInRange($fileId, $start = 1, $end = null) {
  106. if ($fileId === null) {
  107. $returner = null;
  108. return $returner;
  109. }
  110. $articleFiles = array();
  111. if ($end == null) {
  112. $result =& $this->retrieve(
  113. 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ?',
  114. array($fileId, $start)
  115. );
  116. } else {
  117. $result =& $this->retrieve(
  118. 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ? AND revision <= ?',
  119. array($fileId, $start, $end)
  120. );
  121. }
  122. while (!$result->EOF) {
  123. $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
  124. $result->moveNext();
  125. }
  126. $result->Close();
  127. unset($result);
  128. return $articleFiles;
  129. }
  130. /**
  131. * Retrieve the current revision number for a file.
  132. * @param $fileId int
  133. * @return int
  134. */
  135. function &getRevisionNumber($fileId) {
  136. if ($fileId === null) {
  137. $returner = null;
  138. return $returner;
  139. }
  140. $result =& $this->retrieve(
  141. 'SELECT MAX(revision) AS max_revision FROM article_files a WHERE file_id = ?',
  142. $fileId
  143. );
  144. if ($result->RecordCount() == 0) {
  145. $returner = null;
  146. } else {
  147. $row = $result->FetchRow();
  148. $returner = $row['max_revision'];
  149. }
  150. $result->Close();
  151. unset($result);
  152. return $returner;
  153. }
  154. /**
  155. * Retrieve all article files for an article.
  156. * @param $articleId int
  157. * @return array ArticleFiles
  158. */
  159. function &getArticleFilesByArticle($articleId) {
  160. $articleFiles = array();
  161. $result =& $this->retrieve(
  162. 'SELECT * FROM article_files WHERE article_id = ?',
  163. $articleId
  164. );
  165. while (!$result->EOF) {
  166. $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
  167. $result->moveNext();
  168. }
  169. $result->Close();
  170. unset($result);
  171. return $articleFiles;
  172. }
  173. /**
  174. * Retrieve all article files for a type and assoc ID.
  175. * @param $assocId int
  176. * @param $type int
  177. * @return array ArticleFiles
  178. */
  179. function &getArticleFilesByAssocId($assocId, $type) {
  180. import('file.ArticleFileManager');
  181. $articleFiles = array();
  182. $result =& $this->retrieve(
  183. 'SELECT * FROM article_files WHERE assoc_id = ? AND type = ?',
  184. array($assocId, ArticleFileManager::typeToPath($type))
  185. );
  186. while (!$result->EOF) {
  187. $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
  188. $result->moveNext();
  189. }
  190. $result->Close();
  191. unset($result);
  192. return $articleFiles;
  193. }
  194. /**
  195. * Internal function to return an ArticleFile object from a row.
  196. * @param $row array
  197. * @return ArticleFile
  198. */
  199. function &_returnArticleFileFromRow(&$row) {
  200. $articleFile = new ArticleFile();
  201. $articleFile->setFileId($row['file_id']);
  202. $articleFile->setSourceFileId($row['source_file_id']);
  203. $articleFile->setSourceRevision($row['source_revision']);
  204. $articleFile->setRevision($row['revision']);
  205. $articleFile->setArticleId($row['article_id']);
  206. $articleFile->setFileName($row['file_name']);
  207. $articleFile->setFileType($row['file_type']);
  208. $articleFile->setFileSize($row['file_size']);
  209. $articleFile->setOriginalFileName($row['original_file_name']);
  210. $articleFile->setType($row['type']);
  211. $articleFile->setAssocId($row['assoc_id']);
  212. $articleFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
  213. $articleFile->setDateModified($this->datetimeFromDB($row['date_modified']));
  214. $articleFile->setRound($row['round']);
  215. $articleFile->setViewable($row['viewable']);
  216. HookRegistry::call('ArticleFileDAO::_returnArticleFileFromRow', array(&$articleFile, &$row));
  217. return $articleFile;
  218. }
  219. /**
  220. * Insert a new ArticleFile.
  221. * @param $articleFile ArticleFile
  222. * @return int
  223. */
  224. function insertArticleFile(&$articleFile) {
  225. $fileId = $articleFile->getFileId();
  226. $params = array(
  227. $articleFile->getRevision() === null ? 1 : $articleFile->getRevision(),
  228. (int) $articleFile->getArticleId(),
  229. $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
  230. $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
  231. $articleFile->getFileName(),
  232. $articleFile->getFileType(),
  233. $articleFile->getFileSize(),
  234. $articleFile->getOriginalFileName(),
  235. $articleFile->getType(),
  236. (int) $articleFile->getRound(),
  237. $articleFile->getViewable(),
  238. $articleFile->getAssocId()
  239. );
  240. if ($fileId) {
  241. array_unshift($params, $fileId);
  242. }
  243. $this->update(
  244. sprintf('INSERT INTO article_files
  245. (' . ($fileId ? 'file_id, ' : '') . 'revision, article_id, source_file_id, source_revision, file_name, file_type, file_size, original_file_name, type, date_uploaded, date_modified, round, viewable, assoc_id)
  246. VALUES
  247. (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?)',
  248. $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
  249. $params
  250. );
  251. if (!$fileId) {
  252. $articleFile->setFileId($this->getInsertArticleFileId());
  253. }
  254. return $articleFile->getFileId();
  255. }
  256. /**
  257. * Update an existing article file.
  258. * @param $article ArticleFile
  259. */
  260. function updateArticleFile(&$articleFile) {
  261. $this->update(
  262. sprintf('UPDATE article_files
  263. SET
  264. article_id = ?,
  265. source_file_id = ?,
  266. source_revision = ?,
  267. file_name = ?,
  268. file_type = ?,
  269. file_size = ?,
  270. original_file_name = ?,
  271. type = ?,
  272. date_uploaded = %s,
  273. date_modified = %s,
  274. round = ?,
  275. viewable = ?,
  276. assoc_id = ?
  277. WHERE file_id = ? AND revision = ?',
  278. $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
  279. array(
  280. (int) $articleFile->getArticleId(),
  281. $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
  282. $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
  283. $articleFile->getFileName(),
  284. $articleFile->getFileType(),
  285. $articleFile->getFileSize(),
  286. $articleFile->getOriginalFileName(),
  287. $articleFile->getType(),
  288. (int) $articleFile->getRound(),
  289. $articleFile->getViewable(),
  290. $articleFile->getAssocId(),
  291. $articleFile->getFileId(),
  292. $articleFile->getRevision()
  293. )
  294. );
  295. return $articleFile->getFileId();
  296. }
  297. /**
  298. * Delete an article file.
  299. * @param $article ArticleFile
  300. */
  301. function deleteArticleFile(&$articleFile) {
  302. return $this->deleteArticleFileById($articleFile->getFileId(), $articleFile->getRevision());
  303. }
  304. /**
  305. * Delete an article file by ID.
  306. * @param $articleId int
  307. * @param $revision int
  308. */
  309. function deleteArticleFileById($fileId, $revision = null) {
  310. if ($revision == null) {
  311. return $this->update(
  312. 'DELETE FROM article_files WHERE file_id = ?', $fileId
  313. );
  314. } else {
  315. return $this->update(
  316. 'DELETE FROM article_files WHERE file_id = ? AND revision = ?', array($fileId, $revision)
  317. );
  318. }
  319. }
  320. /**
  321. * Delete all article files for an article.
  322. * @param $articleId int
  323. */
  324. function deleteArticleFiles($articleId) {
  325. return $this->update(
  326. 'DELETE FROM article_files WHERE article_id = ?', $articleId
  327. );
  328. }
  329. /**
  330. * Get the ID of the last inserted article file.
  331. * @return int
  332. */
  333. function getInsertArticleFileId() {
  334. return $this->getInsertId('article_files', 'file_id');
  335. }
  336. /**
  337. * Check whether a file may be displayed inline.
  338. * @param $articleFile object
  339. * @return boolean
  340. */
  341. function isInlineable(&$articleFile) {
  342. if (!isset($this->inlineableTypes)) {
  343. $this->inlineableTypes = array_filter(file(INLINEABLE_TYPES_FILE), create_function('&$a', 'return ($a = trim($a)) && !empty($a) && $a[0] != \'#\';'));
  344. }
  345. return in_array($articleFile->getFileType(), $this->inlineableTypes);
  346. }
  347. }
  348. ?>