summaryrefslogtreecommitdiff
path: root/platform/www/inc/Subscriptions/PageSubscriptionSender.php
blob: e5577c1afebfdf64b895554cd2ce7ebf56e71a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php


namespace dokuwiki\Subscriptions;


use Diff;
use InlineDiffFormatter;
use UnifiedDiffFormatter;

class PageSubscriptionSender extends SubscriptionSender
{

    /**
     * Send the diff for some page change
     *
     * @param string   $subscriber_mail The target mail address
     * @param string   $template        Mail template ('subscr_digest', 'subscr_single', 'mailtext', ...)
     * @param string   $id              Page for which the notification is
     * @param int|null $rev             Old revision if any
     * @param string   $summary         Change summary if any
     * @param int|null $current_rev     New revision if any
     *
     * @return bool                     true if successfully sent
     */
    public function sendPageDiff($subscriber_mail, $template, $id, $rev = null, $summary = '', $current_rev = null)
    {
        global $DIFF_INLINESTYLES;

        // prepare replacements (keys not set in hrep will be taken from trep)
        $trep = [
            'PAGE' => $id,
            'NEWPAGE' => wl($id, $current_rev?('rev='.$current_rev):'', true, '&'),
            'SUMMARY' => $summary,
            'SUBSCRIBE' => wl($id, ['do' => 'subscribe'], true, '&'),
        ];
        $hrep = [];

        if ($rev) {
            $subject = 'changed';
            $trep['OLDPAGE'] = wl($id, "rev=$rev", true, '&');

            $old_content = rawWiki($id, $rev);
            $new_content = rawWiki($id);

            $df = new Diff(
                explode("\n", $old_content),
                explode("\n", $new_content)
            );
            $dformat = new UnifiedDiffFormatter();
            $tdiff = $dformat->format($df);

            $DIFF_INLINESTYLES = true;
            $df = new Diff(
                explode("\n", $old_content),
                explode("\n", $new_content)
            );
            $dformat = new InlineDiffFormatter();
            $hdiff = $dformat->format($df);
            $hdiff = '<table>' . $hdiff . '</table>';
            $DIFF_INLINESTYLES = false;
        } else {
            $subject = 'newpage';
            $trep['OLDPAGE'] = '---';
            $tdiff = rawWiki($id);
            $hdiff = nl2br(hsc($tdiff));
        }

        $trep['DIFF'] = $tdiff;
        $hrep['DIFF'] = $hdiff;

        $headers = ['Message-Id' => $this->getMessageID($id)];
        if ($rev) {
            $headers['In-Reply-To'] = $this->getMessageID($id, $rev);
        }

        return $this->send(
            $subscriber_mail,
            $subject,
            $id,
            $template,
            $trep,
            $hrep,
            $headers
        );
    }

}