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

Commit f39dcc7

Browse files
committed
Allow wikis to be redirected when deleting
Fixes #54
1 parent dd2f7aa commit f39dcc7

File tree

4 files changed

+122
-32
lines changed

4 files changed

+122
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ repositories
44
composer
55
composer.lock
66
vendor
7+
redirects.txt
78
node_modules

404.php

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

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+
523
include "header.php";
624

7-
echo new \OOUI\MessageWidget( [
8-
'type' => 'error',
9-
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
10-
] );
25+
if ( $redirect ) {
26+
echo new \OOUI\MessageWidget( [
27+
'type' => 'info',
28+
'icon' => 'articleRedirect',
29+
'label' => new \OOUI\HtmlSnippet(
30+
'This wiki has been deleted. The following wiki was selected as a direct replacement: ' .
31+
'<a href="' . htmlspecialchars( $uri ) . '">' . $uri . '</a>'
32+
)
33+
] );
34+
} else {
35+
echo new \OOUI\MessageWidget( [
36+
'type' => 'error',
37+
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
38+
] );
39+
}
1140

1241
include "footer.html";

delete.php

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,96 @@
3333
'</tr>' .
3434
'</table>';
3535

36-
echo '<form method="POST">' .
37-
'<p>Are you sure you want to delete this wiki?</p>' .
38-
'<p>This cannot be undone.</p>' .
39-
new OOUI\ButtonInputWidget( [
40-
'type' => 'submit',
41-
'name' => 'confirm',
42-
'label' => 'Delete',
43-
'flags' => [ 'primary', 'destructive' ]
44-
] ) .
45-
new OOUI\HiddenInputWidget( [
46-
'name' => 'csrf_token',
47-
'value' => get_csrf_token(),
48-
] ) .
49-
'</form>';
50-
die();
51-
}
36+
$wikilist = [
37+
[
38+
'data' => '',
39+
'label' => 'None',
40+
]
41+
];
42+
$results = $mysqli->query( 'SELECT wiki, creator, UNIX_TIMESTAMP( created ) created FROM wikis WHERE !deleted ORDER BY created DESC' );
43+
if ( !$results ) {
44+
die( $mysqli->error );
45+
}
46+
while ( $data = $results->fetch_assoc() ) {
47+
if ( $data[ 'wiki' ] === $wiki ) {
48+
continue;
49+
}
50+
$wikilist[] = [
51+
'data' => $data[ 'wiki' ],
52+
'label' => substr( $data[ 'wiki' ], 0, 10 ) . ' - ' . $data[ 'creator' ] . ' (' . date( 'Y-m-d H:i:s', $data[ 'created' ] ) . ')',
53+
];
54+
}
55+
echo new OOUI\FormLayout( [
56+
'method' => 'POST',
57+
'items' => [
58+
new OOUI\FieldsetLayout( [
59+
'label' => new OOUI\HtmlSnippet(
60+
'<br>Are you sure you want to delete this wiki? This cannot be undone.'
61+
),
62+
'items' => array_filter( [
63+
count( $wikilist ) > 1 ?
64+
new OOUI\FieldLayout(
65+
new OOUI\DropdownInputWidget( [
66+
'name' => 'redirect',
67+
'options' => $wikilist,
68+
] ),
69+
[
70+
'label' => 'Leave a redirect another wiki (optional):',
71+
'align' => 'left',
72+
]
73+
) :
74+
null,
75+
new OOUI\FieldLayout(
76+
new OOUI\ButtonInputWidget( [
77+
'type' => 'submit',
78+
'name' => 'confirm',
79+
'label' => 'Delete',
80+
'flags' => [ 'primary', 'destructive' ]
81+
] ),
82+
[
83+
'label' => ' ',
84+
'align' => 'left',
85+
]
86+
),
87+
] )
88+
] )
89+
]
90+
] );
5291

53-
if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
54-
die( "Invalid session." );
55-
}
92+
} else {
93+
if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
94+
die( "Invalid session." );
95+
}
5696

57-
ob_implicit_flush( true );
97+
ob_implicit_flush( true );
5898

59-
echo '<div class="consoleLog">';
60-
$error = delete_wiki( $wiki );
61-
echo '</div>';
99+
echo '<div class="consoleLog">';
100+
$error = delete_wiki( $wiki );
101+
echo '</div>';
62102

63-
if ( $error ) {
64-
die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' );
65-
} else {
66-
echo '<p>Wiki deleted.</p>';
103+
if ( $error ) {
104+
die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' );
105+
} else {
106+
echo '<p>Wiki deleted.</p>';
107+
}
108+
109+
function isValidHash( $hash ) {
110+
return preg_match( '/^[0-9a-f]{10,32}$/', $hash );
111+
}
112+
113+
$redirect = $_POST['redirect'] ?? null;
114+
115+
if (
116+
$redirect &&
117+
isValidHash( $redirect ) &&
118+
isValidHash( $wiki )
119+
) {
120+
// TODO: Avoid duplication in redirect file
121+
file_put_contents(
122+
'redirects.txt',
123+
$wiki . ' ' . $redirect . "\n",
124+
FILE_APPEND | LOCK_EX
125+
);
126+
echo ' Redirected to <a href="wikis/' . $redirect . '/w">' . $redirect . '</a>.';
127+
}
67128
}

index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@
182182
'classes' => [ 'form-submit' ],
183183
'label' => 'Create demo',
184184
'type' => 'submit',
185-
// 'disabled' => true,
186185
'flags' => [ 'progressive', 'primary' ]
187186
] ),
188187
[

0 commit comments

Comments
 (0)