includes/paths.php
changeset 279 8acd77a6c19d
parent 266 917dcc6c4ceb
parent 272 e0ec986c0af3
child 304 e2cb5f1432c8
equal deleted inserted replaced
278:53ba55d33abb 279:8acd77a6c19d
   661     
   661     
   662     return $texts;
   662     return $texts;
   663   }
   663   }
   664   
   664   
   665   /**
   665   /**
   666    * Fetches a MySQL search query to use for Searcher::searchMySQL()
   666    * Generates an SQL query to grab all of the text
   667    */
   667    */
   668    
   668    
   669   function fetch_page_search_resource()
   669   function fetch_page_search_resource()
   670   {
   670   {
   671     global $db, $session, $paths, $template, $plugins; // Common objects
   671     global $db, $session, $paths, $template, $plugins; // Common objects
   672     // sha1('') returns "da39a3ee5e6b4b0d3255bfef95601890afd80709"
   672     // sha1('') returns "da39a3ee5e6b4b0d3255bfef95601890afd80709"
   673     $texts = 'SELECT t.page_text,CONCAT(\'ns=\',t.namespace,\';pid=\',t.page_id) FROM '.table_prefix.'page_text AS t
   673     $texts = 'SELECT t.page_text, CONCAT(\'ns=\',t.namespace,\';pid=\',t.page_id) AS page_idstring, t.page_id, t.namespace FROM '.table_prefix.'page_text AS t
   674                            LEFT JOIN '.table_prefix.'pages AS p
   674                            LEFT JOIN '.table_prefix.'pages AS p
   675                              ON ( t.page_id=p.urlname AND t.namespace=p.namespace )
   675                              ON ( t.page_id=p.urlname AND t.namespace=p.namespace )
   676                            WHERE p.namespace=t.namespace
   676                            WHERE p.namespace=t.namespace
   677                              AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' )
   677                              AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' )
   678                              AND p.visible=1;'; // Only indexes "visible" pages
   678                              AND p.visible=1;'; // Only indexes "visible" pages
   688     global $db, $session, $paths, $template, $plugins; // Common objects
   688     global $db, $session, $paths, $template, $plugins; // Common objects
   689     $search = new Searcher();
   689     $search = new Searcher();
   690     $texts = Array();
   690     $texts = Array();
   691     $textq = $db->sql_unbuffered_query($this->fetch_page_search_resource());
   691     $textq = $db->sql_unbuffered_query($this->fetch_page_search_resource());
   692     if(!$textq) $db->_die('');
   692     if(!$textq) $db->_die('');
   693     while($row = $db->fetchrow_num())
   693     while($row = $db->fetchrow())
   694     {
   694     {
   695       $texts[(string)$row[1]] = $row[0];
   695       if ( isset($this->nslist[$row['namespace']]) )
       
   696       {
       
   697         $idstring = $this->nslist[$row['namespace']] . sanitize_page_id($row['page_id']);
       
   698         if ( isset($this->pages[$idstring]) )
       
   699         {
       
   700           $page = $this->pages[$idstring];
       
   701         }
       
   702         else
       
   703         {
       
   704           $page = array('name' => dirtify_page_id($row['page_id']));
       
   705         }
       
   706       }
       
   707       else
       
   708       {
       
   709         $page = array('name' => dirtify_page_id($row['page_id']));
       
   710       }
       
   711       $texts[(string)$row['page_idstring']] = $row['page_text'] . ' ' . $page['name'];
   696     }
   712     }
   697     $search->buildIndex($texts);
   713     $search->buildIndex($texts);
   698     // echo '<pre>'.print_r($search->index, true).'</pre>';
   714     // echo '<pre>'.print_r($search->index, true).'</pre>';
   699     // return;
   715     // return;
   700     $q = $db->sql_query('DELETE FROM '.table_prefix.'search_index');
   716     $q = $db->sql_query('DELETE FROM '.table_prefix.'search_index');
   728     if(!$db->sql_query('SELECT page_text FROM '.table_prefix.'page_text
   744     if(!$db->sql_query('SELECT page_text FROM '.table_prefix.'page_text
   729       WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';'))
   745       WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';'))
   730     {
   746     {
   731       return $db->get_error();
   747       return $db->get_error();
   732     }
   748     }
       
   749     if ( $db->numrows() < 1 )
       
   750       return 'E: No rows';
       
   751     $idstring = $this->nslist[$namespace] . sanitize_page_id($page_id);
       
   752     if ( !isset($this->pages[$idstring]) )
       
   753     {
       
   754       return 'E: Can\'t find page metadata';
       
   755     }
   733     $row = $db->fetchrow();
   756     $row = $db->fetchrow();
   734     $db->free_result();
   757     $db->free_result();
   735     $search = new Searcher();
   758     $search = new Searcher();
   736     $search->buildIndex(Array("ns={$namespace};pid={$page_id}"=>$row['page_text']));
   759     $search->buildIndex(Array("ns={$namespace};pid={$page_id}"=>$row['page_text'] . ' ' . $this->pages[$idstring]['name']));
   737     $new_index = $search->index;
   760     $new_index = $search->index;
   738     
   761     
   739     $keys = array_keys($search->index);
   762     $keys = array_keys($search->index);
   740     foreach($keys as $i => $k)
   763     foreach($keys as $i => $k)
   741     {
   764     {
   742       $c =& $keys[$i];
   765       $c =& $keys[$i];
   743       $c = hexencode($c, '', '');
   766       $c = hexencode($c, '', '');
   744     }
   767     }
   745     $keys = "word=0x" . implode ( " OR word=0x", $keys ) . "";
   768     $keys = "word=0x" . implode ( " OR word=0x", $keys ) . "";
   746     
       
   747     // Zap the cache
       
   748     $cache = array_keys($search->index);
       
   749     if ( count($cache) < 1 )
       
   750     {
       
   751       return false;
       
   752     }
       
   753     foreach ( $cache as $key => $_unused )
       
   754     {
       
   755       $cache[$key] = $db->escape( $cache[$key] );
       
   756     }
       
   757     $cache = "query LIKE '%" . implode ( "%' OR query LIKE '%", $cache ) . "%'";
       
   758     $sql = 'DELETE FROM '.table_prefix.'search_cache WHERE '.$cache;
       
   759     $db->sql_query($sql);
       
   760     
   769     
   761     $query = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index WHERE '.$keys.';');
   770     $query = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index WHERE '.$keys.';');
   762     
   771     
   763     while($row = $db->fetchrow())
   772     while($row = $db->fetchrow())
   764     {
   773     {