Skip to content

Instantly share code, notes, and snippets.

@miko007
Created April 6, 2022 09:43
Show Gist options
  • Save miko007/c35f32f00cbf920afed71d4684e9854c to your computer and use it in GitHub Desktop.
Save miko007/c35f32f00cbf920afed71d4684e9854c to your computer and use it in GitHub Desktop.
A cleanup script to remove orphaned pads from an Etherpad Lite installation
#!/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.");
}
@miko007
Copy link
Author

miko007 commented Apr 6, 2022

put into bin folder and call it with the URL of the etherpad installation as first argument:

./cleanup.php https://etherpad.domain.tld

it will find all etherpads with 0 revisions and asks for every item if it should be deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment