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