Esempio n. 1
0
 /**
  * Create index of resource
  *
  * @param modResource $resource
  */
 public function Index(modResource $resource)
 {
     $words = array();
     $intro = '';
     foreach ($this->fields as $field => $weight) {
         $text = strpos($field, 'tv_') !== false ? $resource->getTVValue(substr($field, 3)) : $resource->get($field);
         $forms = $this->mSearch2->getBaseForms($text);
         $intro .= $this->modx->stripTags(is_array($text) ? $this->mSearch2->implode_r(' ', $text) : $text) . ' ';
         foreach ($forms as $form) {
             if (array_key_exists($form, $words)) {
                 $words[$form] += $weight;
             } else {
                 $words[$form] = $weight;
             }
         }
     }
     $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}';";
     $sql .= "INSERT INTO {$tword} (`resource`, `word`, `weight`) VALUES ";
     if (!empty($words)) {
         $rows = array();
         foreach ($words as $word => $weight) {
             $rows[] = '(' . $resource_id . ', "' . $word . '", ' . $weight . ')';
         }
         if (!empty($rows)) {
             $sql .= 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));
     }
 }
 protected function _getBaseForms($text)
 {
     $text = str_ireplace('ё', 'е', $this->modx->stripTags($text));
     $text = preg_replace('#\\[.*\\]#isU', '', $text);
     $bulk_words = $this->mSearch2->getBulkWords($text, $this->mSearch2->config['split_all'], true);
     $this->mSearch2->loadPhpMorphy();
     /* @var phpMorphy $phpMorphy */
     $base_forms = array();
     foreach ($this->mSearch2->phpMorphy as $phpMorphy) {
         $lang = $phpMorphy->getLocale();
         foreach ($bulk_words as $word => $count) {
             $base = $phpMorphy->getBaseForm(array($word));
             foreach ($base as $forms) {
                 if (!$forms) {
                     if (!$this->mSearch2->config['index_all']) {
                         continue;
                     } else {
                         $forms = array($word);
                     }
                 }
                 $form = current($forms);
                 if ($lang == 'en_EN') {
                     $form = iconv('UTF-8', 'ASCII//TRANSLIT', $word);
                 }
                 if (preg_match('/^[0-9]{2,}$/', $form) || mb_strlen($form, 'UTF-8') >= $this->mSearch2->config['min_word_length']) {
                     if (!isset($base_forms[$form])) {
                         $base_forms[$form] = $count;
                     } else {
                         $base_forms[$form] += $count;
                     }
                 }
             }
         }
     }
     return $base_forms;
 }
<?php

/** @var array $scriptProperties */
/** @var pdoFetch $pdoFetch */
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
if (!($pdoClass = $modx->loadClass($fqn, '', false, true))) {
    return false;
}
$pdoFetch = new $pdoClass($modx, $scriptProperties);
$pdoFetch->addTime('pdoTools loaded.');
/** @var mSearch2 $mSearch2 */
if (!$modx->loadClass('msearch2', MODX_CORE_PATH . 'components/msearch2/model/msearch2/', false, true)) {
    return false;
}
$mSearch2 = new mSearch2($modx, $scriptProperties, $pdoFetch);
$mSearch2->initialize($modx->context->key);
$savedProperties = array();
if (empty($queryVar)) {
    $queryVar = 'query';
}
if (empty($parentsVar)) {
    $parentsVar = 'parents';
}
if (empty($minQuery)) {
    $minQuery = $modx->getOption('index_min_words_length', null, 3, true);
}
if (empty($classActive)) {
    $classActive = 'active';
}
if (isset($scriptProperties['disableSuggestions'])) {
    $scriptProperties['suggestions'] = empty($scriptProperties['disableSuggestions']);
<?php

/** @var array $scriptProperties */
/** @var pdoFetch $pdoFetch */
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
if (!($pdoClass = $modx->loadClass($fqn, '', false, true))) {
    return false;
}
$pdoFetch = new $pdoClass($modx, $scriptProperties);
$pdoFetch->addTime('pdoTools loaded.');
/** @var mSearch2 $mSearch2 */
if (!$modx->loadClass('msearch2', MODX_CORE_PATH . 'components/msearch2/model/msearch2/', false, true)) {
    return false;
}
$mSearch2 = new mSearch2($modx, $scriptProperties, $pdoFetch);
if (empty($queryVar)) {
    $queryVar = 'query';
}
if (empty($parentsVar)) {
    $parentsVar = 'parents';
}
if (empty($minQuery)) {
    $minQuery = $modx->getOption('index_min_words_length', null, 3, true);
}
if (empty($htagOpen)) {
    $htagOpen = '<b>';
}
if (empty($htagClose)) {
    $htagClose = '</b>';
}
if (empty($outputSeparator)) {
<?php

/** @var array $scriptProperties */
/** @var pdoTools $pdoTools */
$fqn = $modx->getOption('pdoTools.class', null, 'pdotools.pdofetch', true);
if (!($pdoClass = $modx->loadClass($fqn, '', false, true))) {
    return false;
}
$pdoTools = new $pdoClass($modx, $scriptProperties);
$pdoTools->addTime('pdoTools loaded.');
/** @var mSearch2 $mSearch2 */
if (!$modx->loadClass('msearch2', MODX_CORE_PATH . 'components/msearch2/model/msearch2/', false, true)) {
    return false;
}
$mSearch2 = new mSearch2($modx, array(), $pdoFetch);
$mSearch2->initialize($modx->context->key);
$config = array('autocomplete' => !empty($autocomplete) ? $autocomplete : '', 'queryVar' => !empty($queryVar) ? $queryVar : 'query', 'minQuery' => !empty($minQuery) ? (int) $minQuery : 3, 'pageId' => !empty($pageId) ? (int) $pageId : $modx->resource->id);
$scriptProperties = array_merge($scriptProperties, $config);
if (empty($tplForm)) {
    $tplForm = 'tpl.mSearch2.form';
}
$form = $pdoTools->getChunk($tplForm, $scriptProperties);
if (!empty($config['autocomplete'])) {
    $hash = sha1(serialize($scriptProperties));
    $_SESSION['mSearch2'][$hash] = $scriptProperties;
    $form = str_ireplace('<form', '<form data-key="' . $hash . '"', $form);
    // Place for enabled log
    if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) {
        $form = str_ireplace('</form>', "</form>\n<pre class=\"mSearchFormLog\"></pre>", $form);
    }
    // Setting values for frontend javascript
Esempio n. 6
0
<?php

/** @var array $scriptProperties */
/** @var mSearch2 $mSearch2 */
if (!$modx->loadClass('msearch2', MODX_CORE_PATH . 'components/msearch2/model/msearch2/', false, true)) {
    return false;
}
$mSearch2 = new mSearch2($modx, $scriptProperties);
$mSearch2->initialize($modx->context->key);
/** @var pdoFetch $pdoFetch */
if (!$modx->loadClass('pdofetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', false, true)) {
    return false;
}
$pdoFetch = new pdoFetch($modx, $scriptProperties);
$pdoFetch->addTime('pdoTools loaded.');
$_SESSION['mFilter2'][$modx->resource->id] = array();
if (empty($queryVar)) {
    $queryVar = 'query';
}
if (empty($parentsVar)) {
    $parentsVar = 'parents';
}
if (empty($minQuery)) {
    $minQuery = $modx->getOption('index_min_words_length', null, 3, true);
}
if ($depth == '') {
    $depth = 10;
}
if (empty($classActive)) {
    $classActive = 'active';
}