/ojs/ojs-2.1.0-1/classes/search/ArticleSearchDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite · PHP · 209 lines · 133 code · 29 blank · 47 comment · 20 complexity · 939e7958a62f8c1c8d7a4ac5820f749b MD5 · raw file

  1. <?php
  2. /**
  3. * ArticleSearchDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2004 The Public Knowledge Project
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package search
  9. *
  10. * DAO class for article search index.
  11. *
  12. * $Id: ArticleSearchDAO.inc.php,v 1.23 2006/01/17 19:57:25 alec Exp $
  13. */
  14. import('search.ArticleSearch');
  15. class ArticleSearchDAO extends DAO {
  16. /**
  17. * Constructor.
  18. */
  19. function ArticleSearchDAO() {
  20. parent::DAO();
  21. }
  22. /**
  23. * Add a word to the keyword list (if it doesn't already exist).
  24. * @param $keyword string
  25. * @return int the keyword ID
  26. */
  27. function insertKeyword($keyword) {
  28. static $articleSearchKeywordIds = array();
  29. if (isset($articleSearchKeywordIds[$keyword])) return $articleSearchKeywordIds[$keyword];
  30. $result = &$this->retrieve(
  31. 'SELECT keyword_id FROM article_search_keyword_list WHERE keyword_text = ?',
  32. $keyword
  33. );
  34. if($result->RecordCount() == 0) {
  35. $this->update(
  36. 'INSERT INTO article_search_keyword_list (keyword_text) VALUES (?)',
  37. $keyword
  38. );
  39. $keywordId = $this->getInsertId('article_search_keyword_list', 'keyword_id');
  40. } else {
  41. $keywordId = $result->fields[0];
  42. }
  43. $articleSearchKeywordIds[$keyword] = $keywordId;
  44. $result->Close();
  45. unset($result);
  46. return $keywordId;
  47. }
  48. /**
  49. * Retrieve the top results for a phrases with the given
  50. * limit (default 500 results).
  51. * @param $keywordId int
  52. * @return array of results (associative arrays)
  53. */
  54. function &getPhraseResults(&$journal, $phrase, $publishedFrom = null, $publishedTo = null, $type = null, $limit = 500, $cacheHours = 24) {
  55. if (empty($phrase)) {
  56. $results = false;
  57. $returner = &new DBRowIterator($results);
  58. return $returner;
  59. }
  60. $sqlFrom = '';
  61. $sqlWhere = '';
  62. for ($i = 0, $count = count($phrase); $i < $count; $i++) {
  63. if (!empty($sqlFrom)) {
  64. $sqlFrom .= ', ';
  65. $sqlWhere .= ' AND ';
  66. }
  67. $sqlFrom .= 'article_search_object_keywords o'.$i.' NATURAL JOIN article_search_keyword_list k'.$i;
  68. if (strstr($phrase[$i], '%') === false) $sqlWhere .= 'k'.$i.'.keyword_text = ?';
  69. else $sqlWhere .= 'k'.$i.'.keyword_text LIKE ?';
  70. if ($i > 0) $sqlWhere .= ' AND o0.object_id = o'.$i.'.object_id AND o0.pos+'.$i.' = o'.$i.'.pos';
  71. $params[] = $phrase[$i];
  72. }
  73. if (!empty($type)) {
  74. $sqlWhere .= ' AND (o.type & ?) != 0';
  75. $params[] = $type;
  76. }
  77. if (!empty($publishedFrom)) {
  78. $sqlWhere .= ' AND pa.date_published >= ' . $this->datetimeToDB($publishedFrom);
  79. }
  80. if (!empty($publishedTo)) {
  81. $sqlWhere .= ' AND pa.date_published <= ' . $this->datetimeToDB($publishedTo);
  82. }
  83. if (!empty($journal)) {
  84. $sqlWhere .= ' AND i.journal_id = ?';
  85. $params[] = $journal->getJournalId();
  86. }
  87. $result = &$this->retrieveCached(
  88. 'SELECT
  89. o.article_id,
  90. COUNT(*) AS count
  91. FROM
  92. published_articles pa,
  93. issues i,
  94. article_search_objects o NATURAL JOIN ' . $sqlFrom . '
  95. WHERE
  96. pa.article_id = o.article_id AND
  97. i.issue_id = pa.issue_id AND
  98. i.published = 1 AND ' . $sqlWhere . '
  99. GROUP BY o.article_id
  100. ORDER BY count DESC
  101. LIMIT ' . $limit,
  102. $params,
  103. 3600 * $cacheHours // Cache for 24 hours
  104. );
  105. $returner = &new DBRowIterator($result);
  106. return $returner;
  107. }
  108. /**
  109. * Delete all keywords for an article object.
  110. * @param $articleId int
  111. * @param $type int optional
  112. * @param $assocId int optional
  113. */
  114. function deleteArticleKeywords($articleId, $type = null, $assocId = null) {
  115. $sql = 'SELECT object_id FROM article_search_objects WHERE article_id = ?';
  116. $params = array($articleId);
  117. if (isset($type)) {
  118. $sql .= ' AND type = ?';
  119. $params[] = $type;
  120. }
  121. if (isset($assocId)) {
  122. $sql .= ' AND assoc_id = ?';
  123. $params[] = $assocId;
  124. }
  125. $result = &$this->retrieve($sql, $params);
  126. while (!$result->EOF) {
  127. $objectId = $result->fields[0];
  128. $this->update('DELETE FROM article_search_object_keywords WHERE object_id = ?', $objectId);
  129. $this->update('DELETE FROM article_search_objects WHERE object_id = ?', $objectId);
  130. $result->MoveNext();
  131. }
  132. $result->Close();
  133. unset($result);
  134. }
  135. /**
  136. * Add an article object to the index (if already exists, indexed keywords are cleared).
  137. * @param $articleId int
  138. * @param $type int
  139. * @param $assocId int
  140. * @return int the object ID
  141. */
  142. function insertObject($articleId, $type, $assocId) {
  143. $result = &$this->retrieve(
  144. 'SELECT object_id FROM article_search_objects WHERE article_id = ? AND type = ? AND assoc_id = ?',
  145. array($articleId, $type, $assocId)
  146. );
  147. if ($result->RecordCount() == 0) {
  148. $this->update(
  149. 'INSERT INTO article_search_objects (article_id, type, assoc_id) VALUES (?, ?, ?)',
  150. array($articleId, $type, $assocId)
  151. );
  152. $objectId = $this->getInsertId('article_search_objects', 'object_id');
  153. } else {
  154. $objectId = $result->fields[0];
  155. $this->update(
  156. 'DELETE FROM article_search_object_keywords WHERE object_id = ?',
  157. $objectId
  158. );
  159. }
  160. $result->Close();
  161. unset($result);
  162. return $objectId;
  163. }
  164. /**
  165. * Index an occurrence of a keyword in an object.s
  166. * @param $objectId int
  167. * @param $keyword string
  168. * @param $position int
  169. * @return $keyword
  170. */
  171. function insertObjectKeyword($objectId, $keyword, $position) {
  172. // FIXME Cache recently retrieved keywords?
  173. $keywordId = $this->insertKeyword($keyword);
  174. $this->update(
  175. 'INSERT INTO article_search_object_keywords (object_id, keyword_id, pos) VALUES (?, ?, ?)',
  176. array($objectId, $keywordId, $position)
  177. );
  178. }
  179. }
  180. ?>