/** * Create index of resource * * @param xPDOObject $resource */ public function Index(xPDOObject $resource) { $words = array(); $intro = ''; // For proper transliterate umlauts setlocale(LC_ALL, 'en_US.UTF8', LC_CTYPE); foreach ($this->mSearch2->fields as $field => $weight) { $text = strpos($field, 'tv_') !== false && $resource instanceof modResource ? $resource->getTVValue(substr($field, 3)) : $resource->get($field); $forms = $this->_getBaseForms($text); $intro .= $this->modx->stripTags(is_array($text) ? $this->_implode_r(' ', $text) : $text) . ' '; foreach ($forms as $form => $count) { $words[$form][$field] = $count; } } $tword = $this->modx->getTableName('mseWord'); $tintro = $this->modx->getTableName('mseIntro'); $resource_id = $resource->get('id'); $intro = str_replace(array("\n", "\r\n", "\r"), ' ', $intro); $intro = preg_replace('/\\s+/', ' ', str_replace(array('\'', '"', '«', '»', '`'), '', $intro)); $sql = "INSERT INTO {$tintro} (`resource`, `intro`) VALUES ('{$resource_id}', '{$intro}') ON DUPLICATE KEY UPDATE `intro` = '{$intro}';"; $sql .= "DELETE FROM {$tword} WHERE `resource` = '{$resource_id}';"; if (!($class_key = $resource->get('class_key'))) { $class_key = get_class($resource); } if (!empty($words)) { $rows = array(); foreach ($words as $word => $fields) { foreach ($fields as $field => $count) { $rows[] = "({$resource_id}, '{$field}', '{$word}', '{$count}', '{$class_key}')"; } } $sql .= "INSERT INTO {$tword} (`resource`, `field`, `word`, `count`, `class_key`) VALUES " . implode(',', $rows); //$sql .= " ON DUPLICATE KEY UPDATE `resource` = '$resource_id';"; } $q = $this->modx->prepare($sql); if (!$q->execute()) { $this->modx->log(modX::LOG_LEVEL_ERROR, '[mSearch2] Could not save search index of resource ' . $resource_id . ': ' . print_r($q->errorInfo(), 1)); } }