From 85289f85554b2a1eda911ad08e3298c558218918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 11 Jun 2025 14:15:13 +0200 Subject: [PATCH] allow to add wiki pages from the repository --- checks/test.nix | 27 ++++++-- modules/nixos-wiki/default.nix | 3 + modules/nixos-wiki/pages.nix | 120 +++++++++++++++++++++++++++++++++ pages/wiki-sync-test-page.wiki | 16 +++++ 4 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 modules/nixos-wiki/pages.nix create mode 100644 pages/wiki-sync-test-page.wiki diff --git a/checks/test.nix b/checks/test.nix index a587775..d4852f2 100644 --- a/checks/test.nix +++ b/checks/test.nix @@ -25,7 +25,16 @@ emergencyContact = "nixos-wiki@thalheim.io"; passwordSender = "nixos-wiki@thalheim.io"; noReplyAddress = "nixos-wiki-no-reply@thalheim.io"; + pages = { + pageConfig = { + "wiki-sync-test-page.wiki" = { + title = "Wiki Sync Test Page"; + namespace = ""; + }; + }; + }; }; + services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = { enableACME = false; forceSSL = false; @@ -36,11 +45,21 @@ testScript = '' start_all() - machine.wait_for_unit("phpfpm-mediawiki.service") - machine.wait_for_unit("nginx.service") - machine.wait_for_unit("mediawiki-init.service") + wiki.wait_for_unit("phpfpm-mediawiki.service") + wiki.wait_for_unit("nginx.service") + wiki.wait_for_unit("mediawiki-init.service") - page = machine.succeed("curl -vL http://nixos-wiki.example.com/") + page = wiki.succeed("curl -vL http://nixos-wiki.example.com/") assert "MediaWiki has been installed" in page + + # Test wiki pages sync functionality + print("Testing wiki pages sync...") + + # Check that the test page was created on the wiki + print("Checking if test page was created...") + test_page = wiki.succeed("curl -s http://nixos-wiki.example.com/wiki/Wiki_Sync_Test_Page") + + # Check for title in HTML + assert "Automatic synchronization from git repository" in test_page, f"Expected title not found in test page: {test_page}" ''; } diff --git a/modules/nixos-wiki/default.nix b/modules/nixos-wiki/default.nix index c61f703..3f988b1 100644 --- a/modules/nixos-wiki/default.nix +++ b/modules/nixos-wiki/default.nix @@ -8,6 +8,9 @@ let cfg = config.services.nixos-wiki; in { + imports = [ + ./pages.nix + ]; options = { services.nixos-wiki = { hostname = lib.mkOption { diff --git a/modules/nixos-wiki/pages.nix b/modules/nixos-wiki/pages.nix new file mode 100644 index 0000000..517b229 --- /dev/null +++ b/modules/nixos-wiki/pages.nix @@ -0,0 +1,120 @@ +{ + config, + lib, + pkgs, + ... +}: + +with lib; + +let + cfg = config.services.nixos-wiki.pages; + + # Path to pages directory + pagesDir = lib.fileset.toSource { + root = ../../.; + fileset = lib.fileset.fileFilter (file: file.hasExt "wiki") ../../pages; + }; + + pageType = types.submodule { + options = { + title = mkOption { + type = types.str; + description = "Wiki page title"; + }; + namespace = mkOption { + type = types.str; + default = ""; + description = "Wiki namespace (empty for main namespace)"; + }; + }; + }; +in +{ + options.services.nixos-wiki.pages = { + + pageConfig = mkOption { + type = types.attrsOf pageType; + default = { }; + description = "Configuration for wiki pages, keyed by filename"; + example = { + "main-page.wiki" = { + title = "Main Page"; + namespace = ""; + }; + "help-editing.wiki" = { + title = "Editing"; + namespace = "Help"; + }; + }; + }; + }; + + config = mkIf (cfg.pageConfig != { }) { + + systemd.services.wiki-pages-sync = { + description = "Synchronize wiki pages from git repository"; + after = [ "mediawiki-init.service" ]; + requiredBy = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + User = "mediawiki"; + Group = "nginx"; + }; + environment = config.services.phpfpm.pools.mediawiki.phpEnv; + script = '' + # Read the JSON config + config='${builtins.toJSON cfg.pageConfig}' + + # Process each .wiki file (they're in the pages subdirectory) + for wiki_file in "${pagesDir}"/pages/*.wiki; do + # Skip if no .wiki files found + [ -e "$wiki_file" ] || continue + + filename=$(basename "$wiki_file") + + # Extract configuration for this file from JSON + title=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].title // empty') + namespace=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].namespace // ""') + + # Skip if no config found for this file + if [ -z "$title" ]; then + echo "Warning: No configuration found for $filename, skipping" + continue + fi + + # Construct full page title + if [ -n "$namespace" ]; then + full_title="$namespace:$title" + else + full_title="$title" + fi + + echo "Processing: $filename -> $full_title" + + # Read page content and add management comment + content=$(cat "$wiki_file") + content_with_comment=" + + $content" + + # Edit the page using maintenance script (no --user means system maintenance user) + echo "$content_with_comment" | ${config.services.phpfpm.pools.mediawiki.phpPackage}/bin/php \ + ${config.services.mediawiki.finalPackage}/share/mediawiki/maintenance/run.php edit \ + --summary="Updated $filename from git repository" \ + --bot \ + "$full_title" + + echo "Successfully updated: $full_title" + done + + echo "Wiki synchronization completed successfully" + ''; + }; + }; +} diff --git a/pages/wiki-sync-test-page.wiki b/pages/wiki-sync-test-page.wiki new file mode 100644 index 0000000..3340c3d --- /dev/null +++ b/pages/wiki-sync-test-page.wiki @@ -0,0 +1,16 @@ +{{DISPLAYTITLE:Wiki Sync Test Page}} +This is a '''test page''' for the git-to-wiki synchronization system. + +== Features == + +* Automatic synchronization from git repository +* Page protection management +* MediaWiki markup support + +== Test Content == + +This page is managed through the git repository and automatically deployed to the wiki. + +Changes made to this file in the repository will be reflected on the wiki after deployment. + +[[Category:Test]] \ No newline at end of file