• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/search/MonographSearchDAO.inc.php

00001 <?php
00002 
00018 import('classes.search.MonographSearch');
00019 
00020 class MonographSearchDAO extends DAO {
00024    function MonographSearchDAO() {
00025       parent::DAO();
00026    }
00027 
00033    function insertKeyword($keyword) {
00034       static $monographSearchKeywordIds = array();
00035       if (isset($monographSearchKeywordIds[$keyword])) return $monographSearchKeywordIds[$keyword];
00036       $result =& $this->retrieve(
00037          'SELECT keyword_id FROM monograph_search_keyword_list WHERE keyword_text = ?',
00038          $keyword
00039       );
00040       if($result->RecordCount() == 0) {
00041          $result->Close();
00042          unset($result);
00043          if ($this->update(
00044             'INSERT INTO monograph_search_keyword_list (keyword_text) VALUES (?)',
00045             $keyword,
00046             true,
00047             false
00048          )) {
00049             $keywordId = $this->getInsertId('monograph_search_keyword_list', 'keyword_id');
00050          } else {
00051             $keywordId = null; // Bug #2324
00052          }
00053       } else {
00054          $keywordId = $result->fields[0];
00055          $result->Close();
00056          unset($result);
00057       }
00058 
00059       $monographSearchKeywordIds[$keyword] = $keywordId;
00060 
00061       return $keywordId;
00062    }
00063 
00070    function &getPhraseResults(&$press, $phrase, $publishedFrom = null, $publishedTo = null, $type = null, $limit = 500, $cacheHours = 24) {
00071       import('lib.pkp.classes.db.DBRowIterator');
00072       if (empty($phrase)) {
00073          $results = false;
00074          $returner = new DBRowIterator($results);
00075          return $returner;
00076       }
00077 
00078       $sqlFrom = '';
00079       $sqlWhere = '';
00080 
00081       for ($i = 0, $count = count($phrase); $i < $count; $i++) {
00082          if (!empty($sqlFrom)) {
00083             $sqlFrom .= ', ';
00084             $sqlWhere .= ' AND ';
00085          }
00086          $sqlFrom .= 'monograph_search_object_keywords o'.$i.' NATURAL JOIN monograph_search_keyword_list k'.$i;
00087          if (strstr($phrase[$i], '%') === false) $sqlWhere .= 'k'.$i.'.keyword_text = ?';
00088          else $sqlWhere .= 'k'.$i.'.keyword_text LIKE ?';
00089          if ($i > 0) $sqlWhere .= ' AND o0.object_id = o'.$i.'.object_id AND o0.pos+'.$i.' = o'.$i.'.pos';
00090 
00091          $params[] = $phrase[$i];
00092       }
00093 
00094       if (!empty($type)) {
00095          $sqlWhere .= ' AND (o.type & ?) != 0';
00096          $params[] = $type;
00097       }
00098 
00099       if (!empty($press)) {
00100          $sqlWhere .= ' AND m.press_id = ?';
00101          $params[] = $press->getId();
00102       }
00103 
00104       $result =& $this->retrieveCached(
00105          'SELECT
00106             o.monograph_id,
00107             COUNT(*) AS count
00108          FROM
00109             monographs m,
00110             published_monographs pm,
00111             monograph_search_objects o NATURAL JOIN ' . $sqlFrom . '
00112          WHERE
00113             m.monograph_id = pm.monograph_id AND o.monograph_id = m.monograph_id AND ' . $sqlWhere . '
00114          GROUP BY o.monograph_id
00115          ORDER BY count DESC
00116          LIMIT ' . $limit,
00117          $params,
00118          3600 * $cacheHours // Cache for 24 hours
00119       );
00120 
00121       $returner = new DBRowIterator($result);
00122       return $returner;
00123    }
00124 
00131    function deleteMonographKeywords($monographId, $type = null, $assocId = null) {
00132       $sql = 'SELECT object_id FROM monograph_search_objects WHERE monograph_id = ?';
00133       $params = array($monographId);
00134 
00135       if (isset($type)) {
00136          $sql .= ' AND type = ?';
00137          $params[] = $type;
00138       }
00139 
00140       if (isset($assocId)) {
00141          $sql .= ' AND assoc_id = ?';
00142          $params[] = $assocId;
00143       }
00144 
00145       $result =& $this->retrieve($sql, $params);
00146       while (!$result->EOF) {
00147          $objectId = $result->fields[0];
00148          $this->update('DELETE FROM monograph_search_object_keywords WHERE object_id = ?', $objectId);
00149          $this->update('DELETE FROM monograph_search_objects WHERE object_id = ?', $objectId);
00150          $result->MoveNext();
00151       }
00152       $result->Close();
00153       unset($result);
00154    }
00155 
00163    function insertObject($monographId, $type, $assocId, $keepExisting = false) {
00164       $result =& $this->retrieve(
00165          'SELECT object_id FROM monograph_search_objects WHERE monograph_id = ? AND type = ? AND assoc_id = ?',
00166          array($monographId, $type, $assocId)
00167       );
00168       if ($result->RecordCount() == 0) {
00169          $this->update(
00170             'INSERT INTO monograph_search_objects (monograph_id, type, assoc_id) VALUES (?, ?, ?)',
00171             array($monographId, $type, (int) $assocId)
00172          );
00173          $objectId = $this->getInsertId('monograph_search_objects', 'object_id');
00174 
00175       } else {
00176          $objectId = $result->fields[0];
00177          $this->update(
00178             'DELETE FROM monograph_search_object_keywords WHERE object_id = ?',
00179             $objectId
00180          );
00181       }
00182       $result->Close();
00183       unset($result);
00184 
00185       return $objectId;
00186    }
00187 
00195    function insertObjectKeyword($objectId, $keyword, $position) {
00196       $keywordId = $this->insertKeyword($keyword);
00197       if ($keywordId === null) return null; // Bug #2324
00198       $this->update(
00199          'INSERT INTO monograph_search_object_keywords (object_id, keyword_id, pos) VALUES (?, ?, ?)',
00200          array($objectId, $keywordId, $position)
00201       );
00202       return $keywordId;
00203    }
00204 }
00205 
00206 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1