Difference between revisions of "Add a wiki database"

From Organic Design wiki
m
({{legacy}})
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{legacy}}
 
{{procedure
 
{{procedure
 
|description = This process defines the steps involved in adding a new version of the MediaWiki software to an existing environment created by [[Install a new server|installing a new server]].
 
|description = This process defines the steps involved in adding a new version of the MediaWiki software to an existing environment created by [[Install a new server|installing a new server]].
 
|role = Sysop
 
|role = Sysop
 +
|status = legacy
 
}}
 
}}
  
== Usage ==
+
New wiki databases are added by copying from a template database for the version of MediaWiki the wiki will run on. A template exist for every code-base in the wikia because they get created during the [[install a MediaWiki code-base]] procedure.
  
'''Note:''' The databases referred to in the add-db command must pre-exist
+
If you intend on creating the new wiki in a new database, you'll first need to create the new database manually, for example by logging in to MySQL:
 +
<source>
 +
create database foo;
 +
</source>
 +
You can then make a copy of the template database and define a table prefix for the new wiki tables using our [[add-db.pl|add-db script]] as follows. The best practice is that a table prefix should always be used even if there is only one wiki, and the prefix should always end with an underscore character.
 +
<source>
 +
/var/www/tools/add-db  /var/www/empty-1.13.sql  foo.bar_  foo.baz_
 +
</source>
 +
In this example, two wiki databases have been added at once, both in the new "foo" database we created, one wiki has tables prefixed by "bar_" and the other by "baz_". Both new wiki's are copies of the ''empty-1.13.sql'' template which was created when the ''MediaWiki-1.13'' code-base was installed into the wikia.
  
<pre>
+
== Next ==
    add-db TEMPLATE DB1.PREFIX1 [DB2.]PREFIX2 [DB3.]PREFIX3...
+
After installing a wiki database, you can then [[install a new wiki]].
  
    Where,
+
== See also ==
        TEMPLATE is the name of the *.sql file or db.prefix to use as the template.
+
*[[Install a MediaWiki code-base]]
 
+
*[[Install a new wiki]]
        DB*.PREFIX* are the databases to replicate the template into and the
+
*[[add-db]]
        table prefixes to use (prefixes are mandatory).
+
[[Category:Wikia]]
</pre>
 
Notes:
 
*There is also an add-wiki script in MW maintenance dir which we need to check out
 
*The database dump being used as a template should only contain one wiki.
 
*It should not include create or drop database statements.
 
*The destination databases must already exist
 
*The destination tables will be replaced if they exist
 
*The DB part is optional for all but the first and means to continue using the previous database.\n"
 
 
 
== Script ==
 
<perl>
 
#!/usr/bin/perl
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Started: 2007-08-25 {{Category:OD2}}
 
use DBI;
 
 
 
$user = '********';
 
$pwd  = '********';
 
$tmp  = '/tmp/replicate-db';
 
 
 
# Display usage info and die if too few or no parameters supplied
 
die "\nReplicate a mediawiki SQL database dump to many databases and prefixes.
 
 
 
Usage:
 
        add-db TEMPLATE DB1.PREFIX1 [DB2.]PREFIX2 [DB3.]PREFIX3...
 
Where,
 
        TEMPLATE is the name of the *.sql file or db.prefix to use as the templa
 
te.
 
 
 
        DB*.PREFIX* are the databases to replicate the template into and the tab
 
le prefixes to use (prefixes are mandatory).
 
 
 
Notes:
 
- The database dump being used as a template should only contain one wiki.
 
- It should not include create or drop database statements.
 
- The destination databases must already exist
 
- The destination tables will be replaced if they exist
 
- The DB part is optional for all but the first and means to continue using the previous database.\n"
 
        unless $#ARGV > 0 and $ARGV[1] =~ /^\w+\.\w+$/;
 
 
 
# Read in the template SQL
 
$prefix = '';
 
$template = shift @ARGV;
 
if ($template !~ /\.sql$/i) {
 
 
 
        # Template is a database, so needs to be exported to tmp file first
 
        $template =~ /^(\w+)\.(\w+)$/;
 
        ($db,$prefix) = ($1,qr($2));
 
        $dbh = DBI->connect("dbi:mysql:$db",$user,$pwd)
 
                or die "\nCan't connect to database '$db': ",$DBI::errstr,"\n";
 
 
 
        # Obtain list of tables with selected prefix
 
        @tbl = ();
 
        $sth = $dbh->prepare('SHOW TABLES');
 
        $sth->execute() or die "\nCould not select tables: ",$DBI::errstr,"\n";
 
        while (@data = $sth->fetchrow_array()) { push @tbl,$data[0] if $data[0] =~ /^$prefix/ }
 
        die "\nNo tables found with matching prefix\n" if $#tbl < 0;
 
  $tbl = join ' ', @tbl;
 
 
 
        # Export tables to tmp file and read into $template
 
        unlink $tmp;
 
        qx(mysqldump -u $user --password='$pwd' $db $tbl > $tmp);
 
        $template = $tmp;
 
 
 
        }
 
 
 
# Read in the template file to $sql
 
open FH,'<',$template or die "\nCould not read template file '$template'\n";
 
sysread FH,$sql,-s $template;
 
close FH;
 
 
 
# Find the prefix (if not already set) being used in the template and prepare for use in replacement regexp
 
if ($prefix eq '') {
 
        die "\nThe template file supplied is not a valid wiki dump\n"
 
                unless $sql =~ /^CREATE TABLE `(\w*)recentchanges`/m;
 
        $prefix = qr($1);
 
        }
 
 
 
# Main replication loop
 
for (@ARGV) {
 
 
 
        # Determine the database and prefix to replicate template into
 
        if (/^(\w+)\.(\w+)$/) { ($db,$pre) = ($1,$2) } else { $pre = $_ }
 
        $pre .= '_' unless $pre =~ /_$/;
 
 
 
        # Make a duplicate of the template modified to the current prefix
 
        $data = $sql;
 
        $data =~ s/$prefix/$pre/g;
 
 
 
        # Write the duplicate into a tmp file
 
        unlink $tmp;
 
        if (open FH,'>',$tmp) { print FH $data; close FH }
 
        else { print "Could not write data for wiki $db.$pre to '$tmp'\n" }
 
 
 
        # Pipe the file into MySQL and remove the tmp file
 
        qx(mysql -u $user --password='$pwd' $db < $tmp);
 
        if ($!) { print "Error creating wiki $db.$pre: $!\n" }
 
        else    { print "Wiki $db.$pre created.\n" }
 
        }
 
</perl>
 

Latest revision as of 22:31, 24 June 2020

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.
Procedure.svg Add a wiki database
Organic Design procedure

New wiki databases are added by copying from a template database for the version of MediaWiki the wiki will run on. A template exist for every code-base in the wikia because they get created during the install a MediaWiki code-base procedure.

If you intend on creating the new wiki in a new database, you'll first need to create the new database manually, for example by logging in to MySQL:

create database foo;

You can then make a copy of the template database and define a table prefix for the new wiki tables using our add-db script as follows. The best practice is that a table prefix should always be used even if there is only one wiki, and the prefix should always end with an underscore character.

/var/www/tools/add-db  /var/www/empty-1.13.sql  foo.bar_  foo.baz_

In this example, two wiki databases have been added at once, both in the new "foo" database we created, one wiki has tables prefixed by "bar_" and the other by "baz_". Both new wiki's are copies of the empty-1.13.sql template which was created when the MediaWiki-1.13 code-base was installed into the wikia.

Next

After installing a wiki database, you can then install a new wiki.

See also