PageSubscriptionSender.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace dokuwiki\Subscriptions;
  3. use Diff;
  4. use InlineDiffFormatter;
  5. use UnifiedDiffFormatter;
  6. class PageSubscriptionSender extends SubscriptionSender
  7. {
  8. /**
  9. * Send the diff for some page change
  10. *
  11. * @param string $subscriber_mail The target mail address
  12. * @param string $template Mail template ('subscr_digest', 'subscr_single', 'mailtext', ...)
  13. * @param string $id Page for which the notification is
  14. * @param int|null $rev Old revision if any
  15. * @param string $summary Change summary if any
  16. * @param int|null $current_rev New revision if any
  17. *
  18. * @return bool true if successfully sent
  19. */
  20. public function sendPageDiff($subscriber_mail, $template, $id, $rev = null, $summary = '', $current_rev = null)
  21. {
  22. global $DIFF_INLINESTYLES;
  23. // prepare replacements (keys not set in hrep will be taken from trep)
  24. $trep = [
  25. 'PAGE' => $id,
  26. 'NEWPAGE' => wl($id, $current_rev?('rev='.$current_rev):'', true, '&'),
  27. 'SUMMARY' => $summary,
  28. 'SUBSCRIBE' => wl($id, ['do' => 'subscribe'], true, '&'),
  29. ];
  30. $hrep = [];
  31. if ($rev) {
  32. $subject = 'changed';
  33. $trep['OLDPAGE'] = wl($id, "rev=$rev", true, '&');
  34. $old_content = rawWiki($id, $rev);
  35. $new_content = rawWiki($id);
  36. $df = new Diff(
  37. explode("\n", $old_content),
  38. explode("\n", $new_content)
  39. );
  40. $dformat = new UnifiedDiffFormatter();
  41. $tdiff = $dformat->format($df);
  42. $DIFF_INLINESTYLES = true;
  43. $df = new Diff(
  44. explode("\n", $old_content),
  45. explode("\n", $new_content)
  46. );
  47. $dformat = new InlineDiffFormatter();
  48. $hdiff = $dformat->format($df);
  49. $hdiff = '<table>' . $hdiff . '</table>';
  50. $DIFF_INLINESTYLES = false;
  51. } else {
  52. $subject = 'newpage';
  53. $trep['OLDPAGE'] = '---';
  54. $tdiff = rawWiki($id);
  55. $hdiff = nl2br(hsc($tdiff));
  56. }
  57. $trep['DIFF'] = $tdiff;
  58. $hrep['DIFF'] = $hdiff;
  59. $headers = ['Message-Id' => $this->getMessageID($id)];
  60. if ($rev) {
  61. $headers['In-Reply-To'] = $this->getMessageID($id, $rev);
  62. }
  63. return $this->send(
  64. $subscriber_mail,
  65. $subject,
  66. $id,
  67. $template,
  68. $trep,
  69. $hrep,
  70. $headers
  71. );
  72. }
  73. }