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