Open Journal Systems  2.4.3
 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  $result =& $this->retrieveCached(
105  'SELECT
106  o.article_id,
107  COUNT(*) AS count
108  FROM
109  published_articles pa,
110  issues i,
111  article_search_objects o NATURAL JOIN ' . $sqlFrom . '
112  WHERE
113  pa.article_id = o.article_id AND
114  i.issue_id = pa.issue_id AND
115  i.published = 1 AND ' . $sqlWhere . '
116  GROUP BY o.article_id
117  ORDER BY count DESC
118  LIMIT ' . $limit,
119  $params,
120  3600 * $cacheHours // Cache for 24 hours
121  );
122 
123  $returner = new DBRowIterator($result);
124  return $returner;
125  }
126 
133  function deleteArticleKeywords($articleId, $type = null, $assocId = null) {
134  $sql = 'SELECT object_id FROM article_search_objects WHERE article_id = ?';
135  $params = array($articleId);
136 
137  if (isset($type)) {
138  $sql .= ' AND type = ?';
139  $params[] = $type;
140  }
141 
142  if (isset($assocId)) {
143  $sql .= ' AND assoc_id = ?';
144  $params[] = $assocId;
145  }
146 
147  $result =& $this->retrieve($sql, $params);
148  while (!$result->EOF) {
149  $objectId = $result->fields[0];
150  $this->update('DELETE FROM article_search_object_keywords WHERE object_id = ?', $objectId);
151  $this->update('DELETE FROM article_search_objects WHERE object_id = ?', $objectId);
152  $result->MoveNext();
153  }
154  $result->Close();
155  unset($result);
156  }
157 
165  function insertObject($articleId, $type, $assocId) {
166  $result =& $this->retrieve(
167  'SELECT object_id FROM article_search_objects WHERE article_id = ? AND type = ? AND assoc_id = ?',
168  array($articleId, $type, $assocId)
169  );
170  if ($result->RecordCount() == 0) {
171  $this->update(
172  'INSERT INTO article_search_objects (article_id, type, assoc_id) VALUES (?, ?, ?)',
173  array($articleId, $type, (int) $assocId)
174  );
175  $objectId = $this->getInsertId('article_search_objects', 'object_id');
176 
177  } else {
178  $objectId = $result->fields[0];
179  $this->update(
180  'DELETE FROM article_search_object_keywords WHERE object_id = ?',
181  $objectId
182  );
183  }
184  $result->Close();
185  unset($result);
186 
187  return $objectId;
188  }
189 
197  function insertObjectKeyword($objectId, $keyword, $position) {
198  $keywordId = $this->_insertKeyword($keyword);
199  if ($keywordId === null) return null; // Bug #2324
200  $this->update(
201  'INSERT INTO article_search_object_keywords (object_id, keyword_id, pos) VALUES (?, ?, ?)',
202  array($objectId, $keywordId, $position)
203  );
204  return $keywordId;
205  }
206 
210  function clearIndex() {
211  $this->update('DELETE FROM article_search_object_keywords');
212  $this->update('DELETE FROM article_search_objects');
213  $this->update('DELETE FROM article_search_keyword_list');
214  $this->setCacheDir(Config::getVar('files', 'files_dir') . '/_db');
215  $dataSource = $this->getDataSource();
216  $dataSource->CacheFlush();
217  }
218 }
219 
220 ?>
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