diff --git a/.gitignore b/.gitignore index cb50cd2c..98dcc8d2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ composer composer.lock vendor wikicache.json +redirects.txt node_modules diff --git a/404.php b/404.php index 98ec41fe..7db20630 100644 --- a/404.php +++ b/404.php @@ -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: ' . + '' . $uri . '' + ) + ] ); +} else { + echo new \OOUI\MessageWidget( [ + 'type' => 'error', + 'label' => 'Page not found. The wiki you are looking for may have been deleted.' + ] ); +} include "footer.html"; diff --git a/delete.php b/delete.php index c4f9cc8e..d36cb43b 100644 --- a/delete.php +++ b/delete.php @@ -11,24 +11,88 @@ } if ( !isset( $_POST['confirm' ] ) ) { - echo '
' . - '

Are you sure you want to delete this wiki: ' . $wiki . '?

' . - '

This cannot be undone.

' . - new OOUI\ButtonInputWidget( [ - 'type' => 'submit', - 'name' => 'confirm', - 'label' => 'Delete', - 'flags' => [ 'primary', 'destructive' ] - ] ) . - '
'; - 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: ' . $wiki . '?
' . + '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 ' . $redirect . '.'; + } +} diff --git a/index.php b/index.php index 23f5c476..804814fe 100644 --- a/index.php +++ b/index.php @@ -151,7 +151,6 @@ 'classes' => [ 'form-submit' ], 'label' => 'Create demo', 'type' => 'submit', - // 'disabled' => true, 'flags' => [ 'progressive', 'primary' ] ] ), [