MediaWiki  1.27.1
SearchHighlighter.php
Go to the documentation of this file.
1 <?php
30  protected $mCleanWikitext = true;
31 
32  function __construct( $cleanupWikitext = true ) {
33  $this->mCleanWikitext = $cleanupWikitext;
34  }
35 
45  public function highlightText( $text, $terms, $contextlines, $contextchars ) {
46  global $wgContLang, $wgSearchHighlightBoundaries;
47 
48  if ( $text == '' ) {
49  return '';
50  }
51 
52  // spli text into text + templates/links/tables
53  $spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
54  // first capture group is for detecting nested templates/links/tables/references
55  $endPatterns = [
56  1 => '/(\{\{)|(\}\})/', // template
57  2 => '/(\[\[)|(\]\])/', // image
58  3 => "/(\n\\{\\|)|(\n\\|\\})/" ]; // table
59 
60  // @todo FIXME: This should prolly be a hook or something
61  // instead of hardcoding a class name from the Cite extension
62  if ( class_exists( 'Cite' ) ) {
63  $spat .= '|(<ref>)'; // references via cite extension
64  $endPatterns[4] = '/(<ref>)|(<\/ref>)/';
65  }
66  $spat .= '/';
67  $textExt = []; // text extracts
68  $otherExt = []; // other extracts
69  $start = 0;
70  $textLen = strlen( $text );
71  $count = 0; // sequence number to maintain ordering
72  while ( $start < $textLen ) {
73  // find start of template/image/table
74  if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE, $start ) ) {
75  $epat = '';
76  foreach ( $matches as $key => $val ) {
77  if ( $key > 0 && $val[1] != - 1 ) {
78  if ( $key == 2 ) {
79  // see if this is an image link
80  $ns = substr( $val[0], 2, - 1 );
81  if ( $wgContLang->getNsIndex( $ns ) != NS_FILE ) {
82  break;
83  }
84 
85  }
86  $epat = $endPatterns[$key];
87  $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
88  $start = $val[1];
89  break;
90  }
91  }
92  if ( $epat ) {
93  // find end (and detect any nested elements)
94  $level = 0;
95  $offset = $start + 1;
96  $found = false;
97  while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
98  if ( array_key_exists( 2, $endMatches ) ) {
99  // found end
100  if ( $level == 0 ) {
101  $len = strlen( $endMatches[2][0] );
102  $off = $endMatches[2][1];
103  $this->splitAndAdd( $otherExt, $count,
104  substr( $text, $start, $off + $len - $start ) );
105  $start = $off + $len;
106  $found = true;
107  break;
108  } else {
109  // end of nested element
110  $level -= 1;
111  }
112  } else {
113  // nested
114  $level += 1;
115  }
116  $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
117  }
118  if ( !$found ) {
119  // couldn't find appropriate closing tag, skip
120  $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen( $matches[0][0] ) ) );
121  $start += strlen( $matches[0][0] );
122  }
123  continue;
124  }
125  }
126  // else: add as text extract
127  $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
128  break;
129  }
130 
131  $all = $textExt + $otherExt; // these have disjunct key sets
132 
133  // prepare regexps
134  foreach ( $terms as $index => $term ) {
135  // manually do upper/lowercase stuff for utf-8 since PHP won't do it
136  if ( preg_match( '/[\x80-\xff]/', $term ) ) {
137  $terms[$index] = preg_replace_callback(
138  '/./us',
139  [ $this, 'caseCallback' ],
140  $terms[$index]
141  );
142  } else {
143  $terms[$index] = $term;
144  }
145  }
146  $anyterm = implode( '|', $terms );
147  $phrase = implode( "$wgSearchHighlightBoundaries+", $terms );
148 
149  // @todo FIXME: A hack to scale contextchars, a correct solution
150  // would be to have contextchars actually be char and not byte
151  // length, and do proper utf-8 substrings and lengths everywhere,
152  // but PHP is making that very hard and unclean to implement :(
153  $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
154  $contextchars = intval( $contextchars * $scale );
155 
156  $patPre = "(^|$wgSearchHighlightBoundaries)";
157  $patPost = "($wgSearchHighlightBoundaries|$)";
158 
159  $pat1 = "/(" . $phrase . ")/ui";
160  $pat2 = "/$patPre(" . $anyterm . ")$patPost/ui";
161 
162  $left = $contextlines;
163 
164  $snippets = [];
165  $offsets = [];
166 
167  // show beginning only if it contains all words
168  $first = 0;
169  $firstText = '';
170  foreach ( $textExt as $index => $line ) {
171  if ( strlen( $line ) > 0 && $line[0] != ';' && $line[0] != ':' ) {
172  $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
173  $first = $index;
174  break;
175  }
176  }
177  if ( $firstText ) {
178  $succ = true;
179  // check if first text contains all terms
180  foreach ( $terms as $term ) {
181  if ( !preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) {
182  $succ = false;
183  break;
184  }
185  }
186  if ( $succ ) {
187  $snippets[$first] = $firstText;
188  $offsets[$first] = 0;
189  }
190  }
191  if ( !$snippets ) {
192  // match whole query on text
193  $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
194  // match whole query on templates/tables/images
195  $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
196  // match any words on text
197  $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
198  // match any words on templates/tables/images
199  $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
200 
201  ksort( $snippets );
202  }
203 
204  // add extra chars to each snippet to make snippets constant size
205  $extended = [];
206  if ( count( $snippets ) == 0 ) {
207  // couldn't find the target words, just show beginning of article
208  if ( array_key_exists( $first, $all ) ) {
209  $targetchars = $contextchars * $contextlines;
210  $snippets[$first] = '';
211  $offsets[$first] = 0;
212  }
213  } else {
214  // if begin of the article contains the whole phrase, show only that !!
215  if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
216  && $offsets[$first] < $contextchars * 2 ) {
217  $snippets = [ $first => $snippets[$first] ];
218  }
219 
220  // calc by how much to extend existing snippets
221  $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
222  }
223 
224  foreach ( $snippets as $index => $line ) {
225  $extended[$index] = $line;
226  $len = strlen( $line );
227  if ( $len < $targetchars - 20 ) {
228  // complete this line
229  if ( $len < strlen( $all[$index] ) ) {
230  $extended[$index] = $this->extract(
231  $all[$index],
232  $offsets[$index],
233  $offsets[$index] + $targetchars,
234  $offsets[$index]
235  );
236  $len = strlen( $extended[$index] );
237  }
238 
239  // add more lines
240  $add = $index + 1;
241  while ( $len < $targetchars - 20
242  && array_key_exists( $add, $all )
243  && !array_key_exists( $add, $snippets ) ) {
244  $offsets[$add] = 0;
245  $tt = "\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
246  $extended[$add] = $tt;
247  $len += strlen( $tt );
248  $add++;
249  }
250  }
251  }
252 
253  // $snippets = array_map( 'htmlspecialchars', $extended );
254  $snippets = $extended;
255  $last = - 1;
256  $extract = '';
257  foreach ( $snippets as $index => $line ) {
258  if ( $last == - 1 ) {
259  $extract .= $line; // first line
260  } elseif ( $last + 1 == $index
261  && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] )
262  ) {
263  $extract .= " " . $line; // continous lines
264  } else {
265  $extract .= '<b> ... </b>' . $line;
266  }
267 
268  $last = $index;
269  }
270  if ( $extract ) {
271  $extract .= '<b> ... </b>';
272  }
273 
274  $processed = [];
275  foreach ( $terms as $term ) {
276  if ( !isset( $processed[$term] ) ) {
277  $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
278  $extract = preg_replace( $pat3,
279  "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
280  $processed[$term] = true;
281  }
282  }
283 
284  return $extract;
285  }
286 
294  function splitAndAdd( &$extracts, &$count, $text ) {
295  $split = explode( "\n", $this->mCleanWikitext ? $this->removeWiki( $text ) : $text );
296  foreach ( $split as $line ) {
297  $tt = trim( $line );
298  if ( $tt ) {
299  $extracts[$count++] = $tt;
300  }
301  }
302  }
303 
310  function caseCallback( $matches ) {
312  if ( strlen( $matches[0] ) > 1 ) {
313  return '[' . $wgContLang->lc( $matches[0] ) . $wgContLang->uc( $matches[0] ) . ']';
314  } else {
315  return $matches[0];
316  }
317  }
318 
329  function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) {
330  if ( $start != 0 ) {
331  $start = $this->position( $text, $start, 1 );
332  }
333  if ( $end >= strlen( $text ) ) {
334  $end = strlen( $text );
335  } else {
336  $end = $this->position( $text, $end );
337  }
338 
339  if ( !is_null( $posStart ) ) {
340  $posStart = $start;
341  }
342  if ( !is_null( $posEnd ) ) {
343  $posEnd = $end;
344  }
345 
346  if ( $end > $start ) {
347  return substr( $text, $start, $end - $start );
348  } else {
349  return '';
350  }
351  }
352 
361  function position( $text, $point, $offset = 0 ) {
362  $tolerance = 10;
363  $s = max( 0, $point - $tolerance );
364  $l = min( strlen( $text ), $point + $tolerance ) - $s;
365  $m = [];
366 
367  if ( preg_match(
368  '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
369  substr( $text, $s, $l ),
370  $m,
371  PREG_OFFSET_CAPTURE
372  ) ) {
373  return $m[0][1] + $s + $offset;
374  } else {
375  // check if point is on a valid first UTF8 char
376  $char = ord( $text[$point] );
377  while ( $char >= 0x80 && $char < 0xc0 ) {
378  // skip trailing bytes
379  $point++;
380  if ( $point >= strlen( $text ) ) {
381  return strlen( $text );
382  }
383  $char = ord( $text[$point] );
384  }
385 
386  return $point;
387 
388  }
389  }
390 
402  function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ) {
403  if ( $linesleft == 0 ) {
404  return; // nothing to do
405  }
406  foreach ( $extracts as $index => $line ) {
407  if ( array_key_exists( $index, $out ) ) {
408  continue; // this line already highlighted
409  }
410 
411  $m = [];
412  if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) {
413  continue;
414  }
415 
416  $offset = $m[0][1];
417  $len = strlen( $m[0][0] );
418  if ( $offset + $len < $contextchars ) {
419  $begin = 0;
420  } elseif ( $len > $contextchars ) {
421  $begin = $offset;
422  } else {
423  $begin = $offset + intval( ( $len - $contextchars ) / 2 );
424  }
425 
426  $end = $begin + $contextchars;
427 
428  $posBegin = $begin;
429  // basic snippet from this line
430  $out[$index] = $this->extract( $line, $begin, $end, $posBegin );
431  $offsets[$index] = $posBegin;
432  $linesleft--;
433  if ( $linesleft == 0 ) {
434  return;
435  }
436  }
437  }
438 
445  function removeWiki( $text ) {
446  $text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text );
447  $text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text );
448  $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text );
449  $text = preg_replace_callback(
450  "/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
451  [ $this, 'linkReplace' ],
452  $text
453  );
454  $text = preg_replace( "/<\/?[^>]+>/", "", $text );
455  $text = preg_replace( "/'''''/", "", $text );
456  $text = preg_replace( "/('''|<\/?[iIuUbB]>)/", "", $text );
457  $text = preg_replace( "/''/", "", $text );
458 
459  return $text;
460  }
461 
469  function linkReplace( $matches ) {
470  $colon = strpos( $matches[1], ':' );
471  if ( $colon === false ) {
472  return $matches[2]; // replace with caption
473  }
475  $ns = substr( $matches[1], 0, $colon );
476  $index = $wgContLang->getNsIndex( $ns );
477  if ( $index !== false && ( $index == NS_FILE || $index == NS_CATEGORY ) ) {
478  return $matches[0]; // return the whole thing
479  } else {
480  return $matches[2];
481  }
482  }
483 
494  public function highlightSimple( $text, $terms, $contextlines, $contextchars ) {
496 
497  $lines = explode( "\n", $text );
498 
499  $terms = implode( '|', $terms );
500  $max = intval( $contextchars ) + 1;
501  $pat1 = "/(.*)($terms)(.{0,$max})/i";
502 
503  $lineno = 0;
504 
505  $extract = "";
506  foreach ( $lines as $line ) {
507  if ( 0 == $contextlines ) {
508  break;
509  }
510  ++$lineno;
511  $m = [];
512  if ( !preg_match( $pat1, $line, $m ) ) {
513  continue;
514  }
515  --$contextlines;
516  // truncate function changes ... to relevant i18n message.
517  $pre = $wgContLang->truncate( $m[1], - $contextchars, '...', false );
518 
519  if ( count( $m ) < 3 ) {
520  $post = '';
521  } else {
522  $post = $wgContLang->truncate( $m[3], $contextchars, '...', false );
523  }
524 
525  $found = $m[2];
526 
527  $line = htmlspecialchars( $pre . $found . $post );
528  $pat2 = '/(' . $terms . ")/i";
529  $line = preg_replace( $pat2, "<span class='searchmatch'>\\1</span>", $line );
530 
531  $extract .= "${line}\n";
532  }
533 
534  return $extract;
535  }
536 
545  public function highlightNone( $text, $contextlines, $contextchars ) {
546  $match = [];
547  $text = ltrim( $text ) . "\n"; // make sure the preg_match may find the last line
548  $text = str_replace( "\n\n", "\n", $text ); // remove empty lines
549  preg_match( "/^(.*\n){0,$contextlines}/", $text, $match );
550 
551  // Trim and limit to max number of chars
552  $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) );
553  return str_replace( "\n", '<br>', $text );
554  }
555 }
process($pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets)
Search extracts for a pattern, and return snippets.
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2558
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:762
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
highlightSimple($text, $terms, $contextlines, $contextchars)
Simple & fast snippet extraction, but gives completely unrelevant snippets.
splitAndAdd(&$extracts, &$count, $text)
Split text into lines and add it to extracts array.
__construct($cleanupWikitext=true)
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
$last
Highlight bits of wikitext.
position($text, $point, $offset=0)
Find a nonletter near a point (index) in the text.
highlightText($text, $terms, $contextlines, $contextchars)
Default implementation of wikitext highlighting.
const NS_CATEGORY
Definition: Defines.php:83
extract($text, $start, $end, &$posStart=null, &$posEnd=null)
Extract part of the text from start to end, but by not chopping up words.
linkReplace($matches)
callback to replace [[target|caption]] kind of links, if the target is category or image...
const NS_FILE
Definition: Defines.php:75
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$lines
Definition: router.php:66
removeWiki($text)
Basic wikitext removal.
caseCallback($matches)
Do manual case conversion for non-ascii chars.
$line
Definition: cdb.php:59
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
$count
$processed
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped $pre
Definition: hooks.txt:1306
highlightNone($text, $contextlines, $contextchars)
Returns the first few lines of the text.
$matches