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

Commit 3bcd75d

Browse files
committed
Allow wikis to be redirected when deleting
Fixes #54
1 parent 99e1dff commit 3bcd75d

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-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: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@
3434
'</tr>' .
3535
'</table>';
3636

37+
$wikilist = [
38+
[
39+
'data' => '',
40+
'label' => 'None',
41+
]
42+
];
43+
$results = $mysqli->query( 'SELECT wiki, creator, UNIX_TIMESTAMP( created ) created FROM wikis WHERE !deleted ORDER BY created DESC' );
44+
if ( !$results ) {
45+
die( $mysqli->error );
46+
}
47+
while ( $data = $results->fetch_assoc() ) {
48+
if ( $data[ 'wiki' ] === $wiki ) {
49+
continue;
50+
}
51+
$wikilist[] = [
52+
'data' => $data[ 'wiki' ],
53+
'label' => substr( $data[ 'wiki' ], 0, 10 ) . ' - ' . $data[ 'creator' ] . ' (' . date( 'Y-m-d H:i:s', $data[ 'created' ] ) . ')',
54+
];
55+
}
3756
echo new OOUI\FormLayout( [
3857
'method' => 'POST',
3958
'items' => [
@@ -42,6 +61,18 @@
4261
'<br>Are you sure you want to delete this wiki? This cannot be undone.'
4362
),
4463
'items' => array_filter( [
64+
count( $wikilist ) > 1 ?
65+
new OOUI\FieldLayout(
66+
new OOUI\DropdownInputWidget( [
67+
'name' => 'redirect',
68+
'options' => $wikilist,
69+
] ),
70+
[
71+
'label' => 'Leave a redirect another wiki (optional):',
72+
'align' => 'left',
73+
]
74+
) :
75+
null,
4576
new OOUI\FieldLayout(
4677
new OOUI\ButtonInputWidget( [
4778
'type' => 'submit',
@@ -51,7 +82,7 @@
5182
] ),
5283
[
5384
'label' => ' ',
54-
'align' => 'inline',
85+
'align' => 'left',
5586
]
5687
),
5788
new OOUI\FieldLayout(
@@ -70,10 +101,12 @@
70101
die( "Invalid session." );
71102
}
72103

104+
$redirect = $_POST['redirect'] ?: null;
105+
73106
ob_implicit_flush( true );
74107

75108
echo '<div class="consoleLog">';
76-
$error = delete_wiki( $wiki );
109+
$error = delete_wiki( $wiki, $redirect );
77110
echo '</div>';
78111

79112
if ( $error ) {
@@ -88,3 +121,7 @@
88121
if ( $wikiData['deleted'] ) {
89122
echo '<p>Wiki deleted.</p>';
90123
}
124+
125+
if ( $wikiData['redirect'] ) {
126+
echo '<p>Redirected to <a href="wikis/' . $wikiData['redirect'] . '/w">' . $wikiData['redirect'] . '</a>.</p>';
127+
}

includes.php

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

7575
$stmt = $mysqli->prepare( '
76-
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created, patches, branch, announcedTasks, timeToCreate, deleted
76+
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created, patches, branch, announcedTasks, timeToCreate, deleted, redirect
7777
FROM wikis WHERE wiki = ?
7878
' );
7979
if ( !$stmt ) {
@@ -305,7 +305,11 @@ function shell( $cmd ) : ?string {
305305
return $error ? null : $process->getOutput();
306306
}
307307

308-
function delete_wiki( string $wiki ) : ?string {
308+
function is_valid_hash( string $hash ) : bool {
309+
return preg_match( '/^[0-9a-f]{10,32}$/', $hash ) !== false;
310+
}
311+
312+
function delete_wiki( string $wiki, ?string $redirect = null ) : ?string {
309313
global $mysqli;
310314

311315
$wikiData = get_wiki_data( $wiki );
@@ -334,12 +338,16 @@ function delete_wiki( string $wiki ) : ?string {
334338
);
335339
}
336340

341+
if ( $redirect && !is_valid_hash( $redirect ) ) {
342+
$redirect = null;
343+
}
344+
337345
$stmt = $mysqli->prepare( '
338346
UPDATE wikis
339-
SET deleted = 1
347+
SET deleted = 1, redirect = ?
340348
WHERE wiki = ?
341349
' );
342-
$stmt->bind_param( 's', $wiki );
350+
$stmt->bind_param( 'ss', $redirect, $wiki );
343351
$stmt->execute();
344352
$stmt->close();
345353

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)