Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SearchUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 */
5
6 /**
7 *
8 */
9 class SearchUpdate {
10
11 /* private */ var $mId = 0, $mNamespace, $mTitle, $mText;
12 /* private */ var $mTitleWords;
13
14 function SearchUpdate( $id, $title, $text = false ) {
15 $nt = Title::newFromText( $title );
16 if( $nt ) {
17 $this->mId = $id;
18 $this->mText = $text;
19
20 $this->mNamespace = $nt->getNamespace();
21 $this->mTitle = $nt->getText(); # Discard namespace
22
23 $this->mTitleWords = $this->mTextWords = array();
24 } else {
25 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
26 }
27 }
28
29 function doUpdate() {
30 global $wgContLang, $wgDisableSearchUpdate;
31
32 if( $wgDisableSearchUpdate || !$this->mId ) {
33 return false;
34 }
35 $fname = 'SearchUpdate::doUpdate';
36 wfProfileIn( $fname );
37
38 $search = SearchEngine::create();
39 $lc = SearchEngine::legalSearchChars() . '&#;';
40
41 if( $this->mText === false ) {
42 $search->updateTitle($this->mId,
43 Title::indexTitle( $this->mNamespace, $this->mTitle ));
44 wfProfileOut( $fname );
45 return;
46 }
47
48 # Language-specific strip/conversion
49 $text = $wgContLang->stripForSearch( $this->mText );
50
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
56
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 );
62
63 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
64 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
65 $text = preg_replace( $p1, "\\1 ", $text );
66 $text = preg_replace( $p2, "\\1 \\3 ", $text );
67
68 # Internal image links
69 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
70 $text = preg_replace( $pat2, " \\1 \\3", $text );
71
72 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
73 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
74
75 # Strip all remaining non-search characters
76 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
77
78 # Handle 's, s'
79 #
80 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
81 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
82 #
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.
89 #
90 # Following are reversed versions which are consistently fast
91 # (about 3 milliseconds on 1GHz-level processor).
92 #
93 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
94 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
95
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 );
102 }
103 }
104
105 /**
106 * Placeholder class
107 */
108 class SearchUpdateMyISAM extends SearchUpdate {
109 # Inherits everything
110 }
111
112 ?>