Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
MonographSearchIndex.inc.php
1 <?php
2 
16 import('lib.pkp.classes.search.SubmissionSearchIndex');
17 
19 
26  function indexObjectKeywords($objectId, $text, &$position) {
27  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
28  $keywords = self::filterKeywords($text);
29  for ($i = 0, $count = count($keywords); $i < $count; $i++) {
30  if ($searchDao->insertObjectKeyword($objectId, $keywords[$i], $position) !== null) {
31  $position += 1;
32  }
33  }
34  }
35 
43  function updateTextIndex($monographId, $type, $text, $assocId = null) {
44  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
45  $objectId = $searchDao->insertObject($monographId, $type, $assocId);
46  $position = 0;
47  self::indexObjectKeywords($objectId, $text, $position);
48  }
49 
56  function updateFileIndex($monographId, $type, $fileId) {
57  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
58  $file = $submissionFileDao->getLatestRevision($fileId);
59 
60  if (isset($file)) {
61  $parser = SearchFileParser::fromFile($file);
62  }
63 
64  if (isset($parser)) {
65  if ($parser->open()) {
66  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
67  $objectId = $searchDao->insertObject($monographId, $type, $fileId);
68 
69  $position = 0;
70  while(($text = $parser->read()) !== false) {
71  self::indexObjectKeywords($objectId, $text, $position);
72  }
73  $parser->close();
74  } else {
75  // cannot open parser; unsupported format?
76  }
77  }
78  }
79 
86  function deleteTextIndex($monographId, $type = null, $assocId = null) {
87  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
88  return $searchDao->deleteSubmissionKeywords($monographId, $type, $assocId);
89  }
90 
95  function indexMonographMetadata(&$monograph) {
96  // Build author keywords
97  $authorText = array();
98  $authorDao = DAORegistry::getDAO('AuthorDAO');
99  $authors = $authorDao->getBySubmissionId($monograph->getId());
100  foreach ($authors as $author) {
101  array_push($authorText, $author->getFirstName());
102  array_push($authorText, $author->getMiddleName());
103  array_push($authorText, $author->getLastName());
104  $affiliations = $author->getAffiliation(null);
105  if (is_array($affiliations)) foreach ($affiliations as $affiliation) { // Localized
106  array_push($authorText, strip_tags($affiliation));
107  }
108  $bios = $author->getBiography(null);
109  if (is_array($bios)) foreach ($bios as $bio) { // Localized
110  array_push($authorText, strip_tags($bio));
111  }
112  }
113 
114  // Update search index
115  import('classes.search.MonographSearch');
116  $monographId = $monograph->getId();
117  self::updateTextIndex($monographId, SUBMISSION_SEARCH_AUTHOR, $authorText);
118  self::updateTextIndex($monographId, SUBMISSION_SEARCH_TITLE, $monograph->getTitle(null));
119  self::updateTextIndex($monographId, SUBMISSION_SEARCH_ABSTRACT, $monograph->getAbstract(null));
120 
121  self::updateTextIndex($monographId, SUBMISSION_SEARCH_DISCIPLINE, (array) $monograph->getDiscipline(null));
122  self::updateTextIndex($monographId, SUBMISSION_SEARCH_SUBJECT, array_merge(array_values((array) $monograph->getSubjectClass(null)), array_values((array) $monograph->getSubject(null))));
123  self::updateTextIndex($monographId, SUBMISSION_SEARCH_TYPE, $monograph->getType(null));
124  self::updateTextIndex($monographId, SUBMISSION_SEARCH_COVERAGE, array_merge(array_values((array) $monograph->getCoverageGeo(null)), array_values((array) $monograph->getCoverageChron(null)), array_values((array) $monograph->getCoverageSample(null))));
125  // FIXME Index sponsors too?
126  }
127 
132  function indexMonographFiles(&$monograph) {
133  // Index galley files
134  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
135  import('classes.monograph.MonographFile'); // Constants
136  import('classes.search.MonographSearch'); // Constants
137  $files = $submissionFileDao->getLatestRevisions($monograph->getId(), SUBMISSION_FILE_PROOF);
138 
139  foreach ($files as $file) {
140  if ($file->getFileId() && $file->getViewable()) {
141  self::updateFileIndex($monograph->getId(), SUBMISSION_SEARCH_GALLEY_FILE, $file->getFileId());
142  }
143  }
144  }
145 
150  function clearMonographFiles($monograph) {
151  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
152  $searchDao->deleteSubmissionKeywords($monograph->getId(), SUBMISSION_SEARCH_GALLEY_FILE);
153  }
154 
158  function rebuildIndex($log = false) {
159  // Clear index
160  if ($log) echo 'Clearing index ... ';
161  $searchDao = DAORegistry::getDAO('MonographSearchDAO');
162  // FIXME Abstract into MonographSearchDAO?
163  $searchDao->update('DELETE FROM submission_search_object_keywords');
164  $searchDao->update('DELETE FROM submission_search_objects');
165  $searchDao->update('DELETE FROM submission_search_keyword_list');
166  $searchDao->setCacheDir(Config::getVar('files', 'files_dir') . '/_db');
167  $searchDao->_dataSource->CacheFlush();
168  if ($log) echo "done\n";
169 
170  // Build index
171  $pressDao = DAORegistry::getDAO('PressDAO');
172  $monographDao = DAORegistry::getDAO('MonographDAO');
173 
174  $presses = $pressDao->getAll();
175  while ($press = $presses->next()) {
176  $numIndexed = 0;
177 
178  if ($log) echo "Indexing \"", $press->getLocalizedName(), "\" ... ";
179 
180  $monographs = $monographDao->getByPressId($press->getId());
181  while (!$monographs->eof()) {
182  $monograph = $monographs->next();
183  if ($monograph->getDatePublished()) {
184  self::indexMonographMetadata($monograph);
185  self::indexMonographFiles($monograph);
186  $numIndexed++;
187  }
188  }
189 
190  if ($log) echo $numIndexed, " monographs indexed\n";
191  }
192  }
193 
194 }
195 
196 ?>
static & getDAO($name, $dbconn=null)
deleteTextIndex($monographId, $type=null, $assocId=null)
Class to add content to the monograph search index.
updateTextIndex($monographId, $type, $text, $assocId=null)
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
Class to maintain a submission search index.
updateFileIndex($monographId, $type, $fileId)
indexObjectKeywords($objectId, $text, &$position)