TYPO3 CMS  TYPO3_6-2
MysqlFulltextIndexHook.php
Go to the documentation of this file.
1 <?php
3 
22 
26  public $pObj;
27 
28  const ANY_PART_OF_THE_WORD = '1';
29  const LAST_PART_OF_THE_WORD = '2';
31  const SOUNDS_LIKE = '10';
32  const SENTENCE = '20';
40  public function getResultRows_SQLpointer($searchWordsArray, $freeIndexUid = -1) {
41  // Build the search string, detect which fulltext index to use, and decide whether boolean search is needed or not
42  $searchData = $this->getSearchString($searchWordsArray);
43  // Perform SQL Search / collection of result rows array:
44  $resource = FALSE;
45  if ($searchData) {
46  // Do the search:
47  $GLOBALS['TT']->push('execFinalQuery');
48  $resource = $this->execFinalQuery_fulltext($searchData, $freeIndexUid);
49  $GLOBALS['TT']->pull();
50  }
51  return $resource;
52  }
53 
60  public function getSearchString($searchWordArray) {
61  // Initialize variables:
62  $count = 0;
63  // Change this to TRUE to force BOOLEAN SEARCH MODE (useful if fulltext index is still empty)
64  $searchBoolean = FALSE;
65  $fulltextIndex = 'index_fulltext.fulltextdata';
66  // This holds the result if the search is natural (doesn't contain any boolean operators)
67  $naturalSearchString = '';
68  // This holds the result if the search is boolen (contains +/-/| operators)
69  $booleanSearchString = '';
70 
71  $searchType = (string)$this->pObj->getSearchType();
72 
73  // Traverse searchwords and prefix them with corresponding operator
74  foreach ($searchWordArray as $searchWordData) {
75  // Making the query for a single search word based on the search-type
76  $searchWord = $searchWordData['sword'];
77  $wildcard = '';
78  if (strstr($searchWord, ' ')) {
79  $searchType = self::SENTENCE;
80  }
81  switch ($searchType) {
82  case self::ANY_PART_OF_THE_WORD:
83 
84  case self::LAST_PART_OF_THE_WORD:
85 
86  case self::FIRST_PART_OF_THE_WORD:
87  // First part of word
88  $wildcard = '*';
89  // Part-of-word search requires boolean mode!
90  $searchBoolean = TRUE;
91  break;
92  case self::SOUNDS_LIKE:
93  $indexerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\IndexedSearch\\Indexer');
94  // Initialize the indexer-class
96  $searchWord = $indexerObj->metaphone($searchWord, $indexerObj->storeMetaphoneInfoAsWords);
97  unset($indexerObj);
98  $fulltextIndex = 'index_fulltext.metaphonedata';
99  break;
100  case self::SENTENCE:
101  $searchBoolean = TRUE;
102  // Remove existing quotes and fix misplaced quotes.
103  $searchWord = trim(str_replace('"', ' ', $searchWord));
104  break;
105  }
106  // Perform search for word:
107  switch ($searchWordData['oper']) {
108  case 'AND NOT':
109  $booleanSearchString .= ' -' . $searchWord . $wildcard;
110  $searchBoolean = TRUE;
111  break;
112  case 'OR':
113  $booleanSearchString .= ' ' . $searchWord . $wildcard;
114  $searchBoolean = TRUE;
115  break;
116  default:
117  $booleanSearchString .= ' +' . $searchWord . $wildcard;
118  $naturalSearchString .= ' ' . $searchWord;
119  }
120  $count++;
121  }
122  if ($searchType == self::SENTENCE) {
123  $searchString = '"' . trim($naturalSearchString) . '"';
124  } elseif ($searchBoolean) {
125  $searchString = trim($booleanSearchString);
126  } else {
127  $searchString = trim($naturalSearchString);
128  }
129  return array(
130  'searchBoolean' => $searchBoolean,
131  'searchString' => $searchString,
132  'fulltextIndex' => $fulltextIndex
133  );
134  }
135 
143  protected function execFinalQuery_fulltext($searchData, $freeIndexUid = -1) {
144  // Setting up methods of filtering results based on page types, access, etc.
145  $pageJoin = '';
146  // Indexing configuration clause:
147  $freeIndexUidClause = $this->pObj->freeIndexUidWhere($freeIndexUid);
148  // Calling hook for alternative creation of page ID list
149  $searchRootPageIdList = $this->pObj->getSearchRootPageIdList();
150  if ($hookObj = &$this->pObj->hookRequest('execFinalQuery_idList')) {
151  $pageWhere = $hookObj->execFinalQuery_idList('');
152  } elseif ($this->pObj->getJoinPagesForQuery()) {
153  // Alternative to getting all page ids by ->getTreeList() where "excludeSubpages" is NOT respected.
154  $pageJoin = ',
155  pages';
156  $pageWhere = 'pages.uid = ISEC.page_id
157  ' . $GLOBALS['TSFE']->cObj->enableFields('pages') . '
158  AND pages.no_search=0
159  AND pages.doktype<200
160  ';
161  } elseif ($searchRootPageIdList[0] >= 0) {
162 
163  // Collecting all pages IDs in which to search;
164  // filtering out ALL pages that are not accessible due to enableFields. Does NOT look for "no_search" field!
165  $idList = array();
166  foreach ($searchRootPageIdList as $rootId) {
168  $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
169  $idList[] = $cObj->getTreeList( -1 * $rootId, 9999);
170  }
171  $pageWhere = ' ISEC.page_id IN (' . implode(',', $idList) . ')';
172  } else {
173  // Disable everything... (select all)
174  $pageWhere = ' 1=1';
175  }
176  $searchBoolean = '';
177  if ($searchData['searchBoolean']) {
178  $searchBoolean = ' IN BOOLEAN MODE';
179  }
180  $resource = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
181  'index_fulltext.*, ISEC.*, IP.*',
182  'index_fulltext, index_section ISEC, index_phash IP' . $pageJoin,
183  'MATCH (' . $searchData['fulltextIndex'] . ')
184  AGAINST (' . $GLOBALS['TYPO3_DB']->fullQuoteStr($searchData['searchString'], 'index_fulltext') . $searchBoolean . ') ' .
185  $this->pObj->mediaTypeWhere() . ' ' . $this->pObj->languageWhere() . $freeIndexUidClause . '
186  AND index_fulltext.phash = IP.phash
187  AND ISEC.phash = IP.phash
188  AND ' . $pageWhere,
189  'IP.phash,ISEC.phash,ISEC.phash_t3,ISEC.rl0,ISEC.rl1,ISEC.rl2,ISEC.page_id,ISEC.uniqid,IP.phash_grouping,IP.data_filename ,IP.data_page_id ,IP.data_page_reg1,IP.data_page_type,IP.data_page_mp,IP.gr_list,IP.item_type,IP.item_title,IP.item_description,IP.item_mtime,IP.tstamp,IP.item_size,IP.contentHash,IP.crdate,IP.parsetime,IP.sys_language_uid,IP.item_crdate,IP.cHashParams,IP.externalUrl,IP.recordUid,IP.freeIndexUid,IP.freeIndexSetId'
190  );
191  return $resource;
192  }
193 
194 }
getResultRows_SQLpointer($searchWordsArray, $freeIndexUid=-1)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]