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

Commit 1558429

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

File tree

4 files changed

+129
-32
lines changed

4 files changed

+129
-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: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,41 @@
22
header( 'HTTP/1.0 404 Not Found' );
33
require_once "includes.php";
44

5+
// Check for redirect
6+
$redirects = file_exists( 'redirects.txt' ) ? file_get_contents( 'redirects.txt' ) : null;
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+
$wiki = $parts[1];
19+
$redirect = true;
20+
}
21+
}
22+
}
23+
524
include "header.php";
625

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

1242
include "footer.html";

delete.php

Lines changed: 94 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,102 @@
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+
new OOUI\FieldLayout(
88+
new OOUI\HiddenInputWidget( [
89+
'name' => 'csrf_token',
90+
'value' => get_csrf_token(),
91+
] )
92+
),
93+
] )
94+
] )
95+
]
96+
] );
5297

53-
if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
54-
die( "Invalid session." );
55-
}
98+
} else {
99+
if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
100+
die( "Invalid session." );
101+
}
56102

57-
ob_implicit_flush( true );
103+
ob_implicit_flush( true );
58104

59-
echo '<div class="consoleLog">';
60-
$error = delete_wiki( $wiki );
61-
echo '</div>';
105+
echo '<div class="consoleLog">';
106+
$error = delete_wiki( $wiki );
107+
echo '</div>';
62108

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

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)