summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/apache-httpd/mediawiki.nix
blob: fa65ec0ef700aefe3e30be0d78330c501fc4e9bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{ config, lib, pkgs, serverInfo, php, ... }:

with lib;

let

  mediawikiConfig = pkgs.writeText "LocalSettings.php"
    ''
      <?php
        # Copied verbatim from the default (generated) LocalSettings.php.
        if( defined( 'MW_INSTALL_PATH' ) ) {
                $IP = MW_INSTALL_PATH;
        } else {
                $IP = dirname( __FILE__ );
        }

        $path = array( $IP, "$IP/includes", "$IP/languages" );
        set_include_path( implode( PATH_SEPARATOR, $path ) . PATH_SEPARATOR . get_include_path() );

        require_once( "$IP/includes/DefaultSettings.php" );

        if ( $wgCommandLineMode ) {
                if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
                        die( "This script must be run from the command line\n" );
                }
        }

        $wgScriptPath = "${config.urlPrefix}";

        # We probably need to set $wgSecretKey and $wgCacheEpoch.

        # Paths to external programs.
        $wgDiff3 = "${pkgs.diffutils}/bin/diff3";
        $wgDiff = "${pkgs.diffutils}/bin/diff";
        $wgImageMagickConvertCommand = "${pkgs.imagemagick}/bin/convert";

        #$wgDebugLogFile = "/tmp/mediawiki_debug_log.txt";

        # Database configuration.
        $wgDBtype = "${config.dbType}";
        $wgDBserver = "${config.dbServer}";
        $wgDBuser = "${config.dbUser}";
        $wgDBpassword = "${config.dbPassword}";
        $wgDBname = "${config.dbName}";

        # E-mail.
        $wgEmergencyContact = "${config.emergencyContact}";
        $wgPasswordSender = "${config.passwordSender}";

        $wgSitename = "${config.siteName}";

        ${optionalString (config.logo != "") ''
          $wgLogo = "${config.logo}";
        ''}

        ${optionalString (config.articleUrlPrefix != "") ''
          $wgArticlePath = "${config.articleUrlPrefix}/$1";
        ''}

        ${optionalString config.enableUploads ''
          $wgEnableUploads = true;
          $wgUploadDirectory = "${config.uploadDir}";
        ''}

        ${optionalString (config.defaultSkin != "") ''
          $wgDefaultSkin = "${config.defaultSkin}";
        ''}

        ${config.extraConfig}
      ?>
    '';

  # Unpack Mediawiki and put the config file in its root directory.
  mediawikiRoot = pkgs.stdenv.mkDerivation rec {
    name= "mediawiki-1.23.1";

    src = pkgs.fetchurl {
      url = "http://download.wikimedia.org/mediawiki/1.23/${name}.tar.gz";
      sha256 = "07z5j8d988cdg4ml4n0vs9fwmj0p594ibbqdid16faxwqm52dkhl";
    };

    patches = [ ./mediawiki-postgresql-fixes.patch ];

    skins = config.skins;

    buildPhase =
      ''
        for skin in $skins; do
          cp -prvd $skin/* skins/
        done
      ''; # */

    installPhase =
      ''
        ensureDir $out
        cp -r * $out
        cp ${mediawikiConfig} $out/LocalSettings.php
        sed -i \
        -e 's|/bin/bash|${pkgs.bash}/bin/bash|g' \
        -e 's|/usr/bin/timeout|${pkgs.coreutils}/bin/timeout|g' \
          $out/includes/limit.sh \
          $out/includes/GlobalFunctions.php
      '';
  };

  mediawikiScripts = pkgs.runCommand "mediawiki-${config.id}-scripts"
    { buildInputs = [ pkgs.makeWrapper ]; }
    ''
      ensureDir $out/bin
      for i in changePassword.php createAndPromote.php userOptions.php edit.php nukePage.php update.php; do
        makeWrapper ${php}/bin/php $out/bin/mediawiki-${config.id}-$(basename $i .php) \
          --add-flags ${mediawikiRoot}/maintenance/$i
      done
    '';

in

{

  extraConfig =
    ''
      ${optionalString config.enableUploads ''
        Alias ${config.urlPrefix}/images ${config.uploadDir}

        <Directory ${config.uploadDir}>
            Order allow,deny
            Allow from all
            Options -Indexes
        </Directory>
      ''}

      ${if config.urlPrefix != "" then "Alias ${config.urlPrefix} ${mediawikiRoot}" else ''
        RewriteEngine On
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
        RewriteRule ${if config.enableUploads
          then "!^/images"
          else "^.*\$"
        } %{DOCUMENT_ROOT}/${if config.articleUrlPrefix == ""
          then ""
          else "${config.articleUrlPrefix}/"
        }index.php [L]
      ''}

      <Directory ${mediawikiRoot}>
          Order allow,deny
          Allow from all
          DirectoryIndex index.php
      </Directory>

      ${optionalString (config.articleUrlPrefix != "") ''
        Alias ${config.articleUrlPrefix} ${mediawikiRoot}/index.php
      ''}
    '';

  documentRoot = if config.urlPrefix == "" then mediawikiRoot else null;

  enablePHP = true;

  options = {

    id = mkOption {
      default = "main";
      description = ''
        A unique identifier necessary to keep multiple MediaWiki server
        instances on the same machine apart.  This is used to
        disambiguate the administrative scripts, which get names like
        mediawiki-$id-change-password.
      '';
    };

    dbType = mkOption {
      default = "postgres";
      example = "mysql";
      description = "Database type.";
    };

    dbName = mkOption {
      default = "mediawiki";
      description = "Name of the database that holds the MediaWiki data.";
    };

    dbServer = mkOption {
      default = ""; # use a Unix domain socket
      example = "10.0.2.2";
      description = ''
        The location of the database server.  Leave empty to use a
        database server running on the same machine through a Unix
        domain socket.
      '';
    };

    dbUser = mkOption {
      default = "mediawiki";
      description = "The user name for accessing the database.";
    };

    dbPassword = mkOption {
      default = "";
      example = "foobar";
      description = ''
        The password of the database user.  Warning: this is stored in
        cleartext in the Nix store!
      '';
    };

    emergencyContact = mkOption {
      default = serverInfo.serverConfig.adminAddr;
      example = "admin@example.com";
      description = ''
        Emergency contact e-mail address.  Defaults to the Apache
        admin address.
      '';
    };

    passwordSender = mkOption {
      default = serverInfo.serverConfig.adminAddr;
      example = "password@example.com";
      description = ''
        E-mail address from which password confirmations originate.
        Defaults to the Apache admin address.
      '';
    };

    siteName = mkOption {
      default = "MediaWiki";
      example = "Foobar Wiki";
      description = "Name of the wiki";
    };

    logo = mkOption {
      default = "";
      example = "/images/logo.png";
      description = "The URL of the site's logo (which should be a 135x135px image).";
    };

    urlPrefix = mkOption {
      default = "/w";
      description = ''
        The URL prefix under which the Mediawiki service appears.
      '';
    };

    articleUrlPrefix = mkOption {
      default = "/wiki";
      example = "";
      description = ''
        The URL prefix under which article pages appear,
        e.g. http://server/wiki/Page.  Leave empty to use the main URL
        prefix, e.g. http://server/w/index.php?title=Page.
      '';
    };

    enableUploads = mkOption {
      default = false;
      description = "Whether to enable file uploads.";
    };

    uploadDir = mkOption {
      default = throw "You must specify `uploadDir'.";
      example = "/data/mediawiki-upload";
      description = "The directory that stores uploaded files.";
    };

    defaultSkin = mkOption {
      default = "";
      example = "nostalgia";
      description = "Set this value to change the default skin used by MediaWiki.";
    };

    skins = mkOption {
      default = [];
      type = types.listOf types.path;
      description =
        ''
          List of paths whose content is copied to the ‘skins’
          subdirectory of the MediaWiki installation.
        '';
    };

    extraConfig = mkOption {
      default = "";
      example =
        ''
          $wgEnableEmail = false;
        '';
      description = ''
        Any additional text to be appended to MediaWiki's
        configuration file.  This is a PHP script.  For configuration
        settings, see <link xlink:href='http://www.mediawiki.org/wiki/Manual:Configuration_settings'/>.
      '';
    };

  };

  extraPath = [ mediawikiScripts ];

  # !!! Need to specify that Apache has a dependency on PostgreSQL!

  startupScript = pkgs.writeScript "mediawiki_startup.sh"
    # Initialise the database automagically if we're using a Postgres
    # server on localhost.
    (optionalString (config.dbType == "postgres" && config.dbServer == "") ''
      if ! ${pkgs.postgresql}/bin/psql -l | grep -q ' ${config.dbName} ' ; then
          ${pkgs.postgresql}/bin/createuser --no-superuser --no-createdb --no-createrole "${config.dbUser}" || true
          ${pkgs.postgresql}/bin/createdb "${config.dbName}" -O "${config.dbUser}"
          ( echo 'CREATE LANGUAGE plpgsql;'
            cat ${mediawikiRoot}/maintenance/postgres/tables.sql
            echo 'CREATE TEXT SEARCH CONFIGURATION public.default ( COPY = pg_catalog.english );'
            echo COMMIT
          ) | ${pkgs.postgresql}/bin/psql -U "${config.dbUser}" "${config.dbName}"
      fi
      ${php}/bin/php ${mediawikiRoot}/maintenance/update.php
    '');

  robotsEntries = optionalString (config.articleUrlPrefix != "")
    ''
      User-agent: *
      Disallow: ${config.urlPrefix}/
      Disallow: ${config.articleUrlPrefix}/Special:Search
      Disallow: ${config.articleUrlPrefix}/Special:Random
    '';

}