PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/typo3/sysext/indexed_search/pi/class.tx_indexedsearch.php

https://bitbucket.org/linxpinx/mercurial
PHP | 2479 lines | 1427 code | 351 blank | 701 comment | 269 complexity | 5d691465b1ddef0298db4af2feb34412 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2001-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Index search frontend
  29. *
  30. * $Id: class.tx_indexedsearch.php 8162 2010-07-12 13:22:14Z dmitry $
  31. *
  32. * Creates a searchform for indexed search. Indexing must be enabled
  33. * for this to make sense.
  34. *
  35. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  36. * @co-author Christian Jul Jensen <christian@typo3.com>
  37. */
  38. /**
  39. * [CLASS/FUNCTION INDEX of SCRIPT]
  40. *
  41. *
  42. *
  43. * 123: class tx_indexedsearch extends tslib_pibase
  44. * 168: function main($content, $conf)
  45. * 200: function initialize()
  46. * 413: function getSearchWords($defOp)
  47. * 447: function procSearchWordsByLexer($SWArr)
  48. *
  49. * SECTION: Main functions
  50. * 491: function doSearch($sWArr)
  51. * 549: function getResultRows($sWArr,$freeIndexUid=-1)
  52. * 623: function getResultRows_SQLpointer($sWArr,$freeIndexUid=-1)
  53. * 647: function getDisplayResults($sWArr, $resData, $freeIndexUid=-1)
  54. * 699: function compileResult($resultRows, $freeIndexUid=-1)
  55. *
  56. * SECTION: Searching functions (SQL)
  57. * 800: function getPhashList($sWArr)
  58. * 901: function execPHashListQuery($wordSel,$plusQ='')
  59. * 921: function sectionTableWhere()
  60. * 968: function mediaTypeWhere()
  61. * 993: function languageWhere()
  62. * 1005: function freeIndexUidWhere($freeIndexUid)
  63. * 1046: function execFinalQuery($list,$freeIndexUid=-1)
  64. * 1189: function checkResume($row)
  65. * 1236: function isDescending($inverse=FALSE)
  66. * 1250: function writeSearchStat($sWArr,$count,$pt)
  67. *
  68. * SECTION: HTML output functions
  69. * 1302: function makeSearchForm($optValues)
  70. * 1436: function renderSelectBoxValues($value,$optValues)
  71. * 1455: function printRules()
  72. * 1474: function printResultSectionLinks()
  73. * 1508: function makeSectionHeader($id, $sectionTitleLinked, $countResultRows)
  74. * 1529: function printResultRow($row, $headerOnly=0)
  75. * 1598: function pi_list_browseresults($showResultCount=1,$addString='',$addPart='',$freeIndexUid=-1)
  76. *
  77. * SECTION: Support functions for HTML output (with a minimum of fixed markup)
  78. * 1686: function prepareResultRowTemplateData($row, $headerOnly)
  79. * 1740: function tellUsWhatIsSeachedFor($sWArr)
  80. * 1774: function wrapSW($str)
  81. * 1786: function renderSelectBox($name,$value,$optValues)
  82. * 1810: function makePointerSelector_link($str,$p,$freeIndexUid)
  83. * 1825: function makeItemTypeIcon($it,$alt='',$specRowConf)
  84. * 1867: function makeRating($row)
  85. * 1911: function makeDescription($row,$noMarkup=0,$lgd=180)
  86. * 1942: function markupSWpartsOfString($str)
  87. * 2022: function makeTitle($row)
  88. * 2046: function makeInfo($row,$tmplArray)
  89. * 2075: function getSpecialConfigForRow($row)
  90. * 2099: function makeLanguageIndication($row)
  91. * 2142: function makeAccessIndication($id)
  92. * 2157: function linkPage($id,$str,$row=array(),$markUpSwParams=array())
  93. * 2201: function getRootLine($id,$pathMP='')
  94. * 2216: function getFirstSysDomainRecordForPage($id)
  95. * 2229: function getPathFromPageId($id,$pathMP='')
  96. * 2281: function getMenu($id)
  97. * 2300: function multiplePagesType($item_type)
  98. * 2310: function utf8_to_currentCharset($str)
  99. * 2320: function &hookRequest($functionName)
  100. *
  101. * TOTAL FUNCTIONS: 48
  102. * (This index is automatically created/updated by the extension "extdeveval")
  103. *
  104. */
  105. require_once(t3lib_extMgm::extPath('indexed_search').'class.indexer.php');
  106. /**
  107. * Index search frontend
  108. *
  109. * Creates a searchform for indexed search. Indexing must be enabled
  110. * for this to make sense.
  111. *
  112. * @package TYPO3
  113. * @subpackage tx_indexedsearch
  114. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  115. */
  116. class tx_indexedsearch extends tslib_pibase {
  117. var $prefixId = 'tx_indexedsearch'; // Same as class name
  118. var $scriptRelPath = 'pi/class.tx_indexedsearch.php'; // Path to this script relative to the extension dir.
  119. var $extKey = 'indexed_search'; // The extension key.
  120. var $join_pages = 0; // See document for info about this flag...
  121. var $defaultResultNumber = 10;
  122. var $operator_translate_table = Array ( // case-sensitive. Defines the words, which will be operators between words
  123. Array ('+' , 'AND'),
  124. Array ('|' , 'OR'),
  125. Array ('-' , 'AND NOT'),
  126. // english
  127. # Array ('AND' , 'AND'),
  128. # Array ('OR' , 'OR'),
  129. # Array ('NOT' , 'AND NOT'),
  130. );
  131. // Internal variable
  132. var $wholeSiteIdList = 0; // Root-page PIDs to search in (rl0 field where clause, see initialize() function)
  133. // Internals:
  134. var $sWArr = array(); // Search Words and operators
  135. var $optValues = array(); // Selector box values for search configuration form
  136. var $firstRow = Array(); // Will hold the first row in result - used to calculate relative hit-ratings.
  137. var $cache_path = array(); // Caching of page path
  138. var $cache_rl = array(); // Caching of root line data
  139. var $fe_groups_required = array(); // Required fe_groups memberships for display of a result.
  140. var $domain_records = array(); // Domain records (?)
  141. var $wSelClauses = array(); // Select clauses for individual words
  142. var $resultSections = array(); // Page tree sections for search result.
  143. var $external_parsers = array(); // External parser objects
  144. var $iconFileNameCache = array(); // Storage of icons....
  145. /**
  146. * Lexer object
  147. *
  148. * @var tx_indexedsearch_lexer
  149. */
  150. var $lexerObj;
  151. /**
  152. * Indexer object
  153. *
  154. * @var tx_indexedsearch_indexer
  155. */
  156. var $indexerObj;
  157. var $templateCode; // Will hold the content of $conf['templateFile']
  158. var $hiddenFieldList = 'ext, type, defOp, media, order, group, lang, desc, results';
  159. /**
  160. * Main function, called from TypoScript as a USER_INT object.
  161. *
  162. * @param string Content input, ignore (just put blank string)
  163. * @param array TypoScript configuration of the plugin!
  164. * @return string HTML code for the search form / result display.
  165. */
  166. function main($content, $conf) {
  167. // Initialize:
  168. $this->conf = $conf;
  169. $this->pi_loadLL();
  170. $this->pi_setPiVarDefaults();
  171. // Initialize the indexer-class - just to use a few function (for making hashes)
  172. $this->indexerObj = t3lib_div::makeInstance('tx_indexedsearch_indexer');
  173. // Initialize:
  174. $this->initialize();
  175. // Do search:
  176. // If there were any search words entered...
  177. if (is_array($this->sWArr)) {
  178. $content = $this->doSearch($this->sWArr);
  179. }
  180. // Finally compile all the content, form, messages and results:
  181. $content = $this->makeSearchForm($this->optValues).
  182. $this->printRules().
  183. $content;
  184. return $this->pi_wrapInBaseClass($content);
  185. }
  186. /**
  187. * Initialize internal variables, especially selector box values for the search form and search words
  188. *
  189. * @return void
  190. */
  191. function initialize() {
  192. global $TYPO3_CONF_VARS;
  193. // Initialize external document parsers for icon display and other soft operations
  194. if (is_array($TYPO3_CONF_VARS['EXTCONF']['indexed_search']['external_parsers'])) {
  195. foreach ($TYPO3_CONF_VARS['EXTCONF']['indexed_search']['external_parsers'] as $extension => $_objRef) {
  196. $this->external_parsers[$extension] = t3lib_div::getUserObj($_objRef);
  197. // Init parser and if it returns false, unset its entry again:
  198. if (!$this->external_parsers[$extension]->softInit($extension)) {
  199. unset($this->external_parsers[$extension]);
  200. }
  201. }
  202. }
  203. // Init lexer (used to post-processing of search words)
  204. $lexerObjRef = $TYPO3_CONF_VARS['EXTCONF']['indexed_search']['lexer'] ?
  205. $TYPO3_CONF_VARS['EXTCONF']['indexed_search']['lexer'] :
  206. 'EXT:indexed_search/class.lexer.php:&tx_indexedsearch_lexer';
  207. $this->lexerObj = t3lib_div::getUserObj($lexerObjRef);
  208. // If "_sections" is set, this value overrides any existing value.
  209. if ($this->piVars['_sections']) $this->piVars['sections'] = $this->piVars['_sections'];
  210. // If "_sections" is set, this value overrides any existing value.
  211. if ($this->piVars['_freeIndexUid']!=='_') $this->piVars['freeIndexUid'] = $this->piVars['_freeIndexUid'];
  212. // Add previous search words to current
  213. if ($this->piVars['sword_prev_include'] && $this->piVars['sword_prev']) {
  214. $this->piVars['sword'] = trim($this->piVars['sword_prev']).' '.$this->piVars['sword'];
  215. }
  216. $this->piVars['results'] = t3lib_div::intInRange($this->piVars['results'],1,100000,$this->defaultResultNumber);
  217. // Selector-box values defined here:
  218. $this->optValues = Array(
  219. 'type' => Array(
  220. '0' => $this->pi_getLL('opt_type_0'),
  221. '1' => $this->pi_getLL('opt_type_1'),
  222. '2' => $this->pi_getLL('opt_type_2'),
  223. '3' => $this->pi_getLL('opt_type_3'),
  224. '10' => $this->pi_getLL('opt_type_10'),
  225. '20' => $this->pi_getLL('opt_type_20'),
  226. ),
  227. 'defOp' => Array(
  228. '0' => $this->pi_getLL('opt_defOp_0'),
  229. '1' => $this->pi_getLL('opt_defOp_1'),
  230. ),
  231. 'sections' => Array(
  232. '0' => $this->pi_getLL('opt_sections_0'),
  233. '-1' => $this->pi_getLL('opt_sections_-1'),
  234. '-2' => $this->pi_getLL('opt_sections_-2'),
  235. '-3' => $this->pi_getLL('opt_sections_-3'),
  236. // Here values like "rl1_" and "rl2_" + a rootlevel 1/2 id can be added to perform searches in rootlevel 1+2 specifically. The id-values can even be commaseparated. Eg. "rl1_1,2" would search for stuff inside pages on menu-level 1 which has the uid's 1 and 2.
  237. ),
  238. 'freeIndexUid' => Array(
  239. '-1' => $this->pi_getLL('opt_freeIndexUid_-1'),
  240. '-2' => $this->pi_getLL('opt_freeIndexUid_-2'),
  241. '0' => $this->pi_getLL('opt_freeIndexUid_0'),
  242. ),
  243. 'media' => Array(
  244. '-1' => $this->pi_getLL('opt_media_-1'),
  245. '0' => $this->pi_getLL('opt_media_0'),
  246. '-2' => $this->pi_getLL('opt_media_-2'),
  247. ),
  248. 'order' => Array(
  249. 'rank_flag' => $this->pi_getLL('opt_order_rank_flag'),
  250. 'rank_freq' => $this->pi_getLL('opt_order_rank_freq'),
  251. 'rank_first' => $this->pi_getLL('opt_order_rank_first'),
  252. 'rank_count' => $this->pi_getLL('opt_order_rank_count'),
  253. 'mtime' => $this->pi_getLL('opt_order_mtime'),
  254. 'title' => $this->pi_getLL('opt_order_title'),
  255. 'crdate' => $this->pi_getLL('opt_order_crdate'),
  256. ),
  257. 'group' => Array (
  258. 'sections' => $this->pi_getLL('opt_group_sections'),
  259. 'flat' => $this->pi_getLL('opt_group_flat'),
  260. ),
  261. 'lang' => Array (
  262. -1 => $this->pi_getLL('opt_lang_-1'),
  263. 0 => $this->pi_getLL('opt_lang_0'),
  264. ),
  265. 'desc' => Array (
  266. '0' => $this->pi_getLL('opt_desc_0'),
  267. '1' => $this->pi_getLL('opt_desc_1'),
  268. ),
  269. 'results' => Array (
  270. '10' => '10',
  271. '20' => '20',
  272. '50' => '50',
  273. '100' => '100',
  274. )
  275. );
  276. // Free Index Uid:
  277. if ($this->conf['search.']['defaultFreeIndexUidList']) {
  278. $uidList = t3lib_div::intExplode(',', $this->conf['search.']['defaultFreeIndexUidList']);
  279. $indexCfgRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,title','index_config','uid IN ('.implode(',',$uidList).')'.$this->cObj->enableFields('index_config'),'','','','uid');
  280. foreach ($uidList as $uidValue) {
  281. if (is_array($indexCfgRecords[$uidValue])) {
  282. $this->optValues['freeIndexUid'][$uidValue] = $indexCfgRecords[$uidValue]['title'];
  283. }
  284. }
  285. }
  286. // Should we use join_pages instead of long lists of uids?
  287. if ($this->conf['search.']['skipExtendToSubpagesChecking']) {
  288. $this->join_pages = 1;
  289. }
  290. // Add media to search in:
  291. if (strlen(trim($this->conf['search.']['mediaList']))) {
  292. $mediaList = implode(',', t3lib_div::trimExplode(',', $this->conf['search.']['mediaList'], 1));
  293. }
  294. foreach ($this->external_parsers as $extension => $obj) {
  295. // Skip unwanted extensions
  296. if ($mediaList && !t3lib_div::inList($mediaList, $extension)) { continue; }
  297. if ($name = $obj->searchTypeMediaTitle($extension)) {
  298. $this->optValues['media'][$extension] = $this->pi_getLL('opt_sections_'.$extension,$name);
  299. }
  300. }
  301. // Add operators for various languages
  302. // Converts the operators to UTF-8 and lowercase
  303. $this->operator_translate_table[] = Array($GLOBALS['TSFE']->csConvObj->conv_case('utf-8',$GLOBALS['TSFE']->csConvObj->utf8_encode($this->pi_getLL('local_operator_AND'), $GLOBALS['TSFE']->renderCharset),'toLower') , 'AND');
  304. $this->operator_translate_table[] = Array($GLOBALS['TSFE']->csConvObj->conv_case('utf-8',$GLOBALS['TSFE']->csConvObj->utf8_encode($this->pi_getLL('local_operator_OR'), $GLOBALS['TSFE']->renderCharset),'toLower') , 'OR');
  305. $this->operator_translate_table[] = Array($GLOBALS['TSFE']->csConvObj->conv_case('utf-8',$GLOBALS['TSFE']->csConvObj->utf8_encode($this->pi_getLL('local_operator_NOT'), $GLOBALS['TSFE']->renderCharset),'toLower') , 'AND NOT');
  306. // This is the id of the site root. This value may be a commalist of integer (prepared for this)
  307. $this->wholeSiteIdList = intval($GLOBALS['TSFE']->config['rootLine'][0]['uid']);
  308. // Creating levels for section menu:
  309. // This selects the first and secondary menus for the "sections" selector - so we can search in sections and sub sections.
  310. if ($this->conf['show.']['L1sections']) {
  311. $firstLevelMenu = $this->getMenu($this->wholeSiteIdList);
  312. foreach ($firstLevelMenu as $kk => $mR) {
  313. // @TODO: RFC #7370: doktype 2&5 are deprecated since TYPO3 4.2-beta1
  314. if ($mR['doktype']!=5 && !$mR['nav_hide']) {
  315. $this->optValues['sections']['rl1_'.$mR['uid']] = trim($this->pi_getLL('opt_RL1').' '.$mR['title']);
  316. if ($this->conf['show.']['L2sections']) {
  317. $secondLevelMenu = $this->getMenu($mR['uid']);
  318. foreach ($secondLevelMenu as $kk2 => $mR2) {
  319. // @TODO: RFC #7370: doktype 2&5 are deprecated since TYPO3 4.2-beta1
  320. if ($mR2['doktype']!=5 && !$mR2['nav_hide']) {
  321. $this->optValues['sections']['rl2_'.$mR2['uid']] = trim($this->pi_getLL('opt_RL2').' '.$mR2['title']);
  322. } else unset($secondLevelMenu[$kk2]);
  323. }
  324. $this->optValues['sections']['rl2_'.implode(',',array_keys($secondLevelMenu))] = $this->pi_getLL('opt_RL2ALL');
  325. }
  326. } else unset($firstLevelMenu[$kk]);
  327. }
  328. $this->optValues['sections']['rl1_'.implode(',',array_keys($firstLevelMenu))] = $this->pi_getLL('opt_RL1ALL');
  329. }
  330. // Setting the list of root PIDs for the search. Notice, these page IDs MUST have a TypoScript template with root flag on them! Basically this list is used to select on the "rl0" field and page ids are registered as "rl0" only if a TypoScript template record with root flag is there.
  331. // This happens AFTER the use of $this->wholeSiteIdList above because the above will then fetch the menu for the CURRENT site - regardless of this kind of searching here. Thus a general search will lookup in the WHOLE database while a specific section search will take the current sections...
  332. if ($this->conf['search.']['rootPidList']) {
  333. $this->wholeSiteIdList = implode(',',t3lib_div::intExplode(',',$this->conf['search.']['rootPidList']));
  334. }
  335. // Load the template
  336. $this->templateCode = $this->cObj->fileResource($this->conf['templateFile']);
  337. // Add search languages:
  338. $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_language', '1=1'.$this->cObj->enableFields('sys_language'));
  339. while($lR = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
  340. $this->optValues['lang'][$lR['uid']] = $lR['title'];
  341. }
  342. // Calling hook for modification of initialized content
  343. if ($hookObj = $this->hookRequest('initialize_postProc')) {
  344. $hookObj->initialize_postProc();
  345. }
  346. // Default values set:
  347. // Setting first values in optValues as default values IF there is not corresponding piVar value set already.
  348. foreach ($this->optValues as $kk => $vv) {
  349. if (!isset($this->piVars[$kk])) {
  350. reset($vv);
  351. $this->piVars[$kk] = key($vv);
  352. }
  353. }
  354. // Blind selectors:
  355. if (is_array($this->conf['blind.'])) {
  356. foreach ($this->conf['blind.'] as $kk => $vv) {
  357. if (is_array($vv)) {
  358. foreach ($vv as $kkk => $vvv) {
  359. if (!is_array($vvv) && $vvv && is_array($this->optValues[substr($kk,0,-1)])) {
  360. unset($this->optValues[substr($kk,0,-1)][$kkk]);
  361. }
  362. }
  363. } elseif ($vv) { // If value is not set, unset the option array.
  364. unset($this->optValues[$kk]);
  365. }
  366. }
  367. }
  368. // This gets the search-words into the $sWArr:
  369. $this->sWArr = $this->getSearchWords($this->piVars['defOp']);
  370. }
  371. /**
  372. * Splits the search word input into an array where each word is represented by an array with key "sword" holding the search word and key "oper" holds the SQL operator (eg. AND, OR)
  373. *
  374. * Only words with 2 or more characters are accepted
  375. * Max 200 chars total
  376. * Space is used to split words, "" can be used search for a whole string (not indexed search then)
  377. * AND, OR and NOT are prefix words, overruling the default operator
  378. * +/|/- equals AND, OR and NOT as operators.
  379. * All search words are converted to lowercase.
  380. *
  381. * $defOp is the default operator. 1=OR, 0=AND
  382. *
  383. * @param boolean If true, the default operator will be OR, not AND
  384. * @return array Returns array with search words if any found
  385. */
  386. function getSearchWords($defOp) {
  387. // Shorten search-word string to max 200 bytes (does NOT take multibyte charsets into account - but never mind, shortening the string here is only a run-away feature!)
  388. $inSW = substr($this->piVars['sword'],0,200);
  389. // Convert to UTF-8 + conv. entities (was also converted during indexing!)
  390. $inSW = $GLOBALS['TSFE']->csConvObj->utf8_encode($inSW, $GLOBALS['TSFE']->metaCharset);
  391. $inSW = $GLOBALS['TSFE']->csConvObj->entities_to_utf8($inSW,TRUE);
  392. if ($hookObj = $this->hookRequest('getSearchWords')) {
  393. return $hookObj->getSearchWords_splitSWords($inSW, $defOp);
  394. } else {
  395. if ($this->piVars['type']==20) {
  396. return array(array('sword'=>trim($inSW), 'oper'=>'AND'));
  397. } else {
  398. $search = t3lib_div::makeInstance('tslib_search');
  399. $search->default_operator = $defOp==1 ? 'OR' : 'AND';
  400. $search->operator_translate_table = $this->operator_translate_table;
  401. $search->register_and_explode_search_string($inSW);
  402. if (is_array($search->sword_array)) {
  403. return $this->procSearchWordsByLexer($search->sword_array);
  404. }
  405. }
  406. }
  407. }
  408. /**
  409. * Post-process the search word array so it will match the words that was indexed (including case-folding if any)
  410. * If any words are splitted into multiple words (eg. CJK will be!) the operator of the main word will remain.
  411. *
  412. * @param array Search word array
  413. * @return array Search word array, processed through lexer
  414. */
  415. function procSearchWordsByLexer($SWArr) {
  416. // Init output variable:
  417. $newSWArr = array();
  418. // Traverse the search word array:
  419. foreach ($SWArr as $wordDef) {
  420. if (!strstr($wordDef['sword'],' ')) { // No space in word (otherwise it might be a sentense in quotes like "there is").
  421. // Split the search word by lexer:
  422. $res = $this->lexerObj->split2Words($wordDef['sword']);
  423. // Traverse lexer result and add all words again:
  424. foreach ($res as $word) {
  425. $newSWArr[] = array('sword'=>$word, 'oper'=>$wordDef['oper']);
  426. }
  427. } else {
  428. $newSWArr[] = $wordDef;
  429. }
  430. }
  431. // Return result:
  432. return $newSWArr;
  433. }
  434. /*****************************
  435. *
  436. * Main functions
  437. *
  438. *****************************/
  439. /**
  440. * Performs the search, the display and writing stats
  441. *
  442. * @param array Search words in array, see ->getSearchWords() for details
  443. * @return string HTML for result display.
  444. */
  445. function doSearch($sWArr) {
  446. // Find free index uid:
  447. $freeIndexUid = $this->piVars['freeIndexUid'];
  448. if ($freeIndexUid==-2) {
  449. $freeIndexUid = $this->conf['search.']['defaultFreeIndexUidList'];
  450. }
  451. $indexCfgs = t3lib_div::intExplode(',',$freeIndexUid);
  452. $accumulatedContent = '';
  453. foreach ($indexCfgs as $freeIndexUid) {
  454. // Get result rows:
  455. $pt1 = t3lib_div::milliseconds();
  456. if ($hookObj = $this->hookRequest('getResultRows')) {
  457. $resData = $hookObj->getResultRows($sWArr,$freeIndexUid);
  458. } else {
  459. $resData = $this->getResultRows($sWArr,$freeIndexUid);
  460. }
  461. // Display search results:
  462. $pt2 = t3lib_div::milliseconds();
  463. if ($hookObj = $this->hookRequest('getDisplayResults')) {
  464. $content = $hookObj->getDisplayResults($sWArr, $resData, $freeIndexUid);
  465. } else {
  466. $content = $this->getDisplayResults($sWArr, $resData, $freeIndexUid);
  467. }
  468. $pt3 = t3lib_div::milliseconds();
  469. // Create header if we are searching more than one indexing configuration:
  470. if (count($indexCfgs)>1) {
  471. if ($freeIndexUid>0) {
  472. list($indexCfgRec) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('title','index_config','uid='.intval($freeIndexUid).$this->cObj->enableFields('index_config'));
  473. $titleString = $indexCfgRec['title'];
  474. } else {
  475. $titleString = $this->pi_getLL('opt_freeIndexUid_header_'.$freeIndexUid);
  476. }
  477. $content = '<h1 class="tx-indexedsearch-category">'.htmlspecialchars($titleString).'</h1>'.$content;
  478. }
  479. $accumulatedContent.=$content;
  480. }
  481. // Write search statistics
  482. $this->writeSearchStat($sWArr,$resData['count'],array($pt1,$pt2,$pt3));
  483. // Return content:
  484. return $accumulatedContent;
  485. }
  486. /**
  487. * Get search result rows / data from database. Returned as data in array.
  488. *
  489. * @param array Search word array
  490. * @param integer Pointer to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
  491. * @return array False if no result, otherwise an array with keys for first row, result rows and total number of results found.
  492. */
  493. function getResultRows($sWArr,$freeIndexUid=-1) {
  494. // Getting SQL result pointer:
  495. $GLOBALS['TT']->push('Searching result');
  496. $res = $this->getResultRows_SQLpointer($sWArr,$freeIndexUid);
  497. $GLOBALS['TT']->pull();
  498. // Organize and process result:
  499. if ($res) {
  500. $count = $GLOBALS['TYPO3_DB']->sql_num_rows($res); // Total search-result count
  501. $pointer = t3lib_div::intInRange($this->piVars['pointer'], 0, floor($count/$this->piVars['results'])); // The pointer is set to the result page that is currently being viewed
  502. // Initialize result accumulation variables:
  503. $c = 0; // Result pointer: Counts up the position in the current search-result
  504. $grouping_phashes = array(); // Used to filter out duplicates.
  505. $grouping_chashes = array(); // Used to filter out duplicates BASED ON cHash.
  506. $firstRow = array(); // Will hold the first row in result - used to calculate relative hit-ratings.
  507. $resultRows = array(); // Will hold the results rows for display.
  508. $exactCount = $this->conf['search.']['exactCount']; // Continue counting and checking of results even if we are sure they are not displayed in this request. This will slow down your page rendering, but it allows precise search result counters.
  509. // Now, traverse result and put the rows to be displayed into an array
  510. // Each row should contain the fields from 'ISEC.*, IP.*' combined + artificial fields "show_resume" (boolean) and "result_number" (counter)
  511. while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
  512. // Set first row:
  513. if (!$c) {
  514. $firstRow = $row;
  515. }
  516. $row['show_resume'] = $this->checkResume($row); // Tells whether we can link directly to a document or not (depends on possible right problems)
  517. $phashGr = !in_array($row['phash_grouping'], $grouping_phashes);
  518. $chashGr = !in_array($row['contentHash'].'.'.$row['data_page_id'], $grouping_chashes);
  519. if ($phashGr && $chashGr) {
  520. if ($row['show_resume'] || $this->conf['show.']['forbiddenRecords']) { // Only if the resume may be shown are we going to filter out duplicates...
  521. if (!$this->multiplePagesType($row['item_type'])) { // Only on documents which are not multiple pages documents
  522. $grouping_phashes[] = $row['phash_grouping'];
  523. }
  524. $grouping_chashes[] = $row['contentHash'].'.'.$row['data_page_id'];
  525. $c++; // Increase the result pointer
  526. // All rows for display is put into resultRows[]
  527. if ($c > $pointer * $this->piVars['results'] && $c <= ($pointer * $this->piVars['results'] + $this->piVars['results'])) {
  528. $row['result_number'] = $c;
  529. $resultRows[] = $row;
  530. // This may lead to a problem: If the result check is not stopped here, the search will take longer. However the result counter will not filter out grouped cHashes/pHashes that were not processed yet. You can change this behavior using the "search.exactCount" property (see above).
  531. if (!$exactCount && (($c+1) > ($pointer+1)*$this->piVars['results'])) { break; }
  532. }
  533. } else {
  534. $count--; // Skip this row if the user cannot view it (missing permission)
  535. }
  536. } else {
  537. $count--; // For each time a phash_grouping document is found (which is thus not displayed) the search-result count is reduced, so that it matches the number of rows displayed.
  538. }
  539. }
  540. return array(
  541. 'resultRows' => $resultRows,
  542. 'firstRow' => $firstRow,
  543. 'count' => $count
  544. );
  545. } else { // No results found:
  546. return FALSE;
  547. }
  548. }
  549. /**
  550. * Gets a SQL result pointer to traverse for the search records.
  551. *
  552. * @param array Search words
  553. * @param integer Pointer to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
  554. * @return pointer
  555. */
  556. function getResultRows_SQLpointer($sWArr,$freeIndexUid=-1) {
  557. // This SEARCHES for the searchwords in $sWArr AND returns a COMPLETE list of phash-integers of the matches.
  558. $list = $this->getPhashList($sWArr);
  559. // Perform SQL Search / collection of result rows array:
  560. if ($list) {
  561. // Do the search:
  562. $GLOBALS['TT']->push('execFinalQuery');
  563. $res = $this->execFinalQuery($list,$freeIndexUid);
  564. $GLOBALS['TT']->pull();
  565. return $res;
  566. } else {
  567. return FALSE;
  568. }
  569. }
  570. /**
  571. * Compiles the HTML display of the incoming array of result rows.
  572. *
  573. * @param array Search words array (for display of text describing what was searched for)
  574. * @param array Array with result rows, count, first row.
  575. * @param integer Pointer to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
  576. * @return string HTML content to display result.
  577. */
  578. function getDisplayResults($sWArr, $resData, $freeIndexUid=-1) {
  579. // Perform display of result rows array:
  580. if ($resData) {
  581. $GLOBALS['TT']->push('Display Final result');
  582. // Set first selected row (for calculation of ranking later)
  583. $this->firstRow = $resData['firstRow'];
  584. // Result display here:
  585. $rowcontent = '';
  586. $rowcontent.= $this->compileResult($resData['resultRows'], $freeIndexUid);
  587. // Browsing box:
  588. if ($resData['count']) {
  589. $this->internal['res_count'] = $resData['count'];
  590. $this->internal['results_at_a_time'] = $this->piVars['results'];
  591. $this->internal['maxPages'] = t3lib_div::intInRange($this->conf['search.']['page_links'],1,100,10);
  592. $addString = ($resData['count']&&$this->piVars['group']=='sections'&&$freeIndexUid<=0 ? ' '.sprintf($this->pi_getLL(count($this->resultSections)>1?'inNsections':'inNsection'),count($this->resultSections)):'');
  593. $browseBox1 = $this->pi_list_browseresults(1,$addString,$this->printResultSectionLinks(),$freeIndexUid);
  594. $browseBox2 = $this->pi_list_browseresults(0,'','',$freeIndexUid);
  595. }
  596. // Browsing nav, bottom.
  597. if ($resData['count']) {
  598. $content = $browseBox1.$rowcontent.$browseBox2;
  599. } else {
  600. $content = '<p'.$this->pi_classParam('noresults').'>'.$this->pi_getLL('noResults','',1).'</p>';
  601. }
  602. $GLOBALS['TT']->pull();
  603. } else {
  604. $content.='<p'.$this->pi_classParam('noresults').'>'.$this->pi_getLL('noResults','',1).'</p>';
  605. }
  606. // Print a message telling which words we searched for, and in which sections etc.
  607. $what = $this->tellUsWhatIsSeachedFor($sWArr).
  608. (substr($this->piVars['sections'],0,2)=='rl'?' '.$this->pi_getLL('inSection','',1).' "'.substr($this->getPathFromPageId(substr($this->piVars['sections'],4)),1).'"':'');
  609. $what = '<div'.$this->pi_classParam('whatis').'>'.$this->cObj->stdWrap($what, $this->conf['whatis_stdWrap.']).'</div>';
  610. $content = $what.$content;
  611. // Return content:
  612. return $content;
  613. }
  614. /**
  615. * Takes the array with resultrows as input and returns the result-HTML-code
  616. * Takes the "group" var into account: Makes a "section" or "flat" display.
  617. *
  618. * @param array Result rows
  619. * @param integer Pointer to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
  620. * @return string HTML
  621. */
  622. function compileResult($resultRows, $freeIndexUid=-1) {
  623. $content = '';
  624. // Transfer result rows to new variable, performing some mapping of sub-results etc.
  625. $newResultRows = array();
  626. foreach ($resultRows as $row) {
  627. $id = md5($row['phash_grouping']);
  628. if (is_array($newResultRows[$id])) {
  629. if (!$newResultRows[$id]['show_resume'] && $row['show_resume']) { // swapping:
  630. // Remove old
  631. $subrows = $newResultRows[$id]['_sub'];
  632. unset($newResultRows[$id]['_sub']);
  633. $subrows[] = $newResultRows[$id];
  634. // Insert new:
  635. $newResultRows[$id] = $row;
  636. $newResultRows[$id]['_sub'] = $subrows;
  637. } else $newResultRows[$id]['_sub'][] = $row;
  638. } else {
  639. $newResultRows[$id] = $row;
  640. }
  641. }
  642. $resultRows = $newResultRows;
  643. $this->resultSections = array();
  644. if ($freeIndexUid<=0) {
  645. switch($this->piVars['group']) {
  646. case 'sections':
  647. $rl2flag = substr($this->piVars['sections'],0,2)=='rl';
  648. $sections = array();
  649. foreach ($resultRows as $row) {
  650. $id = $row['rl0'].'-'.$row['rl1'].($rl2flag?'-'.$row['rl2']:'');
  651. $sections[$id][] = $row;
  652. }
  653. $this->resultSections = array();
  654. foreach ($sections as $id => $resultRows) {
  655. $rlParts = explode('-',$id);
  656. $theId = $rlParts[2] ? $rlParts[2] : ($rlParts[1]?$rlParts[1]:$rlParts[0]);
  657. $theRLid = $rlParts[2] ? 'rl2_'.$rlParts[2]:($rlParts[1]?'rl1_'.$rlParts[1]:'0');
  658. $sectionName = $this->getPathFromPageId($theId);
  659. if ($sectionName{0} == '/') $sectionName = substr($sectionName,1);
  660. if (!trim($sectionName)) {
  661. $sectionTitleLinked = $this->pi_getLL('unnamedSection','',1).':';
  662. } else {
  663. $onclick = 'document.'.$this->prefixId.'[\''.$this->prefixId.'[_sections]\'].value=\''.$theRLid.'\';document.'.$this->prefixId.'.submit();return false;';
  664. $sectionTitleLinked = '<a href="#" onclick="'.htmlspecialchars($onclick).'">'.htmlspecialchars($sectionName).':</a>';
  665. }
  666. $this->resultSections[$id] = array($sectionName,count($resultRows));
  667. // Add content header:
  668. $content.= $this->makeSectionHeader($id,$sectionTitleLinked,count($resultRows));
  669. // Render result rows:
  670. foreach ($resultRows as $row) {
  671. $content.= $this->printResultRow($row);
  672. }
  673. }
  674. break;
  675. default: // flat:
  676. foreach ($resultRows as $row) {
  677. $content.= $this->printResultRow($row);
  678. }
  679. break;
  680. }
  681. } else {
  682. foreach ($resultRows as $row) {
  683. $content.= $this->printResultRow($row);
  684. }
  685. }
  686. return '<div'.$this->pi_classParam('res').'>'.$content.'</div>';
  687. }
  688. /***********************************
  689. *
  690. * Searching functions (SQL)
  691. *
  692. ***********************************/
  693. /**
  694. * Returns a COMPLETE list of phash-integers matching the search-result composed of the search-words in the sWArr array.
  695. * The list of phash integers are unsorted and should be used for subsequent selection of index_phash records for display of the result.
  696. *
  697. * @param array Search word array
  698. * @return string List of integers
  699. */
  700. function getPhashList($sWArr) {
  701. // Initialize variables:
  702. $c=0;
  703. $totalHashList = array(); // This array accumulates the phash-values
  704. $this->wSelClauses = array();
  705. // Traverse searchwords; for each, select all phash integers and merge/diff/intersect them with previous word (based on operator)
  706. foreach ($sWArr as $k => $v) {
  707. // Making the query for a single search word based on the search-type
  708. $sWord = $v['sword']; // $GLOBALS['TSFE']->csConvObj->conv_case('utf-8',$v['sword'],'toLower'); // lower-case all of them...
  709. $theType = (string)$this->piVars['type'];
  710. if (strstr($sWord,' ')) $theType = 20; // If there are spaces in the search-word, make a full text search instead.
  711. $GLOBALS['TT']->push('SearchWord "'.$sWord.'" - $theType='.$theType);
  712. $res = '';
  713. $wSel='';
  714. // Perform search for word:
  715. switch($theType) {
  716. case '1': // Part of word
  717. $wSel = "IW.baseword LIKE '%".$GLOBALS['TYPO3_DB']->quoteStr($sWord, 'index_words')."%'";
  718. $res = $this->execPHashListQuery($wSel,' AND is_stopword=0');
  719. break;
  720. case '2': // First part of word
  721. $wSel = "IW.baseword LIKE '".$GLOBALS['TYPO3_DB']->quoteStr($sWord, 'index_words')."%'";
  722. $res = $this->execPHashListQuery($wSel,' AND is_stopword=0');
  723. break;
  724. case '3': // Last part of word
  725. $wSel = "IW.baseword LIKE '%".$GLOBALS['TYPO3_DB']->quoteStr($sWord, 'index_words')."'";
  726. $res = $this->execPHashListQuery($wSel,' AND is_stopword=0');
  727. break;
  728. case '10': // Sounds like
  729. $wSel = 'IW.metaphone = '.$this->indexerObj->metaphone($sWord);
  730. $res = $this->execPHashListQuery($wSel,' AND is_stopword=0');
  731. break;
  732. case '20': // Sentence
  733. $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
  734. 'ISEC.phash',
  735. 'index_section ISEC, index_fulltext IFT',
  736. 'IFT.fulltextdata LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($sWord, 'index_fulltext').'%\' AND
  737. ISEC.phash = IFT.phash
  738. '.$this->sectionTableWhere(),
  739. 'ISEC.phash'
  740. );
  741. $wSel = '1=1';
  742. if ($this->piVars['type']==20) $this->piVars['order'] = 'mtime'; // If there is a fulltext search for a sentence there is a likeliness that sorting cannot be done by the rankings from the rel-table (because no relations will exist for the sentence in the word-table). So therefore mtime is used instaed. It is not required, but otherwise some hits may be left out.
  743. break;
  744. default: // Distinct word
  745. $wSel = 'IW.wid = '.$hash = $this->indexerObj->md5inthash($sWord);
  746. $res = $this->execPHashListQuery($wSel,' AND is_stopword=0');
  747. break;
  748. }
  749. // Accumulate the word-select clauses
  750. $this->wSelClauses[] = $wSel;
  751. // If there was a query to do, then select all phash-integers which resulted from this.
  752. if ($res) {
  753. // Get phash list by searching for it:
  754. $phashList = array();
  755. while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
  756. $phashList[] = $row['phash'];
  757. }
  758. $GLOBALS['TYPO3_DB']->sql_free_result($res);
  759. // Here the phash list are merged with the existing result based on whether we are dealing with OR, NOT or AND operations.
  760. if ($c) {
  761. switch($v['oper']) {
  762. case 'OR':
  763. $totalHashList = array_unique(array_merge($phashList,$totalHashList));
  764. break;
  765. case 'AND NOT':
  766. $totalHashList = array_diff($totalHashList,$phashList);
  767. break;
  768. default: // AND...
  769. $totalHashList = array_intersect($totalHashList,$phashList);
  770. break;
  771. }
  772. } else {
  773. $totalHashList = $phashList; // First search
  774. }
  775. }
  776. $GLOBALS['TT']->pull();
  777. $c++;
  778. }
  779. return implode(',',$totalHashList);
  780. }
  781. /**
  782. * Returns a query which selects the search-word from the word/rel tables.
  783. *
  784. * @param string WHERE clause selecting the word from phash
  785. * @param string Additional AND clause in the end of the query.
  786. * @return pointer SQL result pointer
  787. */
  788. function execPHashListQuery($wordSel,$plusQ='') {
  789. return $GLOBALS['TYPO3_DB']->exec_SELECTquery(
  790. 'IR.phash',
  791. 'index_words IW,
  792. index_rel IR,
  793. index_section ISEC',
  794. $wordSel.'
  795. AND IW.wid=IR.wid
  796. AND ISEC.phash = IR.phash
  797. '.$this->sectionTableWhere().'
  798. '.$plusQ,
  799. 'IR.phash'
  800. );
  801. }
  802. /**
  803. * Returns AND statement for selection of section in database. (rootlevel 0-2 + page_id)
  804. *
  805. * @return string AND clause for selection of section in database.
  806. */
  807. function sectionTableWhere() {
  808. $out = $this->wholeSiteIdList<0 ? '' : 'AND ISEC.rl0 IN ('.$this->wholeSiteIdList.')';
  809. $match = '';
  810. if (substr($this->piVars['sections'],0,4)=='rl1_') {
  811. $list = implode(',',t3lib_div::intExplode(',',substr($this->piVars['sections'],4)));
  812. $out.= 'AND ISEC.rl1 IN ('.$list.')';
  813. $match = TRUE;
  814. } elseif (substr($this->piVars['sections'],0,4)=='rl2_') {
  815. $list = implode(',',t3lib_div::intExplode(',',substr($this->piVars['sections'],4)));
  816. $out.= 'AND ISEC.rl2 IN ('.$list.')';
  817. $match = TRUE;
  818. } elseif (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['addRootLineFields'])) {
  819. // Traversing user configured fields to see if any of those are used to limit search to a section:
  820. foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['addRootLineFields'] as $fieldName => $rootLineLevel) {
  821. if (substr($this->piVars['sections'],0,strlen($fieldName)+1)==$fieldName.'_') {
  822. $list = implode(',',t3lib_div::intExplode(',',substr($this->piVars['sections'],strlen($fieldName)+1)));
  823. $out.= 'AND ISEC.'.$fieldName.' IN ('.$list.')';
  824. $match = TRUE;
  825. break;
  826. }
  827. }
  828. }
  829. // If no match above, test the static types:
  830. if (!$match) {
  831. switch((string)$this->piVars['sections']) {
  832. case '-1': // '-1' => 'Only this page',
  833. $out.= ' AND ISEC.page_id='.$GLOBALS['TSFE']->id;
  834. break;
  835. case '-2': // '-2' => 'Top + level 1',
  836. $out.= ' AND ISEC.rl2=0';
  837. break;
  838. case '-3': // '-3' => 'Level 2 and out',
  839. $out.= ' AND ISEC.rl2>0';
  840. break;
  841. }
  842. }
  843. return $out;
  844. }
  845. /**
  846. * Returns AND statement for selection of media type
  847. *
  848. * @return string AND statement for selection of media type
  849. */
  850. function mediaTypeWhere() {
  851. switch((string)$this->piVars['media']) {
  852. case '0': // '0' => 'Kun TYPO3 sider',
  853. $out = 'AND IP.item_type='.$GLOBALS['TYPO3_DB']->fullQuoteStr('0', 'index_phash');;
  854. break;
  855. case '-2': // All external documents
  856. $out = 'AND IP.item_type!='.$GLOBALS['TYPO3_DB']->fullQuoteStr('0', 'index_phash');;
  857. break;
  858. case '-1': // All content
  859. $out='';
  860. break;
  861. default:
  862. $out = 'AND IP.item_type='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->piVars['media'], 'index_phash');
  863. break;
  864. }
  865. return $out;
  866. }
  867. /**
  868. * Returns AND statement for selection of langauge
  869. *
  870. * @return string AND statement for selection of langauge
  871. */
  872. function languageWhere() {
  873. if ($this->piVars['lang']>=0) { // -1 is the same as ALL language.
  874. return 'AND IP.sys_language_uid='.intval($this->piVars['lang']);
  875. }
  876. }
  877. /**
  878. * Where-clause for free index-uid value.
  879. *
  880. * @param integer Free Index UID value to limit search to.
  881. * @return string WHERE SQL clause part.
  882. */
  883. function freeIndexUidWhere($freeIndexUid) {
  884. if ($freeIndexUid>=0) {
  885. // First, look if the freeIndexUid is a meta configuration:
  886. list($indexCfgRec) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('indexcfgs','index_config','type=5 AND uid='.intval($freeIndexUid).$this->cObj->enableFields('index_config'));
  887. if (is_array($indexCfgRec)) {
  888. $refs = t3lib_div::trimExplode(',',$indexCfgRec['indexcfgs']);
  889. $list = array(-99); // Default value to protect against empty array.
  890. foreach ($refs as $ref) {
  891. list($table,$uid) = t3lib_div::revExplode('_',$ref,2);
  892. switch ($table) {
  893. case 'index_config':
  894. list($idxRec) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid','index_config','uid='.intval($uid).$this->cObj->enableFields('index_config'));
  895. if ($idxRec) $list[] = $uid;
  896. break;
  897. case 'pages':
  898. $indexCfgRecordsFromPid = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid','index_config','pid='.intval($uid).$this->cObj->enableFields('index_config'));
  899. foreach ($indexCfgRecordsFromPid as $idxRec) {
  900. $list[] = $idxRec['uid'];
  901. }
  902. break;
  903. }
  904. }
  905. $list = array_unique($list);
  906. } else {
  907. $list = array(intval($freeIndexUid));
  908. }
  909. return ' AND IP.freeIndexUid IN ('.implode(',',$list).')';
  910. }
  911. }
  912. /**
  913. * Execute final query, based on phash integer list. The main point is sorting the result in the right order.
  914. *
  915. * @param string List of phash integers which match the search.
  916. * @param integer Pointer to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
  917. * @return pointer Query result pointer
  918. */
  919. function execFinalQuery($list,$freeIndexUid=-1) {
  920. // Setting up methods of filtering results based on page types, access, etc.
  921. $page_join = '';
  922. $page_where = '';
  923. // Indexing configuration clause:
  924. $freeIndexUidClause = $this->freeIndexUidWhere($freeIndexUid);
  925. // Calling hook for alternative creation of page ID list
  926. if ($hookObj = $this->hookRequest('execFinalQuery_idList')) {
  927. $page_where = $hookObj->execFinalQuery_idList($list);
  928. } elseif ($this->join_pages) { // Alternative to getting all page ids by ->getTreeList() where "excludeSubpages" is NOT respected.
  929. $page_join = ',
  930. pages';
  931. $page_where = 'pages.uid = ISEC.page_id
  932. '.$this->cObj->enableFields('pages').'
  933. AND pages.no_search=0
  934. AND pages.doktype<200
  935. ';
  936. } elseif ($this->wholeSiteIdList>=0) { // Collecting all pages IDs in which to search; filtering out ALL pages that are not accessible due to enableFields. Does NOT look for "no_search" field!
  937. $siteIdNumbers = t3lib_div::intExplode(',',$this->wholeSiteIdList);
  938. $id_list=array();
  939. foreach ($siteIdNumbers as $rootId) {
  940. $id_list[] = $this->cObj->getTreeList($rootId,9999,0,0,'','').$rootId;
  941. }
  942. $page_where = 'ISEC.page_id IN ('.implode(',',$id_list).')';
  943. } else { // Disable everything... (select all)
  944. $page_where = ' 1=1 ';
  945. }
  946. // If any of the ranking sortings are selected, we must make a join with the word/rel-table again, because we need to calculate ranking based on all search-words found.
  947. if (substr($this->piVars['order'],0,5)=='rank_') {
  948. /*
  949. OK there were some fancy calculations promoted by Graeme Merrall:
  950. "However, regarding relevance you probably want to look at something like
  951. Salton's formula which is a good easy way to measure relevance.
  952. Oracle Intermedia uses this and it's pretty simple:
  953. Score can be between 0 and 100, but the top-scoring document in the query
  954. will not necessarily have a score of 100 -- scoring is relative, not
  955. absolute. This means that scores are not comparable across indexes, or even
  956. across different queries on the same index. Score for each document is
  957. computed using the standard Salton formula:
  958. 3f(1+log(N/n))
  959. Where f is the frequency of the search term in the document, N is the total
  960. number of rows in the table, and n is the number of rows which contain the
  961. search term. This is converted into an integer in the range 0 - 100.
  962. There's a good doc on it at
  963. http://ls6-www.informatik.uni-dortmund.de/bib/fulltext/ir/Pfeifer:97/
  964. although it may be a little complex for what you require so just pick the
  965. relevant parts out.
  966. "
  967. However I chose not to go with this for several reasons.
  968. I do not claim that my ways of calculating importance here is the best.
  969. ANY (better) suggestion for ranking calculation is accepted! (as long as they are shipped with tested code in exchange for this.)
  970. */
  971. switch($this->piVars['order']) {
  972. case 'rank_flag': // This gives priority to word-position (max-value) so that words in title, keywords, description counts more than in content.
  973. // The ordering is refined with the frequency sum as well.
  974. $grsel = 'MAX(IR.flags) AS order_val1, SUM(IR.freq) AS order_val2';
  975. $orderBy = 'order_val1'.$this->isDescending().',order_val2'.$this->isDescending();
  976. break;
  977. case 'rank_first': // Results in average position of search words on page. Must be inversely sorted (low numbers are closer to top)
  978. $grsel = 'AVG(IR.first) AS order_val';
  979. $orderBy = 'order_val'.$this->isDescending(1);
  980. break;
  981. case 'rank_count': // Number of words found
  982. $grsel = 'SUM(IR.count) AS order_val';
  983. $orderBy = 'order_val'.$this->isDescending();
  984. break;
  985. default: // Frequency sum. I'm not sure if this is the best way to do it (make a sum...). Or should it be the average?
  986. $grsel = 'SUM(IR.freq) AS order_val';
  987. $orderBy = 'order_val'.$this->isDescending();
  988. break;
  989. }
  990. // So, words are imploded into an OR statement (no "sentence search" should be done here - may deselect results)
  991. $wordSel='('.implode(' OR ',$this->wSelClauses).') AND ';
  992. return $GLOBALS['TYPO3_DB']->exec_SELECTquery(
  993. 'ISEC.*, IP.*, '
  994. .$grsel,
  995. 'index_words IW,
  996. index_rel IR,
  997. index_section ISEC,
  998. index_phash IP'.
  999. $page_join,
  1000. $wordSel.'
  1001. IP.phash IN ('.$list.') '.
  1002. $this->mediaTypeWhere().' '.
  1003. $this->languageWhere().
  1004. $freeIndexUidClause.'
  1005. AND IW.wid=IR.wid
  1006. AND ISEC.phash = IR.phash
  1007. AND IP.phash = IR.phash
  1008. AND '.$page_where,
  1009. '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',
  1010. $orderBy
  1011. );
  1012. } else { // Otherwise, if sorting are done with the pages table or other fields, there is no need for joining with the rel/word tables:
  1013. $orderBy = '';
  1014. switch((string)$this->piVars['order']) {
  1015. case 'title':
  1016. $orderBy = 'IP.item_title'.$this->isDescending();
  1017. break;
  1018. case 'crdate':
  1019. $orderBy = 'IP.item_crdate'.$this->isDescending();
  1020. break;
  1021. case 'mtime':
  1022. $orderBy = 'IP.item_mtime'.$this->isDescending();
  1023. break;
  1024. }
  1025. return $GLOBALS['TYPO3_DB']->exec_SELECTquery(
  1026. 'ISEC.*, IP.*',
  1027. 'index_phash IP,index_section ISEC'.$page_join,
  1028. 'IP.phash IN ('.$list.') '.
  1029. $this->mediaTypeWhere().' '.
  1030. $this->languageWhere().
  1031. $freeIndexUidClause.'
  1032. AND IP.phash = ISEC.phash
  1033. AND '.$page_where,
  1034. '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',
  1035. $orderBy
  1036. );
  1037. }
  1038. }
  1039. /**
  1040. * Checking if the resume can be shown for the search result (depending on whether the rights are OK)
  1041. * ? Should it also check for gr_list "0,-1"?
  1042. *
  1043. * @param array Result row array.
  1044. * @return boolean Returns true if resume can safely be shown
  1045. */
  1046. function checkResume($row) {
  1047. // If the record is indexed by an indexing configuration, just show it.
  1048. // At least this is needed for external URLs and files.
  1049. // For records we might need to extend this - for instance block display if record is access restricted.
  1050. if ($row['freeIndexUid']) {
  1051. return TRUE;
  1052. }
  1053. // Evaluate regularly indexed pages based on item_type:
  1054. if ($row['item_type']) { // External media:
  1055. // For external media we will check the access of the parent page on which the media was linked from.
  1056. // "phash_t3" is the phash of the parent TYPO3 page row which initiated the indexing of the documents in this section.
  1057. // So, selecting for the grlist records belonging to the parent phash-row where the current users gr_list exists will help us to know.
  1058. // If this is NOT found, there is still a theoretical possibility that another user accessible page would display a link, so maybe the resume of such a document here may be unjustified hidden. But better safe than sorry.
  1059. $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('phash', 'index_grlist', 'phash='.intval($row['phash_t3']).' AND gr_list='.$GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['TSFE']->gr_list, 'index_grlist'));
  1060. if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
  1061. #debug("Look up for external media '".$row['data_filename']."': phash:".$row['phash_t3'].' YES - ('.$GLOBALS['TSFE']->gr_list.")!");
  1062. return TRUE;
  1063. } else {
  1064. #debug("Look up for external media '".$row['data_filename']."': phash:".$row['phash_t3'].' NO - ('.$GLOBALS['TSFE']->gr_list.")!");
  1065. return FALSE;
  1066. }
  1067. } else { // Ordinary TYPO3 pages:
  1068. if (strcmp($row['gr_list'],$GLOBALS['TSFE']->gr_list)) {
  1069. // Selecting for the grlist records belonging to the phash-row where the current users gr_list exists. If it is found it is proof that this user has direct access to the phash-rows content although he did not himself initiate the indexing...
  1070. $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('phash', 'index_grlist', 'phash='.intval($row['phash']).' AND gr_l…

Large files files are truncated, but you can click here to view the full file