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

Commit d5c1995

Browse files
committed
Allow wikis to be redirected when deleting
Fixes #54
1 parent 70e6e71 commit d5c1995

File tree

4 files changed

+113
-10
lines changed

4 files changed

+113
-10
lines changed

404.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,41 @@
44

55
include "header.php";
66

7-
echo new \OOUI\MessageWidget( [
8-
'type' => 'error',
9-
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
10-
] );
7+
$redirect = false;
8+
9+
// Check for redirect
10+
$uri = $_SERVER['REQUEST_URI'];
11+
if ( preg_match( '`/wikis/([0-9a-f]{10,32})/`', $uri, $matches, PREG_OFFSET_CAPTURE ) !== false ) {
12+
$wiki = $matches[1][0];
13+
$offset = $matches[1][1];
14+
$wikiData = get_wiki_data( $wiki );
15+
// Follow up to 10 redirect steps
16+
$i = 0;
17+
while ( $wikiData['redirect'] && $i < 10 ) {
18+
$redirect = $wikiData['redirect'];
19+
$wikiData = get_wiki_data( $redirect );
20+
$i++;
21+
}
22+
$redirectUri =
23+
substr( $uri, 0, $offset ) .
24+
$redirect .
25+
substr( $uri, $offset + strlen( $wiki ) );
26+
}
27+
28+
if ( $redirect ) {
29+
echo new \OOUI\MessageWidget( [
30+
'type' => 'info',
31+
'icon' => 'articleRedirect',
32+
'label' => new \OOUI\HtmlSnippet(
33+
'This wiki has been deleted and the following wiki was selected as a direct replacement: ' .
34+
'<a href="' . htmlspecialchars( $redirectUri ) . '" class="wiki">' . substr( $redirect, 0, 10 ) . '</a>'
35+
)
36+
] );
37+
} else {
38+
echo new \OOUI\MessageWidget( [
39+
'type' => 'error',
40+
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
41+
] );
42+
}
1143

1244
include "footer.html";

delete.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@
3434
'</tr>' .
3535
'</table>';
3636

37+
$username = $user ? $user->username : null;
38+
$wikilist = [
39+
[
40+
'data' => '',
41+
'label' => 'None',
42+
]
43+
];
44+
$stmt = $mysqli->prepare( '
45+
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created
46+
FROM wikis
47+
WHERE !deleted
48+
ORDER BY IF( creator = ?, 1, 0 ) DESC, created DESC
49+
' );
50+
if ( !$stmt ) {
51+
die( $mysqli->error );
52+
}
53+
$stmt->bind_param( 's', $username );
54+
$stmt->execute();
55+
$results = $stmt->get_result();
56+
if ( !$results ) {
57+
die( $mysqli->error );
58+
}
59+
$shownMyWikis = false;
60+
$shownOtherWikis = false;
61+
while ( $data = $results->fetch_assoc() ) {
62+
if ( $data[ 'wiki' ] === $wiki ) {
63+
continue;
64+
}
65+
$creator = $data[ 'creator' ] ?? '';
66+
if ( !$shownMyWikis && $creator === $username ) {
67+
$wikilist[] = [ 'optgroup' => 'My wikis' ];
68+
$shownMyWikis = true;
69+
}
70+
if ( $shownMyWikis && !$shownOtherWikis && $creator !== $username ) {
71+
$wikilist[] = [ 'optgroup' => 'Other wikis' ];
72+
$shownOtherWikis = true;
73+
}
74+
$wikilist[] = [
75+
'data' => $data[ 'wiki' ],
76+
'label' => substr( $data[ 'wiki' ], 0, 10 ) . ' - ' . $data[ 'creator' ] . ' (' . date( 'Y-m-d H:i:s', $data[ 'created' ] ) . ')',
77+
];
78+
}
3779
echo new OOUI\FormLayout( [
3880
'method' => 'POST',
3981
'items' => [
@@ -42,6 +84,18 @@
4284
'<br>Are you sure you want to delete this wiki? This cannot be undone.'
4385
),
4486
'items' => array_filter( [
87+
count( $wikilist ) > 1 ?
88+
new OOUI\FieldLayout(
89+
new OOUI\DropdownInputWidget( [
90+
'name' => 'redirect',
91+
'options' => $wikilist,
92+
] ),
93+
[
94+
'label' => 'Leave a redirect to another wiki (optional):',
95+
'align' => 'left',
96+
]
97+
) :
98+
null,
4599
new OOUI\FieldLayout(
46100
new OOUI\ButtonInputWidget( [
47101
'type' => 'submit',
@@ -51,7 +105,7 @@
51105
] ),
52106
[
53107
'label' => ' ',
54-
'align' => 'inline',
108+
'align' => 'left',
55109
]
56110
),
57111
new OOUI\FieldLayout(
@@ -70,10 +124,12 @@
70124
die( "Invalid session." );
71125
}
72126

127+
$redirect = $_POST['redirect'] ?: null;
128+
73129
ob_implicit_flush( true );
74130

75131
echo '<div class="consoleLog">';
76-
$error = delete_wiki( $wiki );
132+
$error = delete_wiki( $wiki, $redirect );
77133
echo '</div>';
78134

79135
if ( $error ) {
@@ -88,3 +144,7 @@
88144
if ( $wikiData['deleted'] ) {
89145
echo '<p>Wiki deleted.</p>';
90146
}
147+
148+
if ( $wikiData['redirect'] ) {
149+
echo '<p>Redirected to <a href="wikis/' . $wikiData['redirect'] . '/w">' . $wikiData['redirect'] . '</a>.</p>';
150+
}

includes.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function get_wiki_data( string $wiki ): array {
7777
global $mysqli;
7878

7979
$stmt = $mysqli->prepare( '
80-
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created, patches, branch, announcedTasks, timeToCreate, deleted
80+
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created, patches, branch, announcedTasks, timeToCreate, deleted, redirect
8181
FROM wikis WHERE wiki = ?
8282
' );
8383
if ( !$stmt ) {
@@ -316,7 +316,11 @@ function shell( $cmd, array $env = [] ): ?string {
316316
return $error ? null : $process->getOutput();
317317
}
318318

319-
function delete_wiki( string $wiki ): ?string {
319+
function is_valid_hash( string $hash ): bool {
320+
return preg_match( '/^[0-9a-f]{10,32}$/', $hash ) !== false;
321+
}
322+
323+
function delete_wiki( string $wiki, ?string $redirect = null ): ?string {
320324
global $mysqli;
321325

322326
$wikiData = get_wiki_data( $wiki );
@@ -346,12 +350,16 @@ function delete_wiki( string $wiki ): ?string {
346350
);
347351
}
348352

353+
if ( $redirect && !is_valid_hash( $redirect ) ) {
354+
$redirect = null;
355+
}
356+
349357
$stmt = $mysqli->prepare( '
350358
UPDATE wikis
351-
SET deleted = 1
359+
SET deleted = 1, redirect = ?
352360
WHERE wiki = ?
353361
' );
354-
$stmt->bind_param( 's', $wiki );
362+
$stmt->bind_param( 'ss', $redirect, $wiki );
355363
$stmt->execute();
356364
$stmt->close();
357365

sql/patchdemo.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ ALTER TABLE `tasks`
4242

4343
ALTER TABLE `wikis`
4444
ADD COLUMN IF NOT EXISTS `branch` VARCHAR(64) NOT NULL AFTER `patches`;
45+
46+
ALTER TABLE `wikis`
47+
ADD COLUMN IF NOT EXISTS `redirect` VARCHAR(32) NULL AFTER `deleted`;

0 commit comments

Comments
 (0)