Compare commits

...

2 commits

Author SHA1 Message Date
www-data
f98ef04d7b Use server side CSS based space state indicator 2025-02-17 15:24:42 +01:00
www-data
a0b7f10db5 Add spacestate CSS generator 2025-02-17 14:06:38 +01:00
4 changed files with 123 additions and 3 deletions

1
.gitignore vendored
View file

@ -97,3 +97,4 @@ extensions/AdminLinks
extensions/PageForms
extensions/SemanticMediaWiki
extensions/SemanticResultFormats
extensions/Headscript

View file

@ -185,9 +185,8 @@ wfLoadExtension( 'Nuke' );
wfLoadExtension( 'ParserFunctions' );
wfLoadExtension( 'TextExtracts' );
wfLoadExtension( 'WikiEditor' );
wfLoadExtension( 'PageForms' );
wfLoadExtension( 'Headscript' );
wfLoadExtension( 'SemanticMediaWiki' );
wfLoadExtension( 'SemanticResultFormats' );
@ -238,4 +237,8 @@ $wgGroupPermissions['emailconfirmed']['edit'] = true;
#$wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
#$wgIgnoreImageErrors = true;
$wgHeadScriptCode = <<<'START_END_MARKER'
<link href="spacestate.php" rel="stylesheet" />
START_END_MARKER;
$wgMemoryLimit = "64M";

View file

@ -9,7 +9,8 @@
"mediawiki/semantic-result-formats": "dev-master#616fd091cdce78d706d13e8b10bf70087615bcd4",
"mediawiki/admin-links": "dev-master#4cec145ad82dbea4e0155bf9abc0ae566547da7b",
"mediawiki/page-forms": "dev-master#d8109745866f0860f66aecd31e009b68e5bedaf1",
"x-mediawiki/confirm-edit": "^1.43"
"x-mediawiki/confirm-edit": "^1.43",
"x-mediawiki/headscript": "^1.43"
},
"repositories": [
{
@ -28,6 +29,19 @@
"reference": "REL1_43"
}
}
},
{
"type": "package",
"package": {
"name": "x-mediawiki/headscript",
"type": "mediawiki-extension",
"version": "1.43.0",
"source": {
"type": "git",
"url": "https://gerrit.wikimedia.org/r/mediawiki/extensions/HeadScript.git",
"reference": "REL1_43"
}
}
}
],
"extra": {

102
spacestate.php Normal file
View file

@ -0,0 +1,102 @@
<?php
header("Cache-Control: max-age=10, must-revalidate");
header("Content-Type: text/css");
enum SpaceState {
case Open;
case Closed;
case Unknown;
}
const CACHE_KEY = "techinc_spacestate";
const CACHE_TTL = 10;
function getSpaceState() {
$in_cache = false;
$spacestate = apcu_fetch(CACHE_KEY, $in_cache);
if ($in_cache) {
return $spacestate;
}
$spacestate = SpaceState::Unknown;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://techinc.nl/space/spacestate.json",
CURLOPT_TIMEOUT => 1,
CURLOPT_FORBID_REUSE => true,
CURLOPT_RETURNTRANSFER => true
));
$request = curl_exec($ch);
// Request failed either because the server is offline, or it took too long to respond.
// In that case, just return the default response of Unknown. Also cache this result
// to make sure we are not hammering the space state endpoint when it failed.
if ($request === false) {
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
return $spacestate;
}
// The returned JSON failed to be parsed somehow.
// This should never happen if the spacestate outputs correct JSON.
$json = json_decode($request);
if (is_null($json)) {
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
return $spacestate;
}
// Retrieve the state from the 'open' key in the JSON. If it does not exist,
// the space state JSON is incorrect and we don't actually know what the spacestate is.
$state = $json?->state ?? SpaceState::Unknown;
if ($state === SpaceState::Unknown) {
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
return $spacestate;
}
$spacestate = $state?->open ?? SpaceState::Unknown;
if ($spacestate === SpaceState::Unknown) {
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
return $spacestate;
}
if ($spacestate) {
$spacestate = SpaceState::Open;
} else {
$spacestate = SpaceState::Closed;
}
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
return $spacestate;
}
$spaceState = getSpaceState();
if ($spaceState !== SpaceState::Unknown) {
?>
#p-navigation:before {
content: " ";
white-space: pre;
}
<?php if ($spaceState === SpaceState::Open) { ?>
#p-logo:after {
content: "OPEN" ;
border-radius: 3px;
background: green;
color: white;
margin-left: 51px;
padding: 3px 7px;
}
<?php } else if ($spaceState === SpaceState::Closed) { ?>
#p-logo:after {
content: "CLOSED" ;
border-radius: 3px;
background: red;
color: white;
margin-left: 43px;
padding: 3px 7px;
}
<?php } } ?>