/** * Efficiently loads tags for a set of photos * * @param PinholePhotoWrapper $photos the photos for which to efficiently load * tags. */ protected function loadPhotoTags(PinholePhotoWrapper $photos) { $instance_id = $this->app->getInstance() === null ? null : $this->app->getInstanceId(); $wrapper = SwatDBClassMap::get('PinholeTagDataObjectWrapper'); // get photo ids $photo_ids = array(); foreach ($photos as $photo) { $photo_ids[] = $photo->id; } $photo_ids = $this->db->implodeArray($photo_ids, 'integer'); // build SQL to select all tags $sql = sprintf('select PinholeTag.*, PinholePhotoTagBinding.photo from PinholeTag inner join PinholePhotoTagBinding on PinholeTag.id = PinholePhotoTagBinding.tag where photo in (%s) and PinholeTag.instance %s %s order by photo, createdate desc', $photo_ids, SwatDB::equalityOperator($instance_id), $this->db->quote($instance_id, 'integer')); // get all tags $tags = SwatDB::query($this->db, $sql, $wrapper); // assign tags to correct photos $current_photo_id = null; $current_recordset = null; foreach ($tags as $tag) { $photo_id = $tag->getInternalValue('photo'); if ($photo_id !== $current_photo_id) { $current_photo_id = $photo_id; $current_recordset = new $wrapper(); $photos[$photo_id]->tags = new $wrapper(); } $photos[$photo_id]->tags->add($tag); } }
/** * Efficiently loads tags for a set of posts * * @param BlorgPostWrapper $posts the posts for which to efficiently load * tags. */ protected function loadPostTags(BlorgPostWrapper $posts) { $instance_id = $this->instance === null ? null : $this->instance->id; $wrapper = SwatDBClassMap::get('BlorgTagWrapper'); // get post ids $post_ids = array(); foreach ($posts as $post) { $post_ids[] = $post->id; } $post_ids = $this->db->implodeArray($post_ids, 'integer'); // build SQL to select all tags $sql = sprintf('select BlorgTag.*, BlorgPostTagBinding.post from BlorgTag inner join BlorgPostTagBinding on BlorgTag.id = BlorgPostTagBinding.tag where post in (%s) and BlorgTag.instance %s %s order by post, createdate desc', $post_ids, SwatDB::equalityOperator($instance_id), $this->db->quote($instance_id, 'integer')); // get all tags $tags = SwatDB::query($this->db, $sql, $wrapper); // assign empty recordsets for all posts foreach ($posts as $post) { $recordset = new $wrapper(); $post->setTags($recordset); } // assign tags to correct posts $current_post_id = null; $current_recordset = null; foreach ($tags as $tag) { $post_id = $tag->getInternalValue('post'); if ($post_id !== $current_post_id) { $current_post_id = $post_id; $current_recordset = $posts[$post_id]->getTags(); } $current_recordset->add($tag); } }
/** * Commits keywords indexed by this indexer to the database index table * * If this indexer was created with the <code>$new</code> parameter then * the index is cleared for this indexer's document type before new * keywords are inserted. Otherwise, the new keywords are simply appended * to the existing index. */ public function commit() { try { $this->db->beginTransaction(); if ($this->new) { $this->clear(); $this->new = false; } $indexed_ids = $this->db->implodeArray($this->clear_document_ids, 'integer'); $delete_sql = sprintf('delete from NateGoSearchIndex where document_id in (%s) and document_type = %s', $indexed_ids, $this->db->quote($this->document_type, 'integer')); $result = $this->db->exec($delete_sql); if (MDB2::isError($result)) { throw new NateGoSearchDBException($result); } $keyword = array_pop($this->keywords); while ($keyword !== null) { $sql = sprintf('insert into NateGoSearchIndex ( document_id, document_type, field_id, word, weight, location ) values (%s, %s, %s, %s, %s, %s)', $this->db->quote($keyword->getDocumentId(), 'integer'), $this->db->quote($keyword->getDocumentType(), 'integer'), $this->db->quote($keyword->getTermId(), 'integer'), $this->db->quote($keyword->getWord(), 'text'), $this->db->quote($keyword->getWeight(), 'integer'), $this->db->quote($keyword->getLocation(), 'integer')); $result = $this->db->exec($sql); if (MDB2::isError($result)) { throw new NateGoSearchDBException($result); } unset($keyword); $keyword = array_pop($this->keywords); } $popular_keyword = array_pop($this->popular_keywords); while ($popular_keyword !== null) { // TODO: there must be a better way to handle dupe words... $sql = sprintf('select count(keyword) from NateGoSearchPopularKeywords where keyword = %s', $this->db->quote($popular_keyword, 'text')); $exists = $this->db->queryOne($sql); if (MDB2::isError($result)) { throw new NateGoSearchDBException($result); } if (!$exists) { $sql = sprintf('insert into NateGoSearchPopularKeywords (keyword) values (%s)', $this->db->quote($popular_keyword, 'text')); $result = $this->db->exec($sql); if (MDB2::isError($result)) { throw new NateGoSearchDBException($result); } } unset($popular_keyword); $popular_keyword = array_pop($this->popular_keywords); } $this->clear_document_ids = array(); $this->db->commit(); } catch (NateGoSearchDBException $e) { $this->db->rollback(); throw $e; } }