improve search

This commit is contained in:
John Bintz 2009-09-21 20:53:51 -04:00
parent 40b3d22820
commit a6e1438231
4 changed files with 56 additions and 29 deletions

View File

@ -1,18 +1,19 @@
<?php <?php
class WDTSTranscriptManager { class WDTSTranscriptManager {
var $key = null; var $key = null;
var $search_key = null;
var $post_id = null; var $post_id = null;
var $allow_multiple = false; var $allow_multiple = false;
function __construct($post_id = null) { function __construct($post_id = null) {
if (is_numeric($post_id)) { $this->post_id = $post_id; } if (is_numeric($post_id)) { $this->post_id = $post_id; }
} }
function WDTSTranscriptManager($post_id = null) { function WDTSTranscriptManager($post_id = null) {
$this->__construct($post_id); $this->__construct($post_id);
} }
function _get_transcripts_metadata() { function _get_transcripts_metadata() {
$transcripts = false; $transcripts = false;
if (!is_null($this->key)) { if (!is_null($this->key)) {
@ -24,7 +25,7 @@ class WDTSTranscriptManager {
} }
return $transcripts; return $transcripts;
} }
/** /**
* Save a transcript to a post. * Save a transcript to a post.
* @param int $post_id The post to attach the transcript to. * @param int $post_id The post to attach the transcript to.
@ -32,24 +33,24 @@ class WDTSTranscriptManager {
* @param string $transcript The transcript content. * @param string $transcript The transcript content.
* @return bool True if the transcript was saved, false otherwise. * @return bool True if the transcript was saved, false otherwise.
*/ */
function save_transcript($transcript_info) { function save_transcript($transcript_info) {
$user = wp_get_current_user(); $user = wp_get_current_user();
if (!empty($user)) { if (!empty($user)) {
$transcript_info = (array)$transcript_info; $transcript_info = (array)$transcript_info;
$transcript_info['user_id'] = $user->ID; $transcript_info['user_id'] = $user->ID;
unset($transcript_info['key']); unset($transcript_info['key']);
foreach (array_keys($transcript_info) as $key) { foreach (array_keys($transcript_info) as $key) {
if (strpos($key, "_") === 0) { unset($transcript_info[$key]); } if (strpos($key, "_") === 0) { unset($transcript_info[$key]); }
} }
if (($transcripts = $this->_get_transcripts_metadata()) !== false) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) {
$max_key = 0; $max_key = 0;
foreach ($transcripts as $transcript) { foreach ($transcripts as $transcript) {
$max_key = max($max_key, $transcript['key']) + 1; $max_key = max($max_key, $transcript['key']) + 1;
} }
$transcript_info['key'] = $max_key; $transcript_info['key'] = $max_key;
if ($this->allow_multiple) { if ($this->allow_multiple) {
$transcripts[] = $transcript_info; $transcripts[] = $transcript_info;
} else { } else {
@ -61,29 +62,39 @@ class WDTSTranscriptManager {
$transcript_info['key']--; $transcript_info['key']--;
$new_transcripts[] = $transcript_info; $new_transcripts[] = $transcript_info;
} else { } else {
$new_transcripts[] = $transcript; $new_transcripts[] = $transcript;
} }
} }
if (!$was_added) { $new_transcripts[] = $transcript_info; } if (!$was_added) { $new_transcripts[] = $transcript_info; }
$transcripts = $new_transcripts; $transcripts = $new_transcripts;
} }
$this->_update_search_field($transcripts);
return update_post_meta($this->post_id, $this->key, $transcripts); return update_post_meta($this->post_id, $this->key, $transcripts);
} }
} }
return false; return false;
} }
function _update_search_field($transcripts) {
if (!empty($this->search_key)) {
$search_lines = array();
foreach ($transcripts as $transcript) { $search_lines[] = $transcript['transcript']; }
update_post_meta($this->post_id, $this->search_key, implode(" ", $search_lines));
}
}
function delete_transcript($language = null) { function delete_transcript($language = null) {
return $this->_delete_transcript_by_field('language', $language); return $this->_delete_transcript_by_field('language', $language);
} }
function delete_transcript_by_key($key = null) { function delete_transcript_by_key($key = null) {
return $this->_delete_transcript_by_field('key', $key); return $this->_delete_transcript_by_field('key', $key);
} }
function _delete_transcript_by_field($field, $value) { function _delete_transcript_by_field($field, $value) {
if (($transcripts = $this->_get_transcripts_metadata()) !== false) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) {
$new_transcripts = array(); $new_transcripts = array();
$deleted_transcript = false; $deleted_transcript = false;
foreach ($transcripts as $transcript) { foreach ($transcripts as $transcript) {
@ -94,31 +105,33 @@ class WDTSTranscriptManager {
} }
} }
$this->_update_search_field($new_transcripts);
update_post_meta($this->post_id, $this->key, $new_transcripts); update_post_meta($this->post_id, $this->key, $new_transcripts);
return $deleted_transcript; return $deleted_transcript;
} }
return false; return false;
} }
function get_transcripts() { function get_transcripts() {
return $this->_get_transcripts_metadata(); return $this->_get_transcripts_metadata();
} }
function get_transcripts_for_user($user_id) { function get_transcripts_for_user($user_id) {
$user_transcripts = array(); $user_transcripts = array();
if (($transcripts = $this->_get_transcripts_metadata()) !== false) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) {
foreach ($transcripts as $transcript) { foreach ($transcripts as $transcript) {
if ($transcript['user_id'] == $user_id) { $user_transcripts[] = $transcript; } if ($transcript['user_id'] == $user_id) { $user_transcripts[] = $transcript; }
} }
} }
return $user_transcripts; return $user_transcripts;
} }
function get_languages() { function get_languages() {
$languages = array(); $languages = array();
if (($transcripts = $this->_get_transcripts_metadata()) !== false) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) {
foreach ($transcripts as $transcript) { foreach ($transcripts as $transcript) {
$languages[$transcript['language']] = true; $languages[$transcript['language']] = true;
} }
} }
return array_keys($languages); return array_keys($languages);

View File

@ -9,6 +9,7 @@ class WDTSQueuedTranscript extends WDTSTranscriptManager {
class WDTSApprovedTranscript extends WDTSTranscriptManager { class WDTSApprovedTranscript extends WDTSTranscriptManager {
var $key = "approved_transcripts"; var $key = "approved_transcripts";
var $search_key = "approved_transcripts_words";
} }
?> ?>

View File

@ -80,7 +80,7 @@ class WhatDidTheySayAdmin {
$search = get_query_var('s'); $search = get_query_var('s');
if (!empty($search)) { if (!empty($search)) {
$where .= $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = %s ", 'approved_transcripts'); $where .= $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = %s ", 'approved_transcripts_words');
$search = addslashes_gpc($search); $search = addslashes_gpc($search);
$where .= " AND $wpdb->postmeta.meta_value LIKE '%$search%') "; $where .= " AND $wpdb->postmeta.meta_value LIKE '%$search%') ";
} }

View File

@ -14,6 +14,7 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
$this->w = new WDTSTranscriptManager(1); $this->w = new WDTSTranscriptManager(1);
$this->w->key = "test"; $this->w->key = "test";
$this->w->search_key = "test_search";
} }
function testSaveTranscript() { function testSaveTranscript() {
@ -36,6 +37,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
get_post_meta(1, $this->w->key, true) get_post_meta(1, $this->w->key, true)
); );
$this->assertEquals("this is a transcript", get_post_meta(1, $this->w->search_key, true));
$this->w->save_transcript(array( $this->w->save_transcript(array(
'language' => 'en', 'language' => 'en',
'transcript' => 'this is another transcript' 'transcript' => 'this is another transcript'
@ -53,6 +56,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
get_post_meta(1, $this->w->key, true) get_post_meta(1, $this->w->key, true)
); );
$this->assertEquals("this is another transcript", get_post_meta(1, $this->w->search_key, true));
$this->w->save_transcript(array( $this->w->save_transcript(array(
'language' => 'fr', 'language' => 'fr',
'transcript' => "il s'agit d'une nouvelle transcription" 'transcript' => "il s'agit d'une nouvelle transcription"
@ -76,6 +81,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
get_post_meta(1, $this->w->key, true) get_post_meta(1, $this->w->key, true)
); );
$this->assertEquals("this is another transcript il s'agit d'une nouvelle transcription", get_post_meta(1, $this->w->search_key, true));
$this->w->allow_multiple = true; $this->w->allow_multiple = true;
$this->w->save_transcript(array( $this->w->save_transcript(array(
@ -106,6 +113,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
), ),
get_post_meta(1, $this->w->key, true) get_post_meta(1, $this->w->key, true)
); );
$this->assertEquals("this is another transcript il s'agit d'une nouvelle transcription this is yet another transcript", get_post_meta(1, $this->w->search_key, true));
} }
function testDeleteTranscript() { function testDeleteTranscript() {
@ -129,6 +138,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
'key' => 1 'key' => 1
), ),
), get_post_meta(1, $this->w->key, true)); ), get_post_meta(1, $this->w->key, true));
$this->assertEquals("il s'agit d'une nouvelle transcription", get_post_meta(1, $this->w->search_key, true));
} }
function testDeleteTranscriptByKey() { function testDeleteTranscriptByKey() {
@ -160,6 +171,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
'key' => 1 'key' => 1
), ),
), get_post_meta(1, $this->w->key, true)); ), get_post_meta(1, $this->w->key, true));
$this->assertEquals("il s'agit d'une nouvelle transcription", get_post_meta(1, $this->w->search_key, true));
} }
function testGetLanguages() { function testGetLanguages() {