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