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