Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/publish/search.php - 148 lines - 4259 bytes - Summary - Text - Print

Description: Tools for searching site contents.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2005 Dean Allen
   8   * Copyright (C) 2016 The Textpattern Development Team
   9   *
  10   * This file is part of Textpattern.
  11   *
  12   * Textpattern is free software; you can redistribute it and/or
  13   * modify it under the terms of the GNU General Public License
  14   * as published by the Free Software Foundation, version 2.
  15   *
  16   * Textpattern is distributed in the hope that it will be useful,
  17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19   * GNU General Public License for more details.
  20   *
  21   * You should have received a copy of the GNU General Public License
  22   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  23   */
  24  
  25  /**
  26   * Tools for searching site contents.
  27   *
  28   * @package Search
  29   */
  30  
  31  /**
  32   * Performs searching and returns results.
  33   *
  34   * This is now performed by doArticles().
  35   *
  36   * @param      string $q
  37   * @deprecated in 4.0.4
  38   * @see        doArticles()
  39   */
  40  
  41  function search($q)
  42  {
  43      global $prefs;
  44      $url = $prefs['siteurl'];
  45      extract($prefs);
  46  
  47      $s_filter = filterSearch();
  48  
  49      $form = fetch('form', 'txp_form', 'name', 'search_results');
  50  
  51      // Lose this eventually - only used if search_results form is missing.
  52      $form = (!$form) ? legacy_form() : $form;
  53  
  54      $rs = safe_rows(
  55          "*, ID AS thisid, UNIX_TIMESTAMP(Posted) AS posted, Title AS title,
  56          MATCH (Title,Body) AGAINST ('$q') AS score",
  57          'textpattern',
  58          "(Title RLIKE '$q' OR Body RLIKE '$q') $s_filter
  59          AND Status = 4 AND Posted <= ".now('posted')." ORDER BY score DESC LIMIT 40");
  60  
  61      if ($rs) {
  62          $result_rows = count($rs);
  63          $text = ($result_rows == 1) ? gTxt('article_found') : gTxt('articles_found');
  64      } else {
  65          $result_rows = 0;
  66          $text = gTxt('articles_found');
  67      }
  68  
  69      $results[] = graf($result_rows.' '.$text);
  70  
  71      if ($result_rows > 0) {
  72          foreach ($rs as $a) {
  73              extract($a);
  74  
  75              $result_date = safe_strftime($archive_dateformat, $posted);
  76              $uTitle = ($url_title) ? $url_title : stripSpace($Title);
  77              $hurl = permlinkurl($a);
  78              $result_url = '<a href="'.$hurl.'">'.$hurl.'</a>';
  79              $result_title = '<a href="'.$hurl.'">'.$Title.'</a>';
  80  
  81              $result = preg_replace("/>\s*</", "> <", $Body_html);
  82              preg_match_all("/\s.{1,50}".preg_quote($q).".{1,50}\s/i", $result, $concat);
  83  
  84              $concat = join(" ... ", $concat[0]);
  85  
  86              $concat = strip_tags($concat);
  87              $concat = preg_replace('/^[^>]+>/U', "", $concat);
  88              $concat = preg_replace("/($q)/i", "<strong>$1</strong>", $concat);
  89              $result_excerpt = ($concat) ? "... ".$concat." ..." : '';
  90  
  91              $glob['search_result_title']   = $result_title;
  92              $glob['search_result_excerpt'] = $result_excerpt;
  93              $glob['search_result_url']     = $result_url;
  94              $glob['search_result_date']    = $result_date;
  95  
  96              $GLOBALS['this_result'] = $glob;
  97  
  98              $thisresult = $form;
  99  
 100              $results[] = parse($thisresult);
 101          }
 102      }
 103  
 104      return (is_array($results)) ? join('', $results) : '';
 105  }
 106  
 107  /**
 108   * Limits search to searchable sections.
 109   *
 110   * This function gets a list of searchable sections as an SQL where clause.
 111   * The returned results can be then be used in or as an SQL query.
 112   *
 113   * @return string|bool SQL statement, or FALSE when all sections are included the search
 114   * @example
 115   * if ($r = safe_count('textpattern', "Title LIKE '%a%' " . filterSearch()))
 116   * {
 117   *     echo 'Found {$r} articles with "a" in the title.';
 118   * }
 119   */
 120  
 121  function filterSearch()
 122  {
 123      $rs = safe_column("name", 'txp_section', "searchable != '1'");
 124      if ($rs) {
 125          foreach ($rs as $name) {
 126              $filters[] = "AND Section != '".doSlash($name)."'";
 127          }
 128  
 129          return join(' ', $filters);
 130      }
 131  
 132      return false;
 133  }
 134  
 135  /**
 136   * Legacy search results form.
 137   *
 138   * This is no longer used.
 139   *
 140   * @deprecated in 4.0.4
 141   */
 142  
 143  function legacy_form()
 144  {
 145      return '<h2><txp:search_result_title /></h2>
 146  <p><txp:search_result_excerpt /><br/>
 147  <small><txp:search_result_url /> &middot; <txp:search_result_date /></small></p>';
 148  }

title

Description

title

Description

title

Description

title

title

Body