aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Wiki.php
blob: 7f54628ad19b8deaf6bea6e3bffabf015f5affff (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace RESTAPI\Routes;

/**
 * @author     <mlunzena@uos.de>
 * @license    GPL 2 or later
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 *
 * @condition range_id ^[0-9a-f]{1,32}$
 */
class Wiki extends \RESTAPI\RouteMap
{
    public function before()
    {
        require_once 'User.php';
    }

    /**
     * Wikiseitenindex einer Veranstaltung
     *
     * @get /course/:range_id/wiki
     */
    public function getCourseWiki($range_id)
    {
        $pages = \WikiPage::findBySQL("`range_id` = ? ORDER BY `name` ASC", [$range_id]);

        if (!$pages[0]->isReadable()) {
            $this->error(401);
        }

        $total = sizeof($pages);
        $pages = $pages->limit($this->offset, $this->limit);

        $linked_pages = [];
        foreach ($pages as $page) {
            $url = $this->urlf('/course/%s/wiki/%s', [$range_id, htmlReady($page['keyword'])]);
            $linked_pages[$url] = $this->wikiPageToJson($page, ["content"]);
        }

        $this->etag(md5(serialize($linked_pages)));

        return $this->paginated($linked_pages, $total, compact('range_id'));
    }

    /**
     * Wikiseite auslesen
     *
     * @get /course/:range_id/wiki/:keyword
     * @get /course/:range_id/wiki/:keyword/:version
     */
    public function getCourseWikiKeyword($range_id, $keyword, $version = null)
    {
        $page = $this->requirePage($range_id, $keyword, $version);
        $wiki_json = $this->wikiPageToJson($page);
        $this->etag(md5(serialize($wiki_json)));
        $this->lastmodified($page->chdate);
        return $wiki_json;
    }

    /**
     * Wikiseite ändern/hinzufügen
     *
     * @put /course/:range_id/wiki/:keyword
     */
    public function putCourseWikiKeyword($range_id, $keyword)
    {
        if (!isset($this->data['content'])) {
            $this->error(400, 'No content provided');
        }

        $page =\WikiPage::findOneBySQL("`range_id` = ? AND `name` = ?", [$range_id, $keyword]);
        if (!$page) {
            $page = new \WikiPage();
            $page->range_id = $range_id;
            $page->name = $keyword;
        }

        if (!$page->isEditable()) {
            $this->error(401);
        }

        $page->content = $this->data['content'];
        $page->store();

        $url = sprintf('course/%s/wiki/%s/%d', htmlReady($range_id), htmlReady($keyword), count($page->versions) + 1);
        $this->redirect($url, 201, 'ok');
    }

    /**************************************************/
    /* PRIVATE HELPER METHODS                         */
    /**************************************************/

    private function requirePage($range_id, $keyword, $version = null)
    {
        $page = \WikiPage::findOneBySQL("`range_id` = ? AND `name` = ?", [$range_id, $keyword]);

        if (!$page) {
            $this->notFound();
        }

        if (!$page->isReadable($GLOBALS['user']->id)) {
            $this->error(401);
        }
        if ($version !== null && $version !== count($page->versions) + 1) {
            return $page->versions[count($page->versions) - 1 - $version];
        } else {
            return $page;
        }
    }

    private function wikiPageToJson($page, $without = [])
    {
        $json = [
            'range_id' => $page->range_id,
            'keyword'  => $page->name,
            'chdate'   => $page->chdate,
            'version'  => 1
        ];

        // (pre-rendered) content
        if (!in_array('content', $without)) {
            $json['content']      = $page->content;
            $json['content_html'] = wikiReady($page->content, true, $page->range_id, $page->id);
        }
        if (!in_array('user', $without)) {
            if ($page->author) {
                $json['user'] = User::getMiniUser($this, $page->user_id);
            }
        }

        foreach ($without as $key) {
            if (isset($json[$key])) {
                unset($json[$key]);
            }
        }

        // string to int conversions as SORM does not know about ints
        foreach (['chdate', 'mkdate', 'filesize', 'downloads'] as $key) {
            if (isset($json[$key])) {
                $json[$key] = (int) $json[$key];
            }
        }

        return $json;
    }


}