aboutsummaryrefslogtreecommitdiff
path: root/public/wiki.php
blob: 93398811b8ad7e1d16728d7a1083d98633b3a7e0 (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
<?php
/**
 * wiki.php
 *
 * This script is needed to redirect wiki pages referenced by an URL to the old wiki
 * (before Stud.IP 5.5) to the controller of the new wiki (Stud.IP 5.5 and newer).
 *
 * @author      Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       5.5
 */

// Set up a minimal Stud.IP environment:
require_once __DIR__ . '/../lib/bootstrap.php';
URLHelper::setBaseUrl($GLOBALS['ABSOLUTE_URI_STUDIP']);

// Set exception handler which outputs the message of the exception.
// In case the Stud.IP is in development mode, the stack trace
// is also printed before halting execution.
set_exception_handler(function (Throwable $exception) {
    header('Content-Type: text/plain');
    echo $exception->getMessage();
    if (Studip\ENV === 'development') {
        echo "\n" . $exception->getTraceAsString();
    }
    die;
});

// Handle URL parameters:
$course_id = Request::option('cid');
$keyword   = Request::get('keyword');
$version   = Request::int('version');
if (!$course_id) {
    // Invalid wiki page.
    die(sprintf(_('Fehlender URL-Parameter: %s'), 'cid'));
}

// Load the wiki page or a version of it:
$page         = null;
$page_version = null;

if (!$keyword || $keyword === 'WikiWikiWeb') {
    // Load the start page for the course:
    $page_id = CourseConfig::get($course_id)->getValue('WIKI_STARTPAGE_ID');
    $load_newest_version = true;
    if ($version) {
        // Check if the requested version is the newest version.
        // In that case, the wiki_pages table instead of the wiki_versions
        // table must be used to get the correct version of the page.
        $newest_version = 1 + WikiVersion::countByPage_id($page_id);

        $load_newest_version = $version === $newest_version;
    }
    if ($load_newest_version) {
        // Load the newest version:
        $page = WikiPage::find($page_id);
    } else {
        // Load the requested version:
        $page_version = WikiVersion::findOneBySQL(
            '`page_id` = :page_id
             ORDER BY `mkdate` ASC
             LIMIT 1 OFFSET :version',
            [
                'page_id' => $page_id,
                'version' => $version - 1
            ]
        );
    }
} else {
    // Load the page by its keyword and course-ID:
    $load_newest_version = true;
    if ($version) {
        // Check if the requested version is the newest version.
        // In that case, the wiki_pages table instead of the wiki_versions
        // table must be used to get the correct version of the page.
        $newest_version = 1 + WikiVersion::countBySql(
            'JOIN `wiki_pages` USING (`page_id`)
             WHERE `wiki_pages`.`range_id` = :course_id AND `wiki_pages`.`name` = :keyword',
            [
                'course_id' => $course_id,
                'keyword' => $keyword
            ]
        );
        $load_newest_version = $version === $newest_version;
    }

    if ($load_newest_version) {
        // Load the newest version:
        $page = WikiPage::findOneBySQL(
            '`range_id` = :course_id AND `name` = :keyword',
            [
                'course_id' => $course_id,
                'keyword' => $keyword
            ]
        );
    } else {
        // Load the requested version:
        $page_version = WikiVersion::findOneBySQL(
            'JOIN `wiki_pages` USING (`page_id`)
             WHERE `wiki_pages`.`range_id` = :course_id AND `wiki_pages`.`name` = :keyword
             ORDER BY `wiki_versions`.`mkdate` ASC
             LIMIT 1 OFFSET :version',
            [
                'course_id' => $course_id,
                'keyword'   => $keyword,
                'version'   => $version - 1
            ]
        );
    }
}
if (!$page && !$page_version) {
    // Page not found:
    die(sprintf(_('Die Wikiseite mit dem Schlagwort "%s" wurde nicht gefunden.'), $keyword));
}

// Everything went well: Redirect permanently to the new wiki page.
// Assume that the optional version parameter is not set and redirect to the newest
// version of the wiki page, which should be the usual case.
$url = URLHelper::getURL('dispatch.php/course/wiki/page/' . $page->id, ['cid' => $course_id]);
if ($page_version) {
    //Redirect to the specific version of the wiki page instead of the newest version:
    $url = URLHelper::getURL('dispatch.php/course/wiki/version/' . $page_version->id, ['cid' => $course_id]);
}

header('Location: ' . $url, true, 301);