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