Open Journal Systems  2.4.8
 All Classes Namespaces Functions Variables Groups Pages
ArticleSearchDAO.inc.php
1 <?php
2 
17 import('classes.search.ArticleSearch');
18 
19 class ArticleSearchDAO extends DAO {
25  function _insertKeyword($keyword) {
26  static $articleSearchKeywordIds = array();
27  if (isset($articleSearchKeywordIds[$keyword])) return $articleSearchKeywordIds[$keyword];
28  $result =& $this->retrieve(
29  'SELECT keyword_id FROM article_search_keyword_list WHERE keyword_text = ?',
30  $keyword
31  );
32  if($result->RecordCount() == 0) {
33  $result->Close();
34  unset($result);
35  if ($this->update(
36  'INSERT INTO article_search_keyword_list (keyword_text) VALUES (?)',
37  $keyword,
38  true,
39  false
40  )) {
41  $keywordId = $this->getInsertId('article_search_keyword_list', 'keyword_id');
42  } else {
43  $keywordId = null; // Bug #2324
44  }
45  } else {
46  $keywordId = $result->fields[0];
47  $result->Close();
48  unset($result);
49  }
50 
51  $articleSearchKeywordIds[$keyword] = $keywordId;
52 
53  return $keywordId;
54  }
55 
62  function &getPhraseResults(&$journal, $phrase, $publishedFrom = null, $publishedTo = null, $type = null, $limit = 500, $cacheHours = 24) {
63  import('lib.pkp.classes.db.DBRowIterator');
64  if (empty($phrase)) {
65  $results = false;
66  $returner = new DBRowIterator($results);
67  return $returner;
68  }
69 
70  $sqlFrom = '';
71  $sqlWhere = '';
72 
73  for ($i = 0, $count = count($phrase); $i < $count; $i++) {
74  if (!empty($sqlFrom)) {
75  $sqlFrom .= ', ';
76  $sqlWhere .= ' AND ';
77  }
78  $sqlFrom .= 'article_search_object_keywords o'.$i.' NATURAL JOIN article_search_keyword_list k'.$i;
79  if (strstr($phrase[$i], '%') === false) $sqlWhere .= 'k'.$i.'.keyword_text = ?';
80  else $sqlWhere .= 'k'.$i.'.keyword_text LIKE ?';
81  if ($i > 0) $sqlWhere .= ' AND o0.object_id = o'.$i.'.object_id AND o0.pos+'.$i.' = o'.$i.'.pos';
82 
83  $params[] = $phrase[$i];
84  }
85 
86  if (!empty($type)) {
87  $sqlWhere .= ' AND (o.type & ?) != 0';
88  $params[] = $type;
89  }
90 
91  if (!empty($publishedFrom)) {
92  $sqlWhere .= ' AND pa.date_published >= ' . $this->datetimeToDB($publishedFrom);
93  }
94 
95  if (!empty($publishedTo)) {
96  $sqlWhere .= ' AND pa.date_published <= ' . $this->datetimeToDB($publishedTo);
97  }
98 
99  if (!empty($journal)) {
100  $sqlWhere .= ' AND i.journal_id = ?';
101  $params[] = $journal->getId();
102  }
103 
104  import('classes.article.Article'); // STATUS_PUBLISHED
105  $result =& $this->retrieveCached(
106  'SELECT o.article_id,
107  COUNT(*) AS count
108  FROM articles a,
109  published_articles pa,
110  issues i,
111  article_search_objects o NATURAL JOIN ' . $sqlFrom . '
112  WHERE pa.article_id = a.article_id AND
113  a.status = ' . STATUS_PUBLISHED . ' AND
114  pa.article_id = o.article_id AND
115  i.issue_id = pa.issue_id AND
116  i.published = 1 AND ' . $sqlWhere . '
117  GROUP BY o.article_id
118  ORDER BY count DESC
119  LIMIT ' . $limit,
120  $params,
121  3600 * $cacheHours // Cache for 24 hours
122  );
123 
124  $returner = new DBRowIterator($result);
125  return $returner;
126  }
127 
134  function deleteArticleKeywords($articleId, $type = null, $assocId = null) {
135  $sql = 'SELECT object_id FROM article_search_objects WHERE article_id = ?';
136  $params = array($articleId);
137 
138  if (isset($type)) {
139  $sql .= ' AND type = ?';
140  $params[] = $type;
141  }
142 
143  if (isset($assocId)) {
144  $sql .= ' AND assoc_id = ?';
145  $params[] = $assocId;
146  }
147 
148  $result =& $this->retrieve($sql, $params);
149  while (!$result->EOF) {
150  $objectId = $result->fields[0];
151  $this->update('DELETE FROM article_search_object_keywords WHERE object_id = ?', $objectId);
152  $this->update('DELETE FROM article_search_objects WHERE object_id = ?', $objectId);
153  $result->MoveNext();
154  }
155  $result->Close();
156  unset($result);
157  }
158 
166  function insertObject($articleId, $type, $assocId) {
167  $result =& $this->retrieve(
168  'SELECT object_id FROM article_search_objects WHERE article_id = ? AND type = ? AND assoc_id = ?',
169  array($articleId, $type, $assocId)
170  );
171  if ($result->RecordCount() == 0) {
172  $this->update(
173  'INSERT INTO article_search_objects (article_id, type, assoc_id) VALUES (?, ?, ?)',
174  array($articleId, $type, (int) $assocId)
175  );
176  $objectId = $this->getInsertId('article_search_objects', 'object_id');
177 
178  } else {
179  $objectId = $result->fields[0];
180  $this->update(
181  'DELETE FROM article_search_object_keywords WHERE object_id = ?',
182  $objectId
183  );
184  }
185  $result->Close();
186  unset($result);
187 
188  return $objectId;
189  }
190 
198  function insertObjectKeyword($objectId, $keyword, $position) {
199  $keywordId = $this->_insertKeyword($keyword);
200  if ($keywordId === null) return null; // Bug #2324
201  $this->update(
202  'INSERT INTO article_search_object_keywords (object_id, keyword_id, pos) VALUES (?, ?, ?)',
203  array($objectId, $keywordId, $position)
204  );
205  return $keywordId;
206  }
207 
211  function clearIndex() {
212  $this->update('DELETE FROM article_search_object_keywords');
213  $this->update('DELETE FROM article_search_objects');
214  $this->update('DELETE FROM article_search_keyword_list');
215  $this->setCacheDir(Config::getVar('files', 'files_dir') . '/_db');
216  $dataSource = $this->getDataSource();
217  $dataSource->CacheFlush();
218  }
219 }
220 
221 ?>
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
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
deleteArticleKeywords($articleId, $type=null, $assocId=null)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& getPhraseResults(&$journal, $phrase, $publishedFrom=null, $publishedTo=null, $type=null, $limit=500, $cacheHours=24)
insertObject($articleId, $type, $assocId)
& getDataSource()
Definition: DAO.inc.php:37
DAO class for article search index.
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
insertObjectKeyword($objectId, $keyword, $position)
& retrieveCached($sql, $params=false, $secsToCache=3600, $callHooks=true)
Definition: DAO.inc.php:113
setCacheDir()
Definition: DAO.inc.php:271
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211