Fix.
[mediawiki.git] / includes / SearchUpdate.php
blobaae4a66c86ddcc10e1ea570d1cbafdac0639c7db
1 <?php
2 /**
3 * See deferred.txt
4 */
6 /**
8 */
9 class SearchUpdate {
11 /* private */ var $mId = 0, $mNamespace, $mTitle, $mText;
12 /* private */ var $mTitleWords;
14 function SearchUpdate( $id, $title, $text = false ) {
15 $nt = Title::newFromText( $title );
16 if( $nt ) {
17 $this->mId = $id;
18 $this->mText = $text;
20 $this->mNamespace = $nt->getNamespace();
21 $this->mTitle = $nt->getText(); # Discard namespace
23 $this->mTitleWords = $this->mTextWords = array();
24 } else {
25 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
29 function doUpdate() {
30 global $wgContLang, $wgDisableSearchUpdate;
32 if( $wgDisableSearchUpdate || !$this->mId ) {
33 return false;
35 $fname = 'SearchUpdate::doUpdate';
36 wfProfileIn( $fname );
38 $search = SearchEngine::create();
39 $lc = SearchEngine::legalSearchChars() . '&#;';
41 if( $this->mText === false ) {
42 $search->updateTitle($this->mId,
43 Title::indexTitle( $this->mNamespace, $this->mTitle ));
44 wfProfileOut( $fname );
45 return;
48 # Language-specific strip/conversion
49 $text = $wgContLang->stripForSearch( $this->mText );
51 wfProfileIn( $fname.'-regexps' );
52 $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
53 ' ', strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
54 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
55 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
57 # Strip external URLs
58 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
59 $protos = "http|https|ftp|mailto|news|gopher";
60 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
61 $text = preg_replace( $pat, "\\1 \\3", $text );
63 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
64 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
65 $text = preg_replace( $p1, "\\1 ", $text );
66 $text = preg_replace( $p2, "\\1 \\3 ", $text );
68 # Internal image links
69 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
70 $text = preg_replace( $pat2, " \\1 \\3", $text );
72 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
73 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
75 # Strip all remaining non-search characters
76 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
78 # Handle 's, s'
80 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
81 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
83 # These tail-anchored regexps are insanely slow. The worst case comes
84 # when Japanese or Chinese text (ie, no word spacing) is written on
85 # a wiki configured for Western UTF-8 mode. The Unicode characters are
86 # expanded to hex codes and the "words" are very long paragraph-length
87 # monstrosities. On a large page the above regexps may take over 20
88 # seconds *each* on a 1GHz-level processor.
90 # Following are reversed versions which are consistently fast
91 # (about 3 milliseconds on 1GHz-level processor).
93 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
94 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
96 # Strip wiki '' and '''
97 $text = preg_replace( "/''[']*/", " ", $text );
98 wfProfileOut( "$fname-regexps" );
99 $search->update($this->mId, Title::indexTitle( $this->mNamespace, $this->mTitle ),
100 $text);
101 wfProfileOut( $fname );
106 * Placeholder class
108 class SearchUpdateMyISAM extends SearchUpdate {
109 # Inherits everything