2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2010-08-08 10:44:59 +00:00
|
|
|
* Search index updater
|
|
|
|
|
*
|
2005-04-12 02:07:16 +00:00
|
|
|
* See deferred.txt
|
2010-08-08 10:44:59 +00:00
|
|
|
*
|
2012-04-28 18:41:55 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-08 10:44:59 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Search
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Database independant search index updater
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Search
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2011-09-10 06:50:30 +00:00
|
|
|
class SearchUpdate implements DeferrableUpdate {
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2011-04-18 19:03:14 +00:00
|
|
|
private $mId = 0, $mNamespace, $mTitle, $mText;
|
|
|
|
|
private $mTitleWords;
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2013-06-19 17:55:33 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param int $id Page id to update
|
|
|
|
|
* @param Title|string $title Title of page to update
|
|
|
|
|
* @param Content|string|false $content Content of the page to update.
|
|
|
|
|
* If a Content object, text will be gotten from it. String is for back-compat.
|
|
|
|
|
* Passing false tells the backend to just update the title, not the content
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $id, $title, $content = false ) {
|
2012-12-13 20:48:36 +00:00
|
|
|
if ( is_string( $title ) ) {
|
|
|
|
|
$nt = Title::newFromText( $title );
|
|
|
|
|
} else {
|
|
|
|
|
$nt = $title;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( $nt ) {
|
2003-12-03 00:42:31 +00:00
|
|
|
$this->mId = $id;
|
2013-06-19 17:55:33 +00:00
|
|
|
// @todo This isn't ideal, we'd really like to have content-specific
|
|
|
|
|
// handling here. See similar content in SearchEngine::initText().
|
|
|
|
|
if( is_string( $content ) ) {
|
|
|
|
|
// b/c for ApprovedRevs
|
|
|
|
|
$this->mText = $content;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mText = $content ? $content->getTextForSearchIndex() : false;
|
|
|
|
|
}
|
2003-12-03 00:42:31 +00:00
|
|
|
|
|
|
|
|
$this->mNamespace = $nt->getNamespace();
|
|
|
|
|
$this->mTitle = $nt->getText(); # Discard namespace
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2003-12-03 00:42:31 +00:00
|
|
|
$this->mTitleWords = $this->mTextWords = array();
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
|
|
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-18 20:06:49 +00:00
|
|
|
public function doUpdate() {
|
|
|
|
|
global $wgDisableSearchUpdate;
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( $wgDisableSearchUpdate || !$this->mId ) {
|
2011-10-16 03:27:12 +00:00
|
|
|
return;
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
2010-12-07 10:51:57 +00:00
|
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
2004-08-22 09:08:04 +00:00
|
|
|
|
2005-07-22 11:29:15 +00:00
|
|
|
$search = SearchEngine::create();
|
2013-06-18 20:06:49 +00:00
|
|
|
$normalTitle = $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-06-18 20:06:49 +00:00
|
|
|
if ( WikiPage::newFromId( $this->mId ) === null ) {
|
|
|
|
|
$search->delete( $this->mId, $normalTitle );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return;
|
|
|
|
|
} elseif ( $this->mText === false ) {
|
|
|
|
|
$search->updateTitle( $this->mId, $normalTitle );
|
2010-12-07 10:51:57 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2003-04-14 23:10:40 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2013-06-18 20:06:49 +00:00
|
|
|
$text = self::updateText( $this->mText );
|
|
|
|
|
|
|
|
|
|
wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
|
|
|
|
|
|
|
|
|
|
# Perform the actual update
|
|
|
|
|
$search->update( $this->mId, $normalTitle, $search->normalizeText( $text ) );
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function updateText( $text ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
|
2003-04-14 23:10:40 +00:00
|
|
|
# Language-specific strip/conversion
|
2013-06-18 20:06:49 +00:00
|
|
|
$text = $wgContLang->normalizeForSearch( $text );
|
|
|
|
|
$lc = SearchEngine::legalSearchChars() . '&#;';
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2010-12-07 10:51:57 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-regexps' );
|
2009-11-04 15:18:30 +00:00
|
|
|
$text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
|
2009-10-30 15:25:37 +00:00
|
|
|
' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
|
2005-11-03 23:27:36 +00:00
|
|
|
$text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
|
2013-02-03 18:30:03 +00:00
|
|
|
"\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
|
2003-04-14 23:10:40 +00:00
|
|
|
|
|
|
|
|
# Strip external URLs
|
2011-11-30 00:37:35 +00:00
|
|
|
$uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
|
2003-04-14 23:10:40 +00:00
|
|
|
$protos = "http|https|ftp|mailto|news|gopher";
|
|
|
|
|
$pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
|
|
|
|
|
$text = preg_replace( $pat, "\\1 \\3", $text );
|
|
|
|
|
|
|
|
|
|
$p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
|
|
|
|
|
$p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
|
|
|
|
|
$text = preg_replace( $p1, "\\1 ", $text );
|
|
|
|
|
$text = preg_replace( $p2, "\\1 \\3 ", $text );
|
|
|
|
|
|
|
|
|
|
# Internal image links
|
|
|
|
|
$pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
|
|
|
|
|
$text = preg_replace( $pat2, " \\1 \\3", $text );
|
|
|
|
|
|
|
|
|
|
$text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
|
2013-02-03 18:30:03 +00:00
|
|
|
"\\1\\2 \\2\\3", $text ); # Handle [[game]]s
|
2003-04-14 23:10:40 +00:00
|
|
|
|
|
|
|
|
# Strip all remaining non-search characters
|
|
|
|
|
$text = preg_replace( "/[^{$lc}]+/", " ", $text );
|
|
|
|
|
|
|
|
|
|
# Handle 's, s'
|
2004-08-22 09:08:04 +00:00
|
|
|
#
|
|
|
|
|
# $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
|
|
|
|
|
# $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
|
|
|
|
|
#
|
|
|
|
|
# These tail-anchored regexps are insanely slow. The worst case comes
|
|
|
|
|
# when Japanese or Chinese text (ie, no word spacing) is written on
|
|
|
|
|
# a wiki configured for Western UTF-8 mode. The Unicode characters are
|
|
|
|
|
# expanded to hex codes and the "words" are very long paragraph-length
|
|
|
|
|
# monstrosities. On a large page the above regexps may take over 20
|
|
|
|
|
# seconds *each* on a 1GHz-level processor.
|
|
|
|
|
#
|
|
|
|
|
# Following are reversed versions which are consistently fast
|
|
|
|
|
# (about 3 milliseconds on 1GHz-level processor).
|
|
|
|
|
#
|
|
|
|
|
$text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
|
|
|
|
|
$text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
|
|
|
|
|
# Strip wiki '' and '''
|
|
|
|
|
$text = preg_replace( "/''[']*/", " ", $text );
|
2010-12-07 10:51:57 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-regexps' );
|
2013-06-18 20:06:49 +00:00
|
|
|
return $text;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|