Skip to content

Commit d943967

Browse files
authored
Merge pull request #263 from NixOS/wiki-pages
allow to add wiki pages from the repository
2 parents b91ad88 + 85289f8 commit d943967

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed

checks/test.nix

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@
2525
emergencyContact = "[email protected]";
2626
passwordSender = "[email protected]";
2727
noReplyAddress = "[email protected]";
28+
pages = {
29+
pageConfig = {
30+
"wiki-sync-test-page.wiki" = {
31+
title = "Wiki Sync Test Page";
32+
namespace = "";
33+
};
34+
};
35+
};
2836
};
37+
2938
services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = {
3039
enableACME = false;
3140
forceSSL = false;
@@ -36,11 +45,21 @@
3645
testScript = ''
3746
start_all()
3847
39-
machine.wait_for_unit("phpfpm-mediawiki.service")
40-
machine.wait_for_unit("nginx.service")
41-
machine.wait_for_unit("mediawiki-init.service")
48+
wiki.wait_for_unit("phpfpm-mediawiki.service")
49+
wiki.wait_for_unit("nginx.service")
50+
wiki.wait_for_unit("mediawiki-init.service")
4251
43-
page = machine.succeed("curl -vL http://nixos-wiki.example.com/")
52+
page = wiki.succeed("curl -vL http://nixos-wiki.example.com/")
4453
assert "MediaWiki has been installed" in page
54+
55+
# Test wiki pages sync functionality
56+
print("Testing wiki pages sync...")
57+
58+
# Check that the test page was created on the wiki
59+
print("Checking if test page was created...")
60+
test_page = wiki.succeed("curl -s http://nixos-wiki.example.com/wiki/Wiki_Sync_Test_Page")
61+
62+
# Check for title in HTML
63+
assert "Automatic synchronization from git repository" in test_page, f"Expected title not found in test page: {test_page}"
4564
'';
4665
}

modules/nixos-wiki/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let
88
cfg = config.services.nixos-wiki;
99
in
1010
{
11+
imports = [
12+
./pages.nix
13+
];
1114
options = {
1215
services.nixos-wiki = {
1316
hostname = lib.mkOption {

modules/nixos-wiki/pages.nix

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}:
7+
8+
with lib;
9+
10+
let
11+
cfg = config.services.nixos-wiki.pages;
12+
13+
# Path to pages directory
14+
pagesDir = lib.fileset.toSource {
15+
root = ../../.;
16+
fileset = lib.fileset.fileFilter (file: file.hasExt "wiki") ../../pages;
17+
};
18+
19+
pageType = types.submodule {
20+
options = {
21+
title = mkOption {
22+
type = types.str;
23+
description = "Wiki page title";
24+
};
25+
namespace = mkOption {
26+
type = types.str;
27+
default = "";
28+
description = "Wiki namespace (empty for main namespace)";
29+
};
30+
};
31+
};
32+
in
33+
{
34+
options.services.nixos-wiki.pages = {
35+
36+
pageConfig = mkOption {
37+
type = types.attrsOf pageType;
38+
default = { };
39+
description = "Configuration for wiki pages, keyed by filename";
40+
example = {
41+
"main-page.wiki" = {
42+
title = "Main Page";
43+
namespace = "";
44+
};
45+
"help-editing.wiki" = {
46+
title = "Editing";
47+
namespace = "Help";
48+
};
49+
};
50+
};
51+
};
52+
53+
config = mkIf (cfg.pageConfig != { }) {
54+
55+
systemd.services.wiki-pages-sync = {
56+
description = "Synchronize wiki pages from git repository";
57+
after = [ "mediawiki-init.service" ];
58+
requiredBy = [ "multi-user.target" ];
59+
serviceConfig = {
60+
Type = "oneshot";
61+
User = "mediawiki";
62+
Group = "nginx";
63+
};
64+
environment = config.services.phpfpm.pools.mediawiki.phpEnv;
65+
script = ''
66+
# Read the JSON config
67+
config='${builtins.toJSON cfg.pageConfig}'
68+
69+
# Process each .wiki file (they're in the pages subdirectory)
70+
for wiki_file in "${pagesDir}"/pages/*.wiki; do
71+
# Skip if no .wiki files found
72+
[ -e "$wiki_file" ] || continue
73+
74+
filename=$(basename "$wiki_file")
75+
76+
# Extract configuration for this file from JSON
77+
title=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].title // empty')
78+
namespace=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].namespace // ""')
79+
80+
# Skip if no config found for this file
81+
if [ -z "$title" ]; then
82+
echo "Warning: No configuration found for $filename, skipping"
83+
continue
84+
fi
85+
86+
# Construct full page title
87+
if [ -n "$namespace" ]; then
88+
full_title="$namespace:$title"
89+
else
90+
full_title="$title"
91+
fi
92+
93+
echo "Processing: $filename -> $full_title"
94+
95+
# Read page content and add management comment
96+
content=$(cat "$wiki_file")
97+
content_with_comment="<!--
98+
This page is automatically managed through git repository synchronization.
99+
Do not edit this page directly on the wiki - changes will be overwritten.
100+
To edit this page, modify the file: $filename
101+
Source: https://github.com/NixOS/nixos-wiki-infra/blob/main/pages/$filename
102+
-->
103+
104+
$content"
105+
106+
# Edit the page using maintenance script (no --user means system maintenance user)
107+
echo "$content_with_comment" | ${config.services.phpfpm.pools.mediawiki.phpPackage}/bin/php \
108+
${config.services.mediawiki.finalPackage}/share/mediawiki/maintenance/run.php edit \
109+
--summary="Updated $filename from git repository" \
110+
--bot \
111+
"$full_title"
112+
113+
echo "Successfully updated: $full_title"
114+
done
115+
116+
echo "Wiki synchronization completed successfully"
117+
'';
118+
};
119+
};
120+
}

pages/wiki-sync-test-page.wiki

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{DISPLAYTITLE:Wiki Sync Test Page}}
2+
This is a '''test page''' for the git-to-wiki synchronization system.
3+
4+
== Features ==
5+
6+
* Automatic synchronization from git repository
7+
* Page protection management
8+
* MediaWiki markup support
9+
10+
== Test Content ==
11+
12+
This page is managed through the git repository and automatically deployed to the wiki.
13+
14+
Changes made to this file in the repository will be reflected on the wiki after deployment.
15+
16+
[[Category:Test]]

0 commit comments

Comments
 (0)