Search2Plugin

search2 (fulltext search)

1. Create the Collection "search" with the following .configxml:

<bxcms xmlns="http://bitflux.org/config">
    <plugins>
    <parameter name="xslt" type="pipeline" value="fullsearch.xsl"/>
        <plugin type="navitree"></plugin>
        <plugin type="sitemap"></plugin>
        <plugin type="customsearch" admin="true"></plugin> 
    </plugins> 
 </bxcms>

2. Put the following fullsearch.xsl file into the theme directory of your design. For example, if you use the 2 column design, put fullsearch.xsl in themes/2-cols/ . The fullsearch.xsl should look like this:

<xsl:stylesheet version="1.0" xmlns:i18n="http://apache.org/cocoon/i18n/2.1"  xmlns:php="http://php.net/xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml php i18n">
    <xsl:import href="master.xsl"/>
    <xsl:import href="../standard/common.xsl"/>

    <xsl:output encoding="utf-8" method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>

    <xsl:template name="content">
        <h1>Search</h1>
        <xsl:variable name="results" select="/bx/plugin[@name='customsearch']/search2/results"/>
        <xsl:variable name="query" select="php:functionString('bx_helpers_globals::GET','q','')"/>
            <form action="./" method="get">
     			<p>
     				<input type="text" name="q" value="{$query}"/>
                	<xsl:text> </xsl:text>
                	<input type="submit" value="Search"/>
				</p>
            </form>
			
        <br/>
<xsl:if test="$query">
<xsl:choose>
        <xsl:when test="$results/*">
       

      <xsl:if test="$results[@type='news']/entry">
             <h2>Blog:</h2>

                    <xsl:apply-templates select="$results[@type='news']/entry"/>
					<br/>
            </xsl:if>

        <xsl:if test="$results[@type='fulltext']/entry">
            <h2>Websites:</h2>

                    <xsl:apply-templates select="$results[@type='fulltext']/entry"/>
					<br/>
            </xsl:if>
<!-- Bookmarksearch stuff starting here: -->            
         <xsl:if test="$results[@type='bookmarks']/entry">
            <h2>Bookmarks:</h2>

                    <xsl:apply-templates select="$results[@type='bookmarks']/entry">
                    <xsl:sort select="title"/>
					</xsl:apply-templates>
            </xsl:if>
<!-- Bookmarksearch stuff ending here. -->

        </xsl:when>
<xsl:otherwise>
        <p> 
          <text>Nothing was found. Please try a different search term.<br/><br/>
          Note: <br/>The search term needs to be at least four characters long. This search engine looks for exact word matches. You can, however, append a * to your search term and the engine will match words if they begin with the word preceding the *.</text> 
        </p> 
  
</xsl:otherwise>
</xsl:choose>
</xsl:if>
    </xsl:template>
    
    
    <xsl:template match="results[@type!='bookmarks']/entry">

            <h4>
                <a href="{url}">
                    <xsl:value-of select="title"/>
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="name"/>
                </a>
            </h4>

            <p>
                <xsl:value-of select="text" disable-output-escaping="yes"/>
            </p>

    </xsl:template>
<!-- Bookmarksearch stuff starting here: -->            
    <xsl:template match="results[@type='bookmarks']/entry">
      		<p>
                <a href="{url}" target="_blank">
                    <xsl:value-of select="title"/>
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="name"/>
                </a>
             </p>
			
    </xsl:template>
<!-- Bookmarksearch stuff ending here. -->

    <xsl:template name="contentRight">
    </xsl:template>

</xsl:stylesheet>

3. Put the following customsearch.php in inc/bx/plugins or in localinc/bx/plugins:

<?php

class bx_plugins_customsearch extends bx_plugins_search2 implements bxIplugin {
    
    /**
    * a static var to to save the instances of this plugin
    */
   
    
    
    /**
    * plugins are singleton, they only exists once (for different modes)
    *  per request. The $mode stuff isn't really used, but may be in 
    *  future releases.
    */
    public static function getInstance($mode) {
        
        if (!isset(self::$instance[$mode])) {
            self::$instance[$mode] = new bx_plugins_customsearch($mode);
        } 
        return self::$instance[$mode];
    }
    
    
    protected function __construct() {
       $this->db = $GLOBALS['POOL']->db; 
    }
     
    
    protected function getPages($search,$tag) {
        $pages =  array();
        $options = array ('searchStart' => 0 , 'searchNumber' => 10,
          'lang' => $GLOBALS['POOL']->config->getOutputLanguage()
        );
       
        $p['fulltext'] = $this->getFulltextPages($search,$tag,$options);
        $p['news'] = $this->getNews($search,$tag,$options); 
        $p['bookmarks'] = $this->getBookmarks($search,$tag,$options);   // Please uncoment this line as well, if you do not want to use the Bookmarks Search.
        
        return $p;
    }
    
    protected function getNews($search, $tag, $options) {
    	$tablePrefix = $GLOBALS['POOL']->config->getTablePrefix();
        $search = explode(" ",$search);
        $search = "+".implode(" +",$search);
        $query = "select id, post_uri, post_title, post_content, changed, sum(MATCH ( post_content, post_title) AGAINST (".$this->db->quote($search)." IN BOOLEAN MODE)) as cnt from ".$tablePrefix."blogposts where
        post_status = 1 and MATCH ( post_content, post_title) AGAINST (".$this->db->quote($search)." IN BOOLEAN MODE) group by post_uri order by cnt, post_date DESC LIMIT 10";
        $res = $this->db->query($query);
         
	        $ids = array();
        while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
            $id = $row['id'];
            $ids[$id]['title'] =  $row['post_title'];
            $ids[$id]['url'] = '/news/'.$row['post_uri'].".html";
            $ids[$id]['lastModified'] = $row['changed'];
            $ids[$id]['text'] = bx_helpers_string::truncate(strip_tags($row['post_content']));
            $ids[$id]['id'] = $id;
            $ids[$id]['cnt'] = $row['cnt'];
        }
        
        return array("entries" =>$ids); 
      }
     
    
/* 	The Bookmarksearch starts below. It searches the Bookmarks' titles.  
 */

  
      protected function getBookmarks($search, $tag) {
  		if (strlen($_GET['q']) < 4) { }
  		else {
  		
  			$xml = simplexml_load_file(BX_DATA_DIR.'/bookmarks/index.en.xhtml');
			$ids = array();
			$id = 0 ;
					
  				if (substr($_GET['q'], -1) == "*") {
  		
  					$searchterm = strToLower(trim(rtrim($_GET['q'], "*")));
					$bookmarks = $xml->xpath("//bookmark[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$searchterm')]");


						foreach ( $bookmarks as $bookmark ){
							if (preg_match("/\b".$searchterm."/i", $bookmark->title)){

							$id++ ; 
		    				$ids[$id]['title'] =  htmlspecialchars($bookmark->title) ;
            				$ids[$id]['url'] = htmlspecialchars($bookmark['href']);
            				$ids[$id]['id'] = $id;       
            				$ids[$id]['text'] = '';
            				$ids[$id]['cnt'] = '';
            				$ids[$id]['lastModified'] = '';
            				}
            				else {}
						}
				}
				
				else {
  					$searchterm = strToLower(trim($_GET['q']));
					$bookmarks = $xml->xpath("//bookmark[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$searchterm')]");

						foreach ( $bookmarks as $bookmark ){
							if (preg_match("/\b".$searchterm."\b/i", $bookmark->title)){

							$id++ ; 
				    		$ids[$id]['title'] =  htmlspecialchars($bookmark->title) ;
        		    		$ids[$id]['url'] = htmlspecialchars($bookmark['href']);
       			     		$ids[$id]['id'] = $id;       
        		    		$ids[$id]['text'] = '';
        		    		$ids[$id]['cnt'] = '';
           			 		$ids[$id]['lastModified'] = '';
           			 		}
           			 		else {}
           			 		
						}		
				}
  		return array("entries" =>$ids); 
  	  	}
  	  }

/* END of Bookmarksearch. */

}

note :

  1. if you don't use bookmarks, you should comment line 38 from customsearch.php
  2. in line 55, you should change 'news' to whatever your blog collection name is.

4. Make the search collection visible and enjoy the search.

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.
These projects are supported by Liip AG