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