* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_TableIndexer * * concatenate list of columns into index table * * @version 0.2.1 * @package Wombat * @subpackage base */ class WBEvent_Handler_TableIndexer extends WBEvent_Handler { /** * table access * @var WBDatasource_Table */ private $table; /** * handler config * * - table name of table to update * - column names to index * - foreign array list each having "table" and "columns" * * @var array */ protected $config = array( 'table' => '', 'culumns' => array(), 'foreign' => array() ); /** * update column in table * * Update exactly one column in one table for one row using primary key * Hence primary key is required, as well as column and table name. * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $table = $this->config['table']; $id = null; if (empty($this->config['table'])) { $table = $e->get('table'); } $id = $e->get('id'); if (empty($id) || $id == '__new') { WBClass::load('WBException_Config'); throw new WBException_Config('Key may not be empty or "__new"', 1, __CLASS__); } if (empty($this->config['columns'])) { WBClass::load('WBException_Config'); throw new WBException_Config('List of columns must not be empty', 2, __CLASS__); } $this->table = WBClass::create('WBDatasource_Table'); $this->table->switchTranslation(false); // removae old index $clause = array(); $clause[] = array( 'field' => 'namespace', 'value' => $table ); $clause[] = array( 'field' => 'xid', 'value' => $id ); $this->table->delete('tableindex', null, null, $clause); // load origin $org = $this->table->get($table, $id); if (1 != count($org)) { return true; } $org = $org[0]; $words = array(); $this->addPrimaryWords($words, $org, $table, $id); $words = array_unique($words); // add words from foreign tables $this->addForeignWords($words, $org, $table, $id); $words = array_unique($words); // save word list $save = array(); foreach ($words as $w) { $save[] = array( 'namespace' => $table, 'xid' => $id, 'word' => $w ); } $this->table->save('tableindex', '__new', $save); return true; } /** * Add words from primary table * * @param array $words * @param array $data * @param string $table * @param string $id */ private function addPrimaryWords(&$words, $data, $table, $id) { if (!is_array($this->config['columns'])) { return; } $this->addWords($words, $data, $this->config['columns']); } /** * Add words from foreign tables * * @param array $words * @param array $data * @param string $table * @param string $id */ private function addForeignWords(&$words, $data, $table, $id) { if (!is_array($this->config['foreign'])) { return; } foreach ($this->config['foreign'] as $f) { if (!isset($f['table']) || empty($f['table'])) { continue; } if (!isset($f['columns']) || !is_array($f['columns']) || empty($f['columns'])) { continue; } $fKey = $this->table->getIdentifier($f['table']); $fData = $this->table->get($f['table'], $data[$fKey]); if (1 != count($fData)) { continue; } $fData = $fData[0]; $this->addWords($words, $fData, $f['columns']); } } /** * Add words from data * * @param array $words * @param array $data * @param array $columns */ private function addWords(&$words, $data, $columns) { // extract words for each colum foreach ($columns as $c) { $text = strip_tags($data[$c]); $text = str_replace(array('_', '-', '/', ';', ':', '.', ',', '!', '?', '"', '\'', '(', ')'), ' ', $text); $text = strtolower(trim(preg_replace('/\\s+/', ' ', $text))); if (empty($text)) { continue; } $words = array_merge($words, explode(' ', $text)); } } } ?>