656 |
656 |
657 return $texts; |
657 return $texts; |
658 } |
658 } |
659 |
659 |
660 /** |
660 /** |
661 * Fetches a MySQL search query to use for Searcher::searchMySQL() |
661 * Generates an SQL query to grab all of the text |
662 */ |
662 */ |
663 |
663 |
664 function fetch_page_search_resource() |
664 function fetch_page_search_resource() |
665 { |
665 { |
666 global $db, $session, $paths, $template, $plugins; // Common objects |
666 global $db, $session, $paths, $template, $plugins; // Common objects |
667 // sha1('') returns "da39a3ee5e6b4b0d3255bfef95601890afd80709" |
667 // sha1('') returns "da39a3ee5e6b4b0d3255bfef95601890afd80709" |
668 $texts = 'SELECT t.page_text,CONCAT(\'ns=\',t.namespace,\';pid=\',t.page_id) FROM '.table_prefix.'page_text AS t |
668 $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 |
669 LEFT JOIN '.table_prefix.'pages AS p |
669 LEFT JOIN '.table_prefix.'pages AS p |
670 ON ( t.page_id=p.urlname AND t.namespace=p.namespace ) |
670 ON ( t.page_id=p.urlname AND t.namespace=p.namespace ) |
671 WHERE p.namespace=t.namespace |
671 WHERE p.namespace=t.namespace |
672 AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' ) |
672 AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' ) |
673 AND p.visible=1;'; // Only indexes "visible" pages |
673 AND p.visible=1;'; // Only indexes "visible" pages |
683 global $db, $session, $paths, $template, $plugins; // Common objects |
683 global $db, $session, $paths, $template, $plugins; // Common objects |
684 $search = new Searcher(); |
684 $search = new Searcher(); |
685 $texts = Array(); |
685 $texts = Array(); |
686 $textq = $db->sql_unbuffered_query($this->fetch_page_search_resource()); |
686 $textq = $db->sql_unbuffered_query($this->fetch_page_search_resource()); |
687 if(!$textq) $db->_die(''); |
687 if(!$textq) $db->_die(''); |
688 while($row = $db->fetchrow_num()) |
688 while($row = $db->fetchrow()) |
689 { |
689 { |
690 $texts[(string)$row[1]] = $row[0]; |
690 if ( isset($this->nslist[$row['namespace']]) ) |
|
691 { |
|
692 $idstring = $this->nslist[$row['namespace']] . sanitize_page_id($row['page_id']); |
|
693 if ( isset($this->pages[$idstring]) ) |
|
694 { |
|
695 $page = $this->pages[$idstring]; |
|
696 } |
|
697 else |
|
698 { |
|
699 $page = array('name' => dirtify_page_id($row['page_id'])); |
|
700 } |
|
701 } |
|
702 else |
|
703 { |
|
704 $page = array('name' => dirtify_page_id($row['page_id'])); |
|
705 } |
|
706 $texts[(string)$row['page_idstring']] = $row['page_text'] . ' ' . $page['name']; |
691 } |
707 } |
692 $search->buildIndex($texts); |
708 $search->buildIndex($texts); |
693 // echo '<pre>'.print_r($search->index, true).'</pre>'; |
709 // echo '<pre>'.print_r($search->index, true).'</pre>'; |
694 // return; |
710 // return; |
695 $q = $db->sql_query('DELETE FROM '.table_prefix.'search_index'); |
711 $q = $db->sql_query('DELETE FROM '.table_prefix.'search_index'); |
723 if(!$db->sql_query('SELECT page_text FROM '.table_prefix.'page_text |
739 if(!$db->sql_query('SELECT page_text FROM '.table_prefix.'page_text |
724 WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';')) |
740 WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';')) |
725 { |
741 { |
726 return $db->get_error(); |
742 return $db->get_error(); |
727 } |
743 } |
|
744 if ( $db->numrows() < 1 ) |
|
745 return 'E: No rows'; |
|
746 $idstring = $this->nslist[$namespace] . sanitize_page_id($page_id); |
|
747 if ( !isset($this->pages[$idstring]) ) |
|
748 { |
|
749 return 'E: Can\'t find page metadata'; |
|
750 } |
728 $row = $db->fetchrow(); |
751 $row = $db->fetchrow(); |
729 $db->free_result(); |
752 $db->free_result(); |
730 $search = new Searcher(); |
753 $search = new Searcher(); |
731 $search->buildIndex(Array("ns={$namespace};pid={$page_id}"=>$row['page_text'])); |
754 $search->buildIndex(Array("ns={$namespace};pid={$page_id}"=>$row['page_text'] . ' ' . $this->pages[$idstring]['name'])); |
732 $new_index = $search->index; |
755 $new_index = $search->index; |
733 |
756 |
734 $keys = array_keys($search->index); |
757 $keys = array_keys($search->index); |
735 foreach($keys as $i => $k) |
758 foreach($keys as $i => $k) |
736 { |
759 { |
737 $c =& $keys[$i]; |
760 $c =& $keys[$i]; |
738 $c = hexencode($c, '', ''); |
761 $c = hexencode($c, '', ''); |
739 } |
762 } |
740 $keys = "word=0x" . implode ( " OR word=0x", $keys ) . ""; |
763 $keys = "word=0x" . implode ( " OR word=0x", $keys ) . ""; |
741 |
|
742 // Zap the cache |
|
743 $cache = array_keys($search->index); |
|
744 if ( count($cache) < 1 ) |
|
745 { |
|
746 return false; |
|
747 } |
|
748 foreach ( $cache as $key => $_unused ) |
|
749 { |
|
750 $cache[$key] = $db->escape( $cache[$key] ); |
|
751 } |
|
752 $cache = "query LIKE '%" . implode ( "%' OR query LIKE '%", $cache ) . "%'"; |
|
753 $sql = 'DELETE FROM '.table_prefix.'search_cache WHERE '.$cache; |
|
754 $db->sql_query($sql); |
|
755 |
764 |
756 $query = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index WHERE '.$keys.';'); |
765 $query = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index WHERE '.$keys.';'); |
757 |
766 |
758 while($row = $db->fetchrow()) |
767 while($row = $db->fetchrow()) |
759 { |
768 { |