Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Added functionality for hashing keywords, stored in SearchIndexes table.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchisari authored and The Appleseed Project committed Nov 29, 2010
1 parent f15c97a commit 0060c24
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
54 changes: 54 additions & 0 deletions components/search/controllers/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,59 @@ public function Ask ( $pView = null, $pData = null ) {
return ( true );
}

public function Index ( $pView = null, $pData = null ) {

$text = $pData['text'];
$context = $pData['context'];
$id = $pData['id'];

$this->Model = $this->GetModel();

// Change newlines and tabs into spaces, double spaces into single spaces, remove html tags, and trailing spaces
$text = str_replace ( "\n", " ", $text );
$text = str_replace ( "\t", " ", $text );
$text = str_replace ( " ", " ", $text );
$text = strip_tags ( $text );
$text = ltrim ( rtrim ( $text ) );

$depth = $pData['depth'];

if ( !$depth ) $depth = 5;

$parts = explode ( ' ', $text );

// Only leave "word" characters and whitespace
$sentence = preg_replace('/[^\w\s]+/', '', strtolower($text));

// Tokenize
$tokens = explode(' ', $sentence);

for($i = 0; $i < count($tokens); $i++) {
for($j = 1; $j <= count($tokens) - $i; $j++) {

// If we're above the specified phrase depth, continue.
if ( count ( array_slice($tokens, $i, $j) ) > $depth ) continue;

// Continue if phrase is less than three characters.
if ( strlen ( implode(' ', array_slice($tokens, $i, $j)) ) < 3 ) continue;

$phrases[] = implode(' ', array_slice($tokens, $i, $j));
}
}

foreach ( $phrases as $p => $phrase ) {
$e = hash("sha512", $phrase);
$encrypted[$e] = $e;
}

shuffle ( $encrypted );

$keywords = implode ( ' ', $encrypted );

$this->Model->Index ( $context, $id, $keywords );

return ( true );
}

}

55 changes: 55 additions & 0 deletions components/search/models/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @version $Id$
* @package Appleseed.Components
* @subpackage Search
* @copyright Copyright (C) 2004 - 2010 Michael Chisari. All rights reserved.
* @link http://opensource.appleseedproject.org
* @license GNU General Public License version 2.0 (See LICENSE.txt)
*/

// Restrict direct access
defined( 'APPLESEED' ) or die( 'Direct Access Denied' );

/** Profile Component Summary Model
*
* Profile Component Summary Model Class
*
* @package Appleseed.Components
* @subpackage Profile
*/
class cSearchModel extends cModel {

protected $_Tablename = "SearchIndexes";

/**
* Constructor
*
* @access public
*/
public function __construct ( $pTables = null ) {
parent::__construct( $pTables );
}

public function Index ( $pContext, $pContext_FK, $pKeywords ) {

$this->Retrieve ( array ( 'Context' => $pContext, 'Context_FK' => $pContext_FK ) );

if ( $this->Get ( 'Total' ) > 0 ) {
$this->Fetch();
} else {
$this->Set ( 'Created', NOW() );
}

$this->Set ( 'Context', $pContext );
$this->Set ( 'Context_FK', $pContext_FK );
$this->Set ( 'Keywords', $pKeywords );
$this->Set ( 'Updated', NOW() );

$this->Save();

return ( true );
}

}

7 changes: 7 additions & 0 deletions components/search/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ public function __construct ( ) {
parent::__construct();
}

public function Index ( $pData = null ) {

$return = $this->Load ( 'Search', null, 'Index', $pData );

return ( $return );
}

}

0 comments on commit 0060c24

Please sign in to comment.