4 * Copyright (C) 2010 Olaf Lenz
5 * Copyright 2022, Franck Villaume - TrivialDev
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * This script will automatically create mediawiki instances for
26 * projects that do not yet have it.
28 * It is intended to be started in a cronjob.
31 # TODO: How to use cronjob history?
32 # Required config variables:
33 # src_path: the directory where the mediawiki sources are installed
35 require_once dirname(__FILE__) . '/../../../www/env.inc.php';
36 require_once $gfcommon.'include/pre.php';
37 require_once $gfcommon.'include/cron_utils.php';
39 $src_path = forge_get_config('src_path', 'mediawiki');
40 $master_path = forge_get_config('master_path', 'mediawiki');
44 # Get all projects that use the mediawiki plugin
45 $project_res = db_query_params ("SELECT g.unix_group_name from groups g, group_plugin gp, plugins p where g.group_id = gp.group_id and gp.plugin_id = p.plugin_id and p.plugin_name = $1;", array("mediawiki"));
47 $err = "Error: Database Query Failed: ".db_error();
49 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
53 # Loop over all projects that use the plugin
54 while ( $row = db_fetch_array($project_res) ) {
55 $project = $row['unix_group_name'];
56 $project_dir = forge_get_config('projects_path', 'mediawiki')
58 cron_debug("Checking $project...");
60 $res = db_query_params("SET search_path=public", array());
61 $res = db_query_params('DELETE FROM plugin_mediawiki_interwiki WHERE iw_prefix=$1', array($project));
62 $url = util_make_url('/plugins/mediawiki/wiki/' . $project . '/index.php/$1');
63 $res = db_query_params('INSERT INTO plugin_mediawiki_interwiki VALUES ($1, $2, 1, 0)',
67 // Create the project directory if necessary
68 if (is_dir($project_dir)) {
69 cron_debug(" Project dir $project_dir exists, so I assume the project already exists.");
72 $schema = "plugin_mediawiki_$project";
73 // Sanitize schema name
74 $schema = strtr($schema, "-", "_");
78 cron_debug(" Creating schema $schema.");
79 $res = db_query_params("CREATE SCHEMA $schema", array());
81 $err = "Error: Schema Creation Failed: " .
85 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
89 cron_debug(" Creating mediawiki database.");
90 $table_file_generated = "$src_path/maintenance/postgres/tables-generated.sql";
91 if (file_exists($table_file_generated)) {
92 db_query_params("SET search_path=$schema", array());
93 $res = db_query_from_file($table_file_generated);
96 $err = "Error: Mediawiki Database Creation Failed: " . db_error();
98 db_query_params("SET search_path=public", array());
99 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
104 $table_file = "$src_path/maintenance/postgres/tables.sql";
105 if (!file_exists($table_file)) {
107 $err = "Error: Couldn't find Mediawiki Database Creation File $table_file!";
109 db_query_params("SET search_path=public", array());
110 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
113 db_query_params("SET search_path=$schema", array());
114 $res = db_query_from_file($table_file);
117 $err = "Error: Mediawiki Database Creation Failed: " . db_error();
119 db_query_params("SET search_path=public", array());
120 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
124 $table_file_updatekeys = "$src_path/maintenance/postgres/update-keys.sql";
125 if (file_exists($table_file_updatekeys)) {
126 db_query_params("SET search_path=$schema", array());
127 $res = db_query_from_file($table_file_updatekeys);
130 $err = "Error: Mediawiki Database Creation Failed: " . db_error();
132 db_query_params("SET search_path=public", array());
133 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
138 $res = db_query_params("CREATE TEXT SEARCH CONFIGURATION $schema.default ( COPY = pg_catalog.english )", array());
141 $err = "Error: DB Query Failed: " . db_error();
143 db_query_params("SET search_path=public", array());
144 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
150 $err = "Error: DB Commit Failed: " . db_error();
152 db_query_params("SET search_path=public", array());
153 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS',$err);
157 cron_debug(" Creating project dir $project_dir.");
158 mkdir($project_dir, 0775, true);
159 chmod($project_dir, 0775);
161 $f = fopen("$project_dir/ProjectSettings.php", "w");
163 // Insert your project-local configuration here
166 chmod("$project_dir/ProjectSettings.php", 0775);
168 $mwwrapper = forge_get_config('source_path')."/plugins/mediawiki/bin/mw-wrapper.php" ;
169 $dumpfile = forge_get_config('config_path')."/plugins/mediawiki/initial-content.xml" ;
171 system ("$mwwrapper $project update.php --quick > /dev/null") ;
173 if (file_exists ($dumpfile)) {
174 system ("$mwwrapper $project importDump.php $dumpfile") ;
175 system ("$mwwrapper $project rebuildrecentchanges.php") ;
180 db_query_params("SET search_path=public", array());
181 cron_entry('PLUGIN_MEDIAWIKI_CREATE_WIKIS', $err);