Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit c9e9d68

Browse files
committed
Allow wikis to be redirected when deleting
Fixes #54
1 parent 57a76cf commit c9e9d68

File tree

4 files changed

+116
-23
lines changed

4 files changed

+116
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ composer
55
composer.lock
66
vendor
77
wikicache.json
8+
redirects.txt
89
node_modules

404.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,38 @@
22
header( 'HTTP/1.0 404 Not Found' );
33
require_once "includes.php";
44

5-
echo new \OOUI\MessageWidget( [
6-
'type' => 'error',
7-
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
8-
] );
5+
// Check for redirect
6+
$redirects = get_if_file_exists( 'redirects.txt' );
7+
$redirect = false;
8+
if ( $redirects ) {
9+
$uri = $_SERVER['REQUEST_URI'];
10+
$lines = explode( "\n", $redirects );
11+
foreach ( $lines as $line ) {
12+
if ( !$line ) {
13+
continue;
14+
}
15+
$parts = explode( ' ', $line );
16+
if ( strpos( $uri, $parts[0] ) !== false ) {
17+
$uri = str_replace( $parts[0], $parts[1], $uri );
18+
$redirect = true;
19+
}
20+
}
21+
}
22+
23+
if ( $redirect ) {
24+
echo new \OOUI\MessageWidget( [
25+
'type' => 'info',
26+
'icon' => 'articleRedirect',
27+
'label' => new \OOUI\HtmlSnippet(
28+
'This wiki has been deleted. The following wiki was selected as a direct replacement: ' .
29+
'<a href="' . htmlspecialchars( $uri ) . '">' . $uri . '</a>'
30+
)
31+
] );
32+
} else {
33+
echo new \OOUI\MessageWidget( [
34+
'type' => 'error',
35+
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
36+
] );
37+
}
938

1039
include "footer.html";

delete.php

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,88 @@
1111
}
1212

1313
if ( !isset( $_POST['confirm' ] ) ) {
14-
echo '<form method="POST">' .
15-
'<p>Are you sure you want to delete this wiki: <a href="wikis/' . $wiki . '/w">' . $wiki . '</a>?</p>' .
16-
'<p>This cannot be undone.</p>' .
17-
new OOUI\ButtonInputWidget( [
18-
'type' => 'submit',
19-
'name' => 'confirm',
20-
'label' => 'Delete',
21-
'flags' => [ 'primary', 'destructive' ]
22-
] ) .
23-
'</form>';
24-
die();
25-
}
2614

27-
ob_implicit_flush( true );
15+
$wikilist = [
16+
[
17+
'data' => '',
18+
'label' => 'None',
19+
]
20+
];
21+
$cache = load_wikicache();
22+
if ( $cache ) {
23+
$wikis = json_decode( $cache, true );
24+
foreach ( $wikis as $hash => $data ) {
25+
$wikilist[] = [
26+
'data' => $hash,
27+
'label' => $hash . ' - ' . $data['creator'] . ' (' . date( 'c', $data[ 'mtime' ] ) . ')',
28+
];
29+
}
30+
}
31+
echo new OOUI\FormLayout( [
32+
'method' => 'POST',
33+
'items' => [
34+
new OOUI\FieldsetLayout( [
35+
'label' => new OOUI\HtmlSnippet(
36+
'Are you sure you want to delete this wiki: <a href="wikis/' . $wiki . '/w">' . $wiki . '</a>?<br>' .
37+
'This cannot be undone.'
38+
),
39+
'items' => array_filter( [
40+
count( $wikilist ) > 1 ?
41+
new OOUI\FieldLayout(
42+
new OOUI\DropdownInputWidget( [
43+
'name' => 'redirect',
44+
'options' => $wikilist,
45+
] ),
46+
[
47+
'label' => 'Leave a redirect to this wiki:',
48+
'align' => 'left',
49+
]
50+
) :
51+
null,
52+
new OOUI\FieldLayout(
53+
new OOUI\ButtonInputWidget( [
54+
'type' => 'submit',
55+
'name' => 'confirm',
56+
'label' => 'Delete',
57+
'flags' => [ 'primary', 'destructive' ]
58+
] ),
59+
[
60+
'label' => ' ',
61+
'align' => 'left',
62+
]
63+
),
64+
] )
65+
] )
66+
]
67+
] );
2868

29-
$error = delete_wiki( $wiki );
30-
if ( $error ) {
31-
die( "Wiki not cleanly deleted, may have not been fully setup." );
32-
}
69+
} else {
70+
ob_implicit_flush( true );
71+
72+
$error = delete_wiki( $wiki );
73+
if ( $error ) {
74+
echo( "Wiki not cleanly deleted, may have not been fully setup." );
75+
} else {
76+
echo "Wiki deleted.";
77+
}
3378

34-
echo "Wiki deleted.";
79+
function isValidHash( $hash ) {
80+
return preg_match( '/^[0-9a-f]{32}$/', $hash );
81+
}
82+
83+
$redirect = $_POST['redirect'] ?? null;
84+
85+
if (
86+
$redirect &&
87+
isValidHash( $redirect ) &&
88+
isValidHash( $wiki )
89+
) {
90+
// TODO: Avoid duplication in redirect file
91+
file_put_contents(
92+
'redirects.txt',
93+
$wiki . ' ' . $redirect . "\n",
94+
FILE_APPEND | LOCK_EX
95+
);
96+
echo ' Redirected to <a href="wikis/' . $redirect . '/w">' . $redirect . '</a>.';
97+
}
98+
}

index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
'classes' => [ 'form-submit' ],
152152
'label' => 'Create demo',
153153
'type' => 'submit',
154-
// 'disabled' => true,
155154
'flags' => [ 'progressive', 'primary' ]
156155
] ),
157156
[

0 commit comments

Comments
 (0)