Use server side CSS based space state indicator
This commit is contained in:
parent
c97750a79c
commit
d06ab44330
4 changed files with 77 additions and 17 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -97,3 +97,4 @@ extensions/AdminLinks
|
|||
extensions/PageForms
|
||||
extensions/SemanticMediaWiki
|
||||
extensions/SemanticResultFormats
|
||||
extensions/Headscript
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -1,45 +1,87 @@
|
|||
<?php
|
||||
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate");
|
||||
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;
|
||||
$is_open = apcu_fetch(CACHE_KEY, $in_cache);
|
||||
$spacestate = apcu_fetch(CACHE_KEY, $in_cache);
|
||||
|
||||
if ($in_cache) {
|
||||
return $is_open;
|
||||
return $spacestate;
|
||||
}
|
||||
|
||||
$state = file_get_contents("https://techinc.nl/space/spacestate.json");
|
||||
$spacestate = SpaceState::Unknown;
|
||||
|
||||
if ($state == false) {
|
||||
return false;
|
||||
$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;
|
||||
}
|
||||
|
||||
$json = json_decode($state);
|
||||
// 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)) {
|
||||
return false;
|
||||
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
|
||||
return $spacestate;
|
||||
}
|
||||
|
||||
$is_open = $json?->open ?? false;
|
||||
// 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;
|
||||
}
|
||||
|
||||
apcu_store(CACHE_KEY, $is_open, CACHE_TTL);
|
||||
$spacestate = $state?->open ?? SpaceState::Unknown;
|
||||
if ($spacestate === SpaceState::Unknown) {
|
||||
apcu_store(CACHE_KEY, $spacestate, CACHE_TTL);
|
||||
return $spacestate;
|
||||
}
|
||||
|
||||
return $is_open;
|
||||
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) { ?>
|
||||
<?php if ($spaceState === SpaceState::Open) { ?>
|
||||
#p-logo:after {
|
||||
content: "OPEN" ;
|
||||
border-radius: 3px;
|
||||
|
|
@ -48,7 +90,7 @@ $spaceState = getSpaceState();
|
|||
margin-left: 51px;
|
||||
padding: 3px 7px;
|
||||
}
|
||||
<?php } else { ?>
|
||||
<?php } else if ($spaceState === SpaceState::Closed) { ?>
|
||||
#p-logo:after {
|
||||
content: "CLOSED" ;
|
||||
border-radius: 3px;
|
||||
|
|
@ -57,4 +99,4 @@ $spaceState = getSpaceState();
|
|||
margin-left: 43px;
|
||||
padding: 3px 7px;
|
||||
}
|
||||
<?php } ?>
|
||||
<?php } } ?>
|
||||
|
|
|
|||
Loading…
Reference in a new issue