( jc – 26.11.2008 23:06)

class.search_index.inc.php

Suche beschränken: mehr als eine Kategorie

Diese Klasse ist eine erweiterte Variante der Klasse wie sie mit dem search_index Addon 1.5 ausgeliefert wird. Der Unterschied ist, daß die $path Variable – sie steuert welche Kategorie durchsucht werden darf – im Gegensatz zum Original mehrere Kategorien enthalten kann. Die Schreibweise für die Defintion mehrer Kategorien ist ein einfaches Array welches die IDs der erlaubten Kategorien enthält. Beispiel:

$search->path = array(3,4,1,2,120,33); // Kategorie IDs

Die Originalschreibweise bleibt weiterhin gültig, nur eben auf eine Kategorie beschränkt:

$search->path = 3; // Kategorie ID

Source

class.search_index.inc.php

<?php
 
class rex_search_index
{
 
  var $clang = -1;
  var $path = '';
  var $status = false;
  var $searchIds = false;
  var $surroundchars = 20;
  var $sourround_start_tag = "<b>";
  var $sourround_end_tag = "</b>";
  var $striptags = true;
  var $only_warnings = FALSE;
 
  function rex_indexArticle($id = 0,$clang = 0)
  {
    global $REX;
 
    $tmp = $REX['GG'];
    $REX['GG'] = true;
    $REX_ARTICLE = new rex_article;
    $REX_ARTICLE->setCLang($clang);
    $REX_ARTICLE->setArticleId($id);
    $artcache = $REX_ARTICLE->getArticle();
 
    // HTML Code im Artikel filtern  
    if ($this->striptags)
      $artcache = preg_replace('@<[\/\!]*?[^<>]*?>@si', '', $artcache);
 
    // speichern  
    $sql = 'UPDATE rex_article set vt="'.mysql_escape_string($artcache).'" where id='.$id.' and clang='.$clang.'';
    $db2 = new rex_sql;
    // $db2->debugsql = 1;
    $db2->setQuery($sql);
    $REX['GG'] = $tmp;
 
  }
 
  function rex_indexSite()
  {
 
    global $REX;
 
    if ($this->only_warnings) error_reporting(E_WARNINGS);
 
    $SQL = "SELECT id,clang FROM rex_article ";
    $WHERE = "";
 
    $LIMIT = "";
    $db2 = new rex_sql;
    // $db2->debugsql = 1;
    $stop = false;
    $oldstart = 0;
    if (isset($_REQUEST["oldstart"])) $oldstart = (int) $_REQUEST["oldstart"];
 
    $start = 0;
    if (isset($_REQUEST["start"])) $start = (int) $_REQUEST["start"];
 
    if ($oldstart == $start && $start != 0)
    {
      $stop = true;
    }
 
    if ($start != 0)
    {
      $LIMIT = "LIMIT $start, 10000";
      $oldstart = $start;
    }else
    {
      // $db2->setQuery('TRUNCATE TABLE rex_12_search_index');
    }
 
    if ($stop)
    {
 
      return "Bei der Indexgenerieung ist ein Fehler unterlaufen. Das kann an eventuell fehlerhaften Artikeln liegen. 
      Bei folgendem Artikel kam ein Fehler. <a href=index.php?page=content&article_id=".$_REQUEST["errorid"]."&mode=edit&clang=".$_REQUEST["errorclang"].">-> Artikel</a>";
 
    }else
    {
 
      $i=$start;
      $articles = $db2->getArray("$SQL $LIMIT");
      $CONTENT = ob_get_contents();
      // ob_end_clean();
 
      foreach ($articles as $var)
      {
 
        ob_end_clean();
        ob_start();
        echo "<html><head><title>REX SEARCH</title></head><body bgcolor=#fffff3>
        Scriptlaufzeit war zu kurz, der Prozess wird sofort 
        weitergeführt. Sollten Sie dennoch abbrechen wollen dann <a href=index.php?page=search_index>hier</a>.
        <br><br>
        Sollte das Script sich nicht erneut aufrufen, dann <a href=index.php?page=search_index&subpage=gen_index&start=$i&oldstart=$oldstart&errorid=".$var['id']."&errorclang=".$var['clang'].">hier</a> klicken um den Prozess weiterzuführen.
        <br><br><a href=index.php?page=content&article_id=".$var['id']."&mode=edit&clang=".$var['clang'].">Bei diesem Artikel wurde abgebrochen</a>
        <br><br><br><br>";
 
        $this->rex_indexArticle($var['id'],$var['clang']);
 
        $i++;
      }
 
      ob_end_clean();
      echo $CONTENT;
      $REX['GG'] = false;
 
      return "Suchindex wurde erneuert!";
    }
  }
 
 
 
  function rex_search($keywords)
  {
 
  if ($this->only_warnings) error_reporting(E_WARNINGS);
 
    if (trim($keywords) == '')
      return false;
 
    $keywords = mysql_escape_string((trim($keywords)));
 
    $suche = new rex_sql;
    // $suche->debugsql = true;
 
    // ---------------------- clang check
    if ($this->clang > -1)
    {
      $clang_set = "AND clang='".$this->clang."'";
    }
    else
    {
      $clang_set = '';
    }
 
    // ---------------------- status check
    if ($this->status)
    {
      $status_set = "AND status='".$this->status."'";
    }
    else
    {
      $status_set = '';
    }
 
    // ---------------------- path check
    if ($this->path)
    {
      $path_set = '';
      if (is_array($this->path)) // mehrere IDs als array
      {
        $path_set = 'AND (';
        foreach(($this->path) as $path_id)
        {
          $path_set .= "path LIKE ('|".$path_id."|%') OR ";
        }      
        $path_set = rtrim($path_set, " OR ");
        $path_set .= ') ';
      }
      if (is_int($this->path)) // einzelne ID als integer
      {
        $path_set = "AND path LIKE ('|".$this->path."|%')";
      }
    }
    else
    {
      $path_set = '';
    }
 
    // ---------------------- searchIds check
    if ($this->searchIds)
    {
      $find_set = "(FIND_IN_SET('$keywords',REPLACE(id,' ',',')) * 100) +";
      $like_set = "id = '$keywords' OR";
    }
 
    $sql = "
          SELECT
 
          (FIND_IN_SET('$keywords',REPLACE(name,' ',',')) * 10) +
          (FIND_IN_SET('$keywords',REPLACE(art_keywords,' ',',')) * 5) +
          $find_set
          (FIND_IN_SET('$keywords',REPLACE(vt,' ',',')) * 5)
          AS COUNTWORD, id , art_keywords, name , vt , clang
 
          FROM rex_article
          WHERE
          (
          name LIKE ('%$keywords%') OR art_keywords LIKE ('%$keywords%') OR
          $like_set
          vt LIKE ('%$keywords%')
          )
 
          $clang_set
 
          $path_set
 
          $status_set
 
          ORDER BY COUNTWORD DESC
          LIMIT 0,50
          ";
 
    $suche->setQuery($sql);
 
    for ($c = 0; $c < $suche->getRows(); $c ++)
    {
 
      $regex = "/\b.{0,".$this->surroundchars."}".$keywords.".{0,".$this->surroundchars."}\b/im";
      $regexcontent = $suche->getValue('name').$suche->getValue('art_keywords').$suche->getValue('vt');
 
      preg_match_all($regex, $regexcontent, $matches);
 
      $result[$c]['id'] = $suche->getValue('id');
      $result[$c]['name'] = $suche->getValue('name');
      $result[$c]['clang'] = $suche->getValue('clang');
      if (is_array($matches[0]))
      {
        $result[$c]['highlightedtext'] = '';
        foreach ($matches[0] as $var)
        {
          $result[$c]['highlightedtext'] .= " ...".preg_replace("/(".$keywords.")/ims", $this->sourround_start_tag.'\\1'.$this->sourround_end_tag, $var)."... ";
        }
      }
 
      $suche->next();
    }
 
    return $result;
  }
}
?>