Files
moko-platform/validate/check_wiki_health.php
T
Jonathan Miller 1d87be7d5e
Branch Policy Check / Verify merge target (pull_request) Successful in 1s
fix: standardize file headers — REPO rename, SPDX case, missing fields
- Update REPO: from MokoStandards-API to moko-platform in 125 files
- Fix wrong org path (mokoconsulting-tech → MokoConsulting) in 10 files
- Fix SPDX-LICENSE-IDENTIFIER case in 2 template files
- Add missing REPO: field to 3 files

Authored-by: Moko Consulting

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-11 17:01:17 -05:00

154 lines
4.9 KiB
PHP

#!/usr/bin/env php
<?php
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* FILE INFORMATION
* DEFGROUP: MokoStandards.Validate
* INGROUP: MokoStandards
* REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform
* PATH: /validate/check_wiki_health.php
* BRIEF: Validate wiki health — checks Home page exists, has MokoStandards link, pages are indexed
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use MokoEnterprise\CLIApp;
class CheckWikiHealth extends CLIApp
{
public function __construct()
{
parent::__construct('check-wiki-health', 'Validate wiki health for a repository', '01.00.00');
}
protected function setupArguments(): array
{
return [
'path:' => 'Repository path (default: current directory)',
'gitea-url:' => 'Gitea base URL (default: https://git.mokoconsulting.tech)',
'token:' => 'Gitea API token (or set GITEA_TOKEN env var)',
];
}
protected function run(): int
{
$repoPath = realpath($this->getOption('path', '.')) ?: '.';
$giteaUrl = $this->getOption('gitea-url', 'https://git.mokoconsulting.tech');
$token = $this->getOption('token', getenv('GITEA_TOKEN') ?: '');
// Detect repo owner/name from git config
$configFile = $repoPath . '/.git/config';
$remote = '';
if (is_file($configFile)) {
$config = file_get_contents($configFile);
if (preg_match('/url\s*=\s*(.+)/', $config, $m)) {
$remote = trim($m[1]);
}
}
if (empty($remote)) {
$this->log('Cannot determine git remote — skipping wiki check', 'WARNING');
return 0;
}
// Parse owner/repo from remote URL
if (preg_match('#[:/]([^/]+)/([^/.]+?)(?:\.git)?$#', $remote, $m)) {
$owner = $m[1];
$repo = $m[2];
} else {
$this->log("Cannot parse owner/repo from remote: {$remote}", 'WARNING');
return 0;
}
$this->log("Checking wiki: {$owner}/{$repo}");
$issues = 0;
// Check wiki pages via API
$apiUrl = "{$giteaUrl}/api/v1/repos/{$owner}/{$repo}/wiki/pages";
$headers = $token ? ["Authorization: token {$token}"] : [];
$pages = $this->apiGet($apiUrl, $headers);
if ($pages === null) {
$this->log(' No wiki found or API error', 'WARNING');
$issues++;
if ($this->jsonOutput) {
echo json_encode(['status' => 'no_wiki', 'issues' => $issues]);
}
return 0;
}
$pageCount = count($pages);
$this->log(" Found {$pageCount} wiki page(s)");
// Check Home exists
$hasHome = false;
$pageTitles = [];
foreach ($pages as $page) {
$title = $page['title'] ?? '';
$pageTitles[] = $title;
if (strtolower($title) === 'home') {
$hasHome = true;
}
}
if (!$hasHome) {
$this->log(' FAIL: No Home page', 'ERROR');
$issues++;
} else {
$this->log(' OK: Home page exists');
}
// Check Home has MokoStandards link
if ($hasHome) {
$homeUrl = "{$giteaUrl}/api/v1/repos/{$owner}/{$repo}/wiki/page/Home";
$home = $this->apiGet($homeUrl, $headers);
if ($home) {
$content = base64_decode($home['content_base64'] ?? '');
if (stripos($content, 'moko-platform/wiki') !== false || stripos($content, 'MokoStandards') !== false) {
$this->log(' OK: Has MokoStandards reference');
} else {
$this->log(' WARN: Home page missing MokoStandards reference', 'WARNING');
$issues++;
}
}
}
if ($this->jsonOutput) {
echo json_encode([
'repo' => "{$owner}/{$repo}",
'pages' => $pageCount,
'has_home' => $hasHome,
'issues' => $issues,
'page_titles' => $pageTitles,
], JSON_PRETTY_PRINT);
}
return $issues > 0 ? 1 : 0;
}
private function apiGet(string $url, array $headers = []): ?array
{
$ctx = stream_context_create([
'http' => [
'method' => 'GET',
'header' => array_merge(['Accept: application/json'], $headers),
'timeout' => 10,
'ignore_errors' => true,
],
'ssl' => ['verify_peer' => false],
]);
$response = @file_get_contents($url, false, $ctx);
if ($response === false) return null;
$data = json_decode($response, true);
return is_array($data) ? $data : null;
}
}
(new CheckWikiHealth())->execute();