Change some search index update code to do two regexps backwards.
[lhc/web/wiklou.git] / includes / SearchUpdate.php
1 <?php
2 # $Id$
3 # See deferred.doc
4
5 class SearchUpdate {
6
7 /* private */ var $mId = 0, $mNamespace, $mTitle, $mText;
8 /* private */ var $mTitleWords;
9
10 function SearchUpdate( $id, $title, $text = false )
11 {
12 $nt = Title::newFromText( $title );
13 if( $nt ) {
14 $this->mId = $id;
15 $this->mText = $text;
16
17 $this->mNamespace = $nt->getNamespace();
18 $this->mTitle = $nt->getText(); # Discard namespace
19
20 $this->mTitleWords = $this->mTextWords = array();
21 } else {
22 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
23 }
24 }
25
26 function doUpdate()
27 {
28 global $wgDBminWordLen, $wgLang, $wgDisableSearchUpdate;
29
30 if( $wgDisableSearchUpdate || !$this->mId ) {
31 return false;
32 }
33 $fname = "SearchUpdate::doUpdate";
34 wfProfileIn( $fname );
35
36 require_once( 'SearchEngine.php' );
37 $lc = SearchEngine::legalSearchChars() . "&#;";
38 $db =& wfGetDB( DB_MASTER );
39 $searchindex = $db->tableName( 'searchindex' );
40
41 if( $this->mText == false ) {
42 # Just update the title
43 $lowpri = $db->lowPriorityOption();
44 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
45 $db->strencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) .
46 "' WHERE si_page={$this->mId}";
47 $db->query( $sql, "SearchUpdate::doUpdate" );
48 wfProfileOut( $fname );
49 return;
50 }
51
52 # Language-specific strip/conversion
53 $text = $wgLang->stripForSearch( $this->mText );
54
55 wfProfileIn( "$fname-regexps" );
56 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
57 " ", strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
58 $text = preg_replace( "/(^|\\n)\\s*==\\s+([^\\n]+)\\s+==\\s/sD",
59 "\\2 \\2 \\2 ", $text ); # Emphasize headings
60
61 # Strip external URLs
62 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
63 $protos = "http|https|ftp|mailto|news|gopher";
64 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
65 $text = preg_replace( $pat, "\\1 \\3", $text );
66
67 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
68 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
69 $text = preg_replace( $p1, "\\1 ", $text );
70 $text = preg_replace( $p2, "\\1 \\3 ", $text );
71
72 # Internal image links
73 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
74 $text = preg_replace( $pat2, " \\1 \\3", $text );
75
76 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
77 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
78
79 # Strip all remaining non-search characters
80 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
81
82 # Handle 's, s'
83 #
84 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
85 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
86 #
87 # These tail-anchored regexps are insanely slow. The worst case comes
88 # when Japanese or Chinese text (ie, no word spacing) is written on
89 # a wiki configured for Western UTF-8 mode. The Unicode characters are
90 # expanded to hex codes and the "words" are very long paragraph-length
91 # monstrosities. On a large page the above regexps may take over 20
92 # seconds *each* on a 1GHz-level processor.
93 #
94 # Following are reversed versions which are consistently fast
95 # (about 3 milliseconds on 1GHz-level processor).
96 #
97 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
98 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
99
100 # Strip wiki '' and '''
101 $text = preg_replace( "/''[']*/", " ", $text );
102 wfProfileOut( "$fname-regexps" );
103
104 $sql = "REPLACE INTO $searchindex (si_page,si_title,si_text) VALUES ({$this->mId},'" .
105 $db->strencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) . "','" .
106 $db->strencode( $text ) . "')";
107 $db->query( $sql, "SearchUpdate::doUpdate" );
108 wfProfileOut( $fname );
109 }
110 }
111
112 ?>