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
|
|
|
|
|
*/
|
|
|
|
|
|
2023-11-21 21:08:14 +00:00
|
|
|
namespace MediaWiki\Deferred;
|
|
|
|
|
|
2024-08-08 09:14:35 +00:00
|
|
|
use MediaWiki\Content\Content;
|
2022-06-24 13:25:04 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2016-04-03 08:37:11 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-05-29 22:02:57 +00:00
|
|
|
use MediaWiki\Page\ExistingPageRecord;
|
2021-05-07 22:19:29 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2023-11-21 21:08:14 +00:00
|
|
|
use SearchEngine;
|
2024-09-27 16:12:27 +00:00
|
|
|
use Wikimedia\Rdbms\IDBAccessObject;
|
2016-04-03 08:37:11 +00:00
|
|
|
|
2010-08-08 10:44:59 +00:00
|
|
|
/**
|
2021-02-18 06:38:52 +00:00
|
|
|
* Database independent search index updater
|
2010-08-08 10:44:59 +00:00
|
|
|
*
|
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 {
|
2013-11-20 18:43:39 +00:00
|
|
|
/** @var int Page id being updated */
|
2013-06-20 22:48:51 +00:00
|
|
|
private $id = 0;
|
|
|
|
|
|
2021-05-07 22:19:29 +00:00
|
|
|
/** @var PageIdentity The page we're updating */
|
|
|
|
|
private $page;
|
2013-06-20 22:48:51 +00:00
|
|
|
|
2019-08-01 10:13:45 +00:00
|
|
|
/** @var Content|null Content of the page (not text) */
|
2013-06-20 22:50:31 +00:00
|
|
|
private $content;
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2021-05-29 22:02:57 +00:00
|
|
|
/** @var ExistingPageRecord|null */
|
|
|
|
|
private $latestPage = null;
|
2015-12-02 16:16:17 +00:00
|
|
|
|
2013-06-19 17:55:33 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $id Page id to update
|
2021-05-07 22:19:29 +00:00
|
|
|
* @param PageIdentity $page Page to update
|
2019-08-01 10:13:45 +00:00
|
|
|
* @param Content|null $c Content of the page to update.
|
2013-06-19 17:55:33 +00:00
|
|
|
*/
|
2023-08-24 15:04:08 +00:00
|
|
|
public function __construct( $id, $page, ?Content $c = null ) {
|
2021-05-07 22:19:29 +00:00
|
|
|
$this->page = $page;
|
2019-08-01 10:13:45 +00:00
|
|
|
$this->id = $id;
|
|
|
|
|
$this->content = $c;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 22:48:51 +00:00
|
|
|
/**
|
|
|
|
|
* Perform actual update for the entry
|
|
|
|
|
*/
|
2013-06-18 20:06:49 +00:00
|
|
|
public function doUpdate() {
|
2018-08-18 04:02:39 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2024-04-16 14:07:10 +00:00
|
|
|
$searchEngineConfig = $services->getSearchEngineConfig();
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2024-04-16 14:07:10 +00:00
|
|
|
if ( $services->getMainConfig()->get( MainConfigNames::DisableSearchUpdate ) || !$this->id ) {
|
2022-06-24 13:25:04 +00:00
|
|
|
LoggerFactory::getInstance( "search" )
|
|
|
|
|
->debug( "Skipping update: search updates disabled by config" );
|
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
|
|
|
|
2018-08-18 04:02:39 +00:00
|
|
|
$seFactory = $services->getSearchEngineFactory();
|
2024-04-16 14:07:10 +00:00
|
|
|
foreach ( $searchEngineConfig->getSearchTypes() as $type ) {
|
2016-04-03 08:37:11 +00:00
|
|
|
$search = $seFactory->create( $type );
|
2013-08-13 22:54:03 +00:00
|
|
|
if ( !$search->supports( 'search-update' ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2015-12-02 15:42:18 +00:00
|
|
|
$normalTitle = $this->getNormalizedTitle( $search );
|
2013-06-18 20:06:49 +00:00
|
|
|
|
2015-12-02 16:16:17 +00:00
|
|
|
if ( $this->getLatestPage() === null ) {
|
2013-08-13 22:54:03 +00:00
|
|
|
$search->delete( $this->id, $normalTitle );
|
|
|
|
|
continue;
|
2019-08-01 10:13:45 +00:00
|
|
|
} elseif ( $this->content === null ) {
|
2013-08-13 22:54:03 +00:00
|
|
|
$search->updateTitle( $this->id, $normalTitle );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 14:58:33 +00:00
|
|
|
$text = $this->content !== null ? $this->content->getTextForSearchIndex() : '';
|
|
|
|
|
$text = $this->updateText( $text, $search );
|
2013-08-13 22:54:03 +00:00
|
|
|
|
|
|
|
|
# Perform the actual update
|
|
|
|
|
$search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
|
|
|
|
|
}
|
2013-06-18 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 22:48:51 +00:00
|
|
|
/**
|
|
|
|
|
* Clean text for indexing. Only really suitable for indexing in databases.
|
|
|
|
|
* If you're using a real search engine, you'll probably want to override
|
|
|
|
|
* this behavior and do something nicer with the original wikitext.
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param string $text
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param SearchEngine|null $se Search engine
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return string
|
2013-06-20 22:48:51 +00:00
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
public function updateText( $text, ?SearchEngine $se = null ) {
|
2018-08-18 04:02:39 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$contLang = $services->getContentLanguage();
|
2003-04-14 23:10:40 +00:00
|
|
|
# Language-specific strip/conversion
|
2018-08-18 04:02:39 +00:00
|
|
|
$text = $contLang->normalizeForSearch( $text );
|
|
|
|
|
$se = $se ?: $services->newSearchEngine();
|
2017-06-29 08:29:13 +00:00
|
|
|
$lc = $se->legalSearchChars() . '&#;';
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
# Strip HTML markup
|
2009-11-04 15:18:30 +00:00
|
|
|
$text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
|
2018-08-18 04:02:39 +00:00
|
|
|
' ', $contLang->lc( " " . $text . " " ) );
|
2023-03-24 03:21:20 +00:00
|
|
|
$text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/",
|
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 );
|
|
|
|
|
|
2015-09-28 11:25:15 +00:00
|
|
|
/**
|
|
|
|
|
* Handle 's, s'
|
|
|
|
|
*
|
|
|
|
|
* $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
|
|
|
|
|
* $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
|
|
|
|
|
*
|
2021-11-22 13:35:17 +00:00
|
|
|
* These tail-anchored regexps are very slow. The worst case comes
|
2015-09-28 11:25:15 +00:00
|
|
|
* 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).
|
|
|
|
|
*/
|
2004-08-22 09:08:04 +00:00
|
|
|
$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 );
|
2013-11-20 17:20:36 +00:00
|
|
|
|
2013-06-18 20:06:49 +00:00
|
|
|
return $text;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2014-04-16 00:20:15 +00:00
|
|
|
|
2015-12-02 16:16:17 +00:00
|
|
|
/**
|
2024-01-23 14:01:06 +00:00
|
|
|
* Get ExistingPageRecord for the SearchUpdate $id using IDBAccessObject::READ_LATEST
|
2021-05-29 22:02:57 +00:00
|
|
|
* and ensure using the same ExistingPageRecord object if there are multiple
|
2015-12-02 16:16:17 +00:00
|
|
|
* SearchEngine types.
|
|
|
|
|
*
|
|
|
|
|
* Returns null if a page has been deleted or is not found.
|
|
|
|
|
*
|
2021-05-29 22:02:57 +00:00
|
|
|
* @return ExistingPageRecord|null
|
2015-12-02 16:16:17 +00:00
|
|
|
*/
|
|
|
|
|
private function getLatestPage() {
|
2021-05-29 22:02:57 +00:00
|
|
|
if ( !isset( $this->latestPage ) ) {
|
|
|
|
|
$this->latestPage = MediaWikiServices::getInstance()->getPageStore()
|
2024-01-23 14:01:06 +00:00
|
|
|
->getPageById( $this->id, IDBAccessObject::READ_LATEST );
|
2015-12-02 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 22:02:57 +00:00
|
|
|
return $this->latestPage;
|
2015-12-02 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-16 00:20:15 +00:00
|
|
|
/**
|
2015-12-02 15:42:18 +00:00
|
|
|
* Get a normalized string representation of a title suitable for
|
2014-04-16 00:20:15 +00:00
|
|
|
* including in a search index
|
|
|
|
|
*
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param SearchEngine $search
|
2014-04-19 20:22:20 +00:00
|
|
|
* @return string A stripped-down title string ready for the search index
|
2014-04-16 00:20:15 +00:00
|
|
|
*/
|
2015-12-02 15:42:18 +00:00
|
|
|
private function getNormalizedTitle( SearchEngine $search ) {
|
2018-08-18 04:02:39 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2021-05-07 22:19:29 +00:00
|
|
|
$ns = $this->page->getNamespace();
|
|
|
|
|
$title = str_replace( '_', ' ', $this->page->getDBkey() );
|
2014-04-16 00:20:15 +00:00
|
|
|
|
2017-06-29 08:29:13 +00:00
|
|
|
$lc = $search->legalSearchChars() . '&#;';
|
2018-08-18 04:02:39 +00:00
|
|
|
$t = $contLang->normalizeForSearch( $title );
|
2014-04-16 00:20:15 +00:00
|
|
|
$t = preg_replace( "/[^{$lc}]+/", ' ', $t );
|
2018-08-18 04:02:39 +00:00
|
|
|
$t = $contLang->lc( $t );
|
2014-04-16 00:20:15 +00:00
|
|
|
|
|
|
|
|
# Handle 's, s'
|
|
|
|
|
$t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
|
|
|
|
|
$t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
|
|
|
|
|
|
|
|
|
|
$t = preg_replace( "/\\s+/", ' ', $t );
|
|
|
|
|
|
2020-07-22 17:29:48 +00:00
|
|
|
if ( $ns === NS_FILE ) {
|
2014-04-16 00:20:15 +00:00
|
|
|
$t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
|
|
|
|
|
}
|
2015-12-02 15:42:18 +00:00
|
|
|
|
|
|
|
|
return $search->normalizeText( trim( $t ) );
|
2014-04-16 00:20:15 +00:00
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2023-11-21 21:08:14 +00:00
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2023-11-21 21:08:14 +00:00
|
|
|
class_alias( SearchUpdate::class, 'SearchUpdate' );
|