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