Skip to content

Commit

Permalink
Creating redirect service + fixing getMemberWikis
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinPayravi authored Dec 18, 2023
1 parent a9b97f3 commit c57c3b0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
16 changes: 11 additions & 5 deletions _api.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getMemberWikis($languageCode = null, $includeFormer = null)
$filteredMembers = [];

if ($languageCode) {
// If specific language is requested...
if($includeFormer) {
$filteredMembers = $this->memberWikis->{$languageCode};
} else {
Expand All @@ -66,14 +67,19 @@ function($val){
});
}
} else {
foreach ($this->memberWikis as $key => $data) {
$filteredMembers = array_merge($filteredMembers, $this->memberWikis->{$key});
// If all languages are requested...

// Merge language arrays:
$allMemberWikis = [];
foreach ($this->memberWikis as $memberWikis) {
$allMemberWikis = array_merge($allMemberWikis, $memberWikis);
}
if($includeFormer) {
$filteredMembers = $this->memberWikis->{$languageCode};

if ($includeFormer) {
$filteredMembers = $allMemberWikis;
} else {
$filteredMembers = array_filter(
$this->memberWikis->{$languageCode},
$allMemberWikis,
function($val){
return !($val->former ?? FALSE);
});
Expand Down
68 changes: 68 additions & 0 deletions go.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
$title = 'Redirect Service';
include('_header.php');

$dataHelper = new NiwaDataHelper();
$wikis = $dataHelper->getMemberWikis();

$url = $_SERVER['REQUEST_URI'];
$urlParts = parse_url($url);
$path = $urlParts['path'];
$pathSegments = explode('/', $path);

if (count($pathSegments) > 2) {
$site = $pathSegments[2] ?? null;
$article = implode('/', array_slice($pathSegments, 3)) ?? null;
$target = null;

foreach ($wikis as $wiki) {
if ($wiki->id === $site) {
$target = $wiki->url;
$target = str_replace('$1', $article, $target);
}
}

if ($target) {
header("Location: {$target}");
exit();
}
}
?>

<div class="main">
<h1>NIWA Redirect Service</h1>

This URL serves as a gateway to link to any article on any NIWA member wiki.

<br /><br />

URL format: <code>https://niwanetwork.org/go/{site}/{article}</code>

<br /><br />

Replace <code>{site}</code> with the site ID from the table below, and <code>{article}</code> with your desired article name.

<br /><br />

<table>
<tr>
<th>Site ID</th>
<th>Site URL</th>
</tr>
<?php
foreach ($wikis as $wiki) {
echo "
<tr>
<td>{$wiki->id}</td>
<td>{$wiki->url}</td>
</tr>
";
}
?>
</table>
</div>

<?php
include('_sidebar.php');
include('_footer.php');
?>
12 changes: 12 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ button[disabled]:hover {
cursor: not-allowed;
}

table {
border-collapse: collapse;
}
th {
background-color: #e9e9e9;
}
th, td {
padding: 8px;
}
tr:nth-child(odd) {
background-color: #ededed;
}

/* Color classes */
.white {
Expand Down

0 comments on commit c57c3b0

Please sign in to comment.