1 #################################################################################
2 # Kiriwrite Database Module - MySQL 5.x Database Module (MySQL5.pm) #
3 # Database module for mainipulating data in a MySQL 5.x database. #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk> #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
9 # This program is free software; you can redistribute it and/or modify it under #
10 # the terms of the GNU General Public License as published by the Free #
11 # Software Foundation; as version 2 of the License. #
13 # This program is distributed in the hope that it will be useful, but WITHOUT #
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
17 # You should have received a copy of the GNU General Public License along with #
18 # this program; if not, write to the Free Software Foundation, Inc., 51 #
19 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
20 #################################################################################
22 # Define the package (perl module) name.
24 package Kiriwrite::Database::MySQL5;
26 # Enable strict and use warnings.
30 use Encode qw(decode_utf8);
32 # Load the following Perl modules.
34 use DBI qw(:sql_types);
36 # Set the following values.
38 our $VERSION = "0.1.0";
39 my ($options, %options);
44 my $database_filename;
45 my $second_database_filename;
47 #################################################################################
48 # Generic Subroutines. #
49 #################################################################################
52 #################################################################################
53 # new: Create an instance of Kiriwrite::Database::MySQL #
57 # $dbmodule = Kiriwrite::Database::SQLite->new(); #
58 #################################################################################
60 # Get the perl module name.
65 return bless($self, $class);
70 #################################################################################
71 # loadsettings: Loads settings into the SQLite database module #
75 # $dbmodule->loadsettings(Directory, options); #
77 # options Specifies the following options (in any order). #
79 # Directory Specifies the directory to use for getting databases. #
80 # DateTime Specifies the date and time format to use. #
81 # Server Specifies the server to use. #
82 # Database Specifies the database to use. #
83 # Username Specifies the username to use. #
84 # Password Specifies the password to use. #
85 # Port Specifies the server port to use. #
86 # Protocol Specifies the protocol to use. #
87 # TablePrefix Specifies the table prefix to use. #
88 #################################################################################
90 # Get the data passed to the subroutine.
93 my ($passedoptions) = @_;
95 # Add the directory setting to the list of options (as it's the only
96 # one needed for this database module).
99 "Directory" => $passedoptions->{"Directory"},
100 "DateTime" => $passedoptions->{"DateTime"},
101 "Server" => $passedoptions->{"Server"},
102 "Database" => $passedoptions->{"Database"},
103 "Username" => $passedoptions->{"Username"},
104 "Password" => $passedoptions->{"Password"},
105 "Port" => $passedoptions->{"Port"},
106 "Protocol" => $passedoptions->{"Protocol"},
107 "TablePrefix" => $passedoptions->{"TablePrefix"}
113 #################################################################################
114 # convert: Converts data into SQL formatted data. #
118 # $dbmodule->convert(data); #
120 # data Specifies the data to convert. #
121 #################################################################################
123 # Get the data passed to the subroutine.
140 #################################################################################
141 # dateconvert: Converts a SQL date into a proper date. #
145 # $dbmodule->dateconvert(date); #
147 # date Specifies the date to convert. #
148 #################################################################################
150 # Get the date passed to the subroutine.
155 # Convert the date given into the proper date.
157 # Create the following varialbes to be used later.
184 # Split the date and time.
186 $length = length($data);
192 # Get the character and check if it is a space.
194 $char = substr($data, $seek, 1);
198 # The character is a space, so get the date and time.
200 $date = substr($data, 0, $seek);
201 $timelength = $length - $seek - 1;
202 $time = substr($data, $seek + 1, $timelength);
208 } until ($seek eq $length);
210 # Get the year, month and date.
212 $length = length($date);
217 # Get the character and check if it is a dash.
219 $char = substr($date, $seek, 1);
223 # The character is a dash, so get the year, month or day.
225 $datelength = $seek - $startchar;
229 # Get the year from the date.
231 $year = substr($date, 0, $datelength) + 1900;
235 # Get the last two characters to get the short year
238 $year_short = substr($year, 2, 2);
240 } elsif ($count eq 1){
242 # Get the month and day from the date.
244 $month = substr($date, $startchar + 1, $datelength - 1) + 1;
246 # Check if the month is less then 10, if it is
247 # add a zero to the value.
251 $month_full = '0' . $month;
255 $month_full = $month;
262 $daylength = $length - $seek + 1;
263 $day = substr($date, $startchar + 1, $daylength);
267 # Check if the day is less than 10, if it is
268 # add a zero to the value.
272 $day_full = '0' . $day;
286 } until ($seek eq $length);
288 # Get the length of the time value and reset certain
291 $length = length($time);
298 # Get the character and check if it is a colon.
300 $char = substr($time, $seek, 1);
304 # The character is a colon, so get the hour, minute and day.
306 $timelength = $seek - $startchar;
310 # Get the hour from the time.
312 $hour = substr($time, 0, $timelength);
317 # If the hour is less than ten then add a
322 $hour_full = '0' . $hour;
330 } elsif ($count eq 1){
332 # Get the minute and second from the time.
334 $minute = substr($time, $startchar + 1, $timelength - 1);
338 # If the minute is less than ten then add a
343 $minute_full = '0' . $minute;
347 $minute_full = $minute;
353 $secondlength = $length - $seek + 1;
354 $second = substr($time, $startchar + 1, $secondlength);
357 # If the second is less than ten then add a
362 $second_full = '0' . $second;
366 $second_full = $second;
376 } until ($seek eq $length);
378 # Get the setting for displaying the date and time.
380 $data = $options{"DateTime"};
382 # Process the setting for displaying the date and time
383 # using regular expressions
385 $data =~ s/DD/$day_full/g;
387 $data =~ s/MM/$month_full/g;
388 $data =~ s/M/$month/g;
389 $data =~ s/YY/$year/g;
390 $data =~ s/Y/$year_short/g;
392 $data =~ s/hh/$hour_full/g;
393 $data =~ s/h/$hour/g;
394 $data =~ s/mm/$minute_full/g;
395 $data =~ s/m/$minute/g;
396 $data =~ s/ss/$second_full/g;
397 $data =~ s/s/$second/g;
406 #################################################################################
407 # geterror: Gets the error message (or extended error message). #
411 # $dbmodule->geterror(extended); #
413 # Extended Specifies if the extended error should be retrieved. #
414 #################################################################################
416 # Get the data passed to the subroutine.
419 my $extended = shift;
433 # Check to see if extended information should be returned.
437 # Extended information should be returned.
443 # Basic information should be returned.
452 #################################################################################
453 # dbpermissions: Check if the permissions for the database are valid. #
457 # $database->dbpermissions(dbname, read, write); #
459 # dbname Specifies the database name to check. #
460 # read Check to see if the database can be read. #
461 # write Check to see if the database can be written. #
462 #################################################################################
464 # This subroutine is not needed for this database module.
471 #################################################################################
472 # dbexists: Check if the database exists. #
476 # $dbmodule->dbexists(dbname); #
478 # dbname Specifies the database name to check. #
479 #################################################################################
484 # Get the value that was passed to the subroutine.
490 my $table_exists = 0;
492 # Check if the table exists.
494 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($filename) . '_database_info\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
495 $string_handle->execute();
497 while (@table_data = $string_handle->fetchrow_array()){
503 # Check if the table really does exist.
505 if ($table_exists eq 1){
507 # The table exists so return a value of 0.
513 # The table does not exist so return a value of 1.
522 #################################################################################
523 # General subroutines. #
524 #################################################################################
527 #################################################################################
528 # connect: Connect to the server. #
532 # $dbmodule->connect(); #
533 #################################################################################
538 # Connect to the server.
540 $database_handle = DBI->connect("DBI:mysql:database=" . $options{"Database"} . ";host=" . $options{"Server"} . ";protocol=" . $options{"Protocol"} . "port=" . $options{"Port"}, $options{"Username"}, $options{"Password"}, { "mysql_enable_utf8" => 1 }) or ( $error = "DatabaseConnectionError", $errorext = DBI->errstr, return );
541 $database_handle->do('SET CHARACTER SET utf8');
542 $database_handle->do('SET NAMES utf8');
547 #################################################################################
548 # connect: Disconnect from the server. #
552 # $dbmodule->disconnect(); #
553 #################################################################################
555 # Disconnect from the server.
559 $string_handle->finish();
563 if ($database_handle){
565 $database_handle->disconnect();
571 #################################################################################
572 # Database Subroutines. #
573 #################################################################################
576 #################################################################################
577 # getdblist: Gets the list of available databases. #
581 # $dbmodule->getdblist(); #
582 #################################################################################
587 # Get the list of databases.
589 $string_handle = $database_handle->prepare("SHOW TABLES") or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
590 $string_handle->execute();
592 my @final_table_list;
593 my @database_table_list;
597 while (@table_name = $string_handle->fetchrow_array()){
599 push(@database_table_list, decode_utf8($table_name[0]));
603 my $table_prefix = $options{"TablePrefix"};
605 # Find all the database information tables with the correct table prefix.
607 @database_table_list = grep /^$table_prefix/ , @database_table_list;
608 @database_table_list = grep /m*database_info$/ , @database_table_list;
610 foreach $table (@database_table_list){
612 # Process each table name removing the table prefix name and
613 # the _database_info part.
615 $table =~ s/^$table_prefix(_)//g;
616 $table =~ s/_database_info$//g;
618 push (@final_table_list, $table);
622 # Return the final list of databases.
624 return @final_table_list;
629 #################################################################################
630 # selectdb: Selects the Kiriwrite database. #
634 # $dbmodule->connect(options); #
636 # options Specifies the following options in any order. #
638 # DatabaseName Specifies the Kiriwrite database to use. #
639 #################################################################################
641 # Get the database name.
647 my ($passedoptions) = @_;
649 my $dbname = $passedoptions->{"DatabaseName"};
651 my $database_exists = $class->dbexists($dbname);
653 if ($database_exists eq 1){
655 # The database does not exist so return an error value
656 # saying that the database does not exist.
658 $error = "DoesNotExist";
664 $database_filename = $dbname;
669 #################################################################################
670 # getdatabaseinfo: Get information about the database. #
674 # $dbmodule->getdatabaseinfo(); #
675 #################################################################################
677 # Get the database information.
683 my ($databaseinfo, %databaseinfo);
684 my ($sqldata, @sqldata);
686 $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
687 $string_handle->execute();
689 @sqldata = $string_handle->fetchrow_array();
691 # Process the database information into a hash.
695 "DatabaseName" => decode_utf8($sqldata[0]),
696 "Description" => decode_utf8($sqldata[1]),
697 "Notes" => decode_utf8($sqldata[2]),
698 "Categories" => decode_utf8($sqldata[3]),
699 "Major" => decode_utf8($sqldata[4]),
700 "Minor" => decode_utf8($sqldata[5]),
701 "Revision" => decode_utf8($sqldata[6])
704 $string_handle->finish();
706 return %databaseinfo;
711 #################################################################################
712 # selectseconddb: Selects a second Kiriwrite database for moving and copying #
717 # $dbmodule->selectseconddb(options); #
719 # options Specifies the following options in any order. #
721 # DatabaseName Specifies the Kiriwrite database to use. #
722 #################################################################################
724 # Get the database name.
730 my ($passedoptions) = @_;
731 my (%database, $database);
733 my $dbname = $passedoptions->{"DatabaseName"};
735 # Check if the database exists.
737 my $database_exists = $class->dbexists($dbname);
739 if ($database_exists eq 1){
741 # The database does not exist so return an error value
742 # saying that the database does not exist.
744 $error = "DoesNotExist";
750 # Set the second database filename.
752 $second_database_filename = $dbname;
756 sub getseconddatabaseinfo{
757 #################################################################################
758 # getseconddatabaseinfo: Get information about the database that pages will be #
759 # moved or copied to. #
763 # $dbmodule->getseconddatabaseinfo(); #
764 #################################################################################
767 # Get the database information.
770 my ($databaseinfo, %databaseinfo);
771 my ($sqldata, @sqldata);
776 $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
777 $string_handle->execute();
779 @sqldata = $string_handle->fetchrow_array();
781 # Process the database information into a hash.
784 "DatabaseName" => decode_utf8($sqldata[0]),
785 "Description" => decode_utf8($sqldata[1]),
786 "Notes" => decode_utf8($sqldata[2]),
787 "Categories" => decode_utf8($sqldata[3]),
788 "Major" => decode_utf8($sqldata[4]),
789 "Minor" => decode_utf8($sqldata[5]),
790 "Revision" => decode_utf8($sqldata[6])
793 $string_handle->finish();
795 return %databaseinfo;
800 #################################################################################
801 # adddatabase: Adds a Kiriwrite database. #
805 # $dbmodule->adddatabase(options); #
807 # options Specifies the following options in any order. #
809 # DatabaseFilename Specifies the database file/shortname to use. #
810 # DatabaseName Specifies the database name to use. #
811 # DatabaseDescription Specifies the database description to use. #
812 # DatabaseNotes Specifies the database notes to use. #
813 # DatabaseCategories Specifies the database categories to use. #
814 # VersionMajor Specifies the major version. #
815 # VersionMinor Specifies the minor version. #
816 # VersionRevision Specifies the revision version. #
817 #################################################################################
819 # Get the database that was passed to the subroutine.
825 my ($passedoptions) = @_;
827 my $dbfilename = $passedoptions->{"DatabaseFilename"};
828 my $dbname = $passedoptions->{"DatabaseName"};
829 my $dbdescription = $passedoptions->{"DatabaseDescription"};
830 my $dbnotes = $passedoptions->{"DatabaseNotes"};
831 my $dbcategories = $passedoptions->{"DatabaseCategories"};
832 my $dbmajorver = $passedoptions->{"VersionMajor"};
833 my $dbminorver = $passedoptions->{"VersionMinor"};
834 my $dbrevisionver = $passedoptions->{"VersionRevision"};
836 # Check if the database with the filename given already exists.
838 my $database_exists = $class->dbexists($dbfilename);
840 if ($database_exists eq 0){
842 # The database filename exists so set the error value.
844 $error = "DatabaseExists";
849 # Create the database structure (info and page tables);
851 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (
852 name varchar(256) primary key,
853 description varchar(512),
855 categories varchar(512),
856 kiriwrite_version_major int(4),
857 kiriwrite_version_minor int(4),
858 kiriwrite_version_revision int(4)
859 ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
860 $string_handle->execute();
862 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_pages (
863 filename varchar(256) primary key,
864 pagename varchar(512),
865 pagedescription varchar(512),
866 pagesection varchar(256),
867 pagetemplate varchar(64),
870 lastmodified datetime
871 ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
872 $string_handle->execute();
874 # Convert the values into SQL query formatted values and add an entry
875 # to the kiriwrite_database_info table.
877 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
878 \'' . $class->convert($dbname) . '\',
879 \'' . $class->convert($dbdescription) . '\',
880 \'' . $class->convert($dbnotes) . '\',
881 \'' . $class->convert($dbcategories) . '\',
882 \'' . $class->convert($dbmajorver) . '\',
883 \'' . $class->convert($dbminorver) . '\',
884 \'' . $class->convert($dbrevisionver) . '\'
885 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
886 $string_handle->execute();
891 #################################################################################
892 # editdatabase: Edits a Kiriwrite Database. #
896 # $dbmodule->editdatabase(options); #
898 # options Specifies the following options in any order. #
900 # NewDatabaseFilename Specifies the new database filename to use. #
901 # DatabaseName Specifies the new database name. #
902 # DatabaseDescription Specifies the new database description. #
903 # DatabaseNotes Specifies the new database notes. #
904 # DatabaseCategories Specifies the new database categories. #
905 #################################################################################
911 my ($passedoptions) = @_;
913 my $dbnewfilename = $passedoptions->{"DatabaseNewFilename"};
914 my $dbname = $passedoptions->{"DatabaseName"};
915 my $dbdescription = $passedoptions->{"DatabaseDescription"};
916 my $dbnotes = $passedoptions->{"DatabaseNotes"};
917 my $dbcategories = $passedoptions->{"DatabaseCategories"};
919 # Check if a new database filename has been specified and if a
920 # new database filename has been specified then change the
923 if ($database_filename ne $dbnewfilename){
925 # Check if a table with the filename already exists before using the
928 my $database_newexists = $class->dbexists($dbnewfilename);
930 if ($database_newexists eq 0){
932 # The database filename exists so set the error value.
934 $error = "DatabaseExists";
941 $string_handle = $database_handle->prepare('RENAME TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info, ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_pages');
942 $string_handle->execute();
946 # Get the current database information.
948 $string_handle = $database_handle->prepare('SELECT name FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
949 $string_handle->execute();
951 my @database_oldinfo = $string_handle->fetchrow_array();
953 my $dboldname = decode_utf8($database_oldinfo[0]);
955 # Update the database information.
957 $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info SET name = \'' . $class->convert($dbname) . '\',
958 description = \'' . $class->convert($dbdescription) . '\',
959 notes = \'' . $class->convert($dbnotes) . '\',
960 categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
961 $string_handle->execute();
966 #################################################################################
967 # deletedatabase: Deletes a Kiriwrite database. #
971 # $dbmodule->deletedatabase(options); #
973 # options Specifies the following options in any order. #
975 # DatabaseName Specifies the Kiriwrite database to delete. #
976 #################################################################################
981 # Get the database filename.
984 my ($passedoptions) = shift;
986 my $databasename = $passedoptions->{"DatabaseName"};
991 # Check if the database with the filename given already exists.
993 my $database_exists = $class->dbexists($databasename);
995 if ($database_exists eq 1){
997 # The database does not exist so set the error value.
999 $error = "DoesNotExist";
1006 # Delete the database tables.
1008 $string_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_info') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1009 $string_handle->execute();
1011 # Check if the _database_pages table exists and delete it if it exists.
1013 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1014 $string_handle->execute();
1016 while (@table_data = $string_handle->fetchrow_array()){
1022 # Check if the _database_pages table really does exist.
1024 if ($table_exists eq 1){
1026 # the _database_pages table really does exist so delete it.
1028 $string_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1029 $string_handle->execute();
1035 #################################################################################
1036 # Template subroutines. #
1037 #################################################################################
1039 sub connecttemplate{
1040 #################################################################################
1041 # connecttemplate: Connect to the template database. #
1045 # $dbmodule->connecttemplate(missingignore); #
1047 # missingignore Ignore errror about database being missing. #
1048 #################################################################################
1054 my $ignoremissing = shift;
1055 my $templatedb_exists = 0;
1056 my @templatedb_check;
1058 # Check if the template database exists.
1060 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1061 $string_handle->execute();
1063 while (@templatedb_check = $string_handle->fetchrow_array()){
1065 $templatedb_exists = 1;
1069 if (!$templatedb_exists){
1071 if (!$ignoremissing){
1073 $error = "TemplateDatabaseDoesNotExist";
1082 sub disconnecttemplate{
1083 #################################################################################
1084 # disconnecttemplate: Disconnect from the template database. #
1088 # $dbmodule->disconnecttemplate(); #
1089 #################################################################################
1091 # This subroutine is not used.
1095 sub gettemplatelist{
1096 #################################################################################
1097 # gettemplatelist: Gets the list of templates. #
1101 # $dbmodule->gettemplatelist(); #
1102 #################################################################################
1109 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1110 $string_handle->execute();
1112 my @database_template;
1114 my $template_filename;
1116 while (@database_template = $string_handle->fetchrow_array()){
1118 # Get certain values from the array.
1120 $template_filename = decode_utf8($database_template[0]);
1122 # Add the template to the list of templates.
1124 push(@templates_list, $template_filename);
1128 return @templates_list;
1132 sub gettemplateinfo{
1133 #################################################################################
1134 # gettemplateinfo: Get information on a template. #
1138 # $dbmodule->gettemplateinfo(options); #
1140 # options Specifies the following options in any order. #
1142 # TemplateFilename Specifies the template filename to use. #
1143 #################################################################################
1148 # Get the data passed to the subroutine.
1151 my ($passedoptions) = @_;
1156 my $template_filename;
1158 my $template_description;
1159 my $template_datemodified;
1160 my $template_layout;
1162 my $template_found = 0;
1164 my $filename = $passedoptions->{"TemplateFilename"};
1166 $string_handle = $database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1167 $string_handle->execute();
1169 while (@template_data = $string_handle->fetchrow_array()){
1171 # Get certain values from the array.
1173 $template_filename = decode_utf8($template_data[0]);
1174 $template_name = decode_utf8($template_data[1]);
1175 $template_description = decode_utf8($template_data[2]);
1176 $template_layout = decode_utf8($template_data[3]);
1177 $template_datemodified = decode_utf8($template_data[4]);
1179 # Process them into the hash.
1182 "TemplateFilename" => $template_filename,
1183 "TemplateName" => $template_name,
1184 "TemplateDescription" => $template_description,
1185 "TemplateLayout" => $template_layout,
1186 "TemplateLastModified" => $template_datemodified
1189 $template_found = 1;
1193 if ($template_found eq 0){
1195 # The template was not found in the template database so
1196 # write an error value.
1198 $error = "TemplateDoesNotExist";
1209 #################################################################################
1210 # addtemplate: Adds a template to the template database. #
1214 # $dbmodule->addtemplate(options); #
1216 # options Specifies the following options in any order. #
1218 # TemplateFilename Specifies the new template filename. #
1219 # TemplateName Specifies the new template name. #
1220 # TemplateDescription Specifies the new template description. #
1221 # TemplateLayout Specifies the new template layout. #
1222 #################################################################################
1227 # Get the data passed to the subroutine.
1230 my ($passedoptions) = @_;
1233 my @templatedb_check;
1234 my $templatedb_exists;
1237 my $template_filename = $passedoptions->{"TemplateFilename"};
1238 my $template_name = $passedoptions->{"TemplateName"};
1239 my $template_description = $passedoptions->{"TemplateDescription"};
1240 my $template_layout = $passedoptions->{"TemplateLayout"};
1242 # Check if the template database exists.
1244 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1245 $string_handle->execute();
1247 while (@templatedb_check = $string_handle->fetchrow_array()){
1249 $templatedb_exists = 1;
1253 # Check if the template database table exists and if it doesn't
1254 # then create the template database table.
1256 if (!$templatedb_exists){
1258 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_templates (
1259 filename varchar(256) primary key,
1260 templatename varchar(512),
1261 templatedescription varchar(512),
1262 templatelayout mediumtext,
1263 datemodified datetime
1264 ) DEFAULT CHARSET=utf8') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1265 $string_handle->execute();
1269 # Check if the template already exists before adding.
1271 if (!$templatedb_exists){
1273 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
1275 if ($blankfile eq 0){
1277 $string_handle->execute();
1279 while (@page_exists = $string_handle->fetchrow_array()){
1281 $error = "TemplatePageExists";
1290 # Get the current date.
1292 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1294 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1296 # Check if certain values are undefined and if they
1297 # are then set them blank.
1299 if (!$template_name){
1301 $template_name = "";
1305 if (!$template_description){
1307 $template_description = "";
1311 if (!$template_layout){
1313 $template_layout = "";
1317 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
1318 \'' . $class->convert($template_filename) . '\',
1319 \'' . $class->convert($template_name) . '\',
1320 \'' . $class->convert($template_description) . '\',
1321 \'' . $class->convert($template_layout) . '\',
1322 \'' . $class->convert($template_date) . '\'
1323 )') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1324 $string_handle->execute();
1329 #################################################################################
1330 # editttemplate: Edits a Kiriwrite template. #
1334 # $dbmodule->edittemplate(options); #
1336 # options Specifies the following options in any order. #
1338 # TemplateFilename Specifies the template filename to edit. #
1339 # NewTemplateFilename Specifies the new template filename. #
1340 # NewTemplateName Specifies the new template name. #
1341 # NewTemplateDescription Specifies the new template description. #
1342 # NewTemplateLayout Specifies the new template layout. #
1343 #################################################################################
1345 # Get the values passed.
1348 my ($passedoptions) = @_;
1349 my $template_found = 0;
1352 # Process the values passed.
1354 my $template_filename = $passedoptions->{"TemplateFilename"};
1355 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
1356 my $new_template_name = $passedoptions->{"NewTemplateName"};
1357 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
1358 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
1360 # Check if the template exists.
1362 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1363 $string_handle->execute();
1365 while (@template_info = $string_handle->fetchrow_array()){
1367 $template_found = 1;
1371 # Check to see if the template was found and set an error value if
1374 if ($template_found eq 0){
1376 $error = "TemplateDoesNotExist";
1381 # Get the date and time.
1383 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1384 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1386 # Update the template information.
1388 $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_templates SET
1389 filename = \'' . $class->convert($new_template_filename) . '\',
1390 templatename = \'' . $class->convert($new_template_name) . '\',
1391 templatedescription = \'' . $class->convert($new_template_description) . '\',
1392 templatelayout = \'' . $class->convert($new_template_layout) . '\',
1393 datemodified = \'' . $class->convert($templatenewdate) . '\'
1394 WHERE filename = \'' . $class->convert($template_filename) . '\'
1395 ') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1396 $string_handle->execute();
1401 #################################################################################
1402 # deletetemplate: Deletes a template from the template database. #
1406 # $dbmodule->deletetemplate(options); #
1408 # options Specifies the following options in any order. #
1410 # TemplateFilename Specifies the template filename to delete. #
1411 #################################################################################
1416 # Get the data passed to the subroutine.
1419 my ($passedoptions) = @_;
1422 my $template_filename = $passedoptions->{"TemplateFilename"};
1423 my $template_count = 0;
1425 # Check if the template exists.
1427 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1428 $string_handle->execute();
1430 while (@pagedata = $string_handle->fetchrow_array()){
1436 if ($template_count eq 0){
1438 # No pages were returned so return an error value.
1440 $error = "TemplateDoesNotExist";
1445 # Delete the template from the template database.
1447 $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1448 $string_handle->execute();
1452 #################################################################################
1453 # Page subroutines. #
1454 #################################################################################
1457 #################################################################################
1458 # getpagelist: Gets the list of pages from the database. #
1462 # $dbmodule->getpagelist(); #
1463 #################################################################################
1470 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1471 $string_handle->execute();
1473 my @database_pagefilenames;
1474 my @database_pagefilenames_final;
1476 # Process the collected pages.
1478 while (@database_pagefilenames = $string_handle->fetchrow_array){
1480 # Add each page to the list of pages in the database.
1482 push(@database_pagefilenames_final, decode_utf8($database_pagefilenames[0]));
1486 return @database_pagefilenames_final;
1491 #################################################################################
1492 # getpageinfo: Gets the page information from the filename passed. #
1496 # $dbmodule->getpageinfo(options); #
1498 # options Specifies the following options in any order. #
1500 # PageFilename Specifies the page filename to get the page information from. #
1501 # Reduced Specifies if the reduced version of the page information should #
1503 #################################################################################
1509 my ($passedoptions) = shift;
1510 my (%database_page, $database_page);
1511 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1516 # Get the page from the database.
1518 my $page_filename = $passedoptions->{"PageFilename"};
1519 my $page_reduced = $passedoptions->{"Reduced"};
1521 if ($page_reduced eq 1){
1523 $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1524 $string_handle->execute();
1526 # Check if the page exists in the database.
1528 while (@data_page = $string_handle->fetchrow_array()){
1530 # Get the values from the array.
1532 $pagefilename = decode_utf8($data_page[0]);
1533 $pagename = decode_utf8($data_page[1]);
1534 $pagedescription = decode_utf8($data_page[2]);
1535 $pagelastmodified = decode_utf8($data_page[3]);
1537 # Put the values into the page hash.
1540 "PageFilename" => $pagefilename,
1541 "PageName" => $pagename,
1542 "PageDescription" => $pagedescription,
1543 "PageLastModified" => $class->dateconvert($pagelastmodified),
1552 $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1553 $string_handle->execute();
1555 # Check if the page exists in the database.
1557 while (@data_page = $string_handle->fetchrow_array()){
1559 # Get the values from the array.
1561 $pagefilename = decode_utf8($data_page[0]);
1562 $pagename = decode_utf8($data_page[1]);
1563 $pagedescription = decode_utf8($data_page[2]);
1564 $pagesection = decode_utf8($data_page[3]);
1565 $pagetemplate = decode_utf8($data_page[4]);
1566 $pagedata = decode_utf8($data_page[5]);
1567 $pagesettings = decode_utf8($data_page[6]);
1568 $pagelastmodified = decode_utf8($data_page[7]);
1570 # Put the values into the page hash.
1573 "PageFilename" => $pagefilename,
1574 "PageName" => $pagename,
1575 "PageDescription" => $pagedescription,
1576 "PageSection" => $pagesection,
1577 "PageTemplate" => $pagetemplate,
1578 "PageContent" => $pagedata,
1579 "PageSettings" => $pagesettings,
1580 "PageLastModified" => $class->dateconvert($pagelastmodified),
1589 # Check if the page did exist.
1593 $error = "PageDoesNotExist";
1598 return %database_page;
1604 #################################################################################
1605 # addpage: Add a page to the selected database. #
1609 # $dbmodule->addpage(options); #
1611 # options Specifies the following options in any order. #
1613 # PageFilename Specifies the page filename to use. #
1614 # PageName Specifies the page name to use. #
1615 # PageDescription Specifies the page description to use. #
1616 # PageSection Specifies the page section to use. #
1617 # PageTemplate Specifies the page template to use. #
1618 # PageContent Specifies the page content to use. #
1619 # PageSettings Specifies the page settings to use. #
1620 #################################################################################
1622 # Get the data that was passed to the subroutine.
1628 my ($passedoptions) = shift;
1633 # Get the values passed to the hash.
1635 my $page_filename = $passedoptions->{"PageFilename"};
1636 my $page_name = $passedoptions->{"PageName"};
1637 my $page_description = $passedoptions->{"PageDescription"};
1638 my $page_section = $passedoptions->{"PageSection"};
1639 my $page_template = $passedoptions->{"PageTemplate"};
1640 my $page_content = $passedoptions->{"PageContent"};
1641 my $page_settings = $passedoptions->{"PageSettings"};
1643 # Check to see if the filename given already exists
1644 # in the page database.
1646 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1647 $string_handle->execute();
1649 # Check if a page with the filename given really does
1652 while (@database_page = $string_handle->fetchrow_array()){
1654 # A page does exist so increment the count to 1.
1660 if ($page_count ne 0){
1662 # The page does exist so set the error value.
1664 $error = "PageExists";
1669 # Check if certain values are undefined.
1677 if (!$page_description){
1679 $page_description = "";
1683 if (!$page_section){
1689 if (!$page_content){
1695 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1696 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1698 # Add the page to the selected database.
1700 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1701 \'' . $class->convert($page_filename) . '\',
1702 \'' . $class->convert($page_name) . '\',
1703 \'' . $class->convert($page_description) . '\',
1704 \'' . $class->convert($page_section) . '\',
1705 \'' . $class->convert($page_template) . '\',
1706 \'' . $class->convert($page_content) . '\',
1707 \'' . $class->convert($page_settings) . '\',
1708 \'' . $class->convert($page_date) . '\'
1709 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1710 $string_handle->execute();
1715 #################################################################################
1716 # deletepage: Delete a page from the selected database. #
1720 # $dbmodule->deletepage(options) #
1722 # options Specifies the following options in any order. #
1724 # PageFilename Specifies the page filename to delete. #
1725 #################################################################################
1730 # Get the data that was passed to the subroutine.
1733 my ($passedoptions) = @_;
1737 # Get the page filename.
1739 my $page_filename = $passedoptions->{"PageFilename"};
1741 # Check if the page exists before deleting it.
1743 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1744 $string_handle->execute();
1746 while (@page_info = $string_handle->fetchrow_array()){
1752 # Check if the page really does exist.
1756 $error = "PageDoesNotExist";
1763 $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1764 $string_handle->execute();
1769 #################################################################################
1770 # editpage: Edit a page from the selected database. #
1774 # $dbmodule->editpage(options); #
1776 # options Specifies the following options in any order. #
1778 # PageFilename Specifies the filename to edit. #
1779 # PageNewFilename Specifies the new filename to use. #
1780 # PageNewName Specifies the new page name to use. #
1781 # PageNewDescription Specifies the new page description to use. #
1782 # PageNewSection Specifies the new page section to use. #
1783 # PageNewTemplate Specifies the new page template to use. #
1784 # PageNewContent Specifies the new page content to use. #
1785 # PageNewSettings Specifies the new page settings to use. #
1786 #################################################################################
1791 # Get the values passed to the subroutine.
1794 my ($passedoptions) = @_;
1798 # Get the data that was passed to the subroutine.
1800 my $page_filename = $passedoptions->{"PageFilename"};
1801 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1802 my $page_newname = $passedoptions->{"PageNewName"};
1803 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1804 my $page_newsection = $passedoptions->{"PageNewSection"};
1805 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1806 my $page_newcontent = $passedoptions->{"PageNewContent"};
1807 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1809 # Check if the page with the filename given exists.
1811 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1812 $string_handle->execute();
1814 # Check if the page really does exist.
1816 while (@page_info = $string_handle->fetchrow_array()){
1818 # The page information is found.
1824 # Check if the page really does exist.
1828 $error = "PageDoesNotExist";
1833 # Check if there is a page that already exists with the new
1838 if ($page_filename ne $page_newfilename){
1840 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1841 $string_handle->execute();
1843 # Check if a page really is using the new filename.
1845 while (@page_info = $string_handle->fetchrow_array()){
1847 # The page information is found.
1853 if ($page_found eq 1){
1855 $error = "PageAlreadyExists";
1862 # Get the current date.
1864 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1865 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1867 # Edit the selected page.
1869 $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages SET filename = \'' . $class->convert($page_newfilename) . '\', pagename = \'' . $class->convert($page_newname) . '\', pagedescription = \'' . $class->convert($page_newdescription) . '\', pagesection = \'' . $class->convert($page_newsection) . '\', pagetemplate = \'' . $class->convert($page_newtemplate) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagesettings = \'' . $class->convert($page_newsettings) . '\', lastmodified = \'' . $page_date . '\' WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1870 $string_handle->execute();
1875 #################################################################################
1876 # movepage: Moves a page from the old database to the new database. #
1880 # $dbmodule->movepage(options); #
1882 # options Specifies the following options in any order. #
1884 # PageFilename Specifies the page with the filename to move. #
1885 #################################################################################
1890 # Get the values passed to the subroutine.
1893 my ($passedoptions) = @_;
1895 my (%database_page, $database_page);
1896 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1900 # Get the data that was passed to the subroutine.
1902 my $page_filename = $passedoptions->{"PageFilename"};
1904 # Check if the page with the filename given exists.
1906 $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1907 $string_handle->execute();
1909 # Check if the page really does exist.
1911 while (@page_info = $string_handle->fetchrow_array()){
1913 # Get the values from the array.
1915 $pagefilename = decode_utf8($page_info[0]);
1916 $pagename = decode_utf8($page_info[1]);
1917 $pagedescription = decode_utf8($page_info[2]);
1918 $pagesection = decode_utf8($page_info[3]);
1919 $pagetemplate = decode_utf8($page_info[4]);
1920 $pagedata = decode_utf8($page_info[5]);
1921 $pagesettings = decode_utf8($page_info[6]);
1922 $pagelastmodified = decode_utf8($page_info[7]);
1924 # Put the values into the page hash.
1927 "PageFilename" => $pagefilename,
1928 "PageName" => $pagename,
1929 "PageDescription" => $pagedescription,
1930 "PageSection" => $pagesection,
1931 "PageTemplate" => $pagetemplate,
1932 "PageContent" => $pagedata,
1933 "PageSettings" => $pagesettings,
1934 "PageLastModified" => $pagelastmodified,
1937 # The page information is found.
1943 # Check if the page really does exist.
1947 $error = "PageDoesNotExist";
1952 # Check if the page with the filename given already exists in
1953 # the database the page is being moved to.
1958 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
1959 $string_handle->execute();
1961 while (@page_info = $string_handle->fetchrow_array()){
1967 # Check if the page really does exist.
1971 $error = "PageAlreadyExists";
1976 # Add the page to the new database.
1978 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1979 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1980 \'' . $class->convert($database_page{"PageName"}) . '\',
1981 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1982 \'' . $class->convert($database_page{"PageSection"}) . '\',
1983 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1984 \'' . $class->convert($database_page{"PageContent"}) . '\',
1985 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1986 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1987 )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
1988 $string_handle->execute();
1990 # Delete the page from the old database.
1992 $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1993 $string_handle->execute();
1998 #################################################################################
1999 # copypage: Copies a page from the old database to the new database. #
2003 # $dbmodule->copypage(options); #
2005 # options Specifies the following options in any order. #
2007 # PageFilename Specifies the page with the filename to copy. #
2008 #################################################################################
2013 # Get the values passed to the subroutine.
2016 my ($passedoptions) = @_;
2018 my (%database_page, $database_page);
2019 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
2023 # Get the data that was passed to the subroutine.
2025 my $page_filename = $passedoptions->{"PageFilename"};
2027 # Check if the page with the filename given exists.
2029 $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
2030 $string_handle->execute();
2032 # Check if the page really does exist.
2034 while (@page_info = $string_handle->fetchrow_array()){
2036 # Get the values from the array.
2038 $pagefilename = decode_utf8($page_info[0]);
2039 $pagename = decode_utf8($page_info[1]);
2040 $pagedescription = decode_utf8($page_info[2]);
2041 $pagesection = decode_utf8($page_info[3]);
2042 $pagetemplate = decode_utf8($page_info[4]);
2043 $pagedata = decode_utf8($page_info[5]);
2044 $pagesettings = decode_utf8($page_info[6]);
2045 $pagelastmodified = decode_utf8($page_info[7]);
2047 # Put the values into the page hash.
2050 "PageFilename" => $pagefilename,
2051 "PageName" => $pagename,
2052 "PageDescription" => $pagedescription,
2053 "PageSection" => $pagesection,
2054 "PageTemplate" => $pagetemplate,
2055 "PageContent" => $pagedata,
2056 "PageSettings" => $pagesettings,
2057 "PageLastModified" => $pagelastmodified,
2060 # The page information is found.
2066 # Check if the page really does exist.
2070 $error = "PageDoesNotExist";
2075 # Check if the page with the filename given already exists in
2076 # the database the page is being moved to.
2081 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2082 $string_handle->execute();
2084 while (@page_info = $string_handle->fetchrow_array()){
2090 # Check if the page really does exist.
2094 $error = "PageAlreadyExists";
2099 # Add the page to the new database.
2101 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
2102 \'' . $class->convert($database_page{"PageFilename"}) . '\',
2103 \'' . $class->convert($database_page{"PageName"}) . '\',
2104 \'' . $class->convert($database_page{"PageDescription"}) . '\',
2105 \'' . $class->convert($database_page{"PageSection"}) . '\',
2106 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
2107 \'' . $class->convert($database_page{"PageContent"}) . '\',
2108 \'' . $class->convert($database_page{"PageSettings"}) . '\',
2109 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
2110 )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2111 $string_handle->execute();
2115 #################################################################################
2116 # Filter subroutines. #
2117 #################################################################################
2120 #################################################################################
2121 # connectfilter: Connect to the filter database. #
2125 # $dbmodule->connectfilter(missingignore); #
2127 # missingignore Ignore error about database being missing. #
2128 #################################################################################
2134 my $ignoremissing = shift;
2136 my $filterdb_exists = 0;
2138 # Check if the template database exists.
2140 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2141 $string_handle->execute();
2143 while (@filterdb_check = $string_handle->fetchrow_array()){
2145 $filterdb_exists = 1;
2149 if (!$filterdb_exists){
2151 if (!$ignoremissing){
2153 $error = "FilterDatabaseDoesNotExist";
2162 sub disconnectfilter{
2163 #################################################################################
2164 # disconnectfilter: Disconnect from the filter database. #
2168 # $dbmodule->disconnectfilter(); #
2169 #################################################################################
2171 # This subroutine is not used.
2176 #################################################################################
2177 # getfilterlist: Gets the list of filters in the filter database. #
2181 # $dbmodule->getfilterlist(); #
2182 #################################################################################
2192 # Get the list of filters available.
2194 $string_handle = $database_handle->prepare('SELECT id, priority FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2195 $string_handle->execute();
2197 while (@filter_data = $string_handle->fetchrow_array()){
2199 # Add the filter to the list of available filters.
2201 push(@filter_list, decode_utf8($filter_data[0]));
2205 return @filter_list;
2210 #################################################################################
2211 # getfilterinfo: Gets information about the filter. #
2215 # $dbmodule->getfilterinfo(options); #
2217 # options Specifies the following options in any order. #
2219 # FilterID Specifies the filter ID number to get information from. #
2220 #################################################################################
2225 # Get the values passed to the subroutine.
2228 my ($passedoptions) = @_;
2231 my $filter_exists = 0;
2234 # Get the values that are in the hash.
2236 my $filter_id = $passedoptions->{"FilterID"};
2238 $string_handle = $database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2239 $string_handle->execute();
2241 # Get the filter information.
2243 while (@filter_data = $string_handle->fetchrow_array()){
2245 $filter_info{"FilterID"} = decode_utf8($filter_data[0]);
2246 $filter_info{"FilterPriority"} = decode_utf8($filter_data[1]);
2247 $filter_info{"FilterFind"} = decode_utf8($filter_data[2]);
2248 $filter_info{"FilterReplace"} = decode_utf8($filter_data[3]);
2249 $filter_info{"FilterNotes"} = decode_utf8($filter_data[4]);
2255 # Check if the filter exists.
2257 if (!$filter_exists){
2259 # The filter does not exist so return
2262 $error = "FilterDoesNotExist";
2267 # Return the filter information.
2269 return %filter_info;
2274 #################################################################################
2275 # addfilter: Adds a filter to the filter database. #
2279 # $dbmodule->addfilter(options); #
2281 # options Specifies the following options in any order. #
2283 # FindFilter Specifies the find filter to add. #
2284 # ReplaceFilter Specifies the replace filter to add. #
2285 # Priority Specifies the filter priority to use. #
2286 # Notes Specifies the notes to use. #
2287 #################################################################################
2292 # Get the values passed to the subroutine.
2295 my ($passedoptions) = @_;
2297 # Define some variables for later.
2299 my @database_filters;
2303 my $nofiltertable = 0;
2304 my $filter_found = 0;
2305 my $filter_count = 0;
2306 my $filterdb_exists = 0;
2310 # Get the values from the hash.
2312 my $filter_find = $passedoptions->{"FindFilter"};
2313 my $filter_replace = $passedoptions->{"ReplaceFilter"};
2314 my $filter_priority = $passedoptions->{"Priority"};
2315 my $filter_notes = $passedoptions->{"Notes"};
2317 # Check if the template database exists.
2319 $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2320 $string_handle->execute();
2322 while (@filterdb_check = $string_handle->fetchrow_array()){
2324 $filterdb_exists = 1;
2328 # Check if certain values are undefined and if they
2329 # are then set them blank.
2337 if (!$filter_replace){
2339 $filter_replace = "";
2343 if (!$filter_priority){
2345 $filter_priority = 1;
2349 if (!$filter_notes){
2355 # Check if there is really no filter table.
2357 if (!$filterdb_exists){
2359 # Create the filter database table.
2361 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_filters (
2362 id int(7) primary key,
2364 findsetting varchar(1024),
2365 replacesetting varchar(1024),
2367 ) DEFAULT CHARSET=utf8') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2368 $string_handle->execute();
2372 # Find the lowest filter identification number available.
2374 $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2375 $string_handle->execute();
2377 while (@database_filters = $string_handle->fetchrow_array()){
2379 $filter_id = decode_utf8($database_filters[0]);
2381 # Add the filter identification to the list of filter IDs.
2383 push(@filterid_list, $filter_id);
2389 # Process each filter looking for a blank available filter.
2391 foreach $filter_id (@filterid_list){
2393 # Check the next filter ID to see if it's blank.
2395 $new_id = $filter_id + 1;
2397 $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2398 $string_handle->execute();
2400 # Get the filter identification number.
2402 while (@filterid_check = $string_handle->fetchrow_array()){
2408 # Check if a filter was found.
2410 if (!$filter_found){
2412 # No filter was found using this ID so exit the loop.
2418 # Increment the filter count and reset the filter found value.
2426 # Check if there were any filters in the filters database.
2428 if (!$filter_count && !$new_id){
2430 # There were no filters in the filters database so set
2431 # the new filter identification value to 1.
2437 # Add the filter to the filter database.
2439 $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2440 \'' . $class->convert($new_id) . '\',
2441 \'' . $class->convert($filter_priority) . '\',
2442 \'' . $class->convert($filter_find) . '\',
2443 \'' . $class->convert($filter_replace) .'\',
2444 \'' . $class->convert($filter_notes) . '\'
2445 )') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2446 $string_handle->execute();
2452 #################################################################################
2453 # editfilter: Edits a filter in the filter database. #
2457 # $dbmodule->editfilter(options); #
2459 # options Specifies the following options in any order. #
2461 # FilterID Specifies the filter to edit. #
2462 # NewFindFilter Specifies the new find filter setting. #
2463 # NewReplaceFilter Specifies the new replace filter setting. #
2464 # NewFilterPriority Specifies the new filter priority setting. #
2465 # NewFilterNotes Specifies the new notes for the filter. #
2466 #################################################################################
2471 # Get the values passed to the subroutine.
2474 my ($passedoptions) = @_;
2477 my $filter_exists = 1;
2480 # Get the values from the hash.
2482 my $filter_id = $passedoptions->{"FilterID"};
2483 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2484 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2485 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2486 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2488 # Check if the filter exists before editing it.
2490 $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2491 $string_handle->execute();
2493 # Check if the filter exists.
2495 while (@filter_data = $string_handle->fetchrow_array()){
2501 # Check if the filter really does exist.
2503 if (!$filter_exists){
2505 # The filter does not exist so return
2508 $error = "FilterDoesNotExist";
2513 # Edit the selected filter.
2515 $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_filters SET
2516 findsetting = \'' . $class->convert($filter_newfind) . '\',
2517 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2518 priority = \'' . $class->convert($filter_newpriority) . '\',
2519 notes = \'' . $class->convert($filter_newnotes) . '\'
2520 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2521 $string_handle->execute();
2528 #################################################################################
2529 # deletefilter: Deletes a filter from the filter database. #
2533 # $dbmodule->deletefilter(options); #
2535 # options Specifies the following options in any order. #
2537 # FilterID Specifies the filter to delete from the filter database. #
2538 #################################################################################
2543 # Get the values passed to the subroutine.
2546 my ($passedoptions) = @_;
2548 my $filter_exists = 0;
2551 # Get the values from the hash.
2553 my $filter_id = $passedoptions->{"FilterID"};
2555 # Check if the filter exists before deleting.
2557 $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2558 $string_handle->execute();
2560 while (@filter_data = $string_handle->fetchrow_array()){
2566 # Check to see if the filter really does exist.
2568 if (!$filter_exists){
2570 $error = "FilterDoesNotExist";
2575 # Delete the filter from the filter database.
2577 $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2578 $string_handle->execute();