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.
35 # Set the following values.
37 our $VERSION = "0.1.0";
38 my ($options, %options);
41 my $second_database_handle;
42 my $second_string_handle;
43 my $database_filename;
44 my $second_database_filename;
45 my $templatedb_loaded = 0;
46 my $templatedb_exists = 1;
47 my $template_string_handle;
48 my $template_database_handle;
49 my $filterdb_loaded = 0;
50 my $filterdb_exists = 1;
51 my $filterdb_string_handle;
52 my $filterdb_database_handle;
56 #################################################################################
57 # Generic Subroutines. #
58 #################################################################################
61 #################################################################################
62 # new: Create an instance of Kiriwrite::Database::SQLite #
66 # $dbmodule = Kiriwrite::Database::SQLite->new(); #
67 #################################################################################
69 # Get the perl module name.
74 return bless($self, $class);
79 #################################################################################
80 # loadsettings: Loads settings into the SQLite database module #
84 # $dbmodule->loadsettings(Directory, options); #
86 # options Specifies the following options (in any order). #
88 # Directory Specifies the directory to use for getting databases. #
89 # DateTime Specifies the date and time format to use. #
90 # Server Specifies the server to use. #
91 # Database Specifies the database to use. #
92 # Username Specifies the username to use. #
93 # Password Specifies the password to use. #
94 # HashType Specifies the password hash type to use. #
95 # Port Specifies the server port to use. #
96 # Protocol Specifies the protocol to use. #
97 # TablePrefix Specifies the table prefix to use. #
98 #################################################################################
100 # Get the data passed to the subroutine.
103 my ($passedoptions) = @_;
105 # Add the directory setting to the list of options (as it's the only
106 # one needed for this database module).
109 "Directory" => $passedoptions->{"Directory"},
110 "DateTime" => $passedoptions->{"DateTime"},
116 #################################################################################
117 # convert: Converts data into SQL formatted data. #
121 # $dbmodule->convert(data); #
123 # data Specifies the data to convert. #
124 #################################################################################
126 # Get the data passed to the subroutine.
143 #################################################################################
144 # dateconvert: Converts a SQL date into a proper date. #
148 # $dbmodule->dateconvert(date); #
150 # date Specifies the date to convert. #
151 #################################################################################
153 # Get the date passed to the subroutine.
158 # Convert the date given into the proper date.
160 # Create the following varialbes to be used later.
187 # Split the date and time.
189 $length = length($data);
195 # Get the character and check if it is a space.
197 $char = substr($data, $seek, 1);
201 # The character is a space, so get the date and time.
203 $date = substr($data, 0, $seek);
204 $timelength = $length - $seek - 1;
205 $time = substr($data, $seek + 1, $timelength);
211 } until ($seek eq $length);
213 # Get the year, month and date.
215 $length = length($date);
220 # Get the character and check if it is a dash.
222 $char = substr($date, $seek, 1);
226 # The character is a dash, so get the year, month or day.
228 $datelength = $seek - $startchar;
232 # Get the year from the date.
234 $year = substr($date, 0, $datelength) + 1900;
238 # Get the last two characters to get the short year
241 $year_short = substr($year, 2, 2);
243 } elsif ($count eq 1){
245 # Get the month and day from the date.
247 $month = substr($date, $startchar + 1, $datelength - 1) + 1;
249 # Check if the month is less then 10, if it is
250 # add a zero to the value.
254 $month_full = '0' . $month;
258 $month_full = $month;
265 $daylength = $length - $seek + 1;
266 $day = substr($date, $startchar + 1, $daylength);
268 # Check if the day is less than 10, if it is
269 # add a zero to the value.
273 $day_full = '0' . $day;
287 } until ($seek eq $length);
289 # Get the length of the time value and reset certain
292 $length = length($time);
299 # Get the character and check if it is a colon.
301 $char = substr($time, $seek, 1);
305 # The character is a colon, so get the hour, minute and day.
307 $timelength = $seek - $startchar;
311 # Get the hour from the time.
313 $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);
337 # If the minute is less than ten then add a
342 $minute_full = '0' . $minute;
346 $minute_full = $minute;
352 $secondlength = $length - $seek + 1;
353 $second = substr($time, $startchar + 1, $secondlength);
355 # If the second is less than ten then add a
360 $second_full = '0' . $second;
364 $second_full = $second;
374 } until ($seek eq $length);
376 # Get the setting for displaying the date and time.
378 $data = $options{"DateTime"};
380 # Process the setting for displaying the date and time
381 # using regular expressions
383 $data =~ s/DD/$day_full/g;
385 $data =~ s/MM/$month_full/g;
386 $data =~ s/M/$month/g;
387 $data =~ s/YY/$year/g;
388 $data =~ s/Y/$year_short/g;
390 $data =~ s/hh/$hour_full/g;
391 $data =~ s/h/$hour/g;
392 $data =~ s/mm/$minute_full/g;
393 $data =~ s/m/$minute/g;
394 $data =~ s/ss/$second_full/g;
395 $data =~ s/s/$second/g;
404 #################################################################################
405 # geterror: Gets the error message (or extended error message). #
409 # $dbmodule->geterror(extended); #
411 # Extended Specifies if the extended error should be retrieved. #
412 #################################################################################
414 # Get the data passed to the subroutine.
417 my $extended = shift;
431 # Check to see if extended information should be returned.
435 # Extended information should be returned.
441 # Basic information should be returned.
450 #################################################################################
451 # dbpermissions: Check if the permissions for the database are valid. #
455 # $database->dbpermissions(dbname, read, write); #
457 # dbname Specifies the database name to check. #
458 # read Check to see if the database can be read. #
459 # write Check to see if the database can be written. #
460 #################################################################################
462 # Get the database name, read setting and write setting.
464 my ($class, $dbname, $readper, $writeper) = @_;
466 # Check if the database can be read.
470 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
472 # The database can be read.
476 # The database cannot be read, so return a value
485 # Check if the database can be written.
489 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
491 # The database can be read.
495 # The database cannot be read, so return a value
504 # No errors have occured while checking so return a value
512 #################################################################################
513 # dbexists: Check if the database exists. #
517 # $dbmodule->dbexists(dbname); #
519 # dbname Specifies the database name to check. #
520 #################################################################################
522 # Get the value that was passed to the subroutine.
527 # Check if the filename exists, if it does, return a value of 1, else
528 # return a value of 0, meaning that the file was not found.
530 if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
532 # Specified file does exist so return a value of 0.
538 # Specified file does not exist so return a value of 1.
546 #################################################################################
547 # Database Subroutines. #
548 #################################################################################
551 #################################################################################
552 # getdblist: Gets the list of available databases. #
556 # $dbmodule->getdblist(); #
557 #################################################################################
559 # Get the list of databases.
562 my @data_directory_final;
564 my $database_filename_length;
565 my $database_filename_friendly;
567 # Check if the database directory has valid permission settings.
569 if (-e $options{"Directory"}){
571 # The database directory does exist. So check if
572 # the permission settings are valid.
574 if (-r $options{"Directory"}){
576 # The permission settings for reading the directory
581 # The permission settings for reading the directory
582 # are invalid so return an error value.
584 $error = "DataDirInvalidPermissions";
591 # The database directory does not exist, so return an
594 $error = "DataDirMissing";
599 opendir(DATADIR, $options{"Directory"});
600 @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
603 # Process the list of databases.
605 foreach $database (@data_directory){
607 $database =~ s/.db.sqlite$//g;
608 $database_filename_friendly = $database;
610 #$database_filename_length = length($database);
611 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
612 push(@data_directory_final, $database_filename_friendly);
616 # Return the list of databases.
618 return @data_directory_final;
623 #################################################################################
624 # getdatabaseinfo: Get information about the database. #
628 # $dbmodule->getdatabaseinfo(); #
629 #################################################################################
631 # Get the database information.
634 my ($databaseinfo, %databaseinfo);
635 my ($sqldata, @sqldata);
640 $string_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 (
641 $error = "DatabaseError", $errorext = $database_handle->errstr, return
643 $string_handle->execute();
645 @sqldata = $string_handle->fetchrow_array();
647 # Process the database information into a hash.
650 "DatabaseName" => $sqldata[0],
651 "Description" => $sqldata[1],
652 "Notes" => $sqldata[2],
653 "Categories" => $sqldata[3],
654 "Major" => $sqldata[4],
655 "Minor" => $sqldata[5],
656 "Revision" => $sqldata[6]
659 $string_handle->finish();
660 undef $string_handle;
662 return %databaseinfo;
666 sub getseconddatabaseinfo{
667 #################################################################################
668 # getseconddatabaseinfo: Get information about the database that pages will be #
669 # moved or copied to. #
673 # $dbmodule->getseconddatabaseinfo(); #
674 #################################################################################
676 # Get the database information.
679 my ($databaseinfo, %databaseinfo);
680 my ($sqldata, @sqldata);
685 $second_string_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 (
686 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
688 $second_string_handle->execute();
690 @sqldata = $second_string_handle->fetchrow_array();
692 # Process the database information into a hash.
695 "DatabaseName" => $sqldata[0],
696 "Description" => $sqldata[1],
697 "Notes" => $sqldata[2],
698 "Categories" => $sqldata[3],
699 "Major" => $sqldata[4],
700 "Minor" => $sqldata[5],
701 "Revision" => $sqldata[6]
704 $second_string_handle->finish();
705 undef $second_string_handle;
707 return %databaseinfo;
712 #################################################################################
713 # adddatabase: Adds a Kiriwrite database. #
717 # $dbmodule->adddatabase(options); #
719 # options Specifies the following options in any order. #
721 # DatabaseFilename Specifies the database file/shortname to use. #
722 # DatabaseName Specifies the database name to use. #
723 # DatabaseDescription Specifies the database description to use. #
724 # DatabaseNotes Specifies the database notes to use. #
725 # DatabaseCategories Specifies the database categories to use. #
726 # VersionMajor Specifies the major version. #
727 # VersionMinor Specifies the minor version. #
728 # VersionRevision Specifies the revision version. #
729 #################################################################################
731 # Get the database that was passed to the subroutine.
737 my ($passedoptions) = @_;
739 my $dbfilename = $passedoptions->{"DatabaseFilename"};
740 my $dbname = $passedoptions->{"DatabaseName"};
741 my $dbdescription = $passedoptions->{"DatabaseDescription"};
742 my $dbnotes = $passedoptions->{"DatabaseNotes"};
743 my $dbcategories = $passedoptions->{"DatabaseCategories"};
744 my $dbmajorver = $passedoptions->{"VersionMajor"};
745 my $dbminorver = $passedoptions->{"VersionMinor"};
746 my $dbrevisionver = $passedoptions->{"VersionRevision"};
748 # Check if the database with the filename given already exists.
750 my $database_exists = $class->dbexists($dbfilename);
752 if ($database_exists eq 0){
754 # The database filename exists so set the error value.
756 $error = "DatabaseExists";
761 # Create the database structure.
763 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
764 $database_handle->{unicode} = 1;
765 $string_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
766 name varchar(256) primary key,
767 description varchar(512),
769 categories varchar(512),
770 kiriwrite_version_major int(4),
771 kiriwrite_version_minor int(4),
772 kiriwrite_version_revision int(4)
773 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
774 $string_handle->execute();
776 $string_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
777 filename varchar(256) primary key,
778 pagename varchar(512),
779 pagedescription varchar(512),
780 pagesection varchar(256),
781 pagetemplate varchar(64),
784 lastmodified datetime
785 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
786 $string_handle->execute();
788 # Convert the values into SQL query formatted values and add an entry
789 # to the kiriwrite_database_info table.
791 $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
792 \'' . $class->convert($dbname) . '\',
793 \'' . $class->convert($dbdescription) . '\',
794 \'' . $class->convert($dbnotes) . '\',
795 \'' . $class->convert($dbcategories) . '\',
796 \'' . $class->convert($dbmajorver) . '\',
797 \'' . $class->convert($dbminorver) . '\',
798 \'' . $class->convert($dbrevisionver) . '\'
799 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
800 $string_handle->execute();
805 #################################################################################
806 # editdatabase: Edits a Kiriwrite Database. #
810 # $dbmodule->editdatabase(options); #
812 # options Specifies the following options in any order. #
814 # NewDatabaseFilename Specifies the new database filename to use. #
815 # DatabaseName Specifies the new database name. #
816 # DatabaseDescription Specifies the new database description. #
817 # DatabaseNotes Specifies the new database notes. #
818 # DatabaseCategories Specifies the new database categories. #
819 #################################################################################
825 my ($passedoptions) = @_;
827 my $dbnewfilename = $passedoptions->{"DatabaseNewFilename"};
828 my $dbname = $passedoptions->{"DatabaseName"};
829 my $dbdescription = $passedoptions->{"DatabaseDescription"};
830 my $dbnotes = $passedoptions->{"DatabaseNotes"};
831 my $dbcategories = $passedoptions->{"DatabaseCategories"};
833 # Check if a new database filename has been specified and if a
834 # new database filename has been specified then change the
837 if ($database_filename ne $dbnewfilename){
839 # A new database filename has been given so check if the output
840 # directory has write access.
842 if (-r $options{"Directory"}){
844 # The directory is readable.
848 # The directory is not readable so set the error value.
850 $error = "DataDirInvalidPermissions";
856 if (-w $options{"Directory"}){
858 # The directory is writeable.
862 # The directory is not writeable so set the error value.
864 $error = "DataDirInvalidPermissions";
870 # Check if a database filename already exists before using the
873 my $database_newexists = $class->dbexists($dbnewfilename);
875 if ($database_newexists eq 0){
877 # The database filename exists so set the error value.
879 $error = "DatabaseExists";
884 # Check if the database can be renamed (has write access).
886 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
888 if ($database_permissions eq 1){
890 # The database filename exists so set the error value.
892 $error = "InvalidPermissionsSet";
897 # "Disconnect" from the database.
899 $database_handle->disconnect();
901 # Rename the database.
903 ($database_filename) = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
904 ($dbnewfilename) = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
906 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
908 # Reload the database from the new filename.
910 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
911 $database_handle->{unicode} = 1;
912 $database_filename = $dbnewfilename;
916 # Check if the database can be altered with the new data.
918 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
920 if ($database_permissions eq 1){
922 # The database filename exists so set the error value.
924 $error = "InvalidPermissionsSet";
929 # Get the current database information.
931 $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
932 $string_handle->execute();
934 my @database_oldinfo = $string_handle->fetchrow_array();
936 my $dboldname = $database_oldinfo[0];
938 # Update the database information.
940 $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
941 description = \'' . $class->convert($dbdescription) . '\',
942 notes = \'' . $class->convert($dbnotes) . '\',
943 categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
944 $string_handle->execute();
946 undef $string_handle;
952 #################################################################################
953 # deletedatabase: Deletes a Kiriwrite database. #
957 # $dbmodule->deletedatabase(options); #
959 # options Specifies the following options in any order. #
961 # DatabaseName Specifies the Kiriwrite database to delete. #
962 #################################################################################
967 # Get the database filename.
970 my ($passedoptions) = shift;
972 my $databasename = $passedoptions->{"DatabaseName"};
974 # Check if the database exists.
976 my $database_exists = $class->dbexists($databasename);
978 if ($database_exists eq 1){
980 # The database does not exist so set the error value.
982 $error = "DoesNotExist";
987 # Check if the database permissions are valid.
989 my $database_permissions = $class->dbpermissions($databasename);
991 if ($database_permissions eq 1){
993 # The database permissions are invalid so set the error
996 $error = "InvalidPermissionsSet";
1001 # Delete the database.
1003 ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1005 unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1010 #################################################################################
1011 # selectdb: Selects the Kiriwrite database. #
1015 # $dbmodule->connect(options); #
1017 # options Specifies the following options in any order. #
1019 # DatabaseName Specifies the Kiriwrite database to use. #
1020 #################################################################################
1022 # Get the database name.
1028 my ($passedoptions) = @_;
1029 my (%database, $database);
1031 my $dbname = $passedoptions->{"DatabaseName"};
1033 # Check if the database exists.
1035 my $database_exists = $class->dbexists($dbname);
1037 if ($database_exists eq 1){
1039 # The database does not exist so return an error value
1040 # saying that the database does not exist.
1042 $error = "DoesNotExist";
1048 # Check if the database has valid permissions set.
1050 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1052 if ($database_permissions eq 1){
1054 # The database has invalid permissions set so return
1055 # an error value saying that the database has invalid
1058 $error = "InvalidPermissionsSet";
1064 # Connect to the database.
1066 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1067 $database_handle->{unicode} = 1;
1068 $database_filename = $dbname;
1073 #################################################################################
1074 # selectseconddb: Selects a second Kiriwrite database for moving and copying #
1079 # $dbmodule->selectseconddb(options); #
1081 # options Specifies the following options in any order. #
1083 # DatabaseName Specifies the Kiriwrite database to use. #
1084 #################################################################################
1086 # Get the database name.
1092 my ($passedoptions) = @_;
1093 my (%database, $database);
1095 my $dbname = $passedoptions->{"DatabaseName"};
1097 # Check if the database exists.
1099 my $database_exists = $class->dbexists($dbname);
1101 if ($database_exists eq 1){
1103 # The database does not exist so return an error value
1104 # saying that the database does not exist.
1106 $error = "DoesNotExist";
1112 # Check if the database has valid permissions set.
1114 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1116 if ($database_permissions eq 1){
1118 # The database has invalid permissions set so return
1119 # an error value saying that the database has invalid
1122 $error = "InvalidPermissionsSet";
1128 # Connect to the database.
1130 $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1131 $second_database_handle->{unicode} = 1;
1132 $second_database_filename = $dbname;
1136 #################################################################################
1137 # Page subroutines. #
1138 #################################################################################
1141 #################################################################################
1142 # getpagelist: Gets the list of pages from the database. #
1146 # $dbmodule->getpagelist(); #
1147 #################################################################################
1154 $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1155 $string_handle->execute();
1157 my @database_pagefilenames;
1158 my @database_pagefilenames_final;
1160 # Process the collected pages.
1162 while (@database_pagefilenames = $string_handle->fetchrow_array){
1164 # Add each page to the list of pages in the database.
1166 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1170 undef $string_handle;
1171 return @database_pagefilenames_final;
1176 #################################################################################
1177 # getpageinfo: Gets the page information from the filename passed. #
1181 # $dbmodule->getpageinfo(options); #
1183 # options Specifies the following options in any order. #
1185 # PageFilename Specifies the page filename to get the page information from. #
1186 #################################################################################
1192 my ($passedoptions) = shift;
1193 my (%database_page, $database_page);
1194 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1199 # Get the page from the database.
1201 my $page_filename = $passedoptions->{"PageFilename"};
1203 $string_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 );
1204 $string_handle->execute();
1206 # Check if the page exists in the database.
1208 while (@data_page = $string_handle->fetchrow_array()){
1210 # Get the values from the array.
1212 $pagefilename = $data_page[0];
1213 $pagename = $data_page[1];
1214 $pagedescription = $data_page[2];
1215 $pagesection = $data_page[3];
1216 $pagetemplate = $data_page[4];
1217 $pagedata = $data_page[5];
1218 $pagesettings = $data_page[6];
1219 $pagelastmodified = $data_page[7];
1221 # Put the values into the page hash.
1224 "PageFilename" => $pagefilename,
1225 "PageName" => $pagename,
1226 "PageDescription" => $pagedescription,
1227 "PageSection" => $pagesection,
1228 "PageTemplate" => $pagetemplate,
1229 "PageContent" => $pagedata,
1230 "PageSettings" => $pagesettings,
1231 "PageLastModified" => $class->dateconvert($pagelastmodified),
1238 # Check if the page did exist.
1242 $error = "PageDoesNotExist";
1247 undef $string_handle;
1248 return %database_page;
1253 #################################################################################
1254 # addpage: Add a page to the selected database. #
1258 # $dbmodule->addpage(options); #
1260 # options Specifies the following options in any order. #
1262 # PageFilename Specifies the page filename to use. #
1263 # PageName Specifies the page name to use. #
1264 # PageDescription Specifies the page description to use. #
1265 # PageSection Specifies the page section to use. #
1266 # PageTemplate Specifies the page template to use. #
1267 # PageContent Specifies the page content to use. #
1268 # PageSettings Specifies the page settings to use. #
1269 #################################################################################
1271 # Get the data that was passed to the subroutine.
1277 my ($passedoptions) = shift;
1282 # Get the values passed to the hash.
1284 my $page_filename = $passedoptions->{"PageFilename"};
1285 my $page_name = $passedoptions->{"PageName"};
1286 my $page_description = $passedoptions->{"PageDescription"};
1287 my $page_section = $passedoptions->{"PageSection"};
1288 my $page_template = $passedoptions->{"PageTemplate"};
1289 my $page_content = $passedoptions->{"PageContent"};
1290 my $page_settings = $passedoptions->{"PageSettings"};
1292 # Check to see if the filename given already exists
1293 # in the page database.
1295 $string_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 );
1296 $string_handle->execute();
1298 # Check if a page with the filename given really does
1301 while (@database_page = $string_handle->fetchrow_array()){
1303 # A page does exist so increment the count to 1.
1309 if ($page_count ne 0){
1311 # The page does exist so set the error value.
1313 $error = "PageExists";
1318 # Check if certain values are undefined.
1326 if (!$page_description){
1328 $page_description = "";
1332 if (!$page_section){
1338 if (!$page_content){
1344 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1345 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1347 # Add the page to the selected database.
1349 $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1350 \'' . $class->convert($page_filename) . '\',
1351 \'' . $class->convert($page_name) . '\',
1352 \'' . $class->convert($page_description) . '\',
1353 \'' . $class->convert($page_section) . '\',
1354 \'' . $class->convert($page_template) . '\',
1355 \'' . $class->convert($page_content) . '\',
1356 \'' . $class->convert($page_settings) . '\',
1357 \'' . $class->convert($page_date) . '\'
1358 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1359 $string_handle->execute();
1361 undef $string_handle;
1366 #################################################################################
1367 # deletepage: Delete a page from the selected database. #
1371 # $dbmodule->deletepage(options) #
1373 # options Specifies the following options in any order. #
1375 # PageFilename Specifies the page filename to delete. #
1376 #################################################################################
1381 # Get the data that was passed to the subroutine.
1384 my ($passedoptions) = @_;
1388 # Get the page filename.
1390 my $page_filename = $passedoptions->{"PageFilename"};
1392 # Check if the page exists before deleting it.
1394 $string_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 );
1395 $string_handle->execute();
1397 while (@page_info = $string_handle->fetchrow_array()){
1403 # Check if the page really does exist.
1407 $error = "PageDoesNotExist";
1414 $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1415 $string_handle->execute();
1420 #################################################################################
1421 # editpage: Edit a page from the selected database. #
1425 # $dbmodule->editpage(options); #
1427 # options Specifies the following options in any order. #
1429 # PageFilename Specifies the filename to edit. #
1430 # PageNewFilename Specifies the new filename to use. #
1431 # PageNewName Specifies the new page name to use. #
1432 # PageNewDescription Specifies the new page description to use. #
1433 # PageNewSection Specifies the new page section to use. #
1434 # PageNewTemplate Specifies the new page template to use. #
1435 # PageNewContent Specifies the new page content to use. #
1436 # PageNewSettings Specifies the new page settings to use. #
1437 #################################################################################
1442 # Get the values passed to the subroutine.
1445 my ($passedoptions) = @_;
1449 # Get the data that was passed to the subroutine.
1451 my $page_filename = $passedoptions->{"PageFilename"};
1452 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1453 my $page_newname = $passedoptions->{"PageNewName"};
1454 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1455 my $page_newsection = $passedoptions->{"PageNewSection"};
1456 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1457 my $page_newcontent = $passedoptions->{"PageNewContent"};
1458 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1460 # Check if the page with the filename given exists.
1462 $string_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 );
1463 $string_handle->execute();
1465 # Check if the page really does exist.
1467 while (@page_info = $string_handle->fetchrow_array()){
1469 # The page information is found.
1475 # Check if the page really does exist.
1479 $error = "PageDoesNotExist";
1484 # Get the current date.
1486 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1487 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1489 # Edit the selected page.
1491 $string_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 );
1492 $string_handle->execute();
1497 #################################################################################
1498 # movepage: Moves a page from the old database to the new database. #
1502 # $dbmodule->movepage(options); #
1504 # options Specifies the following options in any order. #
1506 # PageFilename Specifies the page with the filename to move. #
1507 #################################################################################
1512 # Get the values passed to the subroutine.
1515 my ($passedoptions) = @_;
1517 my (%database_page, $database_page);
1518 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1522 # Get the data that was passed to the subroutine.
1524 my $page_filename = $passedoptions->{"PageFilename"};
1526 # Check if the page with the filename given exists.
1528 $string_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 );
1529 $string_handle->execute();
1531 # Check if the page really does exist.
1533 while (@page_info = $string_handle->fetchrow_array()){
1535 # Get the values from the array.
1537 $pagefilename = $page_info[0];
1538 $pagename = $page_info[1];
1539 $pagedescription = $page_info[2];
1540 $pagesection = $page_info[3];
1541 $pagetemplate = $page_info[4];
1542 $pagedata = $page_info[5];
1543 $pagesettings = $page_info[6];
1544 $pagelastmodified = $page_info[7];
1546 # Put the values into the page hash.
1549 "PageFilename" => $pagefilename,
1550 "PageName" => $pagename,
1551 "PageDescription" => $pagedescription,
1552 "PageSection" => $pagesection,
1553 "PageTemplate" => $pagetemplate,
1554 "PageContent" => $pagedata,
1555 "PageSettings" => $pagesettings,
1556 "PageLastModified" => $pagelastmodified,
1559 # The page information is found.
1565 # Check if the page really does exist.
1569 $error = "PageDoesNotExist";
1574 # Check if the page with the filename given already exists in
1575 # the database the page is being moved to.
1580 $second_string_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 );
1581 $second_string_handle->execute();
1583 while (@page_info = $second_string_handle->fetchrow_array()){
1589 # Check if the page really does exist.
1593 $error = "PageAlreadyExists";
1598 # Add the page to the new database.
1600 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1601 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1602 \'' . $class->convert($database_page{"PageName"}) . '\',
1603 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1604 \'' . $class->convert($database_page{"PageSection"}) . '\',
1605 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1606 \'' . $class->convert($database_page{"PageContent"}) . '\',
1607 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1608 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1609 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1610 $second_string_handle->execute();
1612 # Delete the page from the old database.
1614 $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1615 $string_handle->execute();
1620 #################################################################################
1621 # copypage: Copies a page from the old database to the new database. #
1625 # $dbmodule->copypage(options); #
1627 # options Specifies the following options in any order. #
1629 # PageFilename Specifies the page with the filename to copy. #
1630 #################################################################################
1635 # Get the values passed to the subroutine.
1638 my ($passedoptions) = @_;
1640 my (%database_page, $database_page);
1641 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1645 # Get the data that was passed to the subroutine.
1647 my $page_filename = $passedoptions->{"PageFilename"};
1649 # Check if the page with the filename given exists.
1651 $string_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 );
1652 $string_handle->execute();
1654 # Check if the page really does exist.
1656 while (@page_info = $string_handle->fetchrow_array()){
1658 # Get the values from the array.
1660 $pagefilename = $page_info[0];
1661 $pagename = $page_info[1];
1662 $pagedescription = $page_info[2];
1663 $pagesection = $page_info[3];
1664 $pagetemplate = $page_info[4];
1665 $pagedata = $page_info[5];
1666 $pagesettings = $page_info[6];
1667 $pagelastmodified = $page_info[7];
1669 # Put the values into the page hash.
1672 "PageFilename" => $pagefilename,
1673 "PageName" => $pagename,
1674 "PageDescription" => $pagedescription,
1675 "PageSection" => $pagesection,
1676 "PageTemplate" => $pagetemplate,
1677 "PageContent" => $pagedata,
1678 "PageSettings" => $pagesettings,
1679 "PageLastModified" => $pagelastmodified,
1682 # The page information is found.
1688 # Check if the page really does exist.
1692 $error = "PageDoesNotExist";
1697 # Check if the page with the filename given already exists in
1698 # the database the page is being moved to.
1703 $second_string_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 );
1704 $second_string_handle->execute();
1706 while (@page_info = $second_string_handle->fetchrow_array()){
1712 # Check if the page really does exist.
1716 $error = "PageAlreadyExists";
1721 # Add the page to the new database.
1723 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1724 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1725 \'' . $class->convert($database_page{"PageName"}) . '\',
1726 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1727 \'' . $class->convert($database_page{"PageSection"}) . '\',
1728 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1729 \'' . $class->convert($database_page{"PageContent"}) . '\',
1730 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1731 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1732 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1733 $second_string_handle->execute();
1737 #################################################################################
1738 # Filter subroutines. #
1739 #################################################################################
1742 #################################################################################
1743 # connectfilter: Connect to the filter database. #
1747 # $dbmodule->connectfilter(missingignore); #
1749 # missingignore Ignore error about database being missing. #
1750 #################################################################################
1756 my $ignoremissing = shift;
1758 # Check if the template database exists.
1760 my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1762 if ($filterdatabase_exists eq 1){
1764 $filterdb_exists = 0;
1766 if (!$ignoremissing){
1768 $error = "FilterDatabaseDoesNotExist";
1775 # Check if the permission settings for the template database are valid.
1777 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1779 if ($filterdb_permissions eq 1){
1781 # The template database has invalid permissions set
1782 # so return an error value.
1784 if (!$ignoremissing){
1786 $error = "FilterDatabaseInvalidPermissionsSet";
1793 # Connect to the template database.
1795 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1796 $database_handle->{unicode} = 1;
1797 $filterdb_loaded = 1;
1801 sub disconnectfilter{
1802 #################################################################################
1803 # disconnectfilter: Disconnect from the filter database. #
1807 # $dbmodule->disconnectfilter(); #
1808 #################################################################################
1810 # Disconnect the template database.
1812 if ($filterdb_loaded eq 1){
1814 undef $filterdb_string_handle;
1815 $filterdb_database_handle->disconnect();
1822 #################################################################################
1823 # getfilterlist: Gets the list of filters in the filter database. #
1827 # $dbmodule->getfilterlist(); #
1828 #################################################################################
1836 # Get the list of filters available.
1838 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1839 $filterdb_string_handle->execute();
1841 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1843 # Add the filter to the list of available filters.
1845 push(@filter_list, $filter_data[0]);
1849 undef $filterdb_string_handle;
1850 return @filter_list;
1855 #################################################################################
1856 # getfilterinfo: Gets information about the filter. #
1860 # $dbmodule->getfilterinfo(options); #
1862 # options Specifies the following options in any order. #
1864 # FilterID Specifies the filter ID number to get information from. #
1865 #################################################################################
1870 # Get the values passed to the subroutine.
1873 my ($passedoptions) = @_;
1876 my $filter_exists = 0;
1879 # Get the values that are in the hash.
1881 my $filter_id = $passedoptions->{"FilterID"};
1883 $filterdb_string_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 );
1884 $filterdb_string_handle->execute();
1886 # Get the filter information.
1888 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1890 $filter_info{"FilterID"} = $filter_data[0];
1891 $filter_info{"FilterPriority"} = $filter_data[1];
1892 $filter_info{"FilterFind"} = $filter_data[2];
1893 $filter_info{"FilterReplace"} = $filter_data[3];
1894 $filter_info{"FilterNotes"} = $filter_data[4];
1900 # Check if the filter exists.
1902 if (!$filter_exists){
1904 # The filter does not exist so return
1907 $error = "FilterDoesNotExist";
1912 # Return the filter information.
1914 undef $filterdb_string_handle;
1915 return %filter_info;
1920 #################################################################################
1921 # addfilter: Adds a filter to the filter database. #
1925 # $dbmodule->addfilter(options); #
1927 # options Specifies the following options in any order. #
1929 # FindFilter Specifies the find filter to add. #
1930 # ReplaceFilter Specifies the replace filter to add. #
1931 # Priority Specifies the filter priority to use. #
1932 # Notes Specifies the notes to use. #
1933 #################################################################################
1938 # Get the values passed to the subroutine.
1941 my ($passedoptions) = @_;
1943 # Define some variables for later.
1945 my @database_filters;
1948 my $nofiltertable = 0;
1949 my $filter_found = 0;
1950 my $filter_count = 0;
1954 # Get the values from the hash.
1956 my $filter_find = $passedoptions->{"FindFilter"};
1957 my $filter_replace = $passedoptions->{"ReplaceFilter"};
1958 my $filter_priority = $passedoptions->{"Priority"};
1959 my $filter_notes = $passedoptions->{"Notes"};
1961 # Check if the filter database permissions are valid.
1963 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
1964 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
1966 if ($filterdb_permissions eq 1){
1968 if ($filterdb_exists eq 0){
1969 $error = "FilterDatabaseInvalidPermissionsSet";
1975 # Check if certain values are undefined and if they
1976 # are then set them blank.
1984 if (!$filter_replace){
1986 $filter_replace = "";
1990 if (!$filter_priority){
1992 $filter_priority = 1;
1996 if (!$filter_notes){
2002 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2004 if ($directory_permissions eq 1 && $filterdb_exists){
2006 # The template database cannot be created because of invalid directory
2007 # permissions so return an error value.
2009 $error = "FilterDatabaseFileUncreateable";
2014 # Check if the filter table exists.
2016 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2018 # Check if there is really no filter table.
2020 if ($nofiltertable){
2022 # Create the filter database table.
2024 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2025 id int(7) primary key,
2027 findsetting varchar(1024),
2028 replacesetting varchar(1024),
2030 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2031 $filterdb_string_handle->execute();
2035 # Find the lowest filter identification number available.
2037 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2038 $filterdb_string_handle->execute();
2040 while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2042 $filter_id = $database_filters[0];
2044 # Add the filter identification to the list of filter IDs.
2046 push(@filterid_list, $filter_id);
2052 # Process each filter looking for a blank available filter.
2054 foreach $filter_id (@filterid_list){
2056 # Check the next filter ID to see if it's blank.
2058 $new_id = $filter_id + 1;
2060 $filterdb_string_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 );
2061 $filterdb_string_handle->execute();
2063 # Get the filter identification number.
2065 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2071 # Check if a filter was found.
2073 if (!$filter_found){
2075 # No filter was found using this ID so exit the loop.
2081 # Increment the filter count and reset the filter found value.
2089 # Check if there were any filters in the filters database.
2091 if (!$filter_count && !$new_id){
2093 # There were no filters in the filters database so set
2094 # the new filter identification value to 1.
2100 # Add the filter to the filter database.
2102 $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2103 \'' . $class->convert($new_id) . '\',
2104 \'' . $class->convert($filter_priority) . '\',
2105 \'' . $class->convert($filter_find) . '\',
2106 \'' . $class->convert($filter_replace) .'\',
2107 \'' . $class->convert($filter_notes) . '\'
2108 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2109 $filterdb_string_handle->execute();
2114 #################################################################################
2115 # editfilter: Edits a filter in the filter database. #
2119 # $dbmodule->editfilter(options); #
2121 # options Specifies the following options in any order. #
2123 # FilterID Specifies the filter to edit. #
2124 # NewFindFilter Specifies the new find filter setting. #
2125 # NewReplaceFilter Specifies the new replace filter setting. #
2126 # NewFilterPriority Specifies the new filter priority setting. #
2127 # NewFilterNotes Specifies the new notes for the filter. #
2128 #################################################################################
2133 # Get the values passed to the subroutine.
2136 my ($passedoptions) = @_;
2139 my $filter_exists = 1;
2142 # Get the values from the hash.
2144 my $filter_id = $passedoptions->{"FilterID"};
2145 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2146 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2147 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2148 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2150 # Check if the filter database permissions are valid.
2152 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2153 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2155 if ($filterdb_permissions eq 1){
2157 if ($filterdb_exists eq 0){
2158 $error = "FilterDatabaseInvalidPermissionsSet";
2164 # Check if the filter exists before editing it.
2166 $filterdb_string_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 );
2167 $filterdb_string_handle->execute();
2169 # Check if the filter exists.
2171 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2177 # Check if the filter really does exist.
2179 if (!$filter_exists){
2181 # The filter does not exist so return
2184 $error = "FilterDoesNotExist";
2189 # Edit the selected filter.
2191 $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2192 findsetting = \'' . $class->convert($filter_newfind) . '\',
2193 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2194 priority = \'' . $class->convert($filter_newpriority) . '\',
2195 notes = \'' . $class->convert($filter_newnotes) . '\'
2196 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2197 $filterdb_string_handle->execute();
2199 undef $filterdb_string_handle;
2205 #################################################################################
2206 # deletefilter: Deletes a filter from the filter database. #
2210 # $dbmodule->deletefilter(options); #
2212 # options Specifies the following options in any order. #
2214 # FilterID Specifies the filter to delete from the filter database. #
2215 #################################################################################
2220 # Get the values passed to the subroutine.
2223 my ($passedoptions) = @_;
2225 my $filter_exists = 0;
2228 # Get the values from the hash.
2230 my $filter_id = $passedoptions->{"FilterID"};
2232 # Check if the filter exists before deleting.
2234 $filterdb_string_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 );
2235 $filterdb_string_handle->execute();
2237 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2243 # Check to see if the filter really does exist.
2245 if (!$filter_exists){
2247 $error = "FilterDoesNotExist";
2252 # Delete the filter from the filter database.
2254 $filterdb_string_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2255 $filterdb_string_handle->execute();
2257 undef $filterdb_string_handle;
2261 #################################################################################
2262 # Template subroutines. #
2263 #################################################################################
2265 sub connecttemplate{
2266 #################################################################################
2267 # connecttemplate: Connect to the template database. #
2271 # $dbmodule->connecttemplate(missingignore); #
2273 # missingignore Ignore errror about database being missing. #
2274 #################################################################################
2280 my $ignoremissing = shift;
2282 # Check if the template database exists.
2284 my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2286 if ($templatedatabase_exists eq 1){
2288 $templatedb_exists = 0;
2290 if (!$ignoremissing){
2292 $error = "TemplateDatabaseDoesNotExist";
2299 # Check if the permission settings for the template database are valid.
2301 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2303 if ($templatedb_permissions eq 1){
2305 # The template database has invalid permissions set
2306 # so return an error value.
2308 if (!$ignoremissing){
2310 $error = "TemplateDatabaseInvalidPermissionsSet";
2317 # Connect to the template database.
2319 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2320 $database_handle->{unicode} = 1;
2321 $templatedb_loaded = 1;
2325 sub disconnecttemplate{
2326 #################################################################################
2327 # disconnecttemplate: Disconnect from the template database. #
2331 # $dbmodule->disconnecttemplate(); #
2332 #################################################################################
2334 # Disconnect the template database.
2336 if ($templatedb_loaded eq 1){
2338 undef $template_string_handle;
2339 $template_database_handle->disconnect();
2345 sub gettemplatelist{
2346 #################################################################################
2347 # gettemplatelist: Gets the list of templates. #
2351 # $dbmodule->gettemplatelist(); #
2352 #################################################################################
2357 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2358 $template_string_handle->execute();
2360 my @database_template;
2362 my $template_filename;
2364 while (@database_template = $template_string_handle->fetchrow_array()){
2366 # Get certain values from the array.
2368 $template_filename = $database_template[0];
2370 # Add the template to the list of templates.
2372 push(@templates_list, $template_filename);
2376 return @templates_list;
2380 sub gettemplateinfo{
2381 #################################################################################
2382 # gettemplateinfo: Get information on a template. #
2386 # $dbmodule->gettemplateinfo(options); #
2388 # options Specifies the following options in any order. #
2390 # TemplateFilename Specifies the template filename to use. #
2391 #################################################################################
2396 # Get the data passed to the subroutine.
2399 my ($passedoptions) = @_;
2404 my $template_filename;
2406 my $template_description;
2407 my $template_datemodified;
2408 my $template_layout;
2410 my $template_found = 0;
2412 my $filename = $passedoptions->{"TemplateFilename"};
2414 $template_string_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 );
2415 $template_string_handle->execute();
2417 while (@template_data = $template_string_handle->fetchrow_array()){
2419 # Get certain values from the array.
2421 $template_filename = $template_data[0];
2422 $template_name = $template_data[1];
2423 $template_description = $template_data[2];
2424 $template_layout = $template_data[3];
2425 $template_datemodified = $template_data[4];
2427 # Process them into the hash.
2430 "TemplateFilename" => $template_filename,
2431 "TemplateName" => $template_name,
2432 "TemplateDescription" => $template_description,
2433 "TemplateLayout" => $template_layout,
2434 "TemplateLastModified" => $template_datemodified
2437 $template_found = 1;
2441 if ($template_found eq 0){
2443 # The template was not found in the template database so
2444 # write an error value.
2446 $error = "TemplateDoesNotExist";
2456 #################################################################################
2457 # addtemplate: Adds a template to the template database. #
2461 # $dbmodule->addtemplate(); #
2463 # options Specifies the following options in any order. #
2465 # TemplateFilename Specifies the new template filename. #
2466 # TemplateName Specifies the new template name. #
2467 # TemplateDescription Specifies the new template description. #
2468 # TemplateLayout Specifies the new template layout. #
2469 #################################################################################
2474 # Get the data passed to the subroutine.
2477 my ($passedoptions) = @_;
2480 my $notemplatetable;
2483 my $template_filename = $passedoptions->{"TemplateFilename"};
2484 my $template_name = $passedoptions->{"TemplateName"};
2485 my $template_description = $passedoptions->{"TemplateDescription"};
2486 my $template_layout = $passedoptions->{"TemplateLayout"};
2488 # Check if the template database permissions are valid.
2490 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2491 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2493 if ($templatedb_permissions eq 1){
2495 if ($templatedb_exists eq 0){
2496 $error = "TemplateDatabaseInvalidPermissionsSet";
2502 # Check if the template already exists before adding.
2504 if ($templatedb_exists eq 0){
2506 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2508 if ($blankfile eq 0){
2510 $template_string_handle->execute();
2512 while (@page_exists = $template_string_handle->fetchrow_array()){
2514 $error = "TemplatePageExists";
2523 # Get the current date.
2525 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2527 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2529 # Check if certain values are undefined and if they
2530 # are then set them blank.
2532 if (!$template_name){
2534 $template_name = "";
2538 if (!$template_description){
2540 $template_description = "";
2544 if (!$template_layout){
2546 $template_layout = "";
2550 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2552 if ($directory_permissions eq 1 && $templatedb_exists){
2554 # The template database cannot be created because of invalid directory
2555 # permissions so return an error value.
2557 $error = "TemplateDatabaseUncreateable";
2562 # Check to see if a template can be added.
2564 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2565 \'' . $class->convert($template_filename) . '\',
2566 \'' . $class->convert($template_name) . '\',
2567 \'' . $class->convert($template_description) . '\',
2568 \'' . $class->convert($template_layout) . '\',
2569 \'' . $class->convert($template_date) . '\'
2570 )') or ( $notemplatetable = 1 );
2572 if (!$notemplatetable){
2574 $template_string_handle->execute();
2578 # Check to see if there is no template table and attempt to create one.
2580 if ($notemplatetable){
2582 # Create a template table.
2584 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2586 if ($directory_permissions eq 1){
2588 # The template database cannot be created because of invalid directory
2589 # permissions so return an error.
2591 $error = "TemplateDatabaseFileUncreateable";
2596 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2597 filename varchar(256) primary key,
2598 templatename varchar(512),
2599 templatedescription varchar(512),
2600 templatelayout text,
2601 datemodified datetime
2602 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2603 $template_string_handle->execute();
2605 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2606 \'' . $class->convert($template_filename) . '\',
2607 \'' . $class->convert($template_name) . '\',
2608 \'' . $class->convert($template_description) . '\',
2609 \'' . $class->convert($template_layout) . '\',
2610 \'' . $class->convert($template_date) . '\'
2611 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2612 $template_string_handle->execute();
2619 #################################################################################
2620 # deletetemplate: Deletes a template from the template database. #
2624 # $dbmodule->deletetemplate(options); #
2626 # options Specifies the following options in any order. #
2628 # TemplateFilename Specifies the template filename to delete. #
2629 #################################################################################
2634 # Get the data passed to the subroutine.
2637 my ($passedoptions) = @_;
2640 my $template_filename = $passedoptions->{"TemplateFilename"};
2641 my $template_count = 0;
2643 # Check if the template exists.
2645 $template_string_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 );
2646 $template_string_handle->execute();
2648 while (@pagedata = $template_string_handle->fetchrow_array()){
2654 if ($template_count eq 0){
2656 # No pages were returned so return an error value.
2658 $error = "TemplateDoesNotExist";
2663 # Delete the template from the template database.
2665 $template_string_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2666 $template_string_handle->execute();
2671 #################################################################################
2672 # editttemplate: Edits a Kiriwrite template. #
2676 # $dbmodule->edittemplate(options); #
2678 # options Specifies the following options in any order. #
2680 # TemplateFilename Specifies the template filename to edit. #
2681 # NewTemplateFilename Specifies the new template filename. #
2682 # NewTemplateName Specifies the new template name. #
2683 # NewTemplateDescription Specifies the new template description. #
2684 # NewTemplateLayout Specifies the new template layout. #
2685 #################################################################################
2687 # Get the values passed.
2690 my ($passedoptions) = @_;
2691 my $template_found = 0;
2694 # Process the values passed.
2696 my $template_filename = $passedoptions->{"TemplateFilename"};
2697 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2698 my $new_template_name = $passedoptions->{"NewTemplateName"};
2699 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2700 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2702 # Check if the template database permissions are valid.
2704 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2705 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2707 if ($templatedb_permissions eq 1){
2709 if ($templatedb_exists eq 0){
2710 $error = "TemplateDatabaseInvalidPermissionsSet";
2716 # Check if the template exists.
2718 $template_string_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 );
2719 $template_string_handle->execute();
2721 while (@template_info = $template_string_handle->fetchrow_array()){
2723 $template_found = 1;
2727 # Check to see if the template was found and set an error value if
2730 if ($template_found eq 0){
2732 $error = "TemplateDoesNotExist";
2737 # Get the date and time.
2739 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2740 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2742 # Update the template information.
2744 $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2745 filename = \'' . $class->convert($new_template_filename) . '\',
2746 templatename = \'' . $class->convert($new_template_name) . '\',
2747 templatedescription = \'' . $class->convert($new_template_description) . '\',
2748 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2749 datemodified = \'' . $class->convert($templatenewdate) . '\'
2750 WHERE filename = \'' . $class->convert($template_filename) . '\'
2751 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2752 $template_string_handle->execute();
2756 #################################################################################
2757 # General subroutines. #
2758 #################################################################################
2761 #################################################################################
2762 # connect: Connect to the server. #
2766 # $dbmodule->connect(); #
2767 #################################################################################
2769 # This function is not needed in this database module.
2774 #################################################################################
2775 # connect: Disconnect from the server. #
2779 # $dbmodule->disconnect(); #
2780 #################################################################################
2782 # This function is not needed in this database module.
2784 undef $string_handle;