00001 <?php
00006 class SearchUpdate {
00007
00008 var $mId = 0, $mNamespace, $mTitle, $mText;
00009 var $mTitleWords;
00010
00011 function SearchUpdate( $id, $title, $text = false ) {
00012 $nt = Title::newFromText( $title );
00013 if( $nt ) {
00014 $this->mId = $id;
00015 $this->mText = $text;
00016
00017 $this->mNamespace = $nt->getNamespace();
00018 $this->mTitle = $nt->getText(); # Discard namespace
00019
00020 $this->mTitleWords = $this->mTextWords = array();
00021 } else {
00022 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
00023 }
00024 }
00025
00026 function doUpdate() {
00027 global $wgContLang, $wgDisableSearchUpdate;
00028
00029 if( $wgDisableSearchUpdate || !$this->mId ) {
00030 return false;
00031 }
00032 $fname = 'SearchUpdate::doUpdate';
00033 wfProfileIn( $fname );
00034
00035 $search = SearchEngine::create();
00036 $lc = SearchEngine::legalSearchChars() . '&#;';
00037
00038 if( $this->mText === false ) {
00039 $search->updateTitle($this->mId,
00040 Title::indexTitle( $this->mNamespace, $this->mTitle ));
00041 wfProfileOut( $fname );
00042 return;
00043 }
00044
00045 # Language-specific strip/conversion
00046 $text = $wgContLang->stripForSearch( $this->mText );
00047
00048 wfProfileIn( $fname.'-regexps' );
00049 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
00050 ' ', strtolower( " " . $text . " " ) ); # Strip HTML markup
00051 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
00052 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
00053
00054 # Strip external URLs
00055 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
00056 $protos = "http|https|ftp|mailto|news|gopher";
00057 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
00058 $text = preg_replace( $pat, "\\1 \\3", $text );
00059
00060 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
00061 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
00062 $text = preg_replace( $p1, "\\1 ", $text );
00063 $text = preg_replace( $p2, "\\1 \\3 ", $text );
00064
00065 # Internal image links
00066 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
00067 $text = preg_replace( $pat2, " \\1 \\3", $text );
00068
00069 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
00070 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
00071
00072 # Strip all remaining non-search characters
00073 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
00074
00075 # Handle 's, s'
00076 #
00077 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
00078 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
00079 #
00080 # These tail-anchored regexps are insanely slow. The worst case comes
00081 # when Japanese or Chinese text (ie, no word spacing) is written on
00082 # a wiki configured for Western UTF-8 mode. The Unicode characters are
00083 # expanded to hex codes and the "words" are very long paragraph-length
00084 # monstrosities. On a large page the above regexps may take over 20
00085 # seconds *each* on a 1GHz-level processor.
00086 #
00087 # Following are reversed versions which are consistently fast
00088 # (about 3 milliseconds on 1GHz-level processor).
00089 #
00090 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
00091 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
00092
00093 # Strip wiki '' and '''
00094 $text = preg_replace( "/''[']*/", " ", $text );
00095 wfProfileOut( "$fname-regexps" );
00096
00097 wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
00098
00099 # Perform the actual update
00100 $search->update($this->mId, Title::indexTitle( $this->mNamespace, $this->mTitle ),
00101 $text);
00102
00103 wfProfileOut( $fname );
00104 }
00105 }
00106
00111 class SearchUpdateMyISAM extends SearchUpdate {
00112 # Inherits everything
00113 }