add Last-Modified header
[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, $wgIsMySQL;
29
30 if( $wgDisableSearchUpdate || !$this->mId ) {
31 return false;
32 }
33 $lc = SearchEngine::legalSearchChars() . "&#;";
34 if( $this->mText == false ) {
35 # Just update the title
36 $lowpri=$wgIsMySQL?"LOW_PRIORITY":"";
37 $sql = "UPDATE $lowpri searchindex SET si_title='" .
38 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) .
39 "' WHERE si_page={$this->mId}";
40 wfQuery( $sql, DB_WRITE, "SearchUpdate::doUpdate" );
41 return;
42 }
43
44 # Language-specific strip/conversion
45 $text = $wgLang->stripForSearch( $this->mText );
46
47 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
48 " ", strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
49 $text = preg_replace( "/(^|\\n)\\s*==\\s+([^\\n]+)\\s+==\\s/sD",
50 "\\2 \\2 \\2 ", $text ); # Emphasize headings
51
52 # Strip external URLs
53 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
54 $protos = "http|https|ftp|mailto|news|gopher";
55 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
56 $text = preg_replace( $pat, "\\1 \\3", $text );
57
58 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
59 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
60 $text = preg_replace( $p1, "\\1 ", $text );
61 $text = preg_replace( $p2, "\\1 \\3 ", $text );
62
63 # Internal image links
64 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
65 $text = preg_replace( $pat2, " \\1 \\3", $text );
66
67 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
68 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
69
70 # Strip all remaining non-search characters
71 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
72
73 # Handle 's, s'
74 $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
75 $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
76
77 # Strip wiki '' and '''
78 $text = preg_replace( "/''[']*/", " ", $text );
79
80 $sql = "REPLACE INTO searchindex (si_page,si_title,si_text) VALUES ({$this->mId},'" .
81 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) . "','" .
82 wfStrencode( $text ) . "')";
83 wfQuery( $sql, DB_WRITE, "SearchUpdate::doUpdate" );
84 }
85 }
86
87 ?>