1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm) #
3 # Database module for mainipulating SQLite databases in the database directory. #
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::SQLite;
26 # Enable strict and use warnings.
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION = "0.1.0";
38 my ($options, %options);
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_statement_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_statement_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_statement_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines. #
59 #################################################################################
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite #
67 # $dbmodule = Kiriwrite::Database::SQLite->new(); #
68 #################################################################################
70 # Get the perl module name.
75 return bless($self, $class);
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module #
85 # $dbmodule->loadsettings(Directory, options); #
87 # options Specifies the following options (in any order). #
89 # Directory Specifies the directory to use for getting databases. #
90 # DateTime Specifies the date and time format to use. #
91 # Server Specifies the server to use. #
92 # Database Specifies the database to use. #
93 # Username Specifies the username to use. #
94 # Password Specifies the password to use. #
95 # HashType Specifies the password hash type to use. #
96 # Port Specifies the server port to use. #
97 # Protocol Specifies the protocol to use. #
98 # TablePrefix Specifies the table prefix to use. #
99 #################################################################################
101 # Get the data passed to the subroutine.
104 my ($passedoptions) = @_;
106 # Add the directory setting to the list of options (as it's the only
107 # one needed for this database module).
110 "Directory" => $passedoptions->{"Directory"},
111 "DateTime" => $passedoptions->{"DateTime"},
117 #################################################################################
118 # convert: Converts data into SQL formatted data. #
122 # $dbmodule->convert(data); #
124 # data Specifies the data to convert. #
125 #################################################################################
127 # Get the data passed to the subroutine.
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date. #
149 # $dbmodule->dateconvert(date); #
151 # date Specifies the date to convert. #
152 #################################################################################
154 # Get the date passed to the subroutine.
159 # Convert the date given into the proper date.
161 # Create the following varialbes to be used later.
188 # Split the date and time.
190 $length = length($data);
196 # Get the character and check if it is a space.
198 $char = substr($data, $seek, 1);
202 # The character is a space, so get the date and time.
204 $date = substr($data, 0, $seek);
205 $timelength = $length - $seek - 1;
206 $time = substr($data, $seek + 1, $timelength);
212 } until ($seek eq $length);
214 # Get the year, month and date.
216 $length = length($date);
221 # Get the character and check if it is a dash.
223 $char = substr($date, $seek, 1);
227 # The character is a dash, so get the year, month or day.
229 $datelength = $seek - $startchar;
233 # Get the year from the date.
235 $year = substr($date, 0, $datelength) + 1900;
239 # Get the last two characters to get the short year
242 $year_short = substr($year, 2, 2);
244 } elsif ($count eq 1){
246 # Get the month and day from the date.
248 $month = substr($date, $startchar + 1, $datelength - 1) + 1;
250 # Check if the month is less then 10, if it is
251 # add a zero to the value.
255 $month_full = '0' . $month;
259 $month_full = $month;
266 $daylength = $length - $seek + 1;
267 $day = substr($date, $startchar + 1, $daylength);
269 # Check if the day is less than 10, if it is
270 # add a zero to the value.
274 $day_full = '0' . $day;
288 } until ($seek eq $length);
290 # Get the length of the time value and reset certain
293 $length = length($time);
300 # Get the character and check if it is a colon.
302 $char = substr($time, $seek, 1);
306 # The character is a colon, so get the hour, minute and day.
308 $timelength = $seek - $startchar;
312 # Get the hour from the time.
314 $hour = substr($time, 0, $timelength);
318 # If the hour is less than ten then add a
323 $hour_full = '0' . $hour;
331 } elsif ($count eq 1){
333 # Get the minute and second from the time.
335 $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);
356 # If the second is less than ten then add a
361 $second_full = '0' . $second;
365 $second_full = $second;
375 } until ($seek eq $length);
377 # Get the setting for displaying the date and time.
379 $data = $options{"DateTime"};
381 # Process the setting for displaying the date and time
382 # using regular expressions
384 $data =~ s/DD/$day_full/g;
386 $data =~ s/MM/$month_full/g;
387 $data =~ s/M/$month/g;
388 $data =~ s/YY/$year/g;
389 $data =~ s/Y/$year_short/g;
391 $data =~ s/hh/$hour_full/g;
392 $data =~ s/h/$hour/g;
393 $data =~ s/mm/$minute_full/g;
394 $data =~ s/m/$minute/g;
395 $data =~ s/ss/$second_full/g;
396 $data =~ s/s/$second/g;
405 #################################################################################
406 # geterror: Gets the error message (or extended error message). #
410 # $dbmodule->geterror(extended); #
412 # Extended Specifies if the extended error should be retrieved. #
413 #################################################################################
415 # Get the data passed to the subroutine.
418 my $extended = shift;
432 # Check to see if extended information should be returned.
436 # Extended information should be returned.
442 # Basic information should be returned.
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid. #
456 # $database->dbpermissions(dbname, read, write); #
458 # dbname Specifies the database name to check. #
459 # read Check to see if the database can be read. #
460 # write Check to see if the database can be written. #
461 #################################################################################
463 # Get the database name, read setting and write setting.
465 my ($class, $dbname, $readper, $writeper) = @_;
467 # Check if the database can be read.
471 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473 # The database can be read.
477 # The database cannot be read, so return a value
486 # Check if the database can be written.
490 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492 # The database can be read.
496 # The database cannot be read, so return a value
505 # No errors have occured while checking so return a value
513 #################################################################################
514 # dbexists: Check if the database exists. #
518 # $dbmodule->dbexists(dbname); #
520 # dbname Specifies the database name to check. #
521 #################################################################################
523 # Get the value that was passed to the subroutine.
528 # Check if the filename exists, if it does, return a value of 1, else
529 # return a value of 0, meaning that the file was not found.
531 if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
533 # Specified file does exist so return a value of 0.
539 # Specified file does not exist so return a value of 1.
547 #################################################################################
548 # Database Subroutines. #
549 #################################################################################
552 #################################################################################
553 # getdblist: Gets the list of available databases. #
557 # $dbmodule->getdblist(); #
558 #################################################################################
560 # Get the list of databases.
563 my @data_directory_final;
565 my $database_filename_length;
566 my $database_filename_friendly;
568 # Check if the database directory has valid permission settings.
570 if (-e $options{"Directory"}){
572 # The database directory does exist. So check if
573 # the permission settings are valid.
575 if (-r $options{"Directory"}){
577 # The permission settings for reading the directory
582 # The permission settings for reading the directory
583 # are invalid so return an error value.
585 $error = "DataDirInvalidPermissions";
592 # The database directory does not exist, so return an
595 $error = "DataDirMissing";
600 opendir(DATADIR, $options{"Directory"});
601 @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
604 # Process the list of databases.
606 foreach $database (@data_directory){
608 $database =~ s/.db.sqlite$//og;
609 $database_filename_friendly = $database;
611 #$database_filename_length = length($database);
612 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
613 push(@data_directory_final, $database_filename_friendly);
617 # Return the list of databases.
619 return @data_directory_final;
624 #################################################################################
625 # getdatabaseinfo: Get information about the database. #
629 # $dbmodule->getdatabaseinfo(); #
630 #################################################################################
632 # Get the database information.
635 my ($databaseinfo, %databaseinfo);
636 my ($sqldata, @sqldata);
641 $statement_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
642 $error = "DatabaseError", $errorext = $database_handle->errstr, return
644 $statement_handle->execute();
646 @sqldata = $statement_handle->fetchrow_array();
648 # Process the database information into a hash.
651 "DatabaseName" => $sqldata[0],
652 "Description" => $sqldata[1],
653 "Notes" => $sqldata[2],
654 "Categories" => $sqldata[3],
655 "Major" => $sqldata[4],
656 "Minor" => $sqldata[5],
657 "Revision" => $sqldata[6]
660 $statement_handle->finish();
661 undef $statement_handle;
663 return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be #
670 # moved or copied to. #
674 # $dbmodule->getseconddatabaseinfo(); #
675 #################################################################################
677 # Get the database information.
680 my ($databaseinfo, %databaseinfo);
681 my ($sqldata, @sqldata);
686 $second_statement_handle = $second_database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
687 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
689 $second_statement_handle->execute();
691 @sqldata = $second_statement_handle->fetchrow_array();
693 # Process the database information into a hash.
696 "DatabaseName" => $sqldata[0],
697 "Description" => $sqldata[1],
698 "Notes" => $sqldata[2],
699 "Categories" => $sqldata[3],
700 "Major" => $sqldata[4],
701 "Minor" => $sqldata[5],
702 "Revision" => $sqldata[6]
705 $second_statement_handle->finish();
706 undef $second_statement_handle;
708 return %databaseinfo;
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database. #
718 # $dbmodule->adddatabase(options); #
720 # options Specifies the following options in any order. #
722 # DatabaseFilename Specifies the database file/shortname to use. #
723 # DatabaseName Specifies the database name to use. #
724 # DatabaseDescription Specifies the database description to use. #
725 # DatabaseNotes Specifies the database notes to use. #
726 # DatabaseCategories Specifies the database categories to use. #
727 # VersionMajor Specifies the major version. #
728 # VersionMinor Specifies the minor version. #
729 # VersionRevision Specifies the revision version. #
730 #################################################################################
732 # Get the database that was passed to the subroutine.
738 my ($passedoptions) = @_;
740 my $dbfilename = $passedoptions->{"DatabaseFilename"};
741 my $dbname = $passedoptions->{"DatabaseName"};
742 my $dbdescription = $passedoptions->{"DatabaseDescription"};
743 my $dbnotes = $passedoptions->{"DatabaseNotes"};
744 my $dbcategories = $passedoptions->{"DatabaseCategories"};
745 my $dbmajorver = $passedoptions->{"VersionMajor"};
746 my $dbminorver = $passedoptions->{"VersionMinor"};
747 my $dbrevisionver = $passedoptions->{"VersionRevision"};
749 # Check if the database with the filename given already exists.
751 my $database_exists = $class->dbexists($dbfilename);
753 if ($database_exists eq 0){
755 # The database filename exists so set the error value.
757 $error = "DatabaseExists";
762 # Create the database structure.
764 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765 $database_handle->{unicode} = 1;
766 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767 name varchar(256) primary key,
768 description varchar(512),
770 categories varchar(512),
771 kiriwrite_version_major int(4),
772 kiriwrite_version_minor int(4),
773 kiriwrite_version_revision int(4)
774 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775 $statement_handle->execute();
777 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778 filename varchar(256) primary key,
779 pagename varchar(512),
780 pagedescription varchar(512),
781 pagesection varchar(256),
782 pagetemplate varchar(64),
785 lastmodified datetime
786 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787 $statement_handle->execute();
789 # Convert the values into SQL query formatted values and add an entry
790 # to the kiriwrite_database_info table.
792 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793 \'' . $class->convert($dbname) . '\',
794 \'' . $class->convert($dbdescription) . '\',
795 \'' . $class->convert($dbnotes) . '\',
796 \'' . $class->convert($dbcategories) . '\',
797 \'' . $class->convert($dbmajorver) . '\',
798 \'' . $class->convert($dbminorver) . '\',
799 \'' . $class->convert($dbrevisionver) . '\'
800 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801 $statement_handle->execute();
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database. #
811 # $dbmodule->editdatabase(options); #
813 # options Specifies the following options in any order. #
815 # NewDatabaseFilename Specifies the new database filename to use. #
816 # DatabaseName Specifies the new database name. #
817 # DatabaseDescription Specifies the new database description. #
818 # DatabaseNotes Specifies the new database notes. #
819 # DatabaseCategories Specifies the new database categories. #
820 #################################################################################
826 my ($passedoptions) = @_;
828 my $dbnewfilename = $passedoptions->{"DatabaseNewFilename"};
829 my $dbname = $passedoptions->{"DatabaseName"};
830 my $dbdescription = $passedoptions->{"DatabaseDescription"};
831 my $dbnotes = $passedoptions->{"DatabaseNotes"};
832 my $dbcategories = $passedoptions->{"DatabaseCategories"};
834 # Check if a new database filename has been specified and if a
835 # new database filename has been specified then change the
838 if ($database_filename ne $dbnewfilename){
840 # A new database filename has been given so check if the output
841 # directory has write access.
843 if (-r $options{"Directory"}){
845 # The directory is readable.
849 # The directory is not readable so set the error value.
851 $error = "DataDirInvalidPermissions";
857 if (-w $options{"Directory"}){
859 # The directory is writeable.
863 # The directory is not writeable so set the error value.
865 $error = "DataDirInvalidPermissions";
871 # Check if a database filename already exists before using the
874 my $database_newexists = $class->dbexists($dbnewfilename);
876 if ($database_newexists eq 0){
878 # The database filename exists so set the error value.
880 $error = "DatabaseExists";
885 # Check if the database can be renamed (has write access).
887 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
889 if ($database_permissions eq 1){
891 # The database filename exists so set the error value.
893 $error = "InvalidPermissionsSet";
898 # "Disconnect" from the database.
900 $database_handle->disconnect();
902 # Rename the database.
904 ($database_filename) = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
905 ($dbnewfilename) = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
907 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
909 # Reload the database from the new filename.
911 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
912 $database_handle->{unicode} = 1;
913 $database_filename = $dbnewfilename;
917 # Check if the database can be altered with the new data.
919 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
921 if ($database_permissions eq 1){
923 # The database filename exists so set the error value.
925 $error = "InvalidPermissionsSet";
930 # Get the current database information.
932 $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933 $statement_handle->execute();
935 my @database_oldinfo = $statement_handle->fetchrow_array();
937 my $dboldname = $database_oldinfo[0];
939 # Update the database information.
941 $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942 description = \'' . $class->convert($dbdescription) . '\',
943 notes = \'' . $class->convert($dbnotes) . '\',
944 categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945 $statement_handle->execute();
947 undef $statement_handle;
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database. #
958 # $dbmodule->deletedatabase(options); #
960 # options Specifies the following options in any order. #
962 # DatabaseName Specifies the Kiriwrite database to delete. #
963 #################################################################################
968 # Get the database filename.
971 my ($passedoptions) = shift;
973 my $databasename = $passedoptions->{"DatabaseName"};
975 # Check if the database exists.
977 my $database_exists = $class->dbexists($databasename);
979 if ($database_exists eq 1){
981 # The database does not exist so set the error value.
983 $error = "DoesNotExist";
988 # Check if the database permissions are valid.
990 my $database_permissions = $class->dbpermissions($databasename);
992 if ($database_permissions eq 1){
994 # The database permissions are invalid so set the error
997 $error = "InvalidPermissionsSet";
1002 # Delete the database.
1004 ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006 unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database. #
1016 # $dbmodule->connect(options); #
1018 # options Specifies the following options in any order. #
1020 # DatabaseName Specifies the Kiriwrite database to use. #
1021 #################################################################################
1023 # Get the database name.
1029 my ($passedoptions) = @_;
1030 my (%database, $database);
1032 my $dbname = $passedoptions->{"DatabaseName"};
1034 # Check if the database exists.
1036 my $database_exists = $class->dbexists($dbname);
1038 if ($database_exists eq 1){
1040 # The database does not exist so return an error value
1041 # saying that the database does not exist.
1043 $error = "DoesNotExist";
1049 # Check if the database has valid permissions set.
1051 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1053 if ($database_permissions eq 1){
1055 # The database has invalid permissions set so return
1056 # an error value saying that the database has invalid
1059 $error = "InvalidPermissionsSet";
1065 # Connect to the database.
1067 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1068 $database_handle->{unicode} = 1;
1069 $database_filename = $dbname;
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying #
1080 # $dbmodule->selectseconddb(options); #
1082 # options Specifies the following options in any order. #
1084 # DatabaseName Specifies the Kiriwrite database to use. #
1085 #################################################################################
1087 # Get the database name.
1093 my ($passedoptions) = @_;
1094 my (%database, $database);
1096 my $dbname = $passedoptions->{"DatabaseName"};
1098 # Check if the database exists.
1100 my $database_exists = $class->dbexists($dbname);
1102 if ($database_exists eq 1){
1104 # The database does not exist so return an error value
1105 # saying that the database does not exist.
1107 $error = "DoesNotExist";
1113 # Check if the database has valid permissions set.
1115 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1117 if ($database_permissions eq 1){
1119 # The database has invalid permissions set so return
1120 # an error value saying that the database has invalid
1123 $error = "InvalidPermissionsSet";
1129 # Connect to the database.
1131 $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1132 $second_database_handle->{unicode} = 1;
1133 $second_database_filename = $dbname;
1137 #################################################################################
1138 # Page subroutines. #
1139 #################################################################################
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database. #
1147 # $dbmodule->getpagelist(); #
1148 #################################################################################
1155 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1156 $statement_handle->execute();
1158 my @database_pagefilenames;
1159 my @database_pagefilenames_final;
1161 # Process the collected pages.
1163 while (@database_pagefilenames = $statement_handle->fetchrow_array){
1165 # Add each page to the list of pages in the database.
1167 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1171 undef $statement_handle;
1172 return @database_pagefilenames_final;
1177 #################################################################################
1178 # getpageinfo: Gets the page information from the filename passed. #
1182 # $dbmodule->getpageinfo(options); #
1184 # options Specifies the following options in any order. #
1186 # PageFilename Specifies the page filename to get the page information from. #
1187 # Reduced Specifies if the reduced version of the page information should #
1189 #################################################################################
1195 my ($passedoptions) = shift;
1196 my (%database_page, $database_page);
1197 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1202 # Get the page from the database.
1204 my $page_filename = $passedoptions->{"PageFilename"};
1205 my $page_reduced = $passedoptions->{"Reduced"};
1207 if (!$page_reduced){
1213 if ($page_reduced eq 1){
1215 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1217 $statement_handle->execute();
1219 # Check if the page exists in the database.
1221 while (@data_page = $statement_handle->fetchrow_array()){
1223 # Get the values from the array.
1225 $pagefilename = $data_page[0];
1226 $pagename = $data_page[1];
1227 $pagedescription = $data_page[2];
1228 $pagelastmodified = $data_page[3];
1230 # Put the values into the page hash.
1233 "PageFilename" => $pagefilename,
1234 "PageName" => $pagename,
1235 "PageDescription" => $pagedescription,
1236 "PageLastModified" => $class->dateconvert($pagelastmodified),
1246 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1248 $statement_handle->execute();
1250 # Check if the page exists in the database.
1252 while (@data_page = $statement_handle->fetchrow_array()){
1254 # Get the values from the array.
1256 $pagefilename = $data_page[0];
1257 $pagename = $data_page[1];
1258 $pagedescription = $data_page[2];
1259 $pagesection = $data_page[3];
1260 $pagetemplate = $data_page[4];
1261 $pagedata = $data_page[5];
1262 $pagesettings = $data_page[6];
1263 $pagelastmodified = $data_page[7];
1265 # Put the values into the page hash.
1268 "PageFilename" => $pagefilename,
1269 "PageName" => $pagename,
1270 "PageDescription" => $pagedescription,
1271 "PageSection" => $pagesection,
1272 "PageTemplate" => $pagetemplate,
1273 "PageContent" => $pagedata,
1274 "PageSettings" => $pagesettings,
1275 "PageLastModified" => $class->dateconvert($pagelastmodified),
1284 # Check if the page did exist.
1288 $error = "PageDoesNotExist";
1293 undef $statement_handle;
1294 return %database_page;
1299 #################################################################################
1300 # addpage: Add a page to the selected database. #
1304 # $dbmodule->addpage(options); #
1306 # options Specifies the following options in any order. #
1308 # PageFilename Specifies the page filename to use. #
1309 # PageName Specifies the page name to use. #
1310 # PageDescription Specifies the page description to use. #
1311 # PageSection Specifies the page section to use. #
1312 # PageTemplate Specifies the page template to use. #
1313 # PageContent Specifies the page content to use. #
1314 # PageSettings Specifies the page settings to use. #
1315 #################################################################################
1317 # Get the data that was passed to the subroutine.
1323 my ($passedoptions) = shift;
1328 # Get the values passed to the hash.
1330 my $page_filename = $passedoptions->{"PageFilename"};
1331 my $page_name = $passedoptions->{"PageName"};
1332 my $page_description = $passedoptions->{"PageDescription"};
1333 my $page_section = $passedoptions->{"PageSection"};
1334 my $page_template = $passedoptions->{"PageTemplate"};
1335 my $page_content = $passedoptions->{"PageContent"};
1336 my $page_settings = $passedoptions->{"PageSettings"};
1338 # Check to see if the filename given already exists
1339 # in the page database.
1341 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1342 $statement_handle->execute();
1344 # Check if a page with the filename given really does
1347 while (@database_page = $statement_handle->fetchrow_array()){
1349 # A page does exist so increment the count to 1.
1355 if ($page_count ne 0){
1357 # The page does exist so set the error value.
1359 $error = "PageExists";
1364 # Check if certain values are undefined.
1372 if (!$page_description){
1374 $page_description = "";
1378 if (!$page_section){
1384 if (!$page_content){
1390 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1391 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1393 # Add the page to the selected database.
1395 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1396 \'' . $class->convert($page_filename) . '\',
1397 \'' . $class->convert($page_name) . '\',
1398 \'' . $class->convert($page_description) . '\',
1399 \'' . $class->convert($page_section) . '\',
1400 \'' . $class->convert($page_template) . '\',
1401 \'' . $class->convert($page_content) . '\',
1402 \'' . $class->convert($page_settings) . '\',
1403 \'' . $class->convert($page_date) . '\'
1404 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1405 $statement_handle->execute();
1407 undef $statement_handle;
1412 #################################################################################
1413 # deletepage: Delete a page from the selected database. #
1417 # $dbmodule->deletepage(options) #
1419 # options Specifies the following options in any order. #
1421 # PageFilename Specifies the page filename to delete. #
1422 #################################################################################
1427 # Get the data that was passed to the subroutine.
1430 my ($passedoptions) = @_;
1434 # Get the page filename.
1436 my $page_filename = $passedoptions->{"PageFilename"};
1438 # Check if the page exists before deleting it.
1440 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1441 $statement_handle->execute();
1443 while (@page_info = $statement_handle->fetchrow_array()){
1449 # Check if the page really does exist.
1453 $error = "PageDoesNotExist";
1460 $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1461 $statement_handle->execute();
1466 #################################################################################
1467 # editpage: Edit a page from the selected database. #
1471 # $dbmodule->editpage(options); #
1473 # options Specifies the following options in any order. #
1475 # PageFilename Specifies the filename to edit. #
1476 # PageNewFilename Specifies the new filename to use. #
1477 # PageNewName Specifies the new page name to use. #
1478 # PageNewDescription Specifies the new page description to use. #
1479 # PageNewSection Specifies the new page section to use. #
1480 # PageNewTemplate Specifies the new page template to use. #
1481 # PageNewContent Specifies the new page content to use. #
1482 # PageNewSettings Specifies the new page settings to use. #
1483 #################################################################################
1488 # Get the values passed to the subroutine.
1491 my ($passedoptions) = @_;
1495 # Get the data that was passed to the subroutine.
1497 my $page_filename = $passedoptions->{"PageFilename"};
1498 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1499 my $page_newname = $passedoptions->{"PageNewName"};
1500 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1501 my $page_newsection = $passedoptions->{"PageNewSection"};
1502 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1503 my $page_newcontent = $passedoptions->{"PageNewContent"};
1504 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1506 # Check if the page with the filename given exists.
1508 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1509 $statement_handle->execute();
1511 # Check if the page really does exist.
1513 while (@page_info = $statement_handle->fetchrow_array()){
1515 # The page information is found.
1521 # Check if the page really does exist.
1525 $error = "PageDoesNotExist";
1530 # Check if there is a page that already exists with the new
1535 if ($page_filename ne $page_newfilename){
1537 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1538 $statement_handle->execute();
1540 # Check if a page really is using the new filename.
1542 while (@page_info = $statement_handle->fetchrow_array()){
1544 # The page information is found.
1550 if ($page_found eq 1){
1552 $error = "PageExists";
1559 # Get the current date.
1561 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1562 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1564 # Edit the selected page.
1566 $statement_handle = $database_handle->prepare('UPDATE kiriwrite_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 );
1567 $statement_handle->execute();
1572 #################################################################################
1573 # movepage: Moves a page from the old database to the new database. #
1577 # $dbmodule->movepage(options); #
1579 # options Specifies the following options in any order. #
1581 # PageFilename Specifies the page with the filename to move. #
1582 #################################################################################
1587 # Get the values passed to the subroutine.
1590 my ($passedoptions) = @_;
1592 my (%database_page, $database_page);
1593 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1597 # Get the data that was passed to the subroutine.
1599 my $page_filename = $passedoptions->{"PageFilename"};
1601 # Check if the page with the filename given exists.
1603 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1604 $statement_handle->execute();
1606 # Check if the page really does exist.
1608 while (@page_info = $statement_handle->fetchrow_array()){
1610 # Get the values from the array.
1612 $pagefilename = $page_info[0];
1613 $pagename = $page_info[1];
1614 $pagedescription = $page_info[2];
1615 $pagesection = $page_info[3];
1616 $pagetemplate = $page_info[4];
1617 $pagedata = $page_info[5];
1618 $pagesettings = $page_info[6];
1619 $pagelastmodified = $page_info[7];
1621 # Put the values into the page hash.
1624 "PageFilename" => $pagefilename,
1625 "PageName" => $pagename,
1626 "PageDescription" => $pagedescription,
1627 "PageSection" => $pagesection,
1628 "PageTemplate" => $pagetemplate,
1629 "PageContent" => $pagedata,
1630 "PageSettings" => $pagesettings,
1631 "PageLastModified" => $pagelastmodified,
1634 # The page information is found.
1640 # Check if the page really does exist.
1644 $error = "PageDoesNotExist";
1649 # Check if the page with the filename given already exists in
1650 # the database the page is being moved to.
1655 $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1656 $second_statement_handle->execute();
1658 while (@page_info = $second_statement_handle->fetchrow_array()){
1664 # Check if the page really does exist.
1668 $error = "PageAlreadyExists";
1673 # Add the page to the new database.
1675 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1676 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1677 \'' . $class->convert($database_page{"PageName"}) . '\',
1678 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1679 \'' . $class->convert($database_page{"PageSection"}) . '\',
1680 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1681 \'' . $class->convert($database_page{"PageContent"}) . '\',
1682 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1683 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1684 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1685 $second_statement_handle->execute();
1687 # Delete the page from the old database.
1689 $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1690 $statement_handle->execute();
1695 #################################################################################
1696 # copypage: Copies a page from the old database to the new database. #
1700 # $dbmodule->copypage(options); #
1702 # options Specifies the following options in any order. #
1704 # PageFilename Specifies the page with the filename to copy. #
1705 #################################################################################
1710 # Get the values passed to the subroutine.
1713 my ($passedoptions) = @_;
1715 my (%database_page, $database_page);
1716 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1720 # Get the data that was passed to the subroutine.
1722 my $page_filename = $passedoptions->{"PageFilename"};
1724 # Check if the page with the filename given exists.
1726 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1727 $statement_handle->execute();
1729 # Check if the page really does exist.
1731 while (@page_info = $statement_handle->fetchrow_array()){
1733 # Get the values from the array.
1735 $pagefilename = $page_info[0];
1736 $pagename = $page_info[1];
1737 $pagedescription = $page_info[2];
1738 $pagesection = $page_info[3];
1739 $pagetemplate = $page_info[4];
1740 $pagedata = $page_info[5];
1741 $pagesettings = $page_info[6];
1742 $pagelastmodified = $page_info[7];
1744 # Put the values into the page hash.
1747 "PageFilename" => $pagefilename,
1748 "PageName" => $pagename,
1749 "PageDescription" => $pagedescription,
1750 "PageSection" => $pagesection,
1751 "PageTemplate" => $pagetemplate,
1752 "PageContent" => $pagedata,
1753 "PageSettings" => $pagesettings,
1754 "PageLastModified" => $pagelastmodified,
1757 # The page information is found.
1763 # Check if the page really does exist.
1767 $error = "PageDoesNotExist";
1772 # Check if the page with the filename given already exists in
1773 # the database the page is being moved to.
1778 $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1779 $second_statement_handle->execute();
1781 while (@page_info = $second_statement_handle->fetchrow_array()){
1787 # Check if the page really does exist.
1791 $error = "PageAlreadyExists";
1796 # Add the page to the new database.
1798 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1799 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1800 \'' . $class->convert($database_page{"PageName"}) . '\',
1801 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1802 \'' . $class->convert($database_page{"PageSection"}) . '\',
1803 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1804 \'' . $class->convert($database_page{"PageContent"}) . '\',
1805 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1806 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1807 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1808 $second_statement_handle->execute();
1812 #################################################################################
1813 # Filter subroutines. #
1814 #################################################################################
1817 #################################################################################
1818 # connectfilter: Connect to the filter database. #
1822 # $dbmodule->connectfilter(missingignore); #
1824 # missingignore Ignore error about database being missing. #
1825 #################################################################################
1831 my $ignoremissing = shift;
1833 # Check if the template database exists.
1835 my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1837 if ($filterdatabase_exists eq 1){
1839 $filterdb_exists = 0;
1841 if (!$ignoremissing){
1843 $error = "FilterDatabaseDoesNotExist";
1850 # Check if the permission settings for the template database are valid.
1852 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1854 if ($filterdb_permissions eq 1){
1856 # The template database has invalid permissions set
1857 # so return an error value.
1859 if (!$ignoremissing){
1861 $error = "FilterDatabaseInvalidPermissionsSet";
1868 # Connect to the template database.
1870 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1871 $filterdb_database_handle->{unicode} = 1;
1872 $filterdb_loaded = 1;
1876 sub disconnectfilter{
1877 #################################################################################
1878 # disconnectfilter: Disconnect from the filter database. #
1882 # $dbmodule->disconnectfilter(); #
1883 #################################################################################
1885 # Disconnect the template database.
1887 if ($filterdb_loaded eq 1){
1889 undef $filterdb_statement_handle;
1890 $filterdb_database_handle->disconnect();
1897 #################################################################################
1898 # getfilterlist: Gets the list of filters in the filter database. #
1902 # $dbmodule->getfilterlist(); #
1903 #################################################################################
1911 # Get the list of filters available.
1913 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1914 $filterdb_statement_handle->execute();
1916 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
1918 # Add the filter to the list of available filters.
1920 push(@filter_list, $filter_data[0]);
1924 undef $filterdb_statement_handle;
1925 return @filter_list;
1930 #################################################################################
1931 # getfilterinfo: Gets information about the filter. #
1935 # $dbmodule->getfilterinfo(options); #
1937 # options Specifies the following options in any order. #
1939 # FilterID Specifies the filter ID number to get information from. #
1940 #################################################################################
1945 # Get the values passed to the subroutine.
1948 my ($passedoptions) = @_;
1951 my $filter_exists = 0;
1954 # Get the values that are in the hash.
1956 my $filter_id = $passedoptions->{"FilterID"};
1958 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1959 $filterdb_statement_handle->execute();
1961 # Get the filter information.
1963 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
1965 $filter_info{"FilterID"} = $filter_data[0];
1966 $filter_info{"FilterPriority"} = $filter_data[1];
1967 $filter_info{"FilterFind"} = $filter_data[2];
1968 $filter_info{"FilterReplace"} = $filter_data[3];
1969 $filter_info{"FilterNotes"} = $filter_data[4];
1975 # Check if the filter exists.
1977 if (!$filter_exists){
1979 # The filter does not exist so return
1982 $error = "FilterDoesNotExist";
1987 # Return the filter information.
1989 undef $filterdb_statement_handle;
1990 return %filter_info;
1995 #################################################################################
1996 # addfilter: Adds a filter to the filter database. #
2000 # $dbmodule->addfilter(options); #
2002 # options Specifies the following options in any order. #
2004 # FindFilter Specifies the find filter to add. #
2005 # ReplaceFilter Specifies the replace filter to add. #
2006 # Priority Specifies the filter priority to use. #
2007 # Notes Specifies the notes to use. #
2008 #################################################################################
2013 # Get the values passed to the subroutine.
2016 my ($passedoptions) = @_;
2018 # Define some variables for later.
2020 my @database_filters;
2023 my $nofiltertable = 0;
2024 my $filter_found = 0;
2025 my $filter_count = 0;
2029 # Get the values from the hash.
2031 my $filter_find = $passedoptions->{"FindFilter"};
2032 my $filter_replace = $passedoptions->{"ReplaceFilter"};
2033 my $filter_priority = $passedoptions->{"Priority"};
2034 my $filter_notes = $passedoptions->{"Notes"};
2036 # Check if the filter database permissions are valid.
2038 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2039 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2041 if ($filterdb_permissions eq 1){
2043 if ($filterdb_exists eq 0){
2044 $error = "FilterDatabaseInvalidPermissionsSet";
2050 # Check if certain values are undefined and if they
2051 # are then set them blank.
2059 if (!$filter_replace){
2061 $filter_replace = "";
2065 if (!$filter_priority){
2067 $filter_priority = 1;
2071 if (!$filter_notes){
2077 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2079 if ($directory_permissions eq 1 && $filterdb_exists){
2081 # The template database cannot be created because of invalid directory
2082 # permissions so return an error value.
2084 $error = "FilterDatabaseFileUncreateable";
2089 # Check if the filter table exists.
2091 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2093 # Check if there is really no filter table.
2095 if ($nofiltertable){
2097 # Create the filter database table.
2099 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2100 id int(7) primary key,
2102 findsetting varchar(1024),
2103 replacesetting varchar(1024),
2105 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2106 $filterdb_statement_handle->execute();
2110 # Find the lowest filter identification number available.
2112 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2113 $filterdb_statement_handle->execute();
2115 while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2117 $filter_id = $database_filters[0];
2119 # Add the filter identification to the list of filter IDs.
2121 push(@filterid_list, $filter_id);
2127 # Process each filter looking for a blank available filter.
2129 foreach $filter_id (@filterid_list){
2131 # Check the next filter ID to see if it's blank.
2133 $new_id = $filter_id + 1;
2135 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2136 $filterdb_statement_handle->execute();
2138 # Get the filter identification number.
2140 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2146 # Check if a filter was found.
2148 if (!$filter_found){
2150 # No filter was found using this ID so exit the loop.
2156 # Increment the filter count and reset the filter found value.
2164 # Check if there were any filters in the filter database.
2166 if (!$filter_count && !$new_id){
2168 # There were no filters in the filter database so set
2169 # the new filter identification value to 1.
2175 # Add the filter to the filter database.
2177 $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2178 \'' . $class->convert($new_id) . '\',
2179 \'' . $class->convert($filter_priority) . '\',
2180 \'' . $class->convert($filter_find) . '\',
2181 \'' . $class->convert($filter_replace) .'\',
2182 \'' . $class->convert($filter_notes) . '\'
2183 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2184 $filterdb_statement_handle->execute();
2189 #################################################################################
2190 # editfilter: Edits a filter in the filter database. #
2194 # $dbmodule->editfilter(options); #
2196 # options Specifies the following options in any order. #
2198 # FilterID Specifies the filter to edit. #
2199 # NewFindFilter Specifies the new find filter setting. #
2200 # NewReplaceFilter Specifies the new replace filter setting. #
2201 # NewFilterPriority Specifies the new filter priority setting. #
2202 # NewFilterNotes Specifies the new notes for the filter. #
2203 #################################################################################
2208 # Get the values passed to the subroutine.
2211 my ($passedoptions) = @_;
2214 my $filter_exists = 1;
2217 # Get the values from the hash.
2219 my $filter_id = $passedoptions->{"FilterID"};
2220 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2221 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2222 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2223 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2225 # Check if the filter database permissions are valid.
2227 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2228 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2230 if ($filterdb_permissions eq 1){
2232 if ($filterdb_exists eq 0){
2233 $error = "FilterDatabaseInvalidPermissionsSet";
2239 # Check if the filter exists before editing it.
2241 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2242 $filterdb_statement_handle->execute();
2244 # Check if the filter exists.
2246 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2252 # Check if the filter really does exist.
2254 if (!$filter_exists){
2256 # The filter does not exist so return
2259 $error = "FilterDoesNotExist";
2264 # Edit the selected filter.
2266 $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2267 findsetting = \'' . $class->convert($filter_newfind) . '\',
2268 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2269 priority = \'' . $class->convert($filter_newpriority) . '\',
2270 notes = \'' . $class->convert($filter_newnotes) . '\'
2271 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2272 $filterdb_statement_handle->execute();
2274 undef $filterdb_statement_handle;
2280 #################################################################################
2281 # deletefilter: Deletes a filter from the filter database. #
2285 # $dbmodule->deletefilter(options); #
2287 # options Specifies the following options in any order. #
2289 # FilterID Specifies the filter to delete from the filter database. #
2290 #################################################################################
2295 # Get the values passed to the subroutine.
2298 my ($passedoptions) = @_;
2300 my $filter_exists = 0;
2303 # Get the values from the hash.
2305 my $filter_id = $passedoptions->{"FilterID"};
2307 # Check if the filter exists before deleting.
2309 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2310 $filterdb_statement_handle->execute();
2312 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2318 # Check to see if the filter really does exist.
2320 if (!$filter_exists){
2322 $error = "FilterDoesNotExist";
2327 # Delete the filter from the filter database.
2329 $filterdb_statement_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2330 $filterdb_statement_handle->execute();
2332 undef $filterdb_statement_handle;
2336 #################################################################################
2337 # Template subroutines. #
2338 #################################################################################
2340 sub connecttemplate{
2341 #################################################################################
2342 # connecttemplate: Connect to the template database. #
2346 # $dbmodule->connecttemplate(missingignore); #
2348 # missingignore Ignore errror about database being missing. #
2349 #################################################################################
2355 my $ignoremissing = shift;
2357 # Check if the template database exists.
2359 my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2361 if ($templatedatabase_exists eq 1){
2363 $templatedb_exists = 0;
2365 if (!$ignoremissing){
2367 $error = "TemplateDatabaseDoesNotExist";
2374 # Check if the permission settings for the template database are valid.
2376 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2378 if ($templatedb_permissions eq 1){
2380 # The template database has invalid permissions set
2381 # so return an error value.
2383 if (!$ignoremissing){
2385 $error = "TemplateDatabaseInvalidPermissionsSet";
2392 # Connect to the template database.
2394 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2395 $template_database_handle->{unicode} = 1;
2396 $templatedb_loaded = 1;
2400 sub disconnecttemplate{
2401 #################################################################################
2402 # disconnecttemplate: Disconnect from the template database. #
2406 # $dbmodule->disconnecttemplate(); #
2407 #################################################################################
2409 # Disconnect the template database.
2411 if ($templatedb_loaded eq 1){
2413 undef $template_statement_handle;
2414 $template_database_handle->disconnect();
2420 sub gettemplatelist{
2421 #################################################################################
2422 # gettemplatelist: Gets the list of templates. #
2426 # $dbmodule->gettemplatelist(); #
2427 #################################################################################
2432 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2433 $template_statement_handle->execute();
2435 my @database_template;
2437 my $template_filename;
2439 while (@database_template = $template_statement_handle->fetchrow_array()){
2441 # Get certain values from the array.
2443 $template_filename = $database_template[0];
2445 # Add the template to the list of templates.
2447 push(@templates_list, $template_filename);
2451 return @templates_list;
2455 sub gettemplateinfo{
2456 #################################################################################
2457 # gettemplateinfo: Get information on a template. #
2461 # $dbmodule->gettemplateinfo(options); #
2463 # options Specifies the following options in any order. #
2465 # TemplateFilename Specifies the template filename to use. #
2466 #################################################################################
2471 # Get the data passed to the subroutine.
2474 my ($passedoptions) = @_;
2479 my $template_filename;
2481 my $template_description;
2482 my $template_datemodified;
2483 my $template_layout;
2485 my $template_found = 0;
2487 my $filename = $passedoptions->{"TemplateFilename"};
2489 $template_statement_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2490 $template_statement_handle->execute();
2492 while (@template_data = $template_statement_handle->fetchrow_array()){
2494 # Get certain values from the array.
2496 $template_filename = $template_data[0];
2497 $template_name = $template_data[1];
2498 $template_description = $template_data[2];
2499 $template_layout = $template_data[3];
2500 $template_datemodified = $template_data[4];
2502 # Process them into the hash.
2505 "TemplateFilename" => $template_filename,
2506 "TemplateName" => $template_name,
2507 "TemplateDescription" => $template_description,
2508 "TemplateLayout" => $template_layout,
2509 "TemplateLastModified" => $template_datemodified
2512 $template_found = 1;
2516 if ($template_found eq 0){
2518 # The template was not found in the template database so
2519 # write an error value.
2521 $error = "TemplateDoesNotExist";
2531 #################################################################################
2532 # addtemplate: Adds a template to the template database. #
2536 # $dbmodule->addtemplate(); #
2538 # options Specifies the following options in any order. #
2540 # TemplateFilename Specifies the new template filename. #
2541 # TemplateName Specifies the new template name. #
2542 # TemplateDescription Specifies the new template description. #
2543 # TemplateLayout Specifies the new template layout. #
2544 #################################################################################
2549 # Get the data passed to the subroutine.
2552 my ($passedoptions) = @_;
2555 my $notemplatetable;
2558 my $template_filename = $passedoptions->{"TemplateFilename"};
2559 my $template_name = $passedoptions->{"TemplateName"};
2560 my $template_description = $passedoptions->{"TemplateDescription"};
2561 my $template_layout = $passedoptions->{"TemplateLayout"};
2563 # Check if the template database permissions are valid.
2565 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2566 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2568 if ($templatedb_permissions eq 1){
2570 if ($templatedb_exists eq 0){
2571 $error = "TemplateDatabaseInvalidPermissionsSet";
2577 # Check if the template already exists before adding.
2579 if ($templatedb_exists eq 0){
2581 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2583 if ($blankfile eq 0){
2585 $template_statement_handle->execute();
2587 while (@page_exists = $template_statement_handle->fetchrow_array()){
2589 $error = "TemplatePageExists";
2598 # Get the current date.
2600 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2602 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2604 # Check if certain values are undefined and if they
2605 # are then set them blank.
2607 if (!$template_name){
2609 $template_name = "";
2613 if (!$template_description){
2615 $template_description = "";
2619 if (!$template_layout){
2621 $template_layout = "";
2625 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2627 if ($directory_permissions eq 1 && $templatedb_exists){
2629 # The template database cannot be created because of invalid directory
2630 # permissions so return an error value.
2632 $error = "TemplateDatabaseUncreateable";
2637 # Check to see if a template can be added.
2639 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2640 \'' . $class->convert($template_filename) . '\',
2641 \'' . $class->convert($template_name) . '\',
2642 \'' . $class->convert($template_description) . '\',
2643 \'' . $class->convert($template_layout) . '\',
2644 \'' . $class->convert($template_date) . '\'
2645 )') or ( $notemplatetable = 1 );
2647 if (!$notemplatetable){
2649 $template_statement_handle->execute();
2653 # Check to see if there is no template table and attempt to create one.
2655 if ($notemplatetable){
2657 # Create a template table.
2659 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2661 if ($directory_permissions eq 1){
2663 # The template database cannot be created because of invalid directory
2664 # permissions so return an error.
2666 $error = "TemplateDatabaseFileUncreateable";
2671 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2672 filename varchar(256) primary key,
2673 templatename varchar(512),
2674 templatedescription varchar(512),
2675 templatelayout text,
2676 datemodified datetime
2677 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2678 $template_statement_handle->execute();
2680 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2681 \'' . $class->convert($template_filename) . '\',
2682 \'' . $class->convert($template_name) . '\',
2683 \'' . $class->convert($template_description) . '\',
2684 \'' . $class->convert($template_layout) . '\',
2685 \'' . $class->convert($template_date) . '\'
2686 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2687 $template_statement_handle->execute();
2694 #################################################################################
2695 # deletetemplate: Deletes a template from the template database. #
2699 # $dbmodule->deletetemplate(options); #
2701 # options Specifies the following options in any order. #
2703 # TemplateFilename Specifies the template filename to delete. #
2704 #################################################################################
2709 # Get the data passed to the subroutine.
2712 my ($passedoptions) = @_;
2715 my $template_filename = $passedoptions->{"TemplateFilename"};
2716 my $template_count = 0;
2718 # Check if the template exists.
2720 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2721 $template_statement_handle->execute();
2723 while (@pagedata = $template_statement_handle->fetchrow_array()){
2729 if ($template_count eq 0){
2731 # No pages were returned so return an error value.
2733 $error = "TemplateDoesNotExist";
2738 # Delete the template from the template database.
2740 $template_statement_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2741 $template_statement_handle->execute();
2746 #################################################################################
2747 # editttemplate: Edits a Kiriwrite template. #
2751 # $dbmodule->edittemplate(options); #
2753 # options Specifies the following options in any order. #
2755 # TemplateFilename Specifies the template filename to edit. #
2756 # NewTemplateFilename Specifies the new template filename. #
2757 # NewTemplateName Specifies the new template name. #
2758 # NewTemplateDescription Specifies the new template description. #
2759 # NewTemplateLayout Specifies the new template layout. #
2760 #################################################################################
2762 # Get the values passed.
2765 my ($passedoptions) = @_;
2766 my $template_found = 0;
2769 # Process the values passed.
2771 my $template_filename = $passedoptions->{"TemplateFilename"};
2772 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2773 my $new_template_name = $passedoptions->{"NewTemplateName"};
2774 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2775 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2777 # Check if the template database permissions are valid.
2779 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2780 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2782 if ($templatedb_permissions eq 1){
2784 if ($templatedb_exists eq 0){
2785 $error = "TemplateDatabaseInvalidPermissionsSet";
2791 # Check if the template exists.
2793 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2794 $template_statement_handle->execute();
2796 while (@template_info = $template_statement_handle->fetchrow_array()){
2798 $template_found = 1;
2802 # Check to see if the template was found and set an error value if
2805 if ($template_found eq 0){
2807 $error = "TemplateDoesNotExist";
2812 # Get the date and time.
2814 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2815 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2817 # Update the template information.
2819 $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2820 filename = \'' . $class->convert($new_template_filename) . '\',
2821 templatename = \'' . $class->convert($new_template_name) . '\',
2822 templatedescription = \'' . $class->convert($new_template_description) . '\',
2823 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2824 datemodified = \'' . $class->convert($templatenewdate) . '\'
2825 WHERE filename = \'' . $class->convert($template_filename) . '\'
2826 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2827 $template_statement_handle->execute();
2831 #################################################################################
2832 # General subroutines. #
2833 #################################################################################
2836 #################################################################################
2837 # connect: Connect to the server. #
2841 # $dbmodule->connect(); #
2842 #################################################################################
2844 # This function is not needed in this database module.
2849 #################################################################################
2850 # connect: Disconnect from the server. #
2854 # $dbmodule->disconnect(); #
2855 #################################################################################
2857 # This function is not needed in this database module.
2859 undef $statement_handle;