Author: david
Date: Fri Feb 24 17:25:34 2012
New Revision: 10974

Log:
Start merging QubitSearchPdo with QubitSearch

Added:
   trunk/lib/QubitSearchInformationObject.class.php   (contents, props changed)
Modified:
   trunk/lib/QubitSearch.class.php

Modified: trunk/lib/QubitSearch.class.php
==============================================================================
--- trunk/lib/QubitSearch.class.php     Fri Feb 24 17:12:32 2012        (r10973)
+++ trunk/lib/QubitSearch.class.php     Fri Feb 24 17:25:34 2012        (r10974)
@@ -27,9 +27,7 @@
    */
   protected static
     $_instance,
-    $conn,
-    $lookups,
-    $statements;
+    $conn;
 
   public static function getInstance()
   {
@@ -172,40 +170,7 @@
     // index information objects
     if (-1 < $ioOffset)
     {
-      // Get count of all information objects
-      $sql = 'SELECT COUNT(*) from '.QubitInformationObject::TABLE_NAME;
-      $rs = self::$conn->query($sql);
-      $rowcount = $rs->fetchColumn(0);
-
-      // Get info objects (with offset)
-      $criteria = new Criteria;
-      QubitInformationObject::addSelectColumns($criteria);
-
-      if (0 < $ioOffset)
-      {
-        $criteria->setOffset($ioOffset);
-      }
-
-      $informationObjects = QubitInformationObject::get($criteria);
-
-      // Loop through results, and add to search index
-      foreach ($informationObjects as $key => $informationObject)
-      {
-        if ($key == 0 && 0 < $ioOffset)
-        {
-          $this->getLogger()->log('Ignoring first '.$ioOffset.' information 
objects.');
-        }
-
-        if (0 < count($languages = 
$this->getTranslatedLanguages($informationObject)))
-        {
-          foreach ($languages as $language)
-          {
-            self::addInformationObjectIndex($informationObject, $language, 
$options);
-          }
-        }
-
-        $this->getLogger()->log('"'.$informationObject->__toString().'" 
inserted ('.round(microtime(true) - $start, 2).'s) ('.($key + $ioOffset + 
1).'/'.$rowcount.')', $this->getName());
-      }
+      QubitSearchInformationObject::populate($ioOffset, $this->getLogger(), 
$start);
     }
     else
     {
@@ -476,495 +441,13 @@
     {
       return;
     }
-    // Only ROOT node should have no parent, don't index
-    elseif (null === $informationObject->parent)
+    else if (null === $informationObject->parent)
     {
+      // Only ROOT node should have no parent, don't index
       return;
     }
 
     self::deleteByIdLanguage($informationObject->id, $language);
-    self::addInformationObjectIndex($informationObject, $language, $options);
-  }
-
-  public static function addInformationObjectIndex(QubitInformationObject 
$informationObject, $language, $options = array())
-  {
-    // Only ROOT node should have no parent, don't index
-    if (null === $informationObject->parent)
-    {
-      return;
-    }
-
-    $doc = new Zend_Search_Lucene_Document;
-
-    // Reference elements
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('slug', 
$informationObject->slug));
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('culture', $language));
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('className', 
'QubitInformationObject'));
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('parent', 
$informationObject->parent->slug));
-
-    $collectionRoot = $informationObject->getCollectionRoot();
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('collectionRootSlug', 
$collectionRoot->slug));
-    $doc->addField(Zend_Search_Lucene_Field::Text('partOf', 
$collectionRoot->getTitle()));
-
-    // Digital object information
-    if ($digitalObject = $informationObject->getDigitalObject())
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Text('hasDigitalObject', 
'true'));
-
-      $doc->addField(Zend_Search_Lucene_Field::Keyword('do_mediaTypeId', 
$digitalObject->mediaTypeId));
-
-      if (null !== $digitalObject->thumbnail)
-      {
-        
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('do_thumbnail_FullPath', 
$digitalObject->thumbnail->getFullPath()));
-      }
-
-      // $doc->addField(Zend_Search_Lucene_Field::Unstored('mediatype', 
$digitalObject->getMediaType()->getName(array('culture' => $language))));
-      // $doc->addField(Zend_Search_Lucene_Field::Unstored('filename', 
$digitalObject->getName()));
-      // $doc->addField(Zend_Search_Lucene_Field::Unstored('mimetype', 
$digitalObject->mimeType));
-    }
-    else
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Text('hasDigitalObject', 
'false'));
-    }
-
-    // Title
-    // include an i18n fallback for proper search result display in case the 
title field was not translated
-    if (0 < strlen($informationObject->getTitle(array('culture' => 
$language))))
-    {
-      $titleField = Zend_Search_Lucene_Field::Text('title', 
$informationObject->getTitle(array('culture' => $language)));
-    }
-    else
-    {
-      $titleField = Zend_Search_Lucene_Field::Text('title', 
$informationObject->getTitle(array('sourceCulture' => true)));
-    }
-
-    // Boost the hit relevance for the title field
-    $titleField->boost = 10;
-    $doc->addField($titleField);
-
-    // Publication status
-    $doc->addField(Zend_Search_Lucene_Field::Keyword('publicationStatusId', 
$informationObject->getPublicationStatus()->status->id));
-
-    $doc->addField(Zend_Search_Lucene_Field::Text('scopeAndContent', 
$informationObject->getScopeAndContent(array('culture' => $language))));
-
-    $doc->addField(Zend_Search_Lucene_Field::Text('referenceCode', 
$informationObject->referenceCode));
-
-    // Creation dates and creator
-    $doc = self::addCreationEvents($doc, $informationObject->id, $language);
-
-    // Level of Description
-    if (null !== $levelOfDescription = 
$informationObject->getLevelOfDescription())
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Text('levelOfDescription', 
$levelOfDescription->getName(array('culture' => $language))));
-      $doc->addField(Zend_Search_Lucene_Field::Keyword('levelOfDescriptionId', 
$levelOfDescription->id));
-    }
-    else
-    {
-      $doc->addField(Zend_Search_Lucene_Field::UnIndexed('levelOfDescription', 
null));
-      
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('levelOfDescriptionId', 
null));
-    }
-
-    // Repository
-    if (null !== $repository = 
$informationObject->getRepository(array('inherit' => true)))
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Keyword('repositoryId', 
$repository->id));
-      $doc->addField(Zend_Search_Lucene_Field::Keyword('repositorySlug', 
$repository->slug));
-      $doc->addField(Zend_Search_Lucene_Field::Text('repositoryName', 
$repository->getAuthorizedFormOfName(array('culture' => $language))));
-    }
-    else
-    {
-      $doc->addField(Zend_Search_Lucene_Field::UnIndexed('repositoryId', 
null));
-      $doc->addField(Zend_Search_Lucene_Field::UnIndexed('repositorySlug', 
null));
-      $doc->addField(Zend_Search_Lucene_Field::UnIndexed('repositoryName', 
null));
-    }
-
-    // Identifier
-    $identifierField = Zend_Search_Lucene_Field::Text('identifier', 
$informationObject->getIdentifier());
-    $identifierField->boost = 5;
-    $doc->addField($identifierField);
-
-    // I18n fields
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('alternatetitle', 
$informationObject->getAlternateTitle(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('edition', 
$informationObject->getEdition(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('extentandmedium', 
$informationObject->getExtentAndMedium(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('archivalhistory', 
$informationObject->getArchivalHistory(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('acquisition', 
$informationObject->getAcquisition(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('appraisal', 
$informationObject->getAppraisal(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('accruals', 
$informationObject->getAccruals(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('arrangement', 
$informationObject->getArrangement(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('accessconditions', 
$informationObject->getAccessConditions(array('culture' => $language))));
-    
$doc->addField(Zend_Search_Lucene_Field::Unstored('reproductionconditions', 
$informationObject->getReproductionConditions(array('culture' => $language))));
-    
$doc->addField(Zend_Search_Lucene_Field::Unstored('physicalcharacteristics', 
$informationObject->getPhysicalCharacteristics(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('findingaids', 
$informationObject->getFindingAids(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('locationoforiginals', 
$informationObject->getLocationOfOriginals(array('culture' => $language))));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('locationofcopies', 
$informationObject->getLocationOfCopies(array('culture' => $language))));
-    
$doc->addField(Zend_Search_Lucene_Field::Unstored('relatedunitsofdescription', 
$informationObject->getRelatedUnitsOfDescription(array('culture' => 
$language))));
-
-    // NAME ACCESS POINTS
-    $doc = self::addNameAccessPoints($doc, $informationObject->id, $language);
-
-    // SUBJECT AND PLACE ACCESS POINTS
-    $doc = self::addSubjectsAndPlaces($doc, $informationObject->id, $language);
-
-    // LANGUAGES AND SCRIPTS
-    $doc = self::addLanguagesAndScripts($doc, $informationObject->id, 
$language);
-
-    // NOTES
-    $doc = self::addNotes($doc, $informationObject->id, $language);
-
-    // CONTROL AREA
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('sources', 
$informationObject->getSources(array('culture' => $language))));
-
-    // To come:
-    // Add all dynamic metadata fields to index
-    self::getInstance()->getEngine()->getIndex()->addDocument($doc);
-  }
-
-  protected static function addCreationEvents($doc, $informationObjectId, 
$culture)
-  {
-    if (!isset(self::$conn))
-    {
-      self::$conn = Propel::getConnection();
-    }
-
-    $dates = array();
-    $startDates = array();
-    $endDates = array();
-    $creators = array();
-    $creatorLinks = array();
-    $histories = array();
-
-    if (!isset(self::$statements['creationEvent']))
-    {
-      $sql  = 'SELECT
-                  event.id,
-                  event.start_date,
-                  event.end_date,
-                  event.actor_id,
-                  act_slug.slug,
-                  act_i18n.authorized_form_of_name,
-                  act_i18n.history,
-                  i18n.date';
-      $sql .= ' FROM '.QubitEvent::TABLE_NAME.' event';
-      $sql .= ' JOIN '.QubitEventI18n::TABLE_NAME.' i18n
-                  ON event.id = i18n.id';
-      $sql .= ' LEFT JOIN '.QubitActorI18n::TABLE_NAME.' act_i18n
-                  ON event.actor_id = act_i18n.id';
-      $sql .= ' LEFT JOIN '.QubitSlug::TABLE_NAME.' act_slug
-                  ON event.actor_id = act_slug.object_id';
-      $sql .= ' WHERE event.information_object_id = ?
-                  AND event.type_id = ?
-                  AND i18n.culture = ?
-                  AND act_i18n.culture = ?';
-
-      self::$statements['creationEvent'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['creationEvent']->execute(array(
-      $informationObjectId,
-      QubitTerm::CREATION_ID,
-      $culture,
-      $culture));
-
-    foreach (self::$statements['creationEvent']->fetchAll(PDO::FETCH_OBJ) as 
$item)
-    {
-      if (isset($item->date) || isset($item->start_date) || 
isset($item->end_date))
-      {
-        $dates[] = Qubit::renderDateStartEnd($item->date, $item->start_date, 
$item->end_date);
-      }
-
-      if (isset($item->start_date))
-      {
-        $date = new DateTime($item->start_date);
-        $startDates[] = $date->format('Ymd');
-      }
-
-      if (isset($item->end_date))
-      {
-        $date = new DateTime($item->end_date);
-        $endDates[] = $date->format('Ymd');
-      }
-
-      if (isset($item->actor_id))
-      {
-        $creatorNames[] = $item->authorized_form_of_name;
-
-        $creatorLinks[] = array(
-          'name' => $item->authorized_form_of_name,
-          'slug' => $item->slug);
-
-        if (isset($item->history))
-        {
-          $histories[] = $item->history;
-        }
-      }
-    }
-
-    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('datesOfCreation', 
serialize($dates)));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('startDate', implode(' 
', $startDates)));
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('endDate', implode(' ', 
$endDates)));
-
-    // CREATORS
-    $creatorField = Zend_Search_Lucene_Field::Unstored('creators', implode(' 
', $creatorNames));
-    $creatorField->boost = 8; // Boost the relevance
-    $doc->addField($creatorField);
-
-    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('creatorLinks', 
serialize($creatorLinks)));
-
-    $doc->addField(Zend_Search_Lucene_Field::Unstored('creatorHistory', 
implode(' ', $histories)));
-
-    return $doc;
-  }
-
-  protected static function addNameAccessPoints($doc, $informationObjectId, 
$culture)
-  {
-    $names = array();
-
-    // Subject relations
-    if (!isset(self::$statements['actorRelation']))
-    {
-      $sql  = 'SELECT
-                  i18n.authorized_form_of_name as name';
-      $sql .= ' FROM '.QubitActor::TABLE_NAME.' actor';
-      $sql .= ' JOIN '.QubitActorI18n::TABLE_NAME.' i18n
-                  ON actor.id = i18n.id';
-      $sql .= ' JOIN '.QubitRelation::TABLE_NAME.' relation
-                  ON actor.id = relation.object_id';
-      $sql .= ' WHERE i18n.culture = :culture
-                  AND relation.subject_id = :resourceId
-                  AND relation.type_id = :typeId';
-
-      self::$statements['actorRelation'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['actorRelation']->execute(array(
-      ':culture' => $culture,
-      ':resourceId' => $informationObjectId,
-      ':typeId' => QubitTerm::NAME_ACCESS_POINT_ID));
-
-    foreach (self::$statements['actorRelation']->fetchAll(PDO::FETCH_OBJ) as 
$item)
-    {
-      if (!in_array($item->name, $names))
-      {
-        $names[] = $item->name;
-      }
-    }
-
-    // Event relation
-    if (!isset(self::$statements['actorEvent']))
-    {
-      $sql  = 'SELECT
-                  i18n.authorized_form_of_name as name';
-      $sql .= ' FROM '.QubitActor::TABLE_NAME.' actor';
-      $sql .= ' JOIN '.QubitActorI18n::TABLE_NAME.' i18n
-                  ON actor.id = i18n.id';
-      $sql .= ' JOIN '.QubitEvent::TABLE_NAME.' event
-                  ON actor.id = event.actor_id';
-      $sql .= ' WHERE i18n.culture = :culture
-                  AND event.information_object_id = :resourceId';
-
-      self::$statements['actorEvent'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['actorEvent']->execute(array(
-      ':culture' => $culture,
-      ':resourceId' => $informationObjectId));
-
-    foreach (self::$statements['actorEvent']->fetchAll(PDO::FETCH_OBJ) as 
$item)
-    {
-      if (!in_array($item->name, $names))
-      {
-        $names[] = $item->name;
-      }
-    }
-
-    if (0 < count($names))
-    {
-      $field = Zend_Search_Lucene_Field::Unstored('name', implode(' ', 
$names));
-
-      // Boost the hit relevance for the place field
-      $field->boost = 3;
-      $doc->addField($field);
-    }
-
-    return $doc;
-  }
-
-  protected static function addSubjectsAndPlaces($doc, $informationObjectId, 
$culture)
-  {
-    $subject = $places = array();
-
-    if (!isset(self::$statements['subjectsPlaces']))
-    {
-      $sql  = 'SELECT
-                  term.taxonomy_id,
-                  i18n.name';
-      $sql .= ' FROM '.QubitObjectTermRelation::TABLE_NAME.' otr';
-      $sql .= ' JOIN '.QubitTerm::TABLE_NAME.' term 
-                  ON otr.term_id = term.id';
-      $sql .= ' JOIN '.QubitTermI18n::TABLE_NAME.' i18n
-                  ON term.id = i18n.id';
-      $sql .= ' WHERE otr.object_id = ?
-                  AND i18n.culture = ?
-                  AND (term.taxonomy_id = ?
-                    OR term.taxonomy_id = ?)';
-
-      self::$statements['subjectsPlaces'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['subjectsPlaces']->execute(array(
-      $informationObjectId,
-      $culture,
-      QubitTaxonomy::SUBJECT_ID,
-      QubitTaxonomy::PLACE_ID));
-
-    foreach (self::$statements['subjectsPlaces']->fetchAll(PDO::FETCH_OBJ) as 
$item)
-    {
-      switch ($item->taxonomy_id)
-      {
-        // Subject
-        case QubitTaxonomy::SUBJECT_ID:
-          $subjects[] = $item->name;
-          break;
-
-        // Place
-        case QubitTaxonomy::PLACE_ID:
-          $places[] = $item->name;
-          break;
-
-      }
-
-      if (0 < count($subjects))
-      {
-        $subjectField = Zend_Search_Lucene_Field::Unstored('subject', 
implode(' ', $subjects));
-        // Boost the hit relevance for the subject field
-        $subjectField->boost = 5;
-        $doc->addField($subjectField);
-      }
-
-      if (0 < count($places))
-      {
-        $placeField = Zend_Search_Lucene_Field::Unstored('place', implode(' ', 
$places));
-        // Boost the hit relevance for the place field
-        $placeField->boost = 3;
-        $doc->addField($placeField);
-      }
-    }
-
-    return $doc;
-  }
-
-  protected static function addLanguagesAndScripts($doc, $informationObjectId, 
$culture)
-  {
-    // Get lookup tables
-    if (!isset(self::$lookups['languages'][$culture]))
-    {
-      self::$lookups['languages'][$culture] = 
sfCultureInfo::getInstance($culture)->getLanguages();
-    }
-
-    if (!isset(self::$lookups['scripts'][$culture]))
-    {
-      self::$lookups['scripts'][$culture] = 
sfCultureInfo::getInstance($culture)->getScripts();
-    }
-
-    // Find langs and scripts
-    if (!isset(self::$statements['langAndScript']))
-    {
-      $sql  = 'SELECT
-                  node.name,
-                  i18n.value';
-      $sql .= ' FROM '.QubitProperty::TABLE_NAME.' node';
-      $sql .= ' JOIN '.QubitPropertyI18n::TABLE_NAME.' i18n
-                  ON node.id = i18n.id';
-      $sql .= ' WHERE node.source_culture = i18n.culture
-                  AND node.object_id = ?
-                  AND (node.name = ? OR node.name = ?)';
-
-      self::$statements['langAndScript'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['langAndScript']->execute(array(
-      $informationObjectId,
-      'language',
-      'script'));
-
-    // Add to arrays
-    foreach (self::$statements['langAndScript']->fetchAll(PDO::FETCH_OBJ) as 
$item)
-    {
-      $codes = unserialize($item->value);
-
-      if (0 < count($codes))
-      {
-        switch ($item->name)
-        {
-          case 'language':
-            foreach ($codes as $code)
-            {
-              $languages[] = self::$lookups['languages'][$culture][$code];
-            }
-
-            break;
-
-          case 'script':
-            foreach ($codes as $code)
-            {
-              $scripts[] = self::$lookups['scripts'][$culture][$code];
-            }
-
-            break;
-        }
-      }
-    }
-
-    // Add to document
-    if (0 < count($languages))
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Unstored('language', implode(' 
', $languages)));
-    }
-
-    if (0 < count($scripts))
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Unstored('script', implode(' ', 
$scripts)));
-    }
-
-    return $doc;
-  }
-
-  protected static function addNotes($doc, $informationObjectId, $culture)
-  {
-    $notes = array();
-
-    // Subject relations
-    if (!isset(self::$statements['note']))
-    {
-      $sql  = 'SELECT
-                  i18n.content';
-      $sql .= ' FROM '.QubitNote::TABLE_NAME.' note';
-      $sql .= ' JOIN '.QubitNoteI18n::TABLE_NAME.' i18n
-                  ON note.id = i18n.id';
-      $sql .= ' WHERE note.object_id = ?
-                  AND i18n.culture = ?';
-
-      self::$statements['note'] = self::$conn->prepare($sql);
-    }
-
-    self::$statements['note']->execute(array(
-      $informationObjectId,
-      $culture));
-
-    foreach (self::$statements['note']->fetchAll(PDO::FETCH_OBJ) as $item)
-    {
-      if (0 < strlen($item->content))
-      {
-        $notes[] = $item->content;
-      }
-    }
-
-    if (0 < count($notes))
-    {
-      $doc->addField(Zend_Search_Lucene_Field::Unstored('notes', implode(' ', 
$notes)));
-    }
-
-    return $doc;
+    QubitSearchInformationObject::addToIndex($informationObject->id, 
$language, $options);
   }
 }

Added: trunk/lib/QubitSearchInformationObject.class.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/lib/QubitSearchInformationObject.class.php    Fri Feb 24 17:25:34 
2012        (r10974)
@@ -0,0 +1,733 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Manage information objects in search index
+ *
+ * @package    Qubit
+ * @subpackage QubitSearch
+ * @author     David Juhasz <[email protected]>
+ * @version    SVN: $Id$
+ */
+class QubitSearchInformationObject
+{
+  protected static
+    $conn,
+    $lookups,
+    $statements,
+    $fields = array(
+      'accessconditions',
+      'accruals',
+      'acquisition',
+      'alternatetitle',
+      'appraisal',
+      'archivalhistory',
+      'arrangement',
+      'do_mediaTypeId',
+      'do_thumbnail_FullPath',
+      'edition',
+      'extentandmedium',
+      'className',
+      'collectionRootSlug',
+      'culture',
+      'findingaids',
+      'hasDigitalObject',
+      'identifier',
+      'language',
+      'levelOfDescription',
+      'levelOfDescriptionId',
+      'locationofcopies',
+      'locationoforiginals',
+      'notes',
+      'parent',
+      'partOf',
+      'physicalcharacteristics',
+      'place',
+      'publicationStatusId',
+      'referenceCode',
+      'relatedunitsofdescription',
+      'repositoryId',
+      'repositoryName',
+      'repositorySlug',
+      'reproductionconditions',
+      'script',
+      'scopeAndContent',
+      'slug',
+      'sources',
+      'subject',
+      'title'
+    );
+
+  /**
+   * METHODS
+   */
+  public function __construct($id, $culture, $options = array())
+  {
+    if (isset($options['conn']))
+    {
+      self::$conn = $options['conn'];
+    }
+
+    if (!isset(self::$conn))
+    {
+      self::$conn = Propel::getConnection();
+    }
+
+    $this->resource = $this->getResource($id, $culture, $options);
+
+    $this->index = QubitSearch::getInstance()->getEngine()->getIndex();
+    $this->doc = new Zend_Search_Lucene_Document;
+  }
+
+  public static function populate($offset, $logger, $start)
+  {
+    if (!isset(self::$conn))
+    {
+      self::$conn = Propel::getConnection();
+    }
+
+    // Get count of all information objects
+    $sql = 'SELECT COUNT(*) from '.QubitInformationObject::TABLE_NAME;
+    $rs = self::$conn->query($sql);
+    $rowcount = $rs->fetchColumn(0);
+
+    // Get info objects (with offset)
+    $sql = 'SELECT
+              io.id,
+              i18n.culture,
+              i18n.title
+            FROM '.QubitInformationObject::TABLE_NAME.' io
+            JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
+              ON io.id = i18n.id
+            WHERE io.id > 1';
+
+    if (0 < $offset)
+    {
+      $sql .= ' LIMIT '.$offset.', '.$rowcount;
+    }
+
+    $statement = self::$conn->prepare($sql);
+    $statement->execute();
+
+    if (0 < $offset)
+    {
+      $logger->log('Ignoring first '.$offset.' information objects.');
+    }
+
+    // Loop through results, and add to search index
+    foreach ($statement->fetchAll(PDO::FETCH_OBJ) as $i => $item)
+    {
+      self::addToIndex($item, $item->culture, $options);
+
+      $logger->log('"'.$item->title.'" inserted ('.round(microtime(true) - 
$start, 2).'s) ('.($i + $offset + 1).'/'.$rowcount.')', $this->getName());
+    }
+  }
+
+  public function getResource($id, $culture, $options = array())
+  {
+    if (!isset(self::$statements['informationObject']))
+    {
+      $sql = 'SELECT
+         io.*,
+         i18n.*,
+         slug.slug,
+         pubs.status_id as publication_status_id,
+         do.id as digi_id,
+         do.media_type_id as digi_type_id
+       FROM '.QubitInformationObject::TABLE_NAME.' io
+       JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
+         ON io.id = i18n.id
+       JOIN '.QubitSlug::TABLE_NAME.' slug
+         ON io.id = slug.object_id
+       JOIN '.QubitStatus::TABLE_NAME.' pubs
+         ON io.id = pubs.object_id
+       LEFT JOIN '.QubitDigitalObject::TABLE_NAME.' do
+         ON io.id = do.information_object_id
+       WHERE io.id = :id
+         AND i18n.culture = :culture';
+
+      self::$statements['informationObject'] = self::$conn->prepare($sql);
+    }
+
+    // Do select
+    self::$statements['informationObject']->execute(array(
+      ':id' => $id,
+      ':culture' => $culture));
+
+    // Get first result
+    $resource = self::$statements['informationObject']->fetch(PDO::FETCH_OBJ);
+
+    if (false === $resource)
+    {
+      throw new sfException("Couldn't find information object 
(id:'.$this->resource->id.')");
+    }
+
+    return $resource;
+  }
+
+  public static function addToIndex($resourceId, $culture, $options)
+  {
+    $indexer = new QubitSearchInformationObject($resourceId, $culture, 
$options);
+
+    // Add fields
+    foreach ($indexer->fields as $field)
+    {
+      $indexer->addField($field);
+    }
+
+    // Creation dates and creator
+    $indexer->addCreationEvents();
+
+    $indexer->addDocument();
+  }
+
+  public function addDocument()
+  {
+    $this->index->addDocument($this->doc);
+  }
+
+  protected function addField($name)
+  {
+    switch ($name)
+    {
+      case 'className':
+        $field = Zend_Search_Lucene_Field::Keyword('className', 
'QubitInformationObject');
+
+        break;
+
+      case 'collectionRootSlug':
+        // TODO
+
+        break;
+
+      case 'do_mediaTypeId':
+        // TODO
+
+        break;
+
+      case 'do_thumbnail_FullPath':
+        if (isset($this->resource->digitalObjectId))
+        {
+          $criteria = new Criteria;
+          $criteria->add(DigitalObject::PARENT_ID, $resource->digi_id);
+          $criteria->add(DigitalObject::USAGE_ID, QubitTerm::THUMBNAIL_ID);
+
+          if (null !== $thumbnail = QubitDigitalObject::getOne())
+          {
+            
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('do_thumbnail_FullPath', 
$thumbnail->getFullPath()));
+          }
+        }
+
+        break;
+
+      case 'hasDigitalObject':
+        $field = Zend_Search_Lucene_Field::Keyword('hasDigitalObject', 
isset($this->resource->digitalObjectId) ? 'true' : 'false');
+
+        break;
+
+      case 'identifier':
+        $field = Zend_Search_Lucene_Field::Text('identifier', 
$resource->identifier);
+        $field->boost = 5;
+
+        break;
+
+      case 'language':
+        // TODO
+
+        break;
+
+      case 'levelOfDescription':
+        $field = Zend_Search_Lucene_Field::Text('levelOfDescription', 
$this->getLevelOfDescription());
+
+        break;
+
+      case 'name':
+        $field = Zend_Search_Lucene_Field::Unstored('name', 
$this->getNameAccessPoints());
+        $field->boost = 3;
+
+        break;
+
+      case 'notes':
+        // $field = Zend_Search_Lucene_Field::Unstored('notes', 
$this->getNotes());
+
+        break;
+
+      case 'parent':
+        // TODO
+
+        break;
+
+      case 'partOf':
+        // TODO
+
+        break;
+
+      case 'place':
+        $field = Zend_Search_Lucene_Field::Unstored('place', 
$this->getPlaceAccessPoints());
+        $field->boost = 3;
+
+        break;
+
+      case 'referenceCode':
+        // TODO
+
+        break;
+
+      case 'repositoryId':
+        // TODO
+
+        break;
+
+      case 'repositoryName':
+        // TODO
+
+        break;
+
+      case 'repositorySlug':
+        // TODO
+
+        break;
+
+      case 'subject':
+        $field = Zend_Search_Lucene_Field::Unstored('subject', 
$this->getSubjectAccessPoints());
+        $field->boost = 5;
+
+        break;
+
+      case 'script':
+        // $doc = self::addLanguagesAndScripts();
+
+        break;
+
+      case 'title':
+        $title = $this->resource->title;
+        if (0 == strlen($title))
+        {
+          // Include an i18n fallback for proper search result display in case 
the
+          // title field was not translated
+          $title = self::getFallbackTitle($this->resource->id);
+        }
+
+        // Boost the hit relevance for the title field
+        $field = Zend_Search_Lucene_Field::Text('title', $title);
+        $field->boost = 10;
+
+        break;
+
+      // TEXT fields
+      case 'scopeAndContent':
+        $field = Zend_Search_Lucene_Field::Text($name, $this->resource->$name);
+
+        break;
+
+      // KEYWORD fields (internal ids, slugs, etc.)
+      case 'culture':
+      case 'levelOfDescriptionId':
+      case 'slug':
+      case 'publicationStatusId':
+        if (isset($this->resource->$name))
+        {
+          $field = Zend_Search_Lucene_Field::Keyword($name, 
$this->resource->$name);
+        }
+
+        break;
+
+      // UNSTORED fields
+      default:
+        $field = Zend_Search_Lucene_Field::Unstored($name, 
$this->resource->$name);
+    }
+
+    $this->doc->addField($field);
+  }
+
+  protected static function getFallbackTitle($id, $options = array())
+  {
+    $params = array($id);
+
+    // Get info objects (with offset)
+    $sql  = 'SELECT i18n.title';
+    $sql .= ' FROM '.QubitInformationObject::TABLE_NAME.' node';
+    $sql .= ' JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n';
+    $sql .= '   ON node.id = i18n.id';
+    $sql .= ' WHERE node.id = ?';
+
+    if (isset($options['culture']))
+    {
+      $sql .= ' AND culture = ?';
+      $params[] = $options['culture'];
+    }
+    else
+    {
+      $sql .= ' AND node.source_culture = i18n.culture';
+    }
+
+    $statement = self::$conn->prepare($sql);
+    $statement->execute($params);
+    $result = $statement->fetch(PDO::FETCH_OBJ);
+
+    if (false !== $result)
+    {
+      return $result->title;
+    }
+    else if (isset($options['culture']))
+    {
+      // Get source culture title, if current culture returns no results
+      unset ($options['culture']);
+
+      return self::getFallbackTitle($id, $options);
+    }
+  }
+
+  protected function getLevelOfDescription()
+  {
+    if (0 == count(QubitSearchInformationObject::$levelsOfDescription))
+    {
+      $criteria = new Criteria;
+      $criteria->add(QubitTerm::TAXONOMY_ID, 
QubitTaxonomy::LEVEL_OF_DESCRIPTION_ID);
+      foreach (QubitTerm::get($criteria) as $item)
+      {
+        self::$levelsOfDescription[$item->id] = $item;
+      }
+    }
+
+    if (isset(self::$levelsOfDescription[$this->resource->id]))
+    {
+      return 
self::$levelsOfDescription[$this->resource->id]->getName(array('culture' => 
$this->resource->culture));
+    }
+  }
+
+  protected function addCreationEvents()
+  {
+    $dates = array();
+    $startDates = array();
+    $endDates = array();
+    $creators = array();
+    $creatorLinks = array();
+    $histories = array();
+
+    if (!isset(self::$statements['creationEvent']))
+    {
+      $sql  = 'SELECT
+                  event.id,
+                  event.start_date,
+                  event.end_date,
+                  event.actor_id,
+                  act_slug.slug,
+                  act_i18n.authorized_form_of_name,
+                  act_i18n.history,
+                  i18n.date';
+      $sql .= ' FROM '.QubitEvent::TABLE_NAME.' event';
+      $sql .= ' JOIN '.QubitEventI18n::TABLE_NAME.' i18n
+                  ON event.id = i18n.id';
+      $sql .= ' LEFT JOIN '.QubitActorI18n::TABLE_NAME.' act_i18n
+                  ON event.actor_id = act_i18n.id';
+      $sql .= ' LEFT JOIN '.QubitSlug::TABLE_NAME.' act_slug
+                  ON event.actor_id = act_slug.object_id';
+      $sql .= ' WHERE event.information_object_id = ?
+                  AND event.type_id = ?
+                  AND i18n.culture = ?
+                  AND act_i18n.culture = ?';
+
+      self::$statements['creationEvent'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['creationEvent']->execute(array(
+      $this->resource->id,
+      QubitTerm::CREATION_ID,
+      $this->resource->culture,
+      $this->resource->culture));
+
+    foreach (self::$statements['creationEvent']->fetchAll(PDO::FETCH_OBJ) as 
$item)
+    {
+      if (isset($item->date) || isset($item->start_date) || 
isset($item->end_date))
+      {
+        $dates[] = Qubit::renderDateStartEnd($item->date, $item->start_date, 
$item->end_date);
+      }
+
+      if (isset($item->start_date))
+      {
+        $date = new DateTime($item->start_date);
+        $startDates[] = $date->format('Ymd');
+      }
+
+      if (isset($item->end_date))
+      {
+        $date = new DateTime($item->end_date);
+        $endDates[] = $date->format('Ymd');
+      }
+
+      if (isset($item->actor_id))
+      {
+        $creatorNames[] = $item->authorized_form_of_name;
+
+        $creatorLinks[] = array(
+          'name' => $item->authorized_form_of_name,
+          'slug' => $item->slug);
+
+        if (isset($item->history))
+        {
+          $histories[] = $item->history;
+        }
+      }
+    }
+
+    
$this->doc->addField(Zend_Search_Lucene_Field::UnIndexed('datesOfCreation', 
serialize($dates)));
+    $this->doc->addField(Zend_Search_Lucene_Field::Unstored('startDate', 
implode(' ', $startDates)));
+    $this->doc->addField(Zend_Search_Lucene_Field::Unstored('endDate', 
implode(' ', $endDates)));
+
+    // CREATORS
+    $creatorField = Zend_Search_Lucene_Field::Unstored('creators', implode(' 
', $creatorNames));
+    $creatorField->boost = 8; // Boost the relevance
+    $this->doc->addField($creatorField);
+
+    $this->doc->addField(Zend_Search_Lucene_Field::UnIndexed('creatorLinks', 
serialize($creatorLinks)));
+
+    $this->doc->addField(Zend_Search_Lucene_Field::Unstored('creatorHistory', 
implode(' ', $histories)));
+
+    return $this;
+  }
+
+  protected function getNameAccessPoints()
+  {
+    $names = array();
+
+    // Subject relations
+    if (!isset(self::$statements['actorRelation']))
+    {
+      $sql  = 'SELECT
+                  i18n.authorized_form_of_name as name';
+      $sql .= ' FROM '.QubitActor::TABLE_NAME.' actor';
+      $sql .= ' JOIN '.QubitActorI18n::TABLE_NAME.' i18n
+                  ON actor.id = i18n.id';
+      $sql .= ' JOIN '.QubitRelation::TABLE_NAME.' relation
+                  ON actor.id = relation.object_id';
+      $sql .= ' WHERE i18n.culture = :culture
+                  AND relation.subject_id = :resourceId
+                  AND relation.type_id = :typeId';
+
+      self::$statements['actorRelation'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['actorRelation']->execute(array(
+      ':culture' => $this->resource->culture,
+      ':resourceId' => $this->resource->id,
+      ':typeId' => QubitTerm::NAME_ACCESS_POINT_ID));
+
+    foreach (self::$statements['actorRelation']->fetchAll(PDO::FETCH_OBJ) as 
$item)
+    {
+      if (!in_array($item->name, $names))
+      {
+        $names[] = $item->name;
+      }
+    }
+
+    // Event relation
+    if (!isset(self::$statements['actorEvent']))
+    {
+      $sql  = 'SELECT
+                  i18n.authorized_form_of_name as name';
+      $sql .= ' FROM '.QubitActor::TABLE_NAME.' actor';
+      $sql .= ' JOIN '.QubitActorI18n::TABLE_NAME.' i18n
+                  ON actor.id = i18n.id';
+      $sql .= ' JOIN '.QubitEvent::TABLE_NAME.' event
+                  ON actor.id = event.actor_id';
+      $sql .= ' WHERE i18n.culture = :culture
+                  AND event.information_object_id = :resourceId';
+
+      self::$statements['actorEvent'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['actorEvent']->execute(array(
+      ':culture' => $this->resource->culture,
+      ':resourceId' => $this->resource->id));
+
+    foreach (self::$statements['actorEvent']->fetchAll(PDO::FETCH_OBJ) as 
$item)
+    {
+      if (!in_array($item->name, $names))
+      {
+        $names[] = $item->name;
+      }
+    }
+
+    if (0 < count($names))
+    {
+      return implode(' ', $names);
+    }
+  }
+
+  protected function getSubjectAccessPoints()
+  {
+    return $this->getRelatedTerms(QubitTaxonomy::SUBJECT_ID);
+  }
+
+  protected function getPlaceAccessPoints()
+  {
+    return $this->getRelatedTerms(QubitTaxonomy::PLACE_ID);
+  }
+
+  protected function getRelatedTerms($typeId)
+  {
+    if (!isset(self::$statements['relatedTerms']))
+    {
+      $sql  = 'SELECT
+                  term.taxonomy_id,
+                  i18n.name';
+      $sql .= ' FROM '.QubitObjectTermRelation::TABLE_NAME.' otr';
+      $sql .= ' JOIN '.QubitTerm::TABLE_NAME.' term 
+                  ON otr.term_id = term.id';
+      $sql .= ' JOIN '.QubitTermI18n::TABLE_NAME.' i18n
+                  ON term.id = i18n.id';
+      $sql .= ' WHERE otr.object_id = ?
+                  AND i18n.culture = ?
+                  AND term.taxonomy_id = ?';
+
+      self::$statements['relatedTerms'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['relatedTerms']->execute(array(
+      $this->resource->id,
+      $this->resource->culture,
+      $typeId
+    ));
+
+    foreach (self::$statements['relatedTerms']->fetchAll(PDO::FETCH_OBJ) as 
$item)
+    {
+      $terms[] = $item->name;
+    }
+
+    if (0 < count($terms))
+    {
+      return implode(' ', $terms);
+    }
+  }
+
+  protected static function addLanguagesAndScripts($doc, $informationObjectId, 
$culture)
+  {
+    // Get lookup tables
+    if (!isset(self::$lookups['languages'][$culture]))
+    {
+      self::$lookups['languages'][$culture] = 
sfCultureInfo::getInstance($culture)->getLanguages();
+    }
+
+    if (!isset(self::$lookups['scripts'][$culture]))
+    {
+      self::$lookups['scripts'][$culture] = 
sfCultureInfo::getInstance($culture)->getScripts();
+    }
+
+    // Find langs and scripts
+    if (!isset(self::$statements['langAndScript']))
+    {
+      $sql  = 'SELECT
+                  node.name,
+                  i18n.value';
+      $sql .= ' FROM '.QubitProperty::TABLE_NAME.' node';
+      $sql .= ' JOIN '.QubitPropertyI18n::TABLE_NAME.' i18n
+                  ON node.id = i18n.id';
+      $sql .= ' WHERE node.source_culture = i18n.culture
+                  AND node.object_id = ?
+                  AND (node.name = ? OR node.name = ?)';
+
+      self::$statements['langAndScript'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['langAndScript']->execute(array(
+      $informationObjectId,
+      'language',
+      'script'));
+
+    // Add to arrays
+    foreach (self::$statements['langAndScript']->fetchAll(PDO::FETCH_OBJ) as 
$item)
+    {
+      $codes = unserialize($item->value);
+
+      if (0 < count($codes))
+      {
+        switch ($item->name)
+        {
+          case 'language':
+            foreach ($codes as $code)
+            {
+              $languages[] = self::$lookups['languages'][$culture][$code];
+            }
+
+            break;
+
+          case 'script':
+            foreach ($codes as $code)
+            {
+              $scripts[] = self::$lookups['scripts'][$culture][$code];
+            }
+
+            break;
+        }
+      }
+    }
+
+    // Add to document
+    if (0 < count($languages))
+    {
+      $doc->addField(Zend_Search_Lucene_Field::Unstored('language', implode(' 
', $languages)));
+    }
+
+    if (0 < count($scripts))
+    {
+      $doc->addField(Zend_Search_Lucene_Field::Unstored('script', implode(' ', 
$scripts)));
+    }
+
+    return $doc;
+  }
+
+  protected static function addNotes($doc, $informationObjectId, $culture)
+  {
+    $notes = array();
+
+    // Subject relations
+    if (!isset(self::$statements['note']))
+    {
+      $sql  = 'SELECT
+                  i18n.content';
+      $sql .= ' FROM '.QubitNote::TABLE_NAME.' note';
+      $sql .= ' JOIN '.QubitNoteI18n::TABLE_NAME.' i18n
+                  ON note.id = i18n.id';
+      $sql .= ' WHERE note.object_id = ?
+                  AND i18n.culture = ?';
+
+      self::$statements['note'] = self::$conn->prepare($sql);
+    }
+
+    self::$statements['note']->execute(array(
+      $informationObjectId,
+      $culture));
+
+    foreach (self::$statements['note']->fetchAll(PDO::FETCH_OBJ) as $item)
+    {
+      if (0 < strlen($item->content))
+      {
+        $notes[] = $item->content;
+      }
+    }
+
+    if (0 < count($notes))
+    {
+      $doc->addField(Zend_Search_Lucene_Field::Unstored('notes', implode(' ', 
$notes)));
+    }
+
+    return $doc;
+  }
+}

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to