Commit JeLuF's register_globals fixes, first phase
[lhc/web/wiklou.git] / includes / SearchUpdate.php
1 <?
2 # See deferred.doc
3
4 class SearchUpdate {
5
6 /* private */ var $mId, $mNamespace, $mTitle, $mText;
7 /* private */ var $mTitleWords;
8
9 function SearchUpdate( $id, $title, $text = false )
10 {
11 $this->mId = $id;
12 $this->mText = $text;
13
14 $nt = Title::newFromText( $title );
15 $this->mNamespace = $nt->getNamespace();
16 $this->mTitle = $nt->getText(); # Discard namespace
17
18 $this->mTitleWords = $this->mTextWords = array();
19 }
20
21 function doUpdate()
22 {
23 global $wgDBminWordLen, $wgLang, $wgDisableSearchUpdate;
24
25 if( $wgDisableSearchUpdate ) {
26 return false;
27 }
28 $lc = SearchEngine::legalSearchChars() . "&#;";
29 if( $this->mText == false ) {
30 # Just update the title
31 $sql = "UPDATE LOW_PRIORITY searchindex SET si_title='" .
32 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) .
33 "' WHERE si_page={$this->mId}";
34 wfQuery( $sql, DB_WRITE, "SearchUpdate::doUpdate" );
35 return;
36 }
37
38 # Language-specific strip/conversion
39 $text = $wgLang->stripForSearch( $this->mText );
40
41 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
42 " ", strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
43 $text = preg_replace( "/(^|\\n)\\s*==\\s+([^\\n]+)\\s+==\\s/sD",
44 "\\2 \\2 \\2 ", $text ); # Emphasize headings
45
46 # Strip external URLs
47 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
48 $protos = "http|https|ftp|mailto|news|gopher";
49 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
50 $text = preg_replace( $pat, "\\1 \\3", $text );
51
52 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
53 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
54 $text = preg_replace( $p1, "\\1 ", $text );
55 $text = preg_replace( $p2, "\\1 \\3 ", $text );
56
57 # Internal image links
58 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
59 $text = preg_replace( $pat2, " \\1 \\3", $text );
60
61 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
62 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
63
64 # Strip all remaining non-search characters
65 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
66
67 # Handle 's, s'
68 $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
69 $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
70
71 # Strip wiki '' and '''
72 $text = preg_replace( "/''[']*/", " ", $text );
73
74 $sql = "REPLACE DELAYED INTO searchindex (si_page,si_title,si_text) VALUES ({$this->mId},'" .
75 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) . "','" .
76 wfStrencode( $text ) . "')";
77 wfQuery( $sql, DB_WRITE, "SearchUpdate::doUpdate" );
78 }
79 }
80
81 ?>