helper_plugin_issuelinks_util::sendResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * DokuWiki Plugin Issuelinks (Helper Component)
4
 *
5
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
 * @author  Andreas Gohr <[email protected]>
7
 */
8
9
class helper_plugin_issuelinks_util extends DokuWiki_Plugin
10
{
11
12
    /**
13
     * Parse the link header received by DokuHTTPClient
14
     *
15
     * @param string $linkHeader
16
     *
17
     * @return array
18
     */
19 2
    public function parseHTTPLinkHeaders($linkHeader)
20
    {
21 2
        $links = explode(',', $linkHeader);
22 2
        $linkarray = [];
23 2
        foreach ($links as $linkstring) {
24 2
            list($linktarget, $linkrel) = explode(';', $linkstring);
25 2
            $linkrel = substr(trim($linkrel), strlen('rel="'), -1);
26 2
            $linktarget = trim($linktarget, '<> ');
27 2
            $linkarray[$linkrel] = $linktarget;
28
        }
29 2
        return $linkarray;
30
    }
31
32
    /**
33
     * @param string $page_id
34
     * @param int    $revision
35
     *
36
     * @return string
37
     */
38
    public function getDiffUrl($page_id, $revision = 0)
39
    {
40
        if (empty($revision)) {
41
            $currentRevision = filemtime(wikiFN($page_id));
42
            $url = wl(
43
                $page_id,
44
                [
45
                    "rev" => $currentRevision,
46
                    "do" => "diff",
47
                ],
48
                true
49
            );
50
        } else {
51
            $changelog = new PageChangelog($page_id);
52
            $previousRevision = $changelog->getRelativeRevision($revision, -1);
53
            $url = wl(
54
                $page_id,
55
                [
56
                    "do" => "diff",
57
                    "rev2[0]" => $revision,
58
                    "rev2[1]" => $previousRevision,
59
                    "difftype" => "sidebyside",
60
                ],
61
                true
62
            );
63
        }
64
        return $url;
65
    }
66
67
    /**
68
     * Show an error message for the execption, add trace if $conf['allowdebug']
69
     *
70
     * @param Exception $e
71
     */
72
    public function reportException(Exception $e)
73
    {
74
        msg(hsc($e->getMessage()), -1, $e->getLine(), $e->getFile());
75
        global $conf;
76
        if ($conf['allowdebug']) {
77
            msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
78
        }
79
    }
80
81
    /**
82
     * @param int   $code
83
     * @param mixed $msg
84
     */
85
    public function sendResponse($code, $msg)
86
    {
87
        header('Content-Type: application/json');
88
        if ((int)$code === 204) {
89
            http_status(200);
90
        } else {
91
            http_status($code);
92
        }
93
        global $MSG;
94
        echo json_encode(['data' => $msg, 'msg' => $MSG]);
95
    }
96
97
98
    /**
99
     * Check whether a number or string is a valid unix timestamp
100
     *
101
     * Adapted from http://stackoverflow.com/a/2524761/3293343
102
     *
103
     * @param string|int $timestamp
104
     *
105
     * @return bool
106
     */
107 2
    public function isValidTimeStamp($timestamp)
108
    {
109 2
        return ((string)(int)$timestamp === (string)$timestamp);
110
    }
111
}
112