Created
April 6, 2022 09:43
-
-
Save miko007/c35f32f00cbf920afed71d4684e9854c to your computer and use it in GitHub Desktop.
A cleanup script to remove orphaned pads from an Etherpad Lite installation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php declare(strict_types=1); | |
function msg(string $message) : void { | |
echo "$message\n"; | |
} | |
function error(string $message, int $code = 1) : void { | |
msg("[ERROR] $message"); | |
exit($code); | |
} | |
function apiVersion(string $url) : string { | |
$rawResponse = file_get_contents($url."/api"); | |
$response = json_decode($rawResponse); | |
if (!$rawResponse || !isset($response->currentVersion)) | |
error("Could not initialize API communication", 5); | |
return $response->currentVersion; | |
} | |
if (count($argv) < 2) | |
error("You have to provide the URL of the installation as a commandline argument."); | |
$API_KEY = file_get_contents(dirname(__FILE__)."/../APIKEY.txt"); | |
$URL = $argv[1]; | |
$VERSION = apiVersion($URL); | |
function api(string $endpoint, array $params = []) : ?object { | |
global $API_KEY, $URL, $VERSION; | |
$queryString = "?".implode('&', array_merge(["apikey=$API_KEY"], $params)); | |
$request = $URL."/api/".$VERSION."/".$endpoint.$queryString; | |
@$rawResponse = file_get_contents($request); | |
if (!$rawResponse) | |
error("No response for request '$request'", 5); | |
@$response = json_decode($rawResponse); | |
if (!$rawResponse) | |
error("could not parse JSON for response to request '$request'", 3); | |
if (!isset($response->code) || !isset($response->message)) | |
error("malformed response to request '$request': ".json_encode($response)); | |
if ($response->code !== 0) | |
error("request for '$request' returned error: ".$response->message); | |
return $response->data; | |
} | |
msg("Etherpad Lite cleanup utility"); | |
msg("–––––––––––––––––––––––––––––"); | |
msg("\tAPI-Key : $API_KEY"); | |
msg("\tURL : $URL"); | |
msg("\tAPI version: $VERSION"); | |
echo "\n\n"; | |
$deleteCandidates = []; | |
foreach (api("listAllPads")->padIDs as $pad) { | |
$pad = urlencode($pad); | |
$revs = api("getRevisionsCount", ["padID=$pad"]); | |
if ($revs->revisions < 1) | |
array_push($deleteCandidates, $pad); | |
} | |
if (count($deleteCandidates) < 1) | |
error("noting to delete."); | |
msg("List of deletion candidates\n"); | |
foreach ($deleteCandidates as $pad) { | |
$content = api("getText", ["padID=$pad"]); | |
$lastEdit = date("d.m.Y", intVal(api("getLastEdited", ["padID=$pad"])->lastEdited / 1000)); | |
$text = str_replace("\n", "", $content->text); | |
msg("Delete pad '$pad'?"); | |
msg("\tLast edited : $lastEdit"); | |
msg("\tContent : $text"); | |
msg("–––––––––––––––––––––––––––––––––––\n"); | |
$answer = readline("[y/N]?"); | |
$shouldDelete = $answer && $answer === "y"; | |
if ($shouldDelete) { | |
$response = api("deletePad", ["padID=$pad"]); | |
msg("\t----[DELETE] pad '$pad' has been deleted.\n\n"); | |
} else | |
msg("\t----[KEEP] pad '$pad' has _not_ been deleted."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
put into
bin
folder and call it with the URL of the etherpad installation as first argument:it will find all etherpads with
0
revisions and asks for every item if it should be deleted.