SearchUpdate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * See deferred.txt
  4. * @ingroup Search
  5. */
  6. class SearchUpdate {
  7. /* private */ var $mId = 0, $mNamespace, $mTitle, $mText;
  8. /* private */ var $mTitleWords;
  9. function SearchUpdate( $id, $title, $text = false ) {
  10. $nt = Title::newFromText( $title );
  11. if( $nt ) {
  12. $this->mId = $id;
  13. $this->mText = $text;
  14. $this->mNamespace = $nt->getNamespace();
  15. $this->mTitle = $nt->getText(); # Discard namespace
  16. $this->mTitleWords = $this->mTextWords = array();
  17. } else {
  18. wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
  19. }
  20. }
  21. function doUpdate() {
  22. global $wgContLang, $wgDisableSearchUpdate;
  23. if( $wgDisableSearchUpdate || !$this->mId ) {
  24. return false;
  25. }
  26. $fname = 'SearchUpdate::doUpdate';
  27. wfProfileIn( $fname );
  28. $search = SearchEngine::create();
  29. $lc = SearchEngine::legalSearchChars() . '&#;';
  30. if( $this->mText === false ) {
  31. $search->updateTitle($this->mId,
  32. Title::indexTitle( $this->mNamespace, $this->mTitle ));
  33. wfProfileOut( $fname );
  34. return;
  35. }
  36. # Language-specific strip/conversion
  37. $text = $wgContLang->stripForSearch( $this->mText );
  38. wfProfileIn( $fname.'-regexps' );
  39. $text = preg_replace( "/<\\/?\\s*[A-Za-z][A-Za-z0-9]*\\s*([^>]*?)>/",
  40. ' ', strtolower( " " . $text /*$this->mText*/ . " " ) ); # Strip HTML markup
  41. $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
  42. "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
  43. # Strip external URLs
  44. $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\xA0-\\xFF";
  45. $protos = "http|https|ftp|mailto|news|gopher";
  46. $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
  47. $text = preg_replace( $pat, "\\1 \\3", $text );
  48. $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
  49. $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
  50. $text = preg_replace( $p1, "\\1 ", $text );
  51. $text = preg_replace( $p2, "\\1 \\3 ", $text );
  52. # Internal image links
  53. $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
  54. $text = preg_replace( $pat2, " \\1 \\3", $text );
  55. $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
  56. "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
  57. # Strip all remaining non-search characters
  58. $text = preg_replace( "/[^{$lc}]+/", " ", $text );
  59. # Handle 's, s'
  60. #
  61. # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
  62. # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
  63. #
  64. # These tail-anchored regexps are insanely slow. The worst case comes
  65. # when Japanese or Chinese text (ie, no word spacing) is written on
  66. # a wiki configured for Western UTF-8 mode. The Unicode characters are
  67. # expanded to hex codes and the "words" are very long paragraph-length
  68. # monstrosities. On a large page the above regexps may take over 20
  69. # seconds *each* on a 1GHz-level processor.
  70. #
  71. # Following are reversed versions which are consistently fast
  72. # (about 3 milliseconds on 1GHz-level processor).
  73. #
  74. $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
  75. $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
  76. # Strip wiki '' and '''
  77. $text = preg_replace( "/''[']*/", " ", $text );
  78. wfProfileOut( "$fname-regexps" );
  79. wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
  80. # Perform the actual update
  81. $search->update($this->mId, Title::indexTitle( $this->mNamespace, $this->mTitle ),
  82. $text);
  83. wfProfileOut( $fname );
  84. }
  85. }
  86. /**
  87. * Placeholder class
  88. * @ingroup Search
  89. */
  90. class SearchUpdateMyISAM extends SearchUpdate {
  91. # Inherits everything
  92. }