6e9c9c5ec66a175f544d791e95f390f6fefdd950
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;
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
45 public function __construct( $id, $title, $content = false ) {
46 if ( is_string( $title ) ) {
47 $nt = Title
::newFromText( $title );
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;
60 $this->mText
= $content ?
$content->getTextForSearchIndex() : false;
63 $this->mNamespace
= $nt->getNamespace();
64 $this->mTitle
= $nt->getText(); # Discard namespace
66 $this->mTitleWords
= $this->mTextWords
= array();
68 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
72 public function doUpdate() {
73 global $wgDisableSearchUpdate;
75 if ( $wgDisableSearchUpdate ||
!$this->mId
) {
79 wfProfileIn( __METHOD__
);
81 $search = SearchEngine
::create();
82 $normalTitle = $search->normalizeText( Title
::indexTitle( $this->mNamespace
, $this->mTitle
) );
84 if ( WikiPage
::newFromId( $this->mId
) === null ) {
85 $search->delete( $this->mId
, $normalTitle );
86 wfProfileOut( __METHOD__
);
88 } elseif ( $this->mText
=== false ) {
89 $search->updateTitle( $this->mId
, $normalTitle );
90 wfProfileOut( __METHOD__
);
94 $text = self
::updateText( $this->mText
);
96 wfRunHooks( 'SearchUpdate', array( $this->mId
, $this->mNamespace
, $this->mTitle
, &$text ) );
98 # Perform the actual update
99 $search->update( $this->mId
, $normalTitle, $search->normalizeText( $text ) );
101 wfProfileOut( __METHOD__
);
104 public static function updateText( $text ) {
107 # Language-specific strip/conversion
108 $text = $wgContLang->normalizeForSearch( $text );
109 $lc = SearchEngine
::legalSearchChars() . '&#;';
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
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 );
123 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
124 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
125 $text = preg_replace( $p1, "\\1 ", $text );
126 $text = preg_replace( $p2, "\\1 \\3 ", $text );
128 # Internal image links
129 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
130 $text = preg_replace( $pat2, " \\1 \\3", $text );
132 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
133 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
135 # Strip all remaining non-search characters
136 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
140 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
141 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
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.
150 # Following are reversed versions which are consistently fast
151 # (about 3 milliseconds on 1GHz-level processor).
153 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
154 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
156 # Strip wiki '' and '''
157 $text = preg_replace( "/''[']*/", " ", $text );
158 wfProfileOut( __METHOD__
. '-regexps' );