3 * @defgroup Search Search
10 * Contain a class for special pages
17 var $searchTerms = array();
18 var $namespaces = array( NS_MAIN
);
19 var $showRedirects = false;
22 * Perform a full text search query and return a result set.
23 * If title searches are not supported or disabled, return null.
26 * @param $term String: raw search term
27 * @return SearchResultSet
29 function searchText( $term ) {
34 * Perform a title-only search query and return a result set.
35 * If title searches are not supported or disabled, return null.
38 * @param $term String: raw search term
39 * @return SearchResultSet
41 function searchTitle( $term ) {
45 /** If this search backend can list/unlist redirects */
46 function acceptListRedirects() {
51 * When overridden in derived class, performs database-specific conversions
52 * on text to be used for searching or updating search index.
53 * Default implementation does nothing (simply returns $string).
55 * @param $string string: String to process
58 public function normalizeText( $string ) {
63 * Transform search term in cases when parts of the query came as different GET params (when supported)
64 * e.g. for prefix queries: search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive
66 function transformSearchTerm( $term ) {
71 * If an exact title match can be found, or a very slightly close match,
72 * return the title. If no match, returns NULL.
74 * @param $searchterm String
77 public static function getNearMatch( $searchterm ) {
78 $title = self
::getNearMatchInternal( $searchterm );
80 wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
85 * Really find the title match.
87 private static function getNearMatchInternal( $searchterm ) {
90 $allSearchTerms = array($searchterm);
92 if ( $wgContLang->hasVariants() ) {
93 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
96 if( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
100 foreach($allSearchTerms as $term) {
102 # Exact match? No need to look further.
103 $title = Title
::newFromText( $term );
107 if ( $title->getNamespace() == NS_SPECIAL ||
$title->isExternal() ||
$title->exists() ) {
111 # See if it still otherwise has content is some sane sense
112 $article = MediaWiki
::articleFromTitle( $title );
113 if( $article->hasViewableContent() ) {
117 # Now try all lower case (i.e. first letter capitalized)
119 $title = Title
::newFromText( $wgContLang->lc( $term ) );
120 if ( $title && $title->exists() ) {
124 # Now try capitalized string
126 $title = Title
::newFromText( $wgContLang->ucwords( $term ) );
127 if ( $title && $title->exists() ) {
131 # Now try all upper case
133 $title = Title
::newFromText( $wgContLang->uc( $term ) );
134 if ( $title && $title->exists() ) {
138 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
139 $title = Title
::newFromText( $wgContLang->ucwordbreaks($term) );
140 if ( $title && $title->exists() ) {
144 // Give hooks a chance at better match variants
146 if( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
151 $title = Title
::newFromText( $searchterm );
153 # Entering an IP address goes to the contributions page
154 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP($title->getText() ) )
155 || User
::isIP( trim( $searchterm ) ) ) {
156 return SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() );
160 # Entering a user goes to the user page whether it's there or not
161 if ( $title->getNamespace() == NS_USER
) {
165 # Go to images that exist even if there's no local page.
166 # There may have been a funny upload, or it may be on a shared
167 # file repository such as Wikimedia Commons.
168 if( $title->getNamespace() == NS_FILE
) {
169 $image = wfFindFile( $title );
175 # MediaWiki namespace? Page may be "implied" if not customized.
176 # Just return it, with caps forced as the message system likes it.
177 if( $title->getNamespace() == NS_MEDIAWIKI
) {
178 return Title
::makeTitle( NS_MEDIAWIKI
, $wgContLang->ucfirst( $title->getText() ) );
181 # Quoted term? Try without the quotes...
183 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
184 return SearchEngine
::getNearMatch( $matches[1] );
190 public static function legalSearchChars() {
191 return "A-Za-z_'.0-9\\x80-\\xFF\\-";
195 * Set the maximum number of results to return
196 * and how many to skip before returning the first.
198 * @param $limit Integer
199 * @param $offset Integer
201 function setLimitOffset( $limit, $offset = 0 ) {
202 $this->limit
= intval( $limit );
203 $this->offset
= intval( $offset );
207 * Set which namespaces the search should include.
208 * Give an array of namespace index numbers.
210 * @param $namespaces Array
212 function setNamespaces( $namespaces ) {
213 $this->namespaces
= $namespaces;
217 * Parse some common prefixes: all (search everything)
220 * @param $query String
222 function replacePrefixes( $query ){
226 if( strpos($query,':') === false ) { // nothing to do
227 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
231 $allkeyword = wfMsgForContent('searchall').":";
232 if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){
233 $this->namespaces
= null;
234 $parsed = substr($query,strlen($allkeyword));
235 } else if( strpos($query,':') !== false ) {
236 $prefix = substr($query,0,strpos($query,':'));
237 $index = $wgContLang->getNsIndex($prefix);
238 if($index !== false){
239 $this->namespaces
= array($index);
240 $parsed = substr($query,strlen($prefix)+
1);
243 if(trim($parsed) == '')
244 $parsed = $query; // prefix was the whole query
246 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
252 * Make a list of searchable namespaces and their canonical names.
255 public static function searchableNamespaces() {
258 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
259 if( $ns >= NS_MAIN
) {
264 wfRunHooks( 'SearchableNamespaces', array( &$arr ) );
269 * Extract default namespaces to search from the given user's
270 * settings, returning a list of index numbers.
275 public static function userNamespaces( $user ) {
276 global $wgSearchEverythingOnlyLoggedIn;
278 // get search everything preference, that can be set to be read for logged-in users
279 $searcheverything = false;
280 if( ( $wgSearchEverythingOnlyLoggedIn && $user->isLoggedIn() )
281 ||
!$wgSearchEverythingOnlyLoggedIn )
282 $searcheverything = $user->getOption('searcheverything');
284 // searcheverything overrides other options
285 if( $searcheverything )
286 return array_keys(SearchEngine
::searchableNamespaces());
288 $arr = Preferences
::loadOldSearchNs( $user );
289 $searchableNamespaces = SearchEngine
::searchableNamespaces();
291 $arr = array_intersect( $arr, array_keys($searchableNamespaces) ); // Filter
297 * Find snippet highlight settings for a given user
300 * @return Array contextlines, contextchars
302 public static function userHighlightPrefs( &$user ){
303 //$contextlines = $user->getOption( 'contextlines', 5 );
304 //$contextchars = $user->getOption( 'contextchars', 50 );
305 $contextlines = 2; // Hardcode this. Old defaults sucked. :)
306 $contextchars = 75; // same as above.... :P
307 return array($contextlines, $contextchars);
311 * An array of namespaces indexes to be searched by default
315 public static function defaultNamespaces(){
316 global $wgNamespacesToBeSearchedDefault;
318 return array_keys($wgNamespacesToBeSearchedDefault, true);
322 * Get a list of namespace names useful for showing in tooltips
325 * @param $namespaces Array
327 public static function namespacesAsText( $namespaces ){
330 $formatted = array_map( array($wgContLang,'getFormattedNsText'), $namespaces );
331 foreach( $formatted as $key => $ns ){
333 $formatted[$key] = wfMsg( 'blanknamespace' );
339 * Return the help namespaces to be shown on Special:Search
343 public static function helpNamespaces() {
344 global $wgNamespacesToBeSearchedHelp;
346 return array_keys( $wgNamespacesToBeSearchedHelp, true );
350 * Return a 'cleaned up' search string
352 * @param $text String
355 function filter( $text ) {
356 $lc = $this->legalSearchChars();
357 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
360 * Load up the appropriate search engine class for the currently
361 * active database backend, and return a configured instance.
363 * @return SearchEngine
365 public static function create() {
366 global $wgSearchType;
367 $dbr = wfGetDB( DB_SLAVE
);
368 if( $wgSearchType ) {
369 $class = $wgSearchType;
371 $class = $dbr->getSearchEngine();
373 $search = new $class( $dbr );
374 $search->setLimitOffset(0,0);
379 * Create or update the search index record for the given page.
380 * Title and text should be pre-processed.
384 * @param $title String
385 * @param $text String
387 function update( $id, $title, $text ) {
392 * Update a search index record's title only.
393 * Title should be pre-processed.
397 * @param $title String
399 function updateTitle( $id, $title ) {
404 * Get OpenSearch suggestion template
408 public static function getOpenSearchTemplate() {
409 global $wgOpenSearchTemplate, $wgServer, $wgScriptPath;
410 if( $wgOpenSearchTemplate ) {
411 return $wgOpenSearchTemplate;
413 $ns = implode( '|', SearchEngine
::defaultNamespaces() );
414 if( !$ns ) $ns = "0";
415 return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace='.$ns;
420 * Get internal MediaWiki Suggest template
424 public static function getMWSuggestTemplate() {
425 global $wgMWSuggestTemplate, $wgServer, $wgScriptPath;
426 if($wgMWSuggestTemplate)
427 return $wgMWSuggestTemplate;
429 return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}&suggest';
436 class SearchResultSet
{
438 * Fetch an array of regular expression fragments for matching
439 * the search terms as parsed by this engine in a text extract.
444 function termMatches() {
453 * Return true if results are included in this result set.
458 function hasResults() {
463 * Some search modes return a total hit count for the query
464 * in the entire article database. This may include pages
465 * in namespaces that would not be matched on the given
468 * Return null if no total hits number is supported.
472 function getTotalHits() {
477 * Some search modes return a suggested alternate term if there are
478 * no exact hits. Returns true if there is one on this set.
482 function hasSuggestion() {
487 * @return String: suggested query, null if none
489 function getSuggestionQuery(){
494 * @return String: HTML highlighted suggested query, '' if none
496 function getSuggestionSnippet(){
501 * Return information about how and from where the results were fetched,
502 * should be useful for diagnostics and debugging
511 * Return a result set of hits on other (multiple) wikis associated with this one
513 * @return SearchResultSet
515 function getInterwikiResults() {
520 * Check if there are results on other wikis
524 function hasInterwikiResults() {
525 return $this->getInterwikiResults() != null;
529 * Fetches next search result, or false.
532 * @return SearchResult
539 * Frees the result set, if applicable.
547 * This class is used for different SQL-based search engines shipped with MediaWiki
549 class SqlSearchResultSet
extends SearchResultSet
{
550 function __construct( $resultSet, $terms ) {
551 $this->mResultSet
= $resultSet;
552 $this->mTerms
= $terms;
555 function termMatches() {
556 return $this->mTerms
;
560 if ($this->mResultSet
=== false )
563 return $this->mResultSet
->numRows();
567 if ($this->mResultSet
=== false )
570 $row = $this->mResultSet
->fetchObject();
573 return new SearchResult($row);
577 if ($this->mResultSet
=== false )
580 $this->mResultSet
->free();
587 class SearchResultTooMany
{
588 ## Some search engines may bail out if too many matches are found
593 * @todo Fixme: This class is horribly factored. It would probably be better to
594 * have a useful base class to which you pass some standard information, then
595 * let the fancy self-highlighters extend that.
599 var $mRevision = null;
602 function __construct( $row ) {
603 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
604 if( !is_null($this->mTitle
) ){
605 $this->mRevision
= Revision
::newFromTitle( $this->mTitle
);
606 if( $this->mTitle
->getNamespace() === NS_FILE
)
607 $this->mImage
= wfFindFile( $this->mTitle
);
612 * Check if this is result points to an invalid title
616 function isBrokenTitle(){
617 if( is_null($this->mTitle
) )
623 * Check if target page is missing, happens when index is out of date
627 function isMissingRevision(){
628 return !$this->mRevision
&& !$this->mImage
;
634 function getTitle() {
635 return $this->mTitle
;
639 * @return Double or null if not supported
641 function getScore() {
646 * Lazy initialization of article text from DB
648 protected function initText(){
649 if( !isset($this->mText
) ){
650 if($this->mRevision
!= null)
651 $this->mText
= $this->mRevision
->getText();
652 else // TODO: can we fetch raw wikitext for commons images?
659 * @param $terms Array: terms to highlight
660 * @return String: highlighted text snippet, null (and not '') if not supported
662 function getTextSnippet($terms){
663 global $wgUser, $wgAdvancedSearchHighlighting;
665 list($contextlines,$contextchars) = SearchEngine
::userHighlightPrefs($wgUser);
666 $h = new SearchHighlighter();
667 if( $wgAdvancedSearchHighlighting )
668 return $h->highlightText( $this->mText
, $terms, $contextlines, $contextchars );
670 return $h->highlightSimple( $this->mText
, $terms, $contextlines, $contextchars );
674 * @param $terms Array: terms to highlight
675 * @return String: highlighted title, '' if not supported
677 function getTitleSnippet($terms){
682 * @param $terms Array: terms to highlight
683 * @return String: highlighted redirect name (redirect to this page), '' if none or not supported
685 function getRedirectSnippet($terms){
690 * @return Title object for the redirect to this page, null if none or not supported
692 function getRedirectTitle(){
697 * @return string highlighted relevant section name, null if none or not supported
699 function getSectionSnippet(){
704 * @return Title object (pagename+fragment) for the section, null if none or not supported
706 function getSectionTitle(){
711 * @return String: timestamp
713 function getTimestamp(){
714 if( $this->mRevision
)
715 return $this->mRevision
->getTimestamp();
716 else if( $this->mImage
)
717 return $this->mImage
->getTimestamp();
722 * @return Integer: number of words
724 function getWordCount(){
726 return str_word_count( $this->mText
);
730 * @return Integer: size in bytes
732 function getByteSize(){
734 return strlen( $this->mText
);
738 * @return Boolean if hit has related articles
740 function hasRelated(){
745 * @return String: interwiki prefix of the title (return iw even if title is broken)
747 function getInterwikiPrefix(){
753 * Highlight bits of wikitext
757 class SearchHighlighter
{
758 var $mCleanWikitext = true;
760 function SearchHighlighter($cleanupWikitext = true){
761 $this->mCleanWikitext
= $cleanupWikitext;
765 * Default implementation of wikitext highlighting
767 * @param $text String
768 * @param $terms Array: terms to highlight (unescaped)
769 * @param $contextlines Integer
770 * @param $contextchars Integer
773 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
774 global $wgLang, $wgContLang;
775 global $wgSearchHighlightBoundaries;
781 // spli text into text + templates/links/tables
782 $spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
783 // first capture group is for detecting nested templates/links/tables/references
784 $endPatterns = array(
785 1 => '/(\{\{)|(\}\})/', // template
786 2 => '/(\[\[)|(\]\])/', // image
787 3 => "/(\n\\{\\|)|(\n\\|\\})/"); // table
789 // FIXME: this should prolly be a hook or something
790 if(function_exists('wfCite')){
791 $spat .= '|(<ref>)'; // references via cite extension
792 $endPatterns[4] = '/(<ref>)|(<\/ref>)/';
795 $textExt = array(); // text extracts
796 $otherExt = array(); // other extracts
797 wfProfileIn( "$fname-split" );
799 $textLen = strlen($text);
800 $count = 0; // sequence number to maintain ordering
801 while( $start < $textLen ){
802 // find start of template/image/table
803 if( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE
, $start ) ){
805 foreach($matches as $key => $val){
806 if($key > 0 && $val[1] != -1){
808 // see if this is an image link
809 $ns = substr($val[0],2,-1);
810 if( $wgContLang->getNsIndex($ns) != NS_FILE
)
814 $epat = $endPatterns[$key];
815 $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
821 // find end (and detect any nested elements)
823 $offset = $start +
1;
825 while( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE
, $offset ) ){
826 if( array_key_exists(2,$endMatches) ){
829 $len = strlen($endMatches[2][0]);
830 $off = $endMatches[2][1];
831 $this->splitAndAdd( $otherExt, $count,
832 substr( $text, $start, $off +
$len - $start ) );
833 $start = $off +
$len;
837 // end of nested element
844 $offset = $endMatches[0][1] +
strlen($endMatches[0][0]);
847 // couldn't find appropriate closing tag, skip
848 $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen($matches[0][0]) ) );
849 $start +
= strlen($matches[0][0]);
854 // else: add as text extract
855 $this->splitAndAdd( $textExt, $count, substr($text,$start) );
859 $all = $textExt +
$otherExt; // these have disjunct key sets
861 wfProfileOut( "$fname-split" );
864 foreach( $terms as $index => $term ) {
865 // manually do upper/lowercase stuff for utf-8 since PHP won't do it
866 if(preg_match('/[\x80-\xff]/', $term) ){
867 $terms[$index] = preg_replace_callback('/./us',array($this,'caseCallback'),$terms[$index]);
869 $terms[$index] = $term;
872 $anyterm = implode( '|', $terms );
873 $phrase = implode("$wgSearchHighlightBoundaries+", $terms );
875 // FIXME: a hack to scale contextchars, a correct solution
876 // would be to have contextchars actually be char and not byte
877 // length, and do proper utf-8 substrings and lengths everywhere,
878 // but PHP is making that very hard and unclean to implement :(
879 $scale = strlen($anyterm) / mb_strlen($anyterm);
880 $contextchars = intval( $contextchars * $scale );
882 $patPre = "(^|$wgSearchHighlightBoundaries)";
883 $patPost = "($wgSearchHighlightBoundaries|$)";
885 $pat1 = "/(".$phrase.")/ui";
886 $pat2 = "/$patPre(".$anyterm.")$patPost/ui";
888 wfProfileIn( "$fname-extract" );
890 $left = $contextlines;
895 // show beginning only if it contains all words
898 foreach($textExt as $index => $line){
899 if(strlen($line)>0 && $line[0] != ';' && $line[0] != ':'){
900 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
907 // check if first text contains all terms
908 foreach($terms as $term){
909 if( ! preg_match("/$patPre".$term."$patPost/ui", $firstText) ){
915 $snippets[$first] = $firstText;
916 $offsets[$first] = 0;
920 // match whole query on text
921 $this->process($pat1, $textExt, $left, $contextchars, $snippets, $offsets);
922 // match whole query on templates/tables/images
923 $this->process($pat1, $otherExt, $left, $contextchars, $snippets, $offsets);
924 // match any words on text
925 $this->process($pat2, $textExt, $left, $contextchars, $snippets, $offsets);
926 // match any words on templates/tables/images
927 $this->process($pat2, $otherExt, $left, $contextchars, $snippets, $offsets);
932 // add extra chars to each snippet to make snippets constant size
934 if( count( $snippets ) == 0){
935 // couldn't find the target words, just show beginning of article
936 $targetchars = $contextchars * $contextlines;
937 $snippets[$first] = '';
938 $offsets[$first] = 0;
940 // if begin of the article contains the whole phrase, show only that !!
941 if( array_key_exists($first,$snippets) && preg_match($pat1,$snippets[$first])
942 && $offsets[$first] < $contextchars * 2 ){
943 $snippets = array ($first => $snippets[$first]);
946 // calc by how much to extend existing snippets
947 $targetchars = intval( ($contextchars * $contextlines) / count ( $snippets ) );
950 foreach($snippets as $index => $line){
951 $extended[$index] = $line;
952 $len = strlen($line);
953 if( $len < $targetchars - 20 ){
954 // complete this line
955 if($len < strlen( $all[$index] )){
956 $extended[$index] = $this->extract( $all[$index], $offsets[$index], $offsets[$index]+
$targetchars, $offsets[$index]);
957 $len = strlen( $extended[$index] );
962 while( $len < $targetchars - 20
963 && array_key_exists($add,$all)
964 && !array_key_exists($add,$snippets) ){
966 $tt = "\n".$this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
967 $extended[$add] = $tt;
968 $len +
= strlen( $tt );
974 //$snippets = array_map('htmlspecialchars', $extended);
975 $snippets = $extended;
978 foreach($snippets as $index => $line){
980 $extract .= $line; // first line
981 elseif($last+
1 == $index && $offsets[$last]+
strlen($snippets[$last]) >= strlen($all[$last]))
982 $extract .= " ".$line; // continous lines
984 $extract .= '<b> ... </b>' . $line;
989 $extract .= '<b> ... </b>';
991 $processed = array();
992 foreach($terms as $term){
993 if( ! isset($processed[$term]) ){
994 $pat3 = "/$patPre(".$term.")$patPost/ui"; // highlight word
995 $extract = preg_replace( $pat3,
996 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
997 $processed[$term] = true;
1001 wfProfileOut( "$fname-extract" );
1007 * Split text into lines and add it to extracts array
1009 * @param $extracts Array: index -> $line
1010 * @param $count Integer
1011 * @param $text String
1013 function splitAndAdd(&$extracts, &$count, $text){
1014 $split = explode( "\n", $this->mCleanWikitext?
$this->removeWiki($text) : $text );
1015 foreach($split as $line){
1018 $extracts[$count++
] = $tt;
1023 * Do manual case conversion for non-ascii chars
1025 * @param $matches Array
1027 function caseCallback($matches){
1029 if( strlen($matches[0]) > 1 ){
1030 return '['.$wgContLang->lc($matches[0]).$wgContLang->uc($matches[0]).']';
1036 * Extract part of the text from start to end, but by
1037 * not chopping up words
1038 * @param $text String
1039 * @param $start Integer
1040 * @param $end Integer
1041 * @param $posStart Integer: (out) actual start position
1042 * @param $posEnd Integer: (out) actual end position
1045 function extract($text, $start, $end, &$posStart = null, &$posEnd = null ){
1049 $start = $this->position( $text, $start, 1 );
1050 if( $end >= strlen($text) )
1051 $end = strlen($text);
1053 $end = $this->position( $text, $end );
1055 if(!is_null($posStart))
1057 if(!is_null($posEnd))
1061 return substr($text, $start, $end-$start);
1067 * Find a nonletter near a point (index) in the text
1069 * @param $text String
1070 * @param $point Integer
1071 * @param $offset Integer: offset to found index
1072 * @return Integer: nearest nonletter index, or beginning of utf8 char if none
1074 function position($text, $point, $offset=0 ){
1076 $s = max( 0, $point - $tolerance );
1077 $l = min( strlen($text), $point +
$tolerance ) - $s;
1079 if( preg_match('/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', substr($text,$s,$l), $m, PREG_OFFSET_CAPTURE
) ){
1080 return $m[0][1] +
$s +
$offset;
1082 // check if point is on a valid first UTF8 char
1083 $char = ord( $text[$point] );
1084 while( $char >= 0x80 && $char < 0xc0 ) {
1085 // skip trailing bytes
1087 if($point >= strlen($text))
1088 return strlen($text);
1089 $char = ord( $text[$point] );
1097 * Search extracts for a pattern, and return snippets
1099 * @param $pattern String: regexp for matching lines
1100 * @param $extracts Array: extracts to search
1101 * @param $linesleft Integer: number of extracts to make
1102 * @param $contextchars Integer: length of snippet
1103 * @param $out Array: map for highlighted snippets
1104 * @param $offsets Array: map of starting points of snippets
1107 function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ){
1109 return; // nothing to do
1110 foreach($extracts as $index => $line){
1111 if( array_key_exists($index,$out) )
1112 continue; // this line already highlighted
1115 if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE
) )
1119 $len = strlen($m[0][0]);
1120 if($offset +
$len < $contextchars)
1122 elseif( $len > $contextchars)
1125 $begin = $offset +
intval( ($len - $contextchars) / 2 );
1127 $end = $begin +
$contextchars;
1130 // basic snippet from this line
1131 $out[$index] = $this->extract($line,$begin,$end,$posBegin);
1132 $offsets[$index] = $posBegin;
1140 * Basic wikitext removal
1143 function removeWiki($text) {
1144 $fname = __METHOD__
;
1145 wfProfileIn( $fname );
1147 //$text = preg_replace("/'{2,5}/", "", $text);
1148 //$text = preg_replace("/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text);
1149 //$text = preg_replace("/\[\[([^]|]+)\]\]/", "\\1", $text);
1150 //$text = preg_replace("/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text);
1151 //$text = preg_replace("/\\{\\|(.*?)\\|\\}/", "", $text);
1152 //$text = preg_replace("/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text);
1153 $text = preg_replace("/\\{\\{([^|]+?)\\}\\}/", "", $text);
1154 $text = preg_replace("/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text);
1155 $text = preg_replace("/\\[\\[([^|]+?)\\]\\]/", "\\1", $text);
1156 $text = preg_replace_callback("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", array($this,'linkReplace'), $text);
1157 //$text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text);
1158 $text = preg_replace("/<\/?[^>]+>/", "", $text);
1159 $text = preg_replace("/'''''/", "", $text);
1160 $text = preg_replace("/('''|<\/?[iIuUbB]>)/", "", $text);
1161 $text = preg_replace("/''/", "", $text);
1163 wfProfileOut( $fname );
1168 * callback to replace [[target|caption]] kind of links, if
1169 * the target is category or image, leave it
1171 * @param $matches Array
1173 function linkReplace($matches){
1174 $colon = strpos( $matches[1], ':' );
1175 if( $colon === false )
1176 return $matches[2]; // replace with caption
1178 $ns = substr( $matches[1], 0, $colon );
1179 $index = $wgContLang->getNsIndex($ns);
1180 if( $index !== false && ($index == NS_FILE ||
$index == NS_CATEGORY
) )
1181 return $matches[0]; // return the whole thing
1188 * Simple & fast snippet extraction, but gives completely unrelevant
1191 * @param $text String
1192 * @param $terms Array
1193 * @param $contextlines Integer
1194 * @param $contextchars Integer
1197 public function highlightSimple( $text, $terms, $contextlines, $contextchars ) {
1198 global $wgLang, $wgContLang;
1199 $fname = __METHOD__
;
1201 $lines = explode( "\n", $text );
1203 $terms = implode( '|', $terms );
1204 $max = intval( $contextchars ) +
1;
1205 $pat1 = "/(.*)($terms)(.{0,$max})/i";
1210 wfProfileIn( "$fname-extract" );
1211 foreach ( $lines as $line ) {
1212 if ( 0 == $contextlines ) {
1217 if ( ! preg_match( $pat1, $line, $m ) ) {
1221 $pre = $wgContLang->truncate( $m[1], -$contextchars );
1223 if ( count( $m ) < 3 ) {
1226 $post = $wgContLang->truncate( $m[3], $contextchars );
1231 $line = htmlspecialchars( $pre . $found . $post );
1232 $pat2 = '/(' . $terms . ")/i";
1233 $line = preg_replace( $pat2,
1234 "<span class='searchmatch'>\\1</span>", $line );
1236 $extract .= "${line}\n";
1238 wfProfileOut( "$fname-extract" );
1246 * Dummy class to be used when non-supported Database engine is present.
1247 * @todo Fixme: dummy class should probably try something at least mildly useful,
1248 * such as a LIKE search through titles.
1251 class SearchEngineDummy
extends SearchEngine
{