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