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
{
32 /** @var int Page id being updated */
35 /** @var Title Title we're updating */
38 /** @var Content|bool Content of the page (not text) */
44 * @param int $id Page id to update
45 * @param Title|string $title Title of page to update
46 * @param Content|string|bool $c Content of the page to update. Default: false.
47 * If a Content object, text will be gotten from it. String is for back-compat.
48 * Passing false tells the backend to just update the title, not the content
50 public function __construct( $id, $title, $c = false ) {
51 if ( is_string( $title ) ) {
52 $nt = Title
::newFromText( $title );
59 // is_string() check is back-compat for ApprovedRevs
60 if ( is_string( $c ) ) {
61 $this->content
= new TextContent( $c );
63 $this->content
= $c ?
: false;
67 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
72 * Perform actual update for the entry
74 public function doUpdate() {
75 global $wgDisableSearchUpdate;
77 if ( $wgDisableSearchUpdate ||
!$this->id
) {
81 wfProfileIn( __METHOD__
);
83 $page = WikiPage
::newFromId( $this->id
, WikiPage
::READ_LATEST
);
85 foreach ( SearchEngine
::getSearchTypes() as $type ) {
86 $search = SearchEngine
::create( $type );
87 $indexTitle = $this->indexTitle( $search );
88 if ( !$search->supports( 'search-update' ) ) {
92 $normalTitle = $search->normalizeText( $indexTitle );
94 if ( $page === null ) {
95 $search->delete( $this->id
, $normalTitle );
97 } elseif ( $this->content
=== false ) {
98 $search->updateTitle( $this->id
, $normalTitle );
102 $text = $search->getTextFromContent( $this->title
, $this->content
);
103 if ( !$search->textAlreadyUpdatedForIndex() ) {
104 $text = self
::updateText( $text );
107 # Perform the actual update
108 $search->update( $this->id
, $normalTitle, $search->normalizeText( $text ) );
111 wfProfileOut( __METHOD__
);
115 * Clean text for indexing. Only really suitable for indexing in databases.
116 * If you're using a real search engine, you'll probably want to override
117 * this behavior and do something nicer with the original wikitext.
118 * @param string $text
121 public static function updateText( $text ) {
124 # Language-specific strip/conversion
125 $text = $wgContLang->normalizeForSearch( $text );
126 $lc = SearchEngine
::legalSearchChars() . '&#;';
128 wfProfileIn( __METHOD__
. '-regexps' );
129 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
130 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
131 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
132 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
134 # Strip external URLs
135 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
136 $protos = "http|https|ftp|mailto|news|gopher";
137 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
138 $text = preg_replace( $pat, "\\1 \\3", $text );
140 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
141 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
142 $text = preg_replace( $p1, "\\1 ", $text );
143 $text = preg_replace( $p2, "\\1 \\3 ", $text );
145 # Internal image links
146 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
147 $text = preg_replace( $pat2, " \\1 \\3", $text );
149 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
150 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
152 # Strip all remaining non-search characters
153 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
157 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
158 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
160 # These tail-anchored regexps are insanely slow. The worst case comes
161 # when Japanese or Chinese text (ie, no word spacing) is written on
162 # a wiki configured for Western UTF-8 mode. The Unicode characters are
163 # expanded to hex codes and the "words" are very long paragraph-length
164 # monstrosities. On a large page the above regexps may take over 20
165 # seconds *each* on a 1GHz-level processor.
167 # Following are reversed versions which are consistently fast
168 # (about 3 milliseconds on 1GHz-level processor).
170 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
171 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
173 # Strip wiki '' and '''
174 $text = preg_replace( "/''[']*/", " ", $text );
175 wfProfileOut( __METHOD__
. '-regexps' );
181 * Get a string representation of a title suitable for
182 * including in a search index
184 * @param SearchEngine $search
185 * @return string A stripped-down title string ready for the search index
187 private function indexTitle( SearchEngine
$search ) {
190 $ns = $this->title
->getNamespace();
191 $title = $this->title
->getText();
193 $lc = $search->legalSearchChars() . '&#;';
194 $t = $wgContLang->normalizeForSearch( $title );
195 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
196 $t = $wgContLang->lc( $t );
199 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
200 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
202 $t = preg_replace( "/\\s+/", ' ', $t );
204 if ( $ns == NS_FILE
) {
205 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );