Support updating search index when page is deleted
[lhc/web/wiklou.git] / includes / search / SearchUpdate.php
1 <?php
2 /**
3 * Search index updater
4 *
5 * See deferred.txt
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Search
24 */
25
26 /**
27 * Database independant search index updater
28 *
29 * @ingroup Search
30 */
31 class SearchUpdate implements DeferrableUpdate {
32
33 private $mId = 0, $mNamespace, $mTitle, $mText;
34 private $mTitleWords;
35
36 public function __construct( $id, $title, $text = false ) {
37 if ( is_string( $title ) ) {
38 $nt = Title::newFromText( $title );
39 } else {
40 $nt = $title;
41 }
42
43 if ( $nt ) {
44 $this->mId = $id;
45 $this->mText = $text;
46
47 $this->mNamespace = $nt->getNamespace();
48 $this->mTitle = $nt->getText(); # Discard namespace
49
50 $this->mTitleWords = $this->mTextWords = array();
51 } else {
52 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
53 }
54 }
55
56 public function doUpdate() {
57 global $wgDisableSearchUpdate;
58
59 if ( $wgDisableSearchUpdate || !$this->mId ) {
60 return;
61 }
62
63 wfProfileIn( __METHOD__ );
64
65 $search = SearchEngine::create();
66 $normalTitle = $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) );
67
68 if ( WikiPage::newFromId( $this->mId ) === null ) {
69 $search->delete( $this->mId, $normalTitle );
70 wfProfileOut( __METHOD__ );
71 return;
72 } elseif ( $this->mText === false ) {
73 $search->updateTitle( $this->mId, $normalTitle );
74 wfProfileOut( __METHOD__ );
75 return;
76 }
77
78 $text = self::updateText( $this->mText );
79
80 wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
81
82 # Perform the actual update
83 $search->update( $this->mId, $normalTitle, $search->normalizeText( $text ) );
84
85 wfProfileOut( __METHOD__ );
86 }
87
88 public static function updateText( $text ) {
89 global $wgContLang;
90
91 # Language-specific strip/conversion
92 $text = $wgContLang->normalizeForSearch( $text );
93 $lc = SearchEngine::legalSearchChars() . '&#;';
94
95 wfProfileIn( __METHOD__ . '-regexps' );
96 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
97 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
98 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
99 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
100
101 # Strip external URLs
102 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
103 $protos = "http|https|ftp|mailto|news|gopher";
104 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
105 $text = preg_replace( $pat, "\\1 \\3", $text );
106
107 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
108 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
109 $text = preg_replace( $p1, "\\1 ", $text );
110 $text = preg_replace( $p2, "\\1 \\3 ", $text );
111
112 # Internal image links
113 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
114 $text = preg_replace( $pat2, " \\1 \\3", $text );
115
116 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
117 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
118
119 # Strip all remaining non-search characters
120 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
121
122 # Handle 's, s'
123 #
124 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
125 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
126 #
127 # These tail-anchored regexps are insanely slow. The worst case comes
128 # when Japanese or Chinese text (ie, no word spacing) is written on
129 # a wiki configured for Western UTF-8 mode. The Unicode characters are
130 # expanded to hex codes and the "words" are very long paragraph-length
131 # monstrosities. On a large page the above regexps may take over 20
132 # seconds *each* on a 1GHz-level processor.
133 #
134 # Following are reversed versions which are consistently fast
135 # (about 3 milliseconds on 1GHz-level processor).
136 #
137 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
138 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
139
140 # Strip wiki '' and '''
141 $text = preg_replace( "/''[']*/", " ", $text );
142 wfProfileOut( __METHOD__ . '-regexps' );
143 return $text;
144 }
145 }
146
147 /**
148 * Placeholder class
149 *
150 * @ingroup Search
151 */
152 class SearchUpdateMyISAM extends SearchUpdate {
153 # Inherits everything
154 }