1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_CatalogSearch
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * CatalogSearch Mysql resource helper model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogSearch_Model_Resource_Helper_Mysql4 extends Mage_Eav_Model_Resource_Helper_Mysql4
36: {
37:
38: /**
39: * Join information for usin full text search
40: *
41: * @param Varien_Db_Select $select
42: * @return Varien_Db_Select $select
43: */
44: public function chooseFulltext($table, $alias, $select)
45: {
46: $field = new Zend_Db_Expr('MATCH ('.$alias.'.data_index) AGAINST (:query IN BOOLEAN MODE)');
47: $select->columns(array('relevance' => $field));
48: return $field;
49: }
50:
51: /**
52: * Prepare Terms
53: *
54: * @param string $str The source string
55: * @return array(0=>words, 1=>terms)
56: */
57: function prepareTerms($str, $maxWordLength = 0)
58: {
59: $boolWords = array(
60: '+' => '+',
61: '-' => '-',
62: '|' => '|',
63: '<' => '<',
64: '>' => '>',
65: '~' => '~',
66: '*' => '*',
67: );
68: $brackets = array(
69: '(' => '(',
70: ')' => ')'
71: );
72: $words = array(0=>"");
73: $terms = array();
74: preg_match_all('/([\(\)]|[\"\'][^"\']*[\"\']|[^\s\"\(\)]*)/uis', $str, $matches);
75: $isOpenBracket = 0;
76: foreach ($matches[1] as $word) {
77: $word = trim($word);
78: if (strlen($word)) {
79: $word = str_replace('"', '', $word);
80: $isBool = in_array(strtoupper($word), $boolWords);
81: $isBracket = in_array($word, $brackets);
82: if (!$isBool && !$isBracket) {
83: $terms[$word] = $word;
84: $word = '"'.$word.'"';
85: $words[] = $word;
86: } else if ($isBracket) {
87: if ($word == '(') {
88: $isOpenBracket++;
89: } else {
90: $isOpenBracket--;
91: }
92: $words[] = $word;
93: } else if ($isBool) {
94: $words[] = $word;
95: }
96: }
97: }
98: if ($isOpenBracket > 0) {
99: $words[] = sprintf("%')".$isOpenBracket."s", '');
100: } else if ($isOpenBracket < 0) {
101: $words[0] = sprintf("%'(".$isOpenBracket."s", '');
102: }
103: if ($maxWordLength && count($terms) > $maxWordLength) {
104: $terms = array_slice($terms, 0, $maxWordLength);
105: }
106: $result = array($words, $terms);
107: return $result;
108: }
109:
110: /**
111: * Use sql compatible with Full Text indexes
112: *
113: * @param mixed $table The table to insert data into.
114: * @param array $data Column-value pairs or array of column-value pairs.
115: * @param arrat $fields update fields pairs or values
116: * @return int The number of affected rows.
117: */
118: public function insertOnDuplicate($table, array $data, array $fields = array()) {
119: return $this->_getWriteAdapter()->insertOnDuplicate($table, $data, $fields);
120: }
121: }
122: