Make Math rendering options user settable strings in MediaWiki namespace. First batch.
[mediawiki.git] / includes / SearchUpdate.php
blob55bad7907d1b3a5ef88d293c9bca0a0b87990cc8
1 <?php
2 # $Id$
3 # See deferred.doc
5 class SearchUpdate {
7 /* private */ var $mId = 0, $mNamespace, $mTitle, $mText;
8 /* private */ var $mTitleWords;
10 function SearchUpdate( $id, $title, $text = false )
12 $nt = Title::newFromText( $title );
13 if( $nt ) {
14 $this->mId = $id;
15 $this->mText = $text;
17 $this->mNamespace = $nt->getNamespace();
18 $this->mTitle = $nt->getText(); # Discard namespace
20 $this->mTitleWords = $this->mTextWords = array();
21 } else {
22 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
26 function doUpdate()
28 global $wgDBminWordLen, $wgLang, $wgDisableSearchUpdate;
30 if( $wgDisableSearchUpdate || !$this->mId ) {
31 return false;
33 $lc = SearchEngine::legalSearchChars() . "&#;";
34 $db =& wfGetDB( DB_WRITE );
36 if( $this->mText == false ) {
37 # Just update the title
38 $lowpri = $db->lowPriorityOption();
39 $sql = "UPDATE $lowpri searchindex SET si_title='" .
40 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) .
41 "' WHERE si_page={$this->mId}";
42 $db->query( $sql, "SearchUpdate::doUpdate" );
43 return;
46 # Language-specific strip/conversion
47 $text = $wgLang->stripForSearch( $this->mText );
49 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
50 " ", strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
51 $text = preg_replace( "/(^|\\n)\\s*==\\s+([^\\n]+)\\s+==\\s/sD",
52 "\\2 \\2 \\2 ", $text ); # Emphasize headings
54 # Strip external URLs
55 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
56 $protos = "http|https|ftp|mailto|news|gopher";
57 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
58 $text = preg_replace( $pat, "\\1 \\3", $text );
60 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
61 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
62 $text = preg_replace( $p1, "\\1 ", $text );
63 $text = preg_replace( $p2, "\\1 \\3 ", $text );
65 # Internal image links
66 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
67 $text = preg_replace( $pat2, " \\1 \\3", $text );
69 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
70 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
72 # Strip all remaining non-search characters
73 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
75 # Handle 's, s'
76 $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
77 $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
79 # Strip wiki '' and '''
80 $text = preg_replace( "/''[']*/", " ", $text );
82 $sql = "REPLACE INTO searchindex (si_page,si_title,si_text) VALUES ({$this->mId},'" .
83 wfStrencode( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) . "','" .
84 wfStrencode( $text ) . "')";
85 $db->query( $sql, "SearchUpdate::doUpdate" );