Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

tslib_search Class Reference

List of all members.

Public Member Functions

 register_tables_and_columns ($requestedCols, $allowedCols)
 Creates the $this->tables-array.
 explodeCols ($in)
 Function that can convert the syntax for entering which tables/fields the search should be conducted in.
 register_and_explode_search_string ($sword)
 Takes a search-string (WITHOUT SLASHES or else it'll be a little sppooky , NOW REMEMBER to unslash!!) Sets up $this->sword_array op with operators.
 split ($origSword, $specchars='+-', $delchars='+.,-')
 Used to split a search-word line up into elements to search for.
 quotemeta ($str)
 Local version of quotemeta.
 build_search_query ($endClause)
 This creates the search-query.
 build_search_query_for_searchwords ()
 Creates the part of the SQL-sentence, that searches for the search-words ($this->sword_array).
 get_operator ($operator)
 This returns an SQL search-operator (eg.
 count_query ()
 Counts the results and sets the result in $this->res_count.
 execute_query ()
 Executes the search, sets result pointer in $this->result.
 get_searchwords ()
 Returns URL-parameters with the current search words.
 get_searchwordsArray ()
 Returns an array with the search words in.

Public Attributes

 $tables = Array ()
 $group_by = 'PRIMARY_KEY'
 $default_operator = 'AND'
 $operator_translate_table_caseinsensitive = '1'
 $operator_translate_table
 $sword_array
 $queryParts
 $other_where_clauses
 $fTable
 $res_offset = 0
 $res_shows = 20
 $res_count
 $pageIdList = ''
 $listOfSearchFields = ''
return $value

Member Function Documentation

tslib_search::build_search_query endClause  ) 
 

This creates the search-query.

In TypoScript this is used for searching only records not hidden, start/endtimed and fe_grouped! (enable-fields, see tt_content) Sets $this->queryParts

Parameters:
string $endClause is some extra conditions that the search must match.
Returns:
boolean Returns true no matter what - sweet isn't it! private
See also:
tslib_cObj::SEARCHRESULT()

Definition at line 288 of file class.tslib_search.php.

References $key.

00288                                            {
00289 
00290       if (is_array($this->tables))  {
00291          $tables = $this->tables;
00292          $primary_table = '';
00293 
00294             // Primary key table is found.
00295          foreach($tables as $key => $val) {
00296             if ($tables[$key]['primary_key'])   {$primary_table = $key;}
00297          }
00298 
00299          if ($primary_table) {
00300 
00301                // Initialize query parts:
00302             $this->queryParts = array(
00303                'SELECT' => '',
00304                'FROM' => '',
00305                'WHERE' => '',
00306                'GROUPBY' => '',
00307                'ORDERBY' => '',
00308                'LIMIT' => '',
00309             );
00310 
00311                // Find tables / field names to select:
00312             $fieldArray = array();
00313             $tableArray = array();
00314             foreach($tables as $key => $val) {
00315                $tableArray[] = $key;
00316                $resultfields = $tables[$key]['resultfields'];
00317                if (is_array($resultfields))  {
00318                   foreach($resultfields as $key2 => $val2)  {
00319                      $fieldArray[] = $key.'.'.$val2;
00320                   }
00321                }
00322             }
00323             $this->queryParts['SELECT'] = implode(',',$fieldArray);
00324             $this->queryParts['FROM'] = implode(',',$tableArray);
00325 
00326                // Set join WHERE parts:
00327             $whereArray = array();
00328 
00329             $primary_table_and_key = $primary_table.'.'.$tables[$primary_table]['primary_key'];
00330             $primKeys = Array();
00331             foreach($tables as $key => $val) {
00332                $fkey = $tables[$key]['fkey'];
00333                if ($fkey)  {
00334                    $primKeys[] = $key.'.'.$fkey.'='.$primary_table_and_key;
00335                }
00336             }
00337             if (count($primKeys))   {
00338                $whereArray[] = '('.implode(' OR ',$primKeys).')';
00339             }
00340 
00341                // Additional where clause:
00342             if (trim($endClause))   {
00343                $whereArray[] = trim($endClause);
00344             }
00345 
00346                // Add search word where clause:
00347             $query_part = $this->build_search_query_for_searchwords();
00348             if (!$query_part) {
00349                $query_part = '(0!=0)';
00350             }
00351             $whereArray[] = '('.$query_part.')';
00352 
00353                // Implode where clauses:
00354             $this->queryParts['WHERE'] = implode(' AND ',$whereArray);
00355 
00356                // Group by settings:
00357             if ($this->group_by) {
00358                if ($this->group_by == 'PRIMARY_KEY')  {
00359                   $this->queryParts['GROUPBY'] = $primary_table_and_key;
00360                } else {
00361                   $this->queryParts['GROUPBY'] = $this->group_by;
00362                }
00363             }
00364          }
00365       }
00366    }

tslib_search::build_search_query_for_searchwords  ) 
 

Creates the part of the SQL-sentence, that searches for the search-words ($this->sword_array).

Returns:
string Part of where class limiting result to the those having the search word. private

Definition at line 374 of file class.tslib_search.php.

00374                                                    {
00375 
00376       if (is_array($this->sword_array))   {
00377          $main_query_part = array();
00378 
00379          foreach($this->sword_array as $key => $val)  {
00380             $s_sword = $this->sword_array[$key]['sword'];
00381 
00382                // Get subQueryPart
00383             $sub_query_part = array();
00384 
00385             $this->listOfSearchFields='';
00386             foreach($this->tables as $key3 => $val3)  {
00387                $searchfields = $this->tables[$key3]['searchfields'];
00388                if (is_array($searchfields))  {
00389                   foreach($searchfields as $key2 => $val2)  {
00390                      $this->listOfSearchFields.= $key3.'.'.$val2.',';
00391                      $sub_query_part[] = $key3.'.'.$val2.' LIKE "%'.$GLOBALS['TYPO3_DB']->quoteStr($s_sword, $key3).'%"';
00392                   }
00393                }
00394             }
00395 
00396             if (count($sub_query_part))   {
00397                $main_query_part[] = $this->sword_array[$key]['oper'];
00398                $main_query_part[] = '('.implode(' OR ',$sub_query_part).')';
00399             }
00400          }
00401 
00402          if (count($main_query_part))  {
00403             unset($main_query_part[0]);   // Remove first part anyways.
00404             return implode(' ',$main_query_part);
00405          }
00406       }
00407    }

tslib_search::count_query  ) 
 

Counts the results and sets the result in $this->res_count.

Returns:
boolean True, if $this->query was found

Definition at line 439 of file class.tslib_search.php.

00439                           {
00440       if (is_array($this->queryParts)) {
00441          $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($this->queryParts['SELECT'], $this->queryParts['FROM'], $this->queryParts['WHERE'], $this->queryParts['GROUPBY']);
00442           $this->res_count = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
00443          return TRUE;
00444       }
00445    }

tslib_search::execute_query  ) 
 

Executes the search, sets result pointer in $this->result.

Returns:
boolean True, if $this->query was set and query performed

Definition at line 452 of file class.tslib_search.php.

00452                             {
00453       if (is_array($this->queryParts)) {
00454            $this->result = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($this->queryParts);
00455          return TRUE;
00456       }
00457    }

tslib_search::explodeCols in  ) 
 

Function that can convert the syntax for entering which tables/fields the search should be conducted in.

Parameters:
string This is the code-line defining the tables/fields to search. Syntax: '[table1].[field1]-[field2]-[field3] : [table2].[field1]-[field2]'
Returns:
array An array where the values is "[table].[field]" strings to search
See also:
register_tables_and_columns()

Definition at line 171 of file class.tslib_search.php.

References $out, and $val.

Referenced by register_tables_and_columns().

00171                               {
00172       $theArray = explode(':',$in);
00173       $out = Array();
00174       while(list(,$val)=each($theArray))  {
00175          $val=trim($val);
00176          $parts = explode('.',$val);
00177          if ($parts[0] && $parts[1])   {
00178             $subparts = explode('-',$parts[1]);
00179             while(list(,$piece)=each($subparts))   {
00180                $piece=trim($piece);
00181                if ($piece)    $out[]=$parts[0].'.'.$piece;
00182             }
00183          }
00184       }
00185       return $out;
00186    }

tslib_search::get_operator operator  ) 
 

This returns an SQL search-operator (eg.

AND, OR, NOT) translated from the current localized set of operators (eg. in danish OG, ELLER, IKKE).

Parameters:
string The possible operator to find in the internal operator array.
Returns:
string If found, the SQL operator for the localized input operator. private

Definition at line 416 of file class.tslib_search.php.

Referenced by register_and_explode_search_string().

00416                                     {
00417       $operator = trim($operator);
00418       $op_array = $this->operator_translate_table;
00419       reset ($op_array);
00420       if ($this->operator_translate_table_caseinsensitive)  {
00421          $operator = strtoupper($operator);
00422       }
00423       while (list($key,$val) = each($op_array)) {
00424          $item = $op_array[$key][0];
00425          if ($this->operator_translate_table_caseinsensitive)  {
00426             $item = strtoupper($item);
00427          }
00428          if ($operator==$item)   {
00429             return $op_array[$key][1];
00430          }
00431       }
00432    }

tslib_search::get_searchwords  ) 
 

Returns URL-parameters with the current search words.

Used when linking to result pages so that search words can be highlighted.

Returns:
string URL-parameters with the searchwords

Definition at line 465 of file class.tslib_search.php.

00465                               {
00466       $SWORD_PARAMS='';
00467       if (is_array($this->sword_array))   {
00468          foreach($this->sword_array as $key => $val)  {
00469             $SWORD_PARAMS.='&sword_list[]='.rawurlencode($val['sword']);
00470          }
00471       }
00472       return $SWORD_PARAMS;
00473    }

tslib_search::get_searchwordsArray  ) 
 

Returns an array with the search words in.

Returns:
array IF the internal sword_array contained search words it will return these, otherwise "void"

Definition at line 480 of file class.tslib_search.php.

References $val.

00480                                     {
00481       if (is_array($this->sword_array))   {
00482          foreach($this->sword_array as $key => $val)  {
00483             $swords[]=$val['sword'];
00484          }
00485       }
00486       return $swords;
00487    }

tslib_search::quotemeta str  ) 
 

Local version of quotemeta.

This is the same as the PHP function but the vertical line, |, and minus, -, is also escaped with a slash.

Parameters:
string String to pass through quotemeta()
Returns:
string Return value

Definition at line 272 of file class.tslib_search.php.

00272                               {
00273       $str = str_replace('|','\|',quotemeta($str));
00274       #$str = str_replace('-','\-',$str);    // Breaks "-" which should NOT have a slash before it inside of [ ] in a regex.
00275       return $str;
00276    }

tslib_search::register_and_explode_search_string sword  ) 
 

Takes a search-string (WITHOUT SLASHES or else it'll be a little sppooky , NOW REMEMBER to unslash!!) Sets up $this->sword_array op with operators.

This function uses $this->operator_translate_table as well as $this->default_operator

Parameters:
string The input search-word string.
Returns:
void

Definition at line 196 of file class.tslib_search.php.

References get_operator(), and split().

00196                                                          {
00197       $sword = trim($sword);
00198       if ($sword) {
00199          $components = $this->split($sword);
00200          $s_sword = '';  // the searchword is stored here during the loop
00201          if (is_array($components)) {
00202             $i=0;
00203             $lastoper = '';
00204             reset($components);
00205             while (list($key,$val) = each ($components)) {
00206                $operator=$this->get_operator($val);
00207                if ($operator) {
00208                   $lastoper = $operator;
00209                } elseif (strlen($val)>1) {      // A searchword MUST be at least two characters long!
00210                   $this->sword_array[$i]['sword'] = $val;
00211                   $this->sword_array[$i]['oper'] = ($lastoper) ? $lastoper : $this->default_operator;
00212                   $lastoper = '';
00213                   $i++;
00214                }
00215             }
00216          }
00217       }
00218    }

tslib_search::register_tables_and_columns requestedCols,
allowedCols
 

Creates the $this->tables-array.

The 'pages'-table is ALWAYS included as the search is page-based. Apart from this there may be one and only one table, joined with the pages-table. This table is the first table mentioned in the requested-list. If any more tables are set here, they are ignored.

Parameters:
string is a list (-) of columns that we want to search. This could be input from the search-form (see TypoScript documentation)
string $allowedCols: is the list of columns, that MAY be searched. All allowed cols are set as result-fields. All requested cols MUST be in the allowed-fields list.
Returns:
void

Definition at line 130 of file class.tslib_search.php.

References explodeCols().

00130                                                                      {
00131       $rCols=$this->explodeCols($requestedCols);
00132       $aCols=$this->explodeCols($allowedCols);
00133 
00134       foreach ($rCols as $k => $v)  {
00135          $rCols[$k]=trim($v);
00136          if (in_array($rCols[$k], $aCols))   {
00137             $parts = explode('.',$rCols[$k]);
00138             $this->tables[$parts[0]]['searchfields'][] = $parts[1];
00139          }
00140       }
00141       $this->tables['pages']['primary_key'] = 'uid';
00142       $this->tables['pages']['resultfields'][] = 'uid';
00143       unset($this->tables['pages']['fkey']);
00144 
00145       foreach ($aCols as $k => $v)  {
00146          $aCols[$k]=trim($v);
00147          $parts = explode('.',$aCols[$k]);
00148          $this->tables[$parts[0]]['resultfields'][] = $parts[1].' AS '.str_replace('.','_',$aCols[$k]);
00149          $this->tables[$parts[0]]['fkey']='pid';
00150       }
00151 
00152       $this->fTable='';
00153       foreach ($this->tables as $t => $v) {
00154          if ($t!='pages')  {
00155             if (!$this->fTable)  {
00156                $this->fTable = $t;
00157             } else {
00158                unset($this->tables[$t]);
00159             }
00160          }
00161       }
00162    }

tslib_search::split origSword,
specchars = '+-',
delchars = '+.,
-' 
 

Used to split a search-word line up into elements to search for.

This function will detect boolean words like AND and OR, + and -, and even find sentences encapsulated in "" This function could be re-written to be more clean and effective - yet it's not that important.

Parameters:
string The raw sword string from outside
string Special chars which are used as operators (+- is default)
string Special chars which are deleted if the append the searchword (+-., is default)
Returns:
mixed Returns an ARRAY if there were search words, othervise the return value may be unset.

Definition at line 229 of file class.tslib_search.php.

References $value.

Referenced by register_and_explode_search_string().

00229                                                                   {
00230       $sword = $origSword;
00231       $specs = '['.$this->quotemeta($specchars).']';
00232       $delchars = '['.$this->quotemeta($delchars).']';
00233 
00234          // As long as $sword is true (that means $sword MUST be reduced little by little until its empty inside the loop!)
00235       while ($sword) {
00236          if (ereg('^"',$sword))  {     // There was a double-quote and we will then look for the ending quote.
00237             $sword = ereg_replace('^"','',$sword);    // Removes first double-quote
00238             ereg('^[^"]*',$sword,$reg);  // Removes everything till next double-quote
00239             $value[] = $reg[0];  // reg[0] is the value, should not be trimmed
00240             $sword = ereg_replace('^'.$this->quotemeta($reg[0]),'',$sword);
00241             $sword = trim(ereg_replace('^"','',$sword));    // Removes last double-quote
00242          } elseif (ereg('^'.$specs,$sword,$reg)) {
00243             $value[] = $reg[0];
00244             $sword = trim(ereg_replace('^'.$specs,'',$sword));    // Removes = sign
00245          } elseif (ereg('[\+\-]',$sword)) {  // Check if $sword contains + or -
00246                // + and - shall only be interpreted as $specchars when there's whitespace before it
00247                // otherwise it's included in the searchword (e.g. "know-how")
00248             $a_sword = explode(' ',$sword);  // explode $sword to single words
00249             $word = array_shift($a_sword);   // get first word
00250             $word = ereg_replace($delchars.'$','',$word);      // Delete $delchars at end of string
00251             $value[] = $word; // add searchword to values
00252             $sword = implode(' ',$a_sword);  // re-build $sword
00253          } else {
00254                // There are no double-quotes around the value. Looking for next (space) or special char.
00255             ereg('^[^ '.$this->quotemeta($specchars).']*',$sword,$reg);
00256             $word = ereg_replace($delchars.'$','',trim($reg[0]));    // Delete $delchars at end of string
00257             $value[] = $word;
00258             $sword = trim(ereg_replace('^'.$this->quotemeta($reg[0]),'',$sword));
00259          }
00260       }


Member Data Documentation

tslib_search::$default_operator = 'AND'
 

Definition at line 92 of file class.tslib_search.php.

tslib_search::$fTable
 

Definition at line 112 of file class.tslib_search.php.

tslib_search::$group_by = 'PRIMARY_KEY'
 

Definition at line 91 of file class.tslib_search.php.

tslib_search::$listOfSearchFields = ''
 

Definition at line 120 of file class.tslib_search.php.

tslib_search::$operator_translate_table
 

Initial value:

 Array (             // case-sensitiv. Defineres the words, which will be operators between words
      Array ('+' , 'AND'),
      Array ('-' , 'AND NOT'),
         // english
      Array ('AND' , 'AND'),
      Array ('OR' , 'OR'),
      Array ('NOT' , 'AND NOT'),
         // danish
      Array ('OG' , 'AND'),
      Array ('ELLER' , 'OR'),
      Array ('UDEN' , 'AND NOT')
   )

Definition at line 94 of file class.tslib_search.php.

tslib_search::$operator_translate_table_caseinsensitive = '1'
 

Definition at line 93 of file class.tslib_search.php.

tslib_search::$other_where_clauses
 

Definition at line 111 of file class.tslib_search.php.

tslib_search::$pageIdList = ''
 

Definition at line 118 of file class.tslib_search.php.

tslib_search::$queryParts
 

Definition at line 109 of file class.tslib_search.php.

tslib_search::$res_count
 

Definition at line 116 of file class.tslib_search.php.

tslib_search::$res_offset = 0
 

Definition at line 114 of file class.tslib_search.php.

tslib_search::$res_shows = 20
 

Definition at line 115 of file class.tslib_search.php.

tslib_search::$sword_array
 

Definition at line 108 of file class.tslib_search.php.

tslib_search::$tables = Array ()
 

Definition at line 89 of file class.tslib_search.php.

return tslib_search::$value
 

Definition at line 262 of file class.tslib_search.php.

Referenced by split().


The documentation for this class was generated from the following file:
Generated on Sun Oct 3 01:07:46 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913