Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / search / SearchUpdate.php
blob8c1bda21a8a2134b71173f815dad1360eee90753
1 <?php
2 /**
3 * Search index updater
5 * See deferred.txt
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.
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.
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
22 * @file
23 * @ingroup Search
26 /**
27 * Database independant search index updater
29 * @ingroup Search
31 class SearchUpdate implements DeferrableUpdate {
33 private $mId = 0, $mNamespace, $mTitle, $mText;
34 private $mTitleWords;
36 function __construct( $id, $title, $text = false ) {
37 if ( is_string( $title ) ) {
38 $nt = Title::newFromText( $title );
39 } else {
40 $nt = $title;
43 if ( $nt ) {
44 $this->mId = $id;
45 $this->mText = $text;
47 $this->mNamespace = $nt->getNamespace();
48 $this->mTitle = $nt->getText(); # Discard namespace
50 $this->mTitleWords = $this->mTextWords = array();
51 } else {
52 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
56 function doUpdate() {
57 global $wgContLang, $wgDisableSearchUpdate;
59 if ( $wgDisableSearchUpdate || !$this->mId ) {
60 return;
63 wfProfileIn( __METHOD__ );
65 $search = SearchEngine::create();
66 $lc = SearchEngine::legalSearchChars() . '&#;';
68 if ( $this->mText === false ) {
69 $search->updateTitle( $this->mId,
70 $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) );
71 wfProfileOut( __METHOD__ );
72 return;
75 # Language-specific strip/conversion
76 $text = $wgContLang->normalizeForSearch( $this->mText );
78 wfProfileIn( __METHOD__ . '-regexps' );
79 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
80 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
81 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
82 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
84 # Strip external URLs
85 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
86 $protos = "http|https|ftp|mailto|news|gopher";
87 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
88 $text = preg_replace( $pat, "\\1 \\3", $text );
90 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
91 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
92 $text = preg_replace( $p1, "\\1 ", $text );
93 $text = preg_replace( $p2, "\\1 \\3 ", $text );
95 # Internal image links
96 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
97 $text = preg_replace( $pat2, " \\1 \\3", $text );
99 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
100 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
102 # Strip all remaining non-search characters
103 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
105 # Handle 's, s'
107 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
108 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
110 # These tail-anchored regexps are insanely slow. The worst case comes
111 # when Japanese or Chinese text (ie, no word spacing) is written on
112 # a wiki configured for Western UTF-8 mode. The Unicode characters are
113 # expanded to hex codes and the "words" are very long paragraph-length
114 # monstrosities. On a large page the above regexps may take over 20
115 # seconds *each* on a 1GHz-level processor.
117 # Following are reversed versions which are consistently fast
118 # (about 3 milliseconds on 1GHz-level processor).
120 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
121 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
123 # Strip wiki '' and '''
124 $text = preg_replace( "/''[']*/", " ", $text );
125 wfProfileOut( __METHOD__ . '-regexps' );
127 wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
129 # Perform the actual update
130 $search->update( $this->mId, $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) ),
131 $search->normalizeText( $text ) );
133 wfProfileOut( __METHOD__ );
138 * Placeholder class
140 * @ingroup Search
142 class SearchUpdateMyISAM extends SearchUpdate {
143 # Inherits everything