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
27 * Database independant search index updater
31 class SearchUpdate
implements DeferrableUpdate
{
33 private $mId = 0, $mNamespace, $mTitle, $mText;
36 function __construct( $id, $title, $text = false ) {
37 $nt = Title
::newFromText( $title );
42 $this->mNamespace
= $nt->getNamespace();
43 $this->mTitle
= $nt->getText(); # Discard namespace
45 $this->mTitleWords
= $this->mTextWords
= array();
47 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
52 global $wgContLang, $wgDisableSearchUpdate;
54 if( $wgDisableSearchUpdate ||
!$this->mId
) {
58 wfProfileIn( __METHOD__
);
60 $search = SearchEngine
::create();
61 $lc = SearchEngine
::legalSearchChars() . '&#;';
63 if( $this->mText
=== false ) {
64 $search->updateTitle($this->mId
,
65 $search->normalizeText( Title
::indexTitle( $this->mNamespace
, $this->mTitle
) ) );
66 wfProfileOut( __METHOD__
);
70 # Language-specific strip/conversion
71 $text = $wgContLang->normalizeForSearch( $this->mText
);
73 wfProfileIn( __METHOD__
. '-regexps' );
74 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
75 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
76 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
77 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
80 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
81 $protos = "http|https|ftp|mailto|news|gopher";
82 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
83 $text = preg_replace( $pat, "\\1 \\3", $text );
85 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
86 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
87 $text = preg_replace( $p1, "\\1 ", $text );
88 $text = preg_replace( $p2, "\\1 \\3 ", $text );
90 # Internal image links
91 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
92 $text = preg_replace( $pat2, " \\1 \\3", $text );
94 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
95 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
97 # Strip all remaining non-search characters
98 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
102 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
103 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
105 # These tail-anchored regexps are insanely slow. The worst case comes
106 # when Japanese or Chinese text (ie, no word spacing) is written on
107 # a wiki configured for Western UTF-8 mode. The Unicode characters are
108 # expanded to hex codes and the "words" are very long paragraph-length
109 # monstrosities. On a large page the above regexps may take over 20
110 # seconds *each* on a 1GHz-level processor.
112 # Following are reversed versions which are consistently fast
113 # (about 3 milliseconds on 1GHz-level processor).
115 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
116 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
118 # Strip wiki '' and '''
119 $text = preg_replace( "/''[']*/", " ", $text );
120 wfProfileOut( __METHOD__
. '-regexps' );
122 wfRunHooks( 'SearchUpdate', array( $this->mId
, $this->mNamespace
, $this->mTitle
, &$text ) );
124 # Perform the actual update
125 $search->update($this->mId
, $search->normalizeText( Title
::indexTitle( $this->mNamespace
, $this->mTitle
) ),
126 $search->normalizeText( $text ) );
128 wfProfileOut( __METHOD__
);
137 class SearchUpdateMyISAM
extends SearchUpdate
{
138 # Inherits everything