6e9c9c5ec66a175f544d791e95f390f6fefdd950
[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 /**
37 * Constructor
38 *
39 * @param int $id Page id to update
40 * @param Title|string $title Title of page to update
41 * @param Content|string|false $content Content of the page to update.
42 * If a Content object, text will be gotten from it. String is for back-compat.
43 * Passing false tells the backend to just update the title, not the content
44 */
45 public function __construct( $id, $title, $content = false ) {
46 if ( is_string( $title ) ) {
47 $nt = Title::newFromText( $title );
48 } else {
49 $nt = $title;
50 }
51
52 if ( $nt ) {
53 $this->mId = $id;
54 // @todo This isn't ideal, we'd really like to have content-specific
55 // handling here. See similar content in SearchEngine::initText().
56 if( is_string( $content ) ) {
57 // b/c for ApprovedRevs
58 $this->mText = $content;
59 } else {
60 $this->mText = $content ? $content->getTextForSearchIndex() : false;
61 }
62
63 $this->mNamespace = $nt->getNamespace();
64 $this->mTitle = $nt->getText(); # Discard namespace
65
66 $this->mTitleWords = $this->mTextWords = array();
67 } else {
68 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
69 }
70 }
71
72 public function doUpdate() {
73 global $wgDisableSearchUpdate;
74
75 if ( $wgDisableSearchUpdate || !$this->mId ) {
76 return;
77 }
78
79 wfProfileIn( __METHOD__ );
80
81 $search = SearchEngine::create();
82 $normalTitle = $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) );
83
84 if ( WikiPage::newFromId( $this->mId ) === null ) {
85 $search->delete( $this->mId, $normalTitle );
86 wfProfileOut( __METHOD__ );
87 return;
88 } elseif ( $this->mText === false ) {
89 $search->updateTitle( $this->mId, $normalTitle );
90 wfProfileOut( __METHOD__ );
91 return;
92 }
93
94 $text = self::updateText( $this->mText );
95
96 wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
97
98 # Perform the actual update
99 $search->update( $this->mId, $normalTitle, $search->normalizeText( $text ) );
100
101 wfProfileOut( __METHOD__ );
102 }
103
104 public static function updateText( $text ) {
105 global $wgContLang;
106
107 # Language-specific strip/conversion
108 $text = $wgContLang->normalizeForSearch( $text );
109 $lc = SearchEngine::legalSearchChars() . '&#;';
110
111 wfProfileIn( __METHOD__ . '-regexps' );
112 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
113 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
114 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
115 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
116
117 # Strip external URLs
118 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
119 $protos = "http|https|ftp|mailto|news|gopher";
120 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
121 $text = preg_replace( $pat, "\\1 \\3", $text );
122
123 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
124 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
125 $text = preg_replace( $p1, "\\1 ", $text );
126 $text = preg_replace( $p2, "\\1 \\3 ", $text );
127
128 # Internal image links
129 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
130 $text = preg_replace( $pat2, " \\1 \\3", $text );
131
132 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
133 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
134
135 # Strip all remaining non-search characters
136 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
137
138 # Handle 's, s'
139 #
140 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
141 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
142 #
143 # These tail-anchored regexps are insanely slow. The worst case comes
144 # when Japanese or Chinese text (ie, no word spacing) is written on
145 # a wiki configured for Western UTF-8 mode. The Unicode characters are
146 # expanded to hex codes and the "words" are very long paragraph-length
147 # monstrosities. On a large page the above regexps may take over 20
148 # seconds *each* on a 1GHz-level processor.
149 #
150 # Following are reversed versions which are consistently fast
151 # (about 3 milliseconds on 1GHz-level processor).
152 #
153 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
154 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
155
156 # Strip wiki '' and '''
157 $text = preg_replace( "/''[']*/", " ", $text );
158 wfProfileOut( __METHOD__ . '-regexps' );
159 return $text;
160 }
161 }