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

Commit

Permalink
Allow wikis to be redirected when deleting
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
edg2s committed Jan 2, 2021
1 parent 8b48a97 commit 36f635f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ composer
composer.lock
vendor
wikicache.json
redirects.txt
node_modules
37 changes: 33 additions & 4 deletions 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,38 @@
header( 'HTTP/1.0 404 Not Found' );
require_once "includes.php";

echo new \OOUI\MessageWidget( [
'type' => 'error',
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
] );
// Check for redirect
$redirects = get_if_file_exists( 'redirects.txt' );
$redirect = false;
if ( $redirects ) {
$uri = $_SERVER['REQUEST_URI'];
$lines = explode( "\n", $redirects );
foreach ( $lines as $line ) {
if ( !$line ) {
continue;
}
$parts = explode( ' ', $line );
if ( strpos( $uri, $parts[0] ) !== false ) {
$uri = str_replace( $parts[0], $parts[1], $uri );
$redirect = true;
}
}
}

if ( $redirect ) {
echo new \OOUI\MessageWidget( [
'type' => 'info',
'icon' => 'articleRedirect',
'label' => new \OOUI\HtmlSnippet(
'This wiki has been deleted. The following wiki was selected as a direct replacement: ' .
'<a href="' . htmlspecialchars( $uri ) . '">' . $uri . '</a>'
)
] );
} else {
echo new \OOUI\MessageWidget( [
'type' => 'error',
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
] );
}

include "footer.html";
100 changes: 82 additions & 18 deletions delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,88 @@
}

if ( !isset( $_POST['confirm' ] ) ) {
echo '<form method="POST">' .
'<p>Are you sure you want to delete this wiki: <a href="wikis/' . $wiki . '/w">' . $wiki . '</a>?</p>' .
'<p>This cannot be undone.</p>' .
new OOUI\ButtonInputWidget( [
'type' => 'submit',
'name' => 'confirm',
'label' => 'Delete',
'flags' => [ 'primary', 'destructive' ]
] ) .
'</form>';
die();
}

ob_implicit_flush( true );
$wikilist = [
[
'data' => '',
'label' => 'None',
]
];
$cache = load_wikicache();
if ( $cache ) {
$wikis = json_decode( $cache, true );
foreach ( $wikis as $hash => $data ) {
$wikilist[] = [
'data' => $hash,
'label' => $hash . ' - ' . $data['creator'] . ' (' . date( 'c', $data[ 'mtime' ] ) . ')',
];
}
}
echo new OOUI\FormLayout( [
'method' => 'POST',
'items' => [
new OOUI\FieldsetLayout( [
'label' => new OOUI\HtmlSnippet(
'Are you sure you want to delete this wiki: <a href="wikis/' . $wiki . '/w">' . $wiki . '</a>?<br>' .
'This cannot be undone.'
),
'items' => array_filter( [
count( $wikilist ) > 1 ?
new OOUI\FieldLayout(
new OOUI\DropdownInputWidget( [
'name' => 'redirect',
'options' => $wikilist,
] ),
[
'label' => 'Leave a redirect to this wiki:',
'align' => 'left',
]
) :
null,
new OOUI\FieldLayout(
new OOUI\ButtonInputWidget( [
'type' => 'submit',
'name' => 'confirm',
'label' => 'Delete',
'flags' => [ 'primary', 'destructive' ]
] ),
[
'label' => ' ',
'align' => 'left',
]
),
] )
] )
]
] );

$error = delete_wiki( $wiki );
if ( $error ) {
die( "Wiki not cleanly deleted, may have not been fully setup." );
}
} else {
ob_implicit_flush( true );

$error = delete_wiki( $wiki );
if ( $error ) {
echo( "Wiki not cleanly deleted, may have not been fully setup." );
} else {
echo "Wiki deleted.";
}

echo "Wiki deleted.";
function isValidHash( $hash ) {
return preg_match( '/^[0-9a-f]{32}$/', $hash );
}

$redirect = $_POST['redirect'] ?? null;

if (
$redirect &&
isValidHash( $redirect ) &&
isValidHash( $wiki )
) {
// TODO: Avoid duplication in redirect file
file_put_contents(
'redirects.txt',
$wiki . ' ' . $redirect . "\n",
FILE_APPEND | LOCK_EX
);
echo ' Redirected to <a href="wikis/' . $redirect . '/w">' . $redirect . '</a>.';
}
}
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
'classes' => [ 'form-submit' ],
'label' => 'Create demo',
'type' => 'submit',
// 'disabled' => true,
'flags' => [ 'progressive', 'primary' ]
] ),
[
Expand Down

0 comments on commit 36f635f

Please sign in to comment.