/extensions/DidYouMean/DidYouMean.hooks.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 195 lines · 119 code · 43 blank · 33 comment · 37 complexity · b9088936d20a81ef358428cbbdf82211 MD5 · raw file

  1. <?php
  2. /**
  3. * All the hooks for DidYouMean
  4. */
  5. class DidYouMeanHooks {
  6. private static $articleIsRedirect = false;
  7. # TODO this is called even when editing a new page
  8. public static function articleNoArticleText( &$article, &$text ) {
  9. wfDebug( 'HIPP: ' . __METHOD__ . "\n" );
  10. $sees = DidYouMean::lookup( 0, $article->getTitle()->getText() );
  11. sort($sees);
  12. if (count($sees))
  13. $text = DidYouMean::build_sees($sees) . $text;
  14. return true;
  15. }
  16. # this is called when using the Go/Search box but it is not called when entering
  17. # a URL for a non-existing article
  18. public static function specialSearchNoResults( $term ) {
  19. wfDebug( 'HIPP: ' . __METHOD__ . "\n" );
  20. $sees = DidYouMean::lookup( 0, $term );
  21. sort($sees);
  22. if ( count($sees) ) {
  23. global $wgOut;
  24. $wgOut->addWikiText( DidYouMean::build_sees($sees) );
  25. }
  26. return true;
  27. }
  28. # this is called per chunk of wikitext, not per article
  29. public static function parserBeforeStrip( &$parser, &$text, &$stripState ) {
  30. #wfDebug( 'HIPP: ' . __METHOD__ . "\n" );
  31. # if revisionid is 0 this is not an article chunk
  32. if( isset( $parser->mDymFirstChunk ) || !$parser->getVariableValue('revisionid') || $parser->getVariableValue('namespace'))
  33. return true;
  34. $parser->mDymFirstChunk = 'done';
  35. $title = $parser->getTitle();
  36. $parser->mDymSees = DidYouMean::lookup( $title->getArticleID(), $title->getText() );
  37. if (preg_match( "/{{[sS]ee\|([^}]*)}}/", $text, $see )) {
  38. wfDebug( "HIPP: see Hit\n" );
  39. $hasTemplate = true;
  40. $sees = explode("|", $see[1]);
  41. } elseif (preg_match( "/{{[xX]see(\|[^}]*)}}/", $text, $see )) {
  42. wfDebug( "HIPP: xsee Hit\n" );
  43. $hasTemplate = true;
  44. preg_match_all( "/\|\[\[([^]|]*)(?:\|([^|]*))?\]\](?: \(([^)]*)\))?/", $see[1], $ma );
  45. $sees = $ma[1];
  46. } else {
  47. wfDebug( "HIPP: (x)see Miss\n" );
  48. # there's no {{see}} in this chunk of wikitext
  49. # if this is the 1st chunk of the article itself we can put an empty {{see}} there.
  50. $hasTemplate = false;
  51. $sees = array();
  52. }
  53. # normalize entities and urlencoding to pure utf-8
  54. foreach ($sees as &$value)
  55. $value = rawurldecode(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
  56. wfDebug( 'HIPP: Parser: ' . implode(', ', $sees) . "\n" );
  57. wfDebug( 'HIPP: DBase: ' . implode(', ', $parser->mDymSees) . "\n" );
  58. # add in the stuff from the database lookup
  59. $sees = array_unique(array_merge($sees, $parser->mDymSees));
  60. sort($sees);
  61. wfDebug( 'HIPP: Merged: ' . implode(', ', $sees) . "\n" );
  62. # TODO is it better to use $parser->insertStripItem() ?
  63. if (count($sees)) {
  64. if( !$hasTemplate ) {
  65. // We need to squish in a fresh copy of the template...
  66. $text = "{{see|}}\n" . $text;
  67. }
  68. $built_sees = DidYouMean::build_sees($sees);
  69. } else {
  70. $built_sees = '';
  71. }
  72. $text = preg_replace(
  73. '/{{[xX]?[sS]ee\|[^}]*}}/',
  74. #$built_sees . '<div style="text-decoration:line-through">$0</div>',
  75. $built_sees,
  76. $text );
  77. return true;
  78. }
  79. public static function articleDelete( $article, $user, $reason ) {
  80. if ($article->getTitle()->getNamespace() != 0 || $article->isRedirect() == true)
  81. return true;
  82. DidYouMean::doDelete( $article->getID() );
  83. return true;
  84. }
  85. public static function titleMoveComplete( &$title, &$nt, &$wgUser, &$pageid, &$redirid ) {
  86. $oldtitletext = $title->getText();
  87. $oldns = $title->getNamespace();
  88. $newtitletext = $nt->getText();
  89. $newns = $nt->getNamespace();
  90. wfDebug( 'HIPP: ' . __METHOD__ . "\n" );
  91. if ($oldns != 0 && $newns != 0)
  92. return true;
  93. # TODO we can't always check if we're moving a redirect because the old article's content
  94. # TODO has already been replaced with the redirect to the new title but a
  95. # TODO new title's content is still "noarticletext" at this point!
  96. #$a1 = new Article( $title );
  97. #$a2 = new Article( $nt );
  98. #wfDebug( "HIPP: getContent() for isRedirect()\n\tfrom <<<" . $a1->getContent() . ">>>\n\t to <<<" . $a2->getContent() . ">>>\n" );
  99. #if ($a1->isRedirect( $a->getContent() )) {
  100. # wfDebug( "HIPP: moving a redirect (?)\n" );
  101. # return true;
  102. #}
  103. if ($oldns == 0 && $newns == 0) {
  104. DidYouMean::doUpdate( $pageid, $newtitletext );
  105. } elseif ($oldns == 0) {
  106. DidYouMean::doDelete( $pageid );
  107. } elseif ($newns == 0) {
  108. DidYouMean::doInsert( $pageid, $newtitletext );
  109. }
  110. return true;
  111. }
  112. # called at action=edit. can detect if we're about to edit a redirect
  113. public static function alternateEdit( $editpage ) {
  114. if ( $editpage->mArticle->isRedirect() ) {
  115. self::$articleIsRedirect = true;
  116. }
  117. return true;
  118. }
  119. # called at end of action=submit
  120. public static function articleSaveComplete( $article, $user, $text, $summary, $isminor, $dunno1, $dunno2, $flags ) {
  121. if ( $article->getTitle()->getNamespace() != 0 ) {
  122. return true;
  123. }
  124. if ( $article->isRedirect( $text ) ) {
  125. if ( !self::$articleIsRedirect && !( $flags & EDIT_NEW ) ) {
  126. DidYouMean::doDelete( $article->getID() );
  127. }
  128. } else {
  129. if ( self::$articleIsRedirect || $flags & EDIT_NEW ) {
  130. DidYouMean::doInsert( $article->getID(), $article->getTitle()->getText() );
  131. }
  132. }
  133. self::$articleIsRedirect = false;
  134. return true;
  135. }
  136. public static function articleUndelete( &$title, &$create ) {
  137. if ($create == false || $title->getNamespace() != 0)
  138. return true;
  139. # TODO it's not possible to detect if the undeleted article is a redirect!
  140. #$artic1e = new Article( $title );
  141. #if ($article->isRedirect( $article->getContent() )) {
  142. # return true;
  143. #}
  144. DidYouMean::doInsert( $title->getArticleId(), $title->getText() );
  145. return true;
  146. }
  147. public static function parserTestTables( &$tables ) {
  148. $tables[] = 'dympage';
  149. $tables[] = 'dymnorm';
  150. return true;
  151. }
  152. }