00001 <?php
00002
00016
00017
00018
00019 import('search.ArticleSearch');
00020
00021 class ArticleSearchDAO extends DAO {
00027 function insertKeyword($keyword) {
00028 static $articleSearchKeywordIds = array();
00029 if (isset($articleSearchKeywordIds[$keyword])) return $articleSearchKeywordIds[$keyword];
00030 $result = &$this->retrieve(
00031 'SELECT keyword_id FROM article_search_keyword_list WHERE keyword_text = ?',
00032 $keyword
00033 );
00034 if($result->RecordCount() == 0) {
00035 $result->Close();
00036 unset($result);
00037 if ($this->update(
00038 'INSERT INTO article_search_keyword_list (keyword_text) VALUES (?)',
00039 $keyword,
00040 true,
00041 false
00042 )) {
00043 $keywordId = $this->getInsertId('article_search_keyword_list', 'keyword_id');
00044 } else {
00045 $keywordId = null;
00046 }
00047 } else {
00048 $keywordId = $result->fields[0];
00049 $result->Close();
00050 unset($result);
00051 }
00052
00053 $articleSearchKeywordIds[$keyword] = $keywordId;
00054
00055 return $keywordId;
00056 }
00057
00064 function &getPhraseResults(&$journal, $phrase, $publishedFrom = null, $publishedTo = null, $type = null, $limit = 500, $cacheHours = 24) {
00065 if (empty($phrase)) {
00066 $results = false;
00067 $returner = &new DBRowIterator($results);
00068 return $returner;
00069 }
00070
00071 $sqlFrom = '';
00072 $sqlWhere = '';
00073
00074 for ($i = 0, $count = count($phrase); $i < $count; $i++) {
00075 if (!empty($sqlFrom)) {
00076 $sqlFrom .= ', ';
00077 $sqlWhere .= ' AND ';
00078 }
00079 $sqlFrom .= 'article_search_object_keywords o'.$i.' NATURAL JOIN article_search_keyword_list k'.$i;
00080 if (strstr($phrase[$i], '%') === false) $sqlWhere .= 'k'.$i.'.keyword_text = ?';
00081 else $sqlWhere .= 'k'.$i.'.keyword_text LIKE ?';
00082 if ($i > 0) $sqlWhere .= ' AND o0.object_id = o'.$i.'.object_id AND o0.pos+'.$i.' = o'.$i.'.pos';
00083
00084 $params[] = $phrase[$i];
00085 }
00086
00087 if (!empty($type)) {
00088 $sqlWhere .= ' AND (o.type & ?) != 0';
00089 $params[] = $type;
00090 }
00091
00092 if (!empty($publishedFrom)) {
00093 $sqlWhere .= ' AND pa.date_published >= ' . $this->datetimeToDB($publishedFrom);
00094 }
00095
00096 if (!empty($publishedTo)) {
00097 $sqlWhere .= ' AND pa.date_published <= ' . $this->datetimeToDB($publishedTo);
00098 }
00099
00100 if (!empty($journal)) {
00101 $sqlWhere .= ' AND i.journal_id = ?';
00102 $params[] = $journal->getJournalId();
00103 }
00104
00105 $result = &$this->retrieveCached(
00106 'SELECT
00107 o.article_id,
00108 COUNT(*) AS count
00109 FROM
00110 published_articles pa,
00111 issues i,
00112 article_search_objects o NATURAL JOIN ' . $sqlFrom . '
00113 WHERE
00114 pa.article_id = o.article_id AND
00115 i.issue_id = pa.issue_id AND
00116 i.published = 1 AND ' . $sqlWhere . '
00117 GROUP BY o.article_id
00118 ORDER BY count DESC
00119 LIMIT ' . $limit,
00120 $params,
00121 3600 * $cacheHours
00122 );
00123
00124 $returner = &new DBRowIterator($result);
00125 return $returner;
00126 }
00127
00134 function deleteArticleKeywords($articleId, $type = null, $assocId = null) {
00135 $sql = 'SELECT object_id FROM article_search_objects WHERE article_id = ?';
00136 $params = array($articleId);
00137
00138 if (isset($type)) {
00139 $sql .= ' AND type = ?';
00140 $params[] = $type;
00141 }
00142
00143 if (isset($assocId)) {
00144 $sql .= ' AND assoc_id = ?';
00145 $params[] = $assocId;
00146 }
00147
00148 $result = &$this->retrieve($sql, $params);
00149 while (!$result->EOF) {
00150 $objectId = $result->fields[0];
00151 $this->update('DELETE FROM article_search_object_keywords WHERE object_id = ?', $objectId);
00152 $this->update('DELETE FROM article_search_objects WHERE object_id = ?', $objectId);
00153 $result->MoveNext();
00154 }
00155 $result->Close();
00156 unset($result);
00157 }
00158
00166 function insertObject($articleId, $type, $assocId) {
00167 $result = &$this->retrieve(
00168 'SELECT object_id FROM article_search_objects WHERE article_id = ? AND type = ? AND assoc_id = ?',
00169 array($articleId, $type, $assocId)
00170 );
00171 if ($result->RecordCount() == 0) {
00172 $this->update(
00173 'INSERT INTO article_search_objects (article_id, type, assoc_id) VALUES (?, ?, ?)',
00174 array($articleId, $type, (int) $assocId)
00175 );
00176 $objectId = $this->getInsertId('article_search_objects', 'object_id');
00177
00178 } else {
00179 $objectId = $result->fields[0];
00180 $this->update(
00181 'DELETE FROM article_search_object_keywords WHERE object_id = ?',
00182 $objectId
00183 );
00184 }
00185 $result->Close();
00186 unset($result);
00187
00188 return $objectId;
00189 }
00190
00198 function insertObjectKeyword($objectId, $keyword, $position) {
00199
00200 $keywordId = $this->insertKeyword($keyword);
00201 if ($keywordId === null) return null;
00202 $this->update(
00203 'INSERT INTO article_search_object_keywords (object_id, keyword_id, pos) VALUES (?, ?, ?)',
00204 array($objectId, $keywordId, $position)
00205 );
00206 return $keywordId;
00207 }
00208 }
00209
00210 ?>