Merge "Typo fix"
[mediawiki.git] / includes / search / SearchEngine.php
blob4f4e31d2775958a882b9ea4e88001cfd67c0a934
1 <?php
2 /**
3 * Basic search engine
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Search
24 /**
25 * @defgroup Search Search
28 /**
29 * Contain a class for special pages
30 * @ingroup Search
32 class SearchEngine {
33 var $limit = 10;
34 var $offset = 0;
35 var $prefix = '';
36 var $searchTerms = array();
37 var $namespaces = array( NS_MAIN );
38 var $showRedirects = false;
40 /// Feature values
41 protected $features = array();
43 /**
44 * @var DatabaseBase
46 protected $db;
48 function __construct( $db = null ) {
49 if ( $db ) {
50 $this->db = $db;
51 } else {
52 $this->db = wfGetDB( DB_SLAVE );
56 /**
57 * Perform a full text search query and return a result set.
58 * If title searches are not supported or disabled, return null.
59 * STUB
61 * @param string $term raw search term
62 * @return SearchResultSet|Status|null
64 function searchText( $term ) {
65 return null;
68 /**
69 * Perform a title-only search query and return a result set.
70 * If title searches are not supported or disabled, return null.
71 * STUB
73 * @param string $term raw search term
74 * @return SearchResultSet|null
76 function searchTitle( $term ) {
77 return null;
80 /**
81 * If this search backend can list/unlist redirects
82 * @deprecated since 1.18 Call supports( 'list-redirects' );
83 * @return bool
85 function acceptListRedirects() {
86 wfDeprecated( __METHOD__, '1.18' );
87 return $this->supports( 'list-redirects' );
90 /**
91 * @since 1.18
92 * @param $feature String
93 * @return Boolean
95 public function supports( $feature ) {
96 switch ( $feature ) {
97 case 'list-redirects':
98 return true;
99 case 'title-suffix-filter':
100 default:
101 return false;
106 * Way to pass custom data for engines
107 * @since 1.18
108 * @param $feature String
109 * @param $data Mixed
110 * @return bool
112 public function setFeatureData( $feature, $data ) {
113 $this->features[$feature] = $data;
117 * When overridden in derived class, performs database-specific conversions
118 * on text to be used for searching or updating search index.
119 * Default implementation does nothing (simply returns $string).
121 * @param string $string String to process
122 * @return string
124 public function normalizeText( $string ) {
125 global $wgContLang;
127 // Some languages such as Chinese require word segmentation
128 return $wgContLang->segmentByWord( $string );
132 * Transform search term in cases when parts of the query came as different GET params (when supported)
133 * e.g. for prefix queries: search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive
135 function transformSearchTerm( $term ) {
136 return $term;
140 * If an exact title match can be found, or a very slightly close match,
141 * return the title. If no match, returns NULL.
143 * @param $searchterm String
144 * @return Title
146 public static function getNearMatch( $searchterm ) {
147 $title = self::getNearMatchInternal( $searchterm );
149 wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
150 return $title;
154 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
155 * SearchResultSet.
157 * @param $searchterm string
158 * @return SearchResultSet
160 public static function getNearMatchResultSet( $searchterm ) {
161 return new SearchNearMatchResultSet( self::getNearMatch( $searchterm ) );
165 * Really find the title match.
166 * @return null|Title
168 private static function getNearMatchInternal( $searchterm ) {
169 global $wgContLang, $wgEnableSearchContributorsByIP;
171 $allSearchTerms = array( $searchterm );
173 if ( $wgContLang->hasVariants() ) {
174 $allSearchTerms = array_merge( $allSearchTerms, $wgContLang->autoConvertToAllVariants( $searchterm ) );
177 $titleResult = null;
178 if ( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
179 return $titleResult;
182 foreach ( $allSearchTerms as $term ) {
184 # Exact match? No need to look further.
185 $title = Title::newFromText( $term );
186 if ( is_null( $title ) ) {
187 return null;
190 # Try files if searching in the Media: namespace
191 if ( $title->getNamespace() == NS_MEDIA ) {
192 $title = Title::makeTitle( NS_FILE, $title->getText() );
195 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
196 return $title;
199 # See if it still otherwise has content is some sane sense
200 $page = WikiPage::factory( $title );
201 if ( $page->hasViewableContent() ) {
202 return $title;
205 if ( !wfRunHooks( 'SearchAfterNoDirectMatch', array( $term, &$title ) ) ) {
206 return $title;
209 # Now try all lower case (i.e. first letter capitalized)
210 $title = Title::newFromText( $wgContLang->lc( $term ) );
211 if ( $title && $title->exists() ) {
212 return $title;
215 # Now try capitalized string
216 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
217 if ( $title && $title->exists() ) {
218 return $title;
221 # Now try all upper case
222 $title = Title::newFromText( $wgContLang->uc( $term ) );
223 if ( $title && $title->exists() ) {
224 return $title;
227 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
228 $title = Title::newFromText( $wgContLang->ucwordbreaks( $term ) );
229 if ( $title && $title->exists() ) {
230 return $title;
233 // Give hooks a chance at better match variants
234 $title = null;
235 if ( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
236 return $title;
240 $title = Title::newFromText( $searchterm );
242 # Entering an IP address goes to the contributions page
243 if ( $wgEnableSearchContributorsByIP ) {
244 if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
245 || User::isIP( trim( $searchterm ) ) ) {
246 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
250 # Entering a user goes to the user page whether it's there or not
251 if ( $title->getNamespace() == NS_USER ) {
252 return $title;
255 # Go to images that exist even if there's no local page.
256 # There may have been a funny upload, or it may be on a shared
257 # file repository such as Wikimedia Commons.
258 if ( $title->getNamespace() == NS_FILE ) {
259 $image = wfFindFile( $title );
260 if ( $image ) {
261 return $title;
265 # MediaWiki namespace? Page may be "implied" if not customized.
266 # Just return it, with caps forced as the message system likes it.
267 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
268 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
271 # Quoted term? Try without the quotes...
272 $matches = array();
273 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
274 return SearchEngine::getNearMatch( $matches[1] );
277 return null;
280 public static function legalSearchChars() {
281 return "A-Za-z_'.0-9\\x80-\\xFF\\-";
285 * Set the maximum number of results to return
286 * and how many to skip before returning the first.
288 * @param $limit Integer
289 * @param $offset Integer
291 function setLimitOffset( $limit, $offset = 0 ) {
292 $this->limit = intval( $limit );
293 $this->offset = intval( $offset );
297 * Set which namespaces the search should include.
298 * Give an array of namespace index numbers.
300 * @param $namespaces Array
302 function setNamespaces( $namespaces ) {
303 $this->namespaces = $namespaces;
307 * Parse some common prefixes: all (search everything)
308 * or namespace names
310 * @param $query String
311 * @return string
313 function replacePrefixes( $query ) {
314 global $wgContLang;
316 $parsed = $query;
317 if ( strpos( $query, ':' ) === false ) { // nothing to do
318 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
319 return $parsed;
322 $allkeyword = wfMessage( 'searchall' )->inContentLanguage()->text() . ":";
323 if ( strncmp( $query, $allkeyword, strlen( $allkeyword ) ) == 0 ) {
324 $this->namespaces = null;
325 $parsed = substr( $query, strlen( $allkeyword ) );
326 } elseif ( strpos( $query, ':' ) !== false ) {
327 $prefix = substr( $query, 0, strpos( $query, ':' ) );
328 $index = $wgContLang->getNsIndex( $prefix );
329 if ( $index !== false ) {
330 $this->namespaces = array( $index );
331 $parsed = substr( $query, strlen( $prefix ) + 1 );
334 if ( trim( $parsed ) == '' ) {
335 $parsed = $query; // prefix was the whole query
338 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
340 return $parsed;
344 * Make a list of searchable namespaces and their canonical names.
345 * @return Array
347 public static function searchableNamespaces() {
348 global $wgContLang;
349 $arr = array();
350 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
351 if ( $ns >= NS_MAIN ) {
352 $arr[$ns] = $name;
356 wfRunHooks( 'SearchableNamespaces', array( &$arr ) );
357 return $arr;
361 * Extract default namespaces to search from the given user's
362 * settings, returning a list of index numbers.
364 * @param $user User
365 * @return Array
367 public static function userNamespaces( $user ) {
368 global $wgSearchEverythingOnlyLoggedIn;
370 $searchableNamespaces = SearchEngine::searchableNamespaces();
372 // get search everything preference, that can be set to be read for logged-in users
373 // it overrides other options
374 if ( !$wgSearchEverythingOnlyLoggedIn || $user->isLoggedIn() ) {
375 if ( $user->getOption( 'searcheverything' ) ) {
376 return array_keys( $searchableNamespaces );
380 $arr = array();
381 foreach ( $searchableNamespaces as $ns => $name ) {
382 if ( $user->getOption( 'searchNs' . $ns ) ) {
383 $arr[] = $ns;
387 return $arr;
391 * Find snippet highlight settings for all users
393 * @return Array contextlines, contextchars
395 public static function userHighlightPrefs() {
396 $contextlines = 2; // Hardcode this. Old defaults sucked. :)
397 $contextchars = 75; // same as above.... :P
398 return array( $contextlines, $contextchars );
402 * An array of namespaces indexes to be searched by default
404 * @return Array
406 public static function defaultNamespaces() {
407 global $wgNamespacesToBeSearchedDefault;
409 return array_keys( $wgNamespacesToBeSearchedDefault, true );
413 * Get a list of namespace names useful for showing in tooltips
414 * and preferences
416 * @param $namespaces Array
417 * @return array
419 public static function namespacesAsText( $namespaces ) {
420 global $wgContLang;
422 $formatted = array_map( array( $wgContLang, 'getFormattedNsText' ), $namespaces );
423 foreach ( $formatted as $key => $ns ) {
424 if ( empty( $ns ) ) {
425 $formatted[$key] = wfMessage( 'blanknamespace' )->text();
428 return $formatted;
432 * Return the help namespaces to be shown on Special:Search
434 * @return Array
436 public static function helpNamespaces() {
437 global $wgNamespacesToBeSearchedHelp;
439 return array_keys( $wgNamespacesToBeSearchedHelp, true );
443 * Return a 'cleaned up' search string
445 * @param $text String
446 * @return String
448 function filter( $text ) {
449 $lc = $this->legalSearchChars();
450 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
453 * Load up the appropriate search engine class for the currently
454 * active database backend, and return a configured instance.
456 * @return SearchEngine
458 public static function create() {
459 global $wgSearchType;
460 $dbr = null;
461 if ( $wgSearchType ) {
462 $class = $wgSearchType;
463 } else {
464 $dbr = wfGetDB( DB_SLAVE );
465 $class = $dbr->getSearchEngine();
467 $search = new $class( $dbr );
468 $search->setLimitOffset( 0, 0 );
469 return $search;
473 * Create or update the search index record for the given page.
474 * Title and text should be pre-processed.
475 * STUB
477 * @param $id Integer
478 * @param $title String
479 * @param $text String
481 function update( $id, $title, $text ) {
482 // no-op
486 * Update a search index record's title only.
487 * Title should be pre-processed.
488 * STUB
490 * @param $id Integer
491 * @param $title String
493 function updateTitle( $id, $title ) {
494 // no-op
498 * Delete an indexed page
499 * Title should be pre-processed.
500 * STUB
502 * @param Integer $id Page id that was deleted
503 * @param String $title Title of page that was deleted
505 function delete( $id, $title ) {
506 // no-op
510 * Get OpenSearch suggestion template
512 * @return String
514 public static function getOpenSearchTemplate() {
515 global $wgOpenSearchTemplate, $wgCanonicalServer;
516 if ( $wgOpenSearchTemplate ) {
517 return $wgOpenSearchTemplate;
518 } else {
519 $ns = implode( '|', SearchEngine::defaultNamespaces() );
520 if ( !$ns ) {
521 $ns = "0";
523 return $wgCanonicalServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
528 * Get the raw text for updating the index from a content object
529 * Nicer search backends could possibly do something cooler than
530 * just returning raw text
532 * @todo This isn't ideal, we'd really like to have content-specific handling here
533 * @param Title $t Title we're indexing
534 * @param Content $c Content of the page to index
535 * @return string
537 public function getTextFromContent( Title $t, Content $c = null ) {
538 return $c ? $c->getTextForSearchIndex() : '';
543 * @ingroup Search
545 class SearchResultSet {
547 * Fetch an array of regular expression fragments for matching
548 * the search terms as parsed by this engine in a text extract.
549 * STUB
551 * @return Array
553 function termMatches() {
554 return array();
557 function numRows() {
558 return 0;
562 * Return true if results are included in this result set.
563 * STUB
565 * @return Boolean
567 function hasResults() {
568 return false;
572 * Some search modes return a total hit count for the query
573 * in the entire article database. This may include pages
574 * in namespaces that would not be matched on the given
575 * settings.
577 * Return null if no total hits number is supported.
579 * @return Integer
581 function getTotalHits() {
582 return null;
586 * Some search modes return a suggested alternate term if there are
587 * no exact hits. Returns true if there is one on this set.
589 * @return Boolean
591 function hasSuggestion() {
592 return false;
596 * @return String: suggested query, null if none
598 function getSuggestionQuery() {
599 return null;
603 * @return String: HTML highlighted suggested query, '' if none
605 function getSuggestionSnippet() {
606 return '';
610 * Return information about how and from where the results were fetched,
611 * should be useful for diagnostics and debugging
613 * @return String
615 function getInfo() {
616 return null;
620 * Return a result set of hits on other (multiple) wikis associated with this one
622 * @return SearchResultSet
624 function getInterwikiResults() {
625 return null;
629 * Check if there are results on other wikis
631 * @return Boolean
633 function hasInterwikiResults() {
634 return $this->getInterwikiResults() != null;
638 * Fetches next search result, or false.
639 * STUB
641 * @return SearchResult
643 function next() {
644 return false;
648 * Frees the result set, if applicable.
650 function free() {
651 // ...
656 * This class is used for different SQL-based search engines shipped with MediaWiki
658 class SqlSearchResultSet extends SearchResultSet {
660 protected $mResultSet;
662 function __construct( $resultSet, $terms ) {
663 $this->mResultSet = $resultSet;
664 $this->mTerms = $terms;
667 function termMatches() {
668 return $this->mTerms;
671 function numRows() {
672 if ( $this->mResultSet === false ) {
673 return false;
676 return $this->mResultSet->numRows();
679 function next() {
680 if ( $this->mResultSet === false ) {
681 return false;
684 $row = $this->mResultSet->fetchObject();
685 if ( $row === false ) {
686 return false;
689 return SearchResult::newFromRow( $row );
692 function free() {
693 if ( $this->mResultSet === false ) {
694 return false;
697 $this->mResultSet->free();
702 * @ingroup Search
704 class SearchResultTooMany {
705 # # Some search engines may bail out if too many matches are found
709 * @todo FIXME: This class is horribly factored. It would probably be better to
710 * have a useful base class to which you pass some standard information, then
711 * let the fancy self-highlighters extend that.
712 * @ingroup Search
714 class SearchResult {
717 * @var Revision
719 var $mRevision = null;
720 var $mImage = null;
723 * @var Title
725 var $mTitle;
728 * @var String
730 var $mText;
733 * Return a new SearchResult and initializes it with a title.
735 * @param $title Title
736 * @return SearchResult
738 public static function newFromTitle( $title ) {
739 $result = new self();
740 $result->initFromTitle( $title );
741 return $result;
744 * Return a new SearchResult and initializes it with a row.
746 * @param $row object
747 * @return SearchResult
749 public static function newFromRow( $row ) {
750 $result = new self();
751 $result->initFromRow( $row );
752 return $result;
755 public function __construct( $row = null ) {
756 if ( !is_null( $row ) ) {
757 // Backwards compatibility with pre-1.17 callers
758 $this->initFromRow( $row );
763 * Initialize from a database row. Makes a Title and passes that to
764 * initFromTitle.
766 * @param $row object
768 protected function initFromRow( $row ) {
769 $this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
773 * Initialize from a Title and if possible initializes a corresponding
774 * Revision and File.
776 * @param $title Title
778 protected function initFromTitle( $title ) {
779 $this->mTitle = $title;
780 if ( !is_null( $this->mTitle ) ) {
781 $id = false;
782 wfRunHooks( 'SearchResultInitFromTitle', array( $title, &$id ) );
783 $this->mRevision = Revision::newFromTitle(
784 $this->mTitle, $id, Revision::READ_NORMAL );
785 if ( $this->mTitle->getNamespace() === NS_FILE ) {
786 $this->mImage = wfFindFile( $this->mTitle );
792 * Check if this is result points to an invalid title
794 * @return Boolean
796 function isBrokenTitle() {
797 if ( is_null( $this->mTitle ) ) {
798 return true;
800 return false;
804 * Check if target page is missing, happens when index is out of date
806 * @return Boolean
808 function isMissingRevision() {
809 return !$this->mRevision && !$this->mImage;
813 * @return Title
815 function getTitle() {
816 return $this->mTitle;
820 * @return float|null if not supported
822 function getScore() {
823 return null;
827 * Lazy initialization of article text from DB
829 protected function initText() {
830 if ( !isset( $this->mText ) ) {
831 if ( $this->mRevision != null ) {
832 $this->mText = SearchEngine::create()
833 ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() );
834 } else { // TODO: can we fetch raw wikitext for commons images?
835 $this->mText = '';
841 * @param array $terms terms to highlight
842 * @return String: highlighted text snippet, null (and not '') if not supported
844 function getTextSnippet( $terms ) {
845 global $wgUser, $wgAdvancedSearchHighlighting;
846 $this->initText();
848 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
849 list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs( $wgUser );
850 $h = new SearchHighlighter();
851 if ( $wgAdvancedSearchHighlighting ) {
852 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
853 } else {
854 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
859 * @param array $terms terms to highlight
860 * @return String: highlighted title, '' if not supported
862 function getTitleSnippet( $terms ) {
863 return '';
867 * @param array $terms terms to highlight
868 * @return String: highlighted redirect name (redirect to this page), '' if none or not supported
870 function getRedirectSnippet( $terms ) {
871 return '';
875 * @return Title object for the redirect to this page, null if none or not supported
877 function getRedirectTitle() {
878 return null;
882 * @return string highlighted relevant section name, null if none or not supported
884 function getSectionSnippet() {
885 return '';
889 * @return Title object (pagename+fragment) for the section, null if none or not supported
891 function getSectionTitle() {
892 return null;
896 * @return String: timestamp
898 function getTimestamp() {
899 if ( $this->mRevision ) {
900 return $this->mRevision->getTimestamp();
901 } elseif ( $this->mImage ) {
902 return $this->mImage->getTimestamp();
904 return '';
908 * @return Integer: number of words
910 function getWordCount() {
911 $this->initText();
912 return str_word_count( $this->mText );
916 * @return Integer: size in bytes
918 function getByteSize() {
919 $this->initText();
920 return strlen( $this->mText );
924 * @return Boolean if hit has related articles
926 function hasRelated() {
927 return false;
931 * @return String: interwiki prefix of the title (return iw even if title is broken)
933 function getInterwikiPrefix() {
934 return '';
938 * A SearchResultSet wrapper for SearchEngine::getNearMatch
940 class SearchNearMatchResultSet extends SearchResultSet {
941 private $fetched = false;
943 * @param $match mixed Title if matched, else null
945 public function __construct( $match ) {
946 $this->result = $match;
948 public function hasResult() {
949 return (bool)$this->result;
951 public function numRows() {
952 return $this->hasResults() ? 1 : 0;
954 public function next() {
955 if ( $this->fetched || !$this->result ) {
956 return false;
958 $this->fetched = true;
959 return SearchResult::newFromTitle( $this->result );
964 * Highlight bits of wikitext
966 * @ingroup Search
968 class SearchHighlighter {
969 var $mCleanWikitext = true;
971 function __construct( $cleanupWikitext = true ) {
972 $this->mCleanWikitext = $cleanupWikitext;
976 * Default implementation of wikitext highlighting
978 * @param $text String
979 * @param array $terms terms to highlight (unescaped)
980 * @param $contextlines Integer
981 * @param $contextchars Integer
982 * @return String
984 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
985 global $wgContLang;
986 global $wgSearchHighlightBoundaries;
987 $fname = __METHOD__;
989 if ( $text == '' ) {
990 return '';
993 // spli text into text + templates/links/tables
994 $spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
995 // first capture group is for detecting nested templates/links/tables/references
996 $endPatterns = array(
997 1 => '/(\{\{)|(\}\})/', // template
998 2 => '/(\[\[)|(\]\])/', // image
999 3 => "/(\n\\{\\|)|(\n\\|\\})/" ); // table
1001 // @todo FIXME: This should prolly be a hook or something
1002 if ( function_exists( 'wfCite' ) ) {
1003 $spat .= '|(<ref>)'; // references via cite extension
1004 $endPatterns[4] = '/(<ref>)|(<\/ref>)/';
1006 $spat .= '/';
1007 $textExt = array(); // text extracts
1008 $otherExt = array(); // other extracts
1009 wfProfileIn( "$fname-split" );
1010 $start = 0;
1011 $textLen = strlen( $text );
1012 $count = 0; // sequence number to maintain ordering
1013 while ( $start < $textLen ) {
1014 // find start of template/image/table
1015 if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE, $start ) ) {
1016 $epat = '';
1017 foreach ( $matches as $key => $val ) {
1018 if ( $key > 0 && $val[1] != - 1 ) {
1019 if ( $key == 2 ) {
1020 // see if this is an image link
1021 $ns = substr( $val[0], 2, - 1 );
1022 if ( $wgContLang->getNsIndex( $ns ) != NS_FILE ) {
1023 break;
1027 $epat = $endPatterns[$key];
1028 $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
1029 $start = $val[1];
1030 break;
1033 if ( $epat ) {
1034 // find end (and detect any nested elements)
1035 $level = 0;
1036 $offset = $start + 1;
1037 $found = false;
1038 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
1039 if ( array_key_exists( 2, $endMatches ) ) {
1040 // found end
1041 if ( $level == 0 ) {
1042 $len = strlen( $endMatches[2][0] );
1043 $off = $endMatches[2][1];
1044 $this->splitAndAdd( $otherExt, $count,
1045 substr( $text, $start, $off + $len - $start ) );
1046 $start = $off + $len;
1047 $found = true;
1048 break;
1049 } else {
1050 // end of nested element
1051 $level -= 1;
1053 } else {
1054 // nested
1055 $level += 1;
1057 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
1059 if ( ! $found ) {
1060 // couldn't find appropriate closing tag, skip
1061 $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen( $matches[0][0] ) ) );
1062 $start += strlen( $matches[0][0] );
1064 continue;
1067 // else: add as text extract
1068 $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
1069 break;
1072 $all = $textExt + $otherExt; // these have disjunct key sets
1074 wfProfileOut( "$fname-split" );
1076 // prepare regexps
1077 foreach ( $terms as $index => $term ) {
1078 // manually do upper/lowercase stuff for utf-8 since PHP won't do it
1079 if ( preg_match( '/[\x80-\xff]/', $term ) ) {
1080 $terms[$index] = preg_replace_callback( '/./us', array( $this, 'caseCallback' ), $terms[$index] );
1081 } else {
1082 $terms[$index] = $term;
1085 $anyterm = implode( '|', $terms );
1086 $phrase = implode( "$wgSearchHighlightBoundaries+", $terms );
1088 // @todo FIXME: A hack to scale contextchars, a correct solution
1089 // would be to have contextchars actually be char and not byte
1090 // length, and do proper utf-8 substrings and lengths everywhere,
1091 // but PHP is making that very hard and unclean to implement :(
1092 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
1093 $contextchars = intval( $contextchars * $scale );
1095 $patPre = "(^|$wgSearchHighlightBoundaries)";
1096 $patPost = "($wgSearchHighlightBoundaries|$)";
1098 $pat1 = "/(" . $phrase . ")/ui";
1099 $pat2 = "/$patPre(" . $anyterm . ")$patPost/ui";
1101 wfProfileIn( "$fname-extract" );
1103 $left = $contextlines;
1105 $snippets = array();
1106 $offsets = array();
1108 // show beginning only if it contains all words
1109 $first = 0;
1110 $firstText = '';
1111 foreach ( $textExt as $index => $line ) {
1112 if ( strlen( $line ) > 0 && $line[0] != ';' && $line[0] != ':' ) {
1113 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
1114 $first = $index;
1115 break;
1118 if ( $firstText ) {
1119 $succ = true;
1120 // check if first text contains all terms
1121 foreach ( $terms as $term ) {
1122 if ( ! preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) {
1123 $succ = false;
1124 break;
1127 if ( $succ ) {
1128 $snippets[$first] = $firstText;
1129 $offsets[$first] = 0;
1132 if ( ! $snippets ) {
1133 // match whole query on text
1134 $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
1135 // match whole query on templates/tables/images
1136 $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
1137 // match any words on text
1138 $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
1139 // match any words on templates/tables/images
1140 $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
1142 ksort( $snippets );
1145 // add extra chars to each snippet to make snippets constant size
1146 $extended = array();
1147 if ( count( $snippets ) == 0 ) {
1148 // couldn't find the target words, just show beginning of article
1149 if ( array_key_exists( $first, $all ) ) {
1150 $targetchars = $contextchars * $contextlines;
1151 $snippets[$first] = '';
1152 $offsets[$first] = 0;
1154 } else {
1155 // if begin of the article contains the whole phrase, show only that !!
1156 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
1157 && $offsets[$first] < $contextchars * 2 ) {
1158 $snippets = array( $first => $snippets[$first] );
1161 // calc by how much to extend existing snippets
1162 $targetchars = intval( ( $contextchars * $contextlines ) / count ( $snippets ) );
1165 foreach ( $snippets as $index => $line ) {
1166 $extended[$index] = $line;
1167 $len = strlen( $line );
1168 if ( $len < $targetchars - 20 ) {
1169 // complete this line
1170 if ( $len < strlen( $all[$index] ) ) {
1171 $extended[$index] = $this->extract( $all[$index], $offsets[$index], $offsets[$index] + $targetchars, $offsets[$index] );
1172 $len = strlen( $extended[$index] );
1175 // add more lines
1176 $add = $index + 1;
1177 while ( $len < $targetchars - 20
1178 && array_key_exists( $add, $all )
1179 && !array_key_exists( $add, $snippets ) ) {
1180 $offsets[$add] = 0;
1181 $tt = "\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
1182 $extended[$add] = $tt;
1183 $len += strlen( $tt );
1184 $add++;
1189 // $snippets = array_map( 'htmlspecialchars', $extended );
1190 $snippets = $extended;
1191 $last = - 1;
1192 $extract = '';
1193 foreach ( $snippets as $index => $line ) {
1194 if ( $last == - 1 ) {
1195 $extract .= $line; // first line
1196 } elseif ( $last + 1 == $index && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] ) ) {
1197 $extract .= " " . $line; // continous lines
1198 } else {
1199 $extract .= '<b> ... </b>' . $line;
1202 $last = $index;
1204 if ( $extract ) {
1205 $extract .= '<b> ... </b>';
1208 $processed = array();
1209 foreach ( $terms as $term ) {
1210 if ( ! isset( $processed[$term] ) ) {
1211 $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
1212 $extract = preg_replace( $pat3,
1213 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
1214 $processed[$term] = true;
1218 wfProfileOut( "$fname-extract" );
1220 return $extract;
1224 * Split text into lines and add it to extracts array
1226 * @param array $extracts index -> $line
1227 * @param $count Integer
1228 * @param $text String
1230 function splitAndAdd( &$extracts, &$count, $text ) {
1231 $split = explode( "\n", $this->mCleanWikitext ? $this->removeWiki( $text ) : $text );
1232 foreach ( $split as $line ) {
1233 $tt = trim( $line );
1234 if ( $tt ) {
1235 $extracts[$count++] = $tt;
1241 * Do manual case conversion for non-ascii chars
1243 * @param $matches Array
1244 * @return string
1246 function caseCallback( $matches ) {
1247 global $wgContLang;
1248 if ( strlen( $matches[0] ) > 1 ) {
1249 return '[' . $wgContLang->lc( $matches[0] ) . $wgContLang->uc( $matches[0] ) . ']';
1250 } else {
1251 return $matches[0];
1256 * Extract part of the text from start to end, but by
1257 * not chopping up words
1258 * @param $text String
1259 * @param $start Integer
1260 * @param $end Integer
1261 * @param $posStart Integer: (out) actual start position
1262 * @param $posEnd Integer: (out) actual end position
1263 * @return String
1265 function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) {
1266 if ( $start != 0 ) {
1267 $start = $this->position( $text, $start, 1 );
1269 if ( $end >= strlen( $text ) ) {
1270 $end = strlen( $text );
1271 } else {
1272 $end = $this->position( $text, $end );
1275 if ( !is_null( $posStart ) ) {
1276 $posStart = $start;
1278 if ( !is_null( $posEnd ) ) {
1279 $posEnd = $end;
1282 if ( $end > $start ) {
1283 return substr( $text, $start, $end - $start );
1284 } else {
1285 return '';
1290 * Find a nonletter near a point (index) in the text
1292 * @param $text String
1293 * @param $point Integer
1294 * @param $offset Integer: offset to found index
1295 * @return Integer: nearest nonletter index, or beginning of utf8 char if none
1297 function position( $text, $point, $offset = 0 ) {
1298 $tolerance = 10;
1299 $s = max( 0, $point - $tolerance );
1300 $l = min( strlen( $text ), $point + $tolerance ) - $s;
1301 $m = array();
1302 if ( preg_match( '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', substr( $text, $s, $l ), $m, PREG_OFFSET_CAPTURE ) ) {
1303 return $m[0][1] + $s + $offset;
1304 } else {
1305 // check if point is on a valid first UTF8 char
1306 $char = ord( $text[$point] );
1307 while ( $char >= 0x80 && $char < 0xc0 ) {
1308 // skip trailing bytes
1309 $point++;
1310 if ( $point >= strlen( $text ) ) {
1311 return strlen( $text );
1313 $char = ord( $text[$point] );
1315 return $point;
1321 * Search extracts for a pattern, and return snippets
1323 * @param string $pattern regexp for matching lines
1324 * @param array $extracts extracts to search
1325 * @param $linesleft Integer: number of extracts to make
1326 * @param $contextchars Integer: length of snippet
1327 * @param array $out map for highlighted snippets
1328 * @param array $offsets map of starting points of snippets
1329 * @protected
1331 function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ) {
1332 if ( $linesleft == 0 ) {
1333 return; // nothing to do
1335 foreach ( $extracts as $index => $line ) {
1336 if ( array_key_exists( $index, $out ) ) {
1337 continue; // this line already highlighted
1340 $m = array();
1341 if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) {
1342 continue;
1345 $offset = $m[0][1];
1346 $len = strlen( $m[0][0] );
1347 if ( $offset + $len < $contextchars ) {
1348 $begin = 0;
1349 } elseif ( $len > $contextchars ) {
1350 $begin = $offset;
1351 } else {
1352 $begin = $offset + intval( ( $len - $contextchars ) / 2 );
1355 $end = $begin + $contextchars;
1357 $posBegin = $begin;
1358 // basic snippet from this line
1359 $out[$index] = $this->extract( $line, $begin, $end, $posBegin );
1360 $offsets[$index] = $posBegin;
1361 $linesleft--;
1362 if ( $linesleft == 0 ) {
1363 return;
1369 * Basic wikitext removal
1370 * @protected
1371 * @return mixed
1373 function removeWiki( $text ) {
1374 $fname = __METHOD__;
1375 wfProfileIn( $fname );
1377 // $text = preg_replace( "/'{2,5}/", "", $text );
1378 // $text = preg_replace( "/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text );
1379 // $text = preg_replace( "/\[\[([^]|]+)\]\]/", "\\1", $text );
1380 // $text = preg_replace( "/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text );
1381 // $text = preg_replace( "/\\{\\|(.*?)\\|\\}/", "", $text );
1382 // $text = preg_replace( "/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text );
1383 $text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text );
1384 $text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text );
1385 $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text );
1386 $text = preg_replace_callback( "/\\[\\[([^|]+\\|)(.*?)\\]\\]/", array( $this, 'linkReplace' ), $text );
1387 // $text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text);
1388 $text = preg_replace( "/<\/?[^>]+>/", "", $text );
1389 $text = preg_replace( "/'''''/", "", $text );
1390 $text = preg_replace( "/('''|<\/?[iIuUbB]>)/", "", $text );
1391 $text = preg_replace( "/''/", "", $text );
1393 wfProfileOut( $fname );
1394 return $text;
1398 * callback to replace [[target|caption]] kind of links, if
1399 * the target is category or image, leave it
1401 * @param $matches Array
1403 function linkReplace( $matches ) {
1404 $colon = strpos( $matches[1], ':' );
1405 if ( $colon === false ) {
1406 return $matches[2]; // replace with caption
1408 global $wgContLang;
1409 $ns = substr( $matches[1], 0, $colon );
1410 $index = $wgContLang->getNsIndex( $ns );
1411 if ( $index !== false && ( $index == NS_FILE || $index == NS_CATEGORY ) ) {
1412 return $matches[0]; // return the whole thing
1413 } else {
1414 return $matches[2];
1419 * Simple & fast snippet extraction, but gives completely unrelevant
1420 * snippets
1422 * @param $text String
1423 * @param $terms Array
1424 * @param $contextlines Integer
1425 * @param $contextchars Integer
1426 * @return String
1428 public function highlightSimple( $text, $terms, $contextlines, $contextchars ) {
1429 global $wgContLang;
1430 $fname = __METHOD__;
1432 $lines = explode( "\n", $text );
1434 $terms = implode( '|', $terms );
1435 $max = intval( $contextchars ) + 1;
1436 $pat1 = "/(.*)($terms)(.{0,$max})/i";
1438 $lineno = 0;
1440 $extract = "";
1441 wfProfileIn( "$fname-extract" );
1442 foreach ( $lines as $line ) {
1443 if ( 0 == $contextlines ) {
1444 break;
1446 ++$lineno;
1447 $m = array();
1448 if ( ! preg_match( $pat1, $line, $m ) ) {
1449 continue;
1451 --$contextlines;
1452 // truncate function changes ... to relevant i18n message.
1453 $pre = $wgContLang->truncate( $m[1], - $contextchars, '...', false );
1455 if ( count( $m ) < 3 ) {
1456 $post = '';
1457 } else {
1458 $post = $wgContLang->truncate( $m[3], $contextchars, '...', false );
1461 $found = $m[2];
1463 $line = htmlspecialchars( $pre . $found . $post );
1464 $pat2 = '/(' . $terms . ")/i";
1465 $line = preg_replace( $pat2, "<span class='searchmatch'>\\1</span>", $line );
1467 $extract .= "${line}\n";
1469 wfProfileOut( "$fname-extract" );
1471 return $extract;
1477 * Dummy class to be used when non-supported Database engine is present.
1478 * @todo FIXME: Dummy class should probably try something at least mildly useful,
1479 * such as a LIKE search through titles.
1480 * @ingroup Search
1482 class SearchEngineDummy extends SearchEngine {
1483 // no-op