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_string_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_string_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_string_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$//g;
609 $database_filename_friendly = $database;
611 #$database_filename_length = length($database);
612 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
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 $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 (
642 $error = "DatabaseError", $errorext = $database_handle->errstr, return
644 $string_handle->execute();
646 @sqldata = $string_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 $string_handle->finish();
661 undef $string_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_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 (
687 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
689 $second_string_handle->execute();
691 @sqldata = $second_string_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_string_handle->finish();
706 undef $second_string_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 $string_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 $string_handle->execute();
777 $string_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 $string_handle->execute();
789 # Convert the values into SQL query formatted values and add an entry
790 # to the kiriwrite_database_info table.
792 $string_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 $string_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 $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933 $string_handle->execute();
935 my @database_oldinfo = $string_handle->fetchrow_array();
937 my $dboldname = $database_oldinfo[0];
939 # Update the database information.
941 $string_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 $string_handle->execute();
947 undef $string_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 $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1156 $string_handle->execute();
1158 my @database_pagefilenames;
1159 my @database_pagefilenames_final;
1161 # Process the collected pages.
1163 while (@database_pagefilenames = $string_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 $string_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 #################################################################################
1193 my ($passedoptions) = shift;
1194 my (%database_page, $database_page);
1195 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1200 # Get the page from the database.
1202 my $page_filename = $passedoptions->{"PageFilename"};
1204 $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 );
1205 $string_handle->execute();
1207 # Check if the page exists in the database.
1209 while (@data_page = $string_handle->fetchrow_array()){
1211 # Get the values from the array.
1213 $pagefilename = $data_page[0];
1214 $pagename = $data_page[1];
1215 $pagedescription = $data_page[2];
1216 $pagesection = $data_page[3];
1217 $pagetemplate = $data_page[4];
1218 $pagedata = $data_page[5];
1219 $pagesettings = $data_page[6];
1220 $pagelastmodified = $data_page[7];
1222 # Put the values into the page hash.
1225 "PageFilename" => $pagefilename,
1226 "PageName" => $pagename,
1227 "PageDescription" => $pagedescription,
1228 "PageSection" => $pagesection,
1229 "PageTemplate" => $pagetemplate,
1230 "PageContent" => $pagedata,
1231 "PageSettings" => $pagesettings,
1232 "PageLastModified" => $class->dateconvert($pagelastmodified),
1239 # Check if the page did exist.
1243 $error = "PageDoesNotExist";
1248 undef $string_handle;
1249 return %database_page;
1254 #################################################################################
1255 # addpage: Add a page to the selected database. #
1259 # $dbmodule->addpage(options); #
1261 # options Specifies the following options in any order. #
1263 # PageFilename Specifies the page filename to use. #
1264 # PageName Specifies the page name to use. #
1265 # PageDescription Specifies the page description to use. #
1266 # PageSection Specifies the page section to use. #
1267 # PageTemplate Specifies the page template to use. #
1268 # PageContent Specifies the page content to use. #
1269 # PageSettings Specifies the page settings to use. #
1270 #################################################################################
1272 # Get the data that was passed to the subroutine.
1278 my ($passedoptions) = shift;
1283 # Get the values passed to the hash.
1285 my $page_filename = $passedoptions->{"PageFilename"};
1286 my $page_name = $passedoptions->{"PageName"};
1287 my $page_description = $passedoptions->{"PageDescription"};
1288 my $page_section = $passedoptions->{"PageSection"};
1289 my $page_template = $passedoptions->{"PageTemplate"};
1290 my $page_content = $passedoptions->{"PageContent"};
1291 my $page_settings = $passedoptions->{"PageSettings"};
1293 # Check to see if the filename given already exists
1294 # in the page database.
1296 $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 );
1297 $string_handle->execute();
1299 # Check if a page with the filename given really does
1302 while (@database_page = $string_handle->fetchrow_array()){
1304 # A page does exist so increment the count to 1.
1310 if ($page_count ne 0){
1312 # The page does exist so set the error value.
1314 $error = "PageExists";
1319 # Check if certain values are undefined.
1327 if (!$page_description){
1329 $page_description = "";
1333 if (!$page_section){
1339 if (!$page_content){
1345 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1346 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1348 # Add the page to the selected database.
1350 $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1351 \'' . $class->convert($page_filename) . '\',
1352 \'' . $class->convert($page_name) . '\',
1353 \'' . $class->convert($page_description) . '\',
1354 \'' . $class->convert($page_section) . '\',
1355 \'' . $class->convert($page_template) . '\',
1356 \'' . $class->convert($page_content) . '\',
1357 \'' . $class->convert($page_settings) . '\',
1358 \'' . $class->convert($page_date) . '\'
1359 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1360 $string_handle->execute();
1362 undef $string_handle;
1367 #################################################################################
1368 # deletepage: Delete a page from the selected database. #
1372 # $dbmodule->deletepage(options) #
1374 # options Specifies the following options in any order. #
1376 # PageFilename Specifies the page filename to delete. #
1377 #################################################################################
1382 # Get the data that was passed to the subroutine.
1385 my ($passedoptions) = @_;
1389 # Get the page filename.
1391 my $page_filename = $passedoptions->{"PageFilename"};
1393 # Check if the page exists before deleting it.
1395 $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 );
1396 $string_handle->execute();
1398 while (@page_info = $string_handle->fetchrow_array()){
1404 # Check if the page really does exist.
1408 $error = "PageDoesNotExist";
1415 $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1416 $string_handle->execute();
1421 #################################################################################
1422 # editpage: Edit a page from the selected database. #
1426 # $dbmodule->editpage(options); #
1428 # options Specifies the following options in any order. #
1430 # PageFilename Specifies the filename to edit. #
1431 # PageNewFilename Specifies the new filename to use. #
1432 # PageNewName Specifies the new page name to use. #
1433 # PageNewDescription Specifies the new page description to use. #
1434 # PageNewSection Specifies the new page section to use. #
1435 # PageNewTemplate Specifies the new page template to use. #
1436 # PageNewContent Specifies the new page content to use. #
1437 # PageNewSettings Specifies the new page settings to use. #
1438 #################################################################################
1443 # Get the values passed to the subroutine.
1446 my ($passedoptions) = @_;
1450 # Get the data that was passed to the subroutine.
1452 my $page_filename = $passedoptions->{"PageFilename"};
1453 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1454 my $page_newname = $passedoptions->{"PageNewName"};
1455 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1456 my $page_newsection = $passedoptions->{"PageNewSection"};
1457 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1458 my $page_newcontent = $passedoptions->{"PageNewContent"};
1459 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1461 # Check if the page with the filename given exists.
1463 $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 );
1464 $string_handle->execute();
1466 # Check if the page really does exist.
1468 while (@page_info = $string_handle->fetchrow_array()){
1470 # The page information is found.
1476 # Check if the page really does exist.
1480 $error = "PageDoesNotExist";
1485 # Check if there is a page that already exists with the new
1488 if ($page_filename ne $page_newfilename){
1490 $string_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 );
1491 $string_handle->execute();
1493 # Check if a page really is using the new filename.
1495 while (@page_info = $string_handle->fetchrow_array()){
1497 # The page information is found.
1503 if ($page_found eq 1){
1505 $error = "PageExists";
1512 # Get the current date.
1514 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1515 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1517 # Edit the selected page.
1519 $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 );
1520 $string_handle->execute();
1525 #################################################################################
1526 # movepage: Moves a page from the old database to the new database. #
1530 # $dbmodule->movepage(options); #
1532 # options Specifies the following options in any order. #
1534 # PageFilename Specifies the page with the filename to move. #
1535 #################################################################################
1540 # Get the values passed to the subroutine.
1543 my ($passedoptions) = @_;
1545 my (%database_page, $database_page);
1546 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1550 # Get the data that was passed to the subroutine.
1552 my $page_filename = $passedoptions->{"PageFilename"};
1554 # Check if the page with the filename given exists.
1556 $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 );
1557 $string_handle->execute();
1559 # Check if the page really does exist.
1561 while (@page_info = $string_handle->fetchrow_array()){
1563 # Get the values from the array.
1565 $pagefilename = $page_info[0];
1566 $pagename = $page_info[1];
1567 $pagedescription = $page_info[2];
1568 $pagesection = $page_info[3];
1569 $pagetemplate = $page_info[4];
1570 $pagedata = $page_info[5];
1571 $pagesettings = $page_info[6];
1572 $pagelastmodified = $page_info[7];
1574 # Put the values into the page hash.
1577 "PageFilename" => $pagefilename,
1578 "PageName" => $pagename,
1579 "PageDescription" => $pagedescription,
1580 "PageSection" => $pagesection,
1581 "PageTemplate" => $pagetemplate,
1582 "PageContent" => $pagedata,
1583 "PageSettings" => $pagesettings,
1584 "PageLastModified" => $pagelastmodified,
1587 # The page information is found.
1593 # Check if the page really does exist.
1597 $error = "PageDoesNotExist";
1602 # Check if the page with the filename given already exists in
1603 # the database the page is being moved to.
1608 $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 );
1609 $second_string_handle->execute();
1611 while (@page_info = $second_string_handle->fetchrow_array()){
1617 # Check if the page really does exist.
1621 $error = "PageAlreadyExists";
1626 # Add the page to the new database.
1628 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1629 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1630 \'' . $class->convert($database_page{"PageName"}) . '\',
1631 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1632 \'' . $class->convert($database_page{"PageSection"}) . '\',
1633 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1634 \'' . $class->convert($database_page{"PageContent"}) . '\',
1635 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1636 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1637 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1638 $second_string_handle->execute();
1640 # Delete the page from the old database.
1642 $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 );
1643 $string_handle->execute();
1648 #################################################################################
1649 # copypage: Copies a page from the old database to the new database. #
1653 # $dbmodule->copypage(options); #
1655 # options Specifies the following options in any order. #
1657 # PageFilename Specifies the page with the filename to copy. #
1658 #################################################################################
1663 # Get the values passed to the subroutine.
1666 my ($passedoptions) = @_;
1668 my (%database_page, $database_page);
1669 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1673 # Get the data that was passed to the subroutine.
1675 my $page_filename = $passedoptions->{"PageFilename"};
1677 # Check if the page with the filename given exists.
1679 $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 );
1680 $string_handle->execute();
1682 # Check if the page really does exist.
1684 while (@page_info = $string_handle->fetchrow_array()){
1686 # Get the values from the array.
1688 $pagefilename = $page_info[0];
1689 $pagename = $page_info[1];
1690 $pagedescription = $page_info[2];
1691 $pagesection = $page_info[3];
1692 $pagetemplate = $page_info[4];
1693 $pagedata = $page_info[5];
1694 $pagesettings = $page_info[6];
1695 $pagelastmodified = $page_info[7];
1697 # Put the values into the page hash.
1700 "PageFilename" => $pagefilename,
1701 "PageName" => $pagename,
1702 "PageDescription" => $pagedescription,
1703 "PageSection" => $pagesection,
1704 "PageTemplate" => $pagetemplate,
1705 "PageContent" => $pagedata,
1706 "PageSettings" => $pagesettings,
1707 "PageLastModified" => $pagelastmodified,
1710 # The page information is found.
1716 # Check if the page really does exist.
1720 $error = "PageDoesNotExist";
1725 # Check if the page with the filename given already exists in
1726 # the database the page is being moved to.
1731 $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 );
1732 $second_string_handle->execute();
1734 while (@page_info = $second_string_handle->fetchrow_array()){
1740 # Check if the page really does exist.
1744 $error = "PageAlreadyExists";
1749 # Add the page to the new database.
1751 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1752 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1753 \'' . $class->convert($database_page{"PageName"}) . '\',
1754 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1755 \'' . $class->convert($database_page{"PageSection"}) . '\',
1756 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1757 \'' . $class->convert($database_page{"PageContent"}) . '\',
1758 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1759 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1760 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1761 $second_string_handle->execute();
1765 #################################################################################
1766 # Filter subroutines. #
1767 #################################################################################
1770 #################################################################################
1771 # connectfilter: Connect to the filter database. #
1775 # $dbmodule->connectfilter(missingignore); #
1777 # missingignore Ignore error about database being missing. #
1778 #################################################################################
1784 my $ignoremissing = shift;
1786 # Check if the template database exists.
1788 my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1790 if ($filterdatabase_exists eq 1){
1792 $filterdb_exists = 0;
1794 if (!$ignoremissing){
1796 $error = "FilterDatabaseDoesNotExist";
1803 # Check if the permission settings for the template database are valid.
1805 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1807 if ($filterdb_permissions eq 1){
1809 # The template database has invalid permissions set
1810 # so return an error value.
1812 if (!$ignoremissing){
1814 $error = "FilterDatabaseInvalidPermissionsSet";
1821 # Connect to the template database.
1823 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1824 $filterdb_database_handle->{unicode} = 1;
1825 $filterdb_loaded = 1;
1829 sub disconnectfilter{
1830 #################################################################################
1831 # disconnectfilter: Disconnect from the filter database. #
1835 # $dbmodule->disconnectfilter(); #
1836 #################################################################################
1838 # Disconnect the template database.
1840 if ($filterdb_loaded eq 1){
1842 undef $filterdb_string_handle;
1843 $filterdb_database_handle->disconnect();
1850 #################################################################################
1851 # getfilterlist: Gets the list of filters in the filter database. #
1855 # $dbmodule->getfilterlist(); #
1856 #################################################################################
1864 # Get the list of filters available.
1866 $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 );
1867 $filterdb_string_handle->execute();
1869 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1871 # Add the filter to the list of available filters.
1873 push(@filter_list, $filter_data[0]);
1877 undef $filterdb_string_handle;
1878 return @filter_list;
1883 #################################################################################
1884 # getfilterinfo: Gets information about the filter. #
1888 # $dbmodule->getfilterinfo(options); #
1890 # options Specifies the following options in any order. #
1892 # FilterID Specifies the filter ID number to get information from. #
1893 #################################################################################
1898 # Get the values passed to the subroutine.
1901 my ($passedoptions) = @_;
1904 my $filter_exists = 0;
1907 # Get the values that are in the hash.
1909 my $filter_id = $passedoptions->{"FilterID"};
1911 $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 );
1912 $filterdb_string_handle->execute();
1914 # Get the filter information.
1916 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1918 $filter_info{"FilterID"} = $filter_data[0];
1919 $filter_info{"FilterPriority"} = $filter_data[1];
1920 $filter_info{"FilterFind"} = $filter_data[2];
1921 $filter_info{"FilterReplace"} = $filter_data[3];
1922 $filter_info{"FilterNotes"} = $filter_data[4];
1928 # Check if the filter exists.
1930 if (!$filter_exists){
1932 # The filter does not exist so return
1935 $error = "FilterDoesNotExist";
1940 # Return the filter information.
1942 undef $filterdb_string_handle;
1943 return %filter_info;
1948 #################################################################################
1949 # addfilter: Adds a filter to the filter database. #
1953 # $dbmodule->addfilter(options); #
1955 # options Specifies the following options in any order. #
1957 # FindFilter Specifies the find filter to add. #
1958 # ReplaceFilter Specifies the replace filter to add. #
1959 # Priority Specifies the filter priority to use. #
1960 # Notes Specifies the notes to use. #
1961 #################################################################################
1966 # Get the values passed to the subroutine.
1969 my ($passedoptions) = @_;
1971 # Define some variables for later.
1973 my @database_filters;
1976 my $nofiltertable = 0;
1977 my $filter_found = 0;
1978 my $filter_count = 0;
1982 # Get the values from the hash.
1984 my $filter_find = $passedoptions->{"FindFilter"};
1985 my $filter_replace = $passedoptions->{"ReplaceFilter"};
1986 my $filter_priority = $passedoptions->{"Priority"};
1987 my $filter_notes = $passedoptions->{"Notes"};
1989 # Check if the filter database permissions are valid.
1991 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
1992 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
1994 if ($filterdb_permissions eq 1){
1996 if ($filterdb_exists eq 0){
1997 $error = "FilterDatabaseInvalidPermissionsSet";
2003 # Check if certain values are undefined and if they
2004 # are then set them blank.
2012 if (!$filter_replace){
2014 $filter_replace = "";
2018 if (!$filter_priority){
2020 $filter_priority = 1;
2024 if (!$filter_notes){
2030 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2032 if ($directory_permissions eq 1 && $filterdb_exists){
2034 # The template database cannot be created because of invalid directory
2035 # permissions so return an error value.
2037 $error = "FilterDatabaseFileUncreateable";
2042 # Check if the filter table exists.
2044 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2046 # Check if there is really no filter table.
2048 if ($nofiltertable){
2050 # Create the filter database table.
2052 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2053 id int(7) primary key,
2055 findsetting varchar(1024),
2056 replacesetting varchar(1024),
2058 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2059 $filterdb_string_handle->execute();
2063 # Find the lowest filter identification number available.
2065 $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 );
2066 $filterdb_string_handle->execute();
2068 while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2070 $filter_id = $database_filters[0];
2072 # Add the filter identification to the list of filter IDs.
2074 push(@filterid_list, $filter_id);
2080 # Process each filter looking for a blank available filter.
2082 foreach $filter_id (@filterid_list){
2084 # Check the next filter ID to see if it's blank.
2086 $new_id = $filter_id + 1;
2088 $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 );
2089 $filterdb_string_handle->execute();
2091 # Get the filter identification number.
2093 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2099 # Check if a filter was found.
2101 if (!$filter_found){
2103 # No filter was found using this ID so exit the loop.
2109 # Increment the filter count and reset the filter found value.
2117 # Check if there were any filters in the filters database.
2119 if (!$filter_count && !$new_id){
2121 # There were no filters in the filters database so set
2122 # the new filter identification value to 1.
2128 # Add the filter to the filter database.
2130 $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2131 \'' . $class->convert($new_id) . '\',
2132 \'' . $class->convert($filter_priority) . '\',
2133 \'' . $class->convert($filter_find) . '\',
2134 \'' . $class->convert($filter_replace) .'\',
2135 \'' . $class->convert($filter_notes) . '\'
2136 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2137 $filterdb_string_handle->execute();
2142 #################################################################################
2143 # editfilter: Edits a filter in the filter database. #
2147 # $dbmodule->editfilter(options); #
2149 # options Specifies the following options in any order. #
2151 # FilterID Specifies the filter to edit. #
2152 # NewFindFilter Specifies the new find filter setting. #
2153 # NewReplaceFilter Specifies the new replace filter setting. #
2154 # NewFilterPriority Specifies the new filter priority setting. #
2155 # NewFilterNotes Specifies the new notes for the filter. #
2156 #################################################################################
2161 # Get the values passed to the subroutine.
2164 my ($passedoptions) = @_;
2167 my $filter_exists = 1;
2170 # Get the values from the hash.
2172 my $filter_id = $passedoptions->{"FilterID"};
2173 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2174 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2175 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2176 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2178 # Check if the filter database permissions are valid.
2180 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2181 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2183 if ($filterdb_permissions eq 1){
2185 if ($filterdb_exists eq 0){
2186 $error = "FilterDatabaseInvalidPermissionsSet";
2192 # Check if the filter exists before editing it.
2194 $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 );
2195 $filterdb_string_handle->execute();
2197 # Check if the filter exists.
2199 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2205 # Check if the filter really does exist.
2207 if (!$filter_exists){
2209 # The filter does not exist so return
2212 $error = "FilterDoesNotExist";
2217 # Edit the selected filter.
2219 $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2220 findsetting = \'' . $class->convert($filter_newfind) . '\',
2221 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2222 priority = \'' . $class->convert($filter_newpriority) . '\',
2223 notes = \'' . $class->convert($filter_newnotes) . '\'
2224 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2225 $filterdb_string_handle->execute();
2227 undef $filterdb_string_handle;
2233 #################################################################################
2234 # deletefilter: Deletes a filter from the filter database. #
2238 # $dbmodule->deletefilter(options); #
2240 # options Specifies the following options in any order. #
2242 # FilterID Specifies the filter to delete from the filter database. #
2243 #################################################################################
2248 # Get the values passed to the subroutine.
2251 my ($passedoptions) = @_;
2253 my $filter_exists = 0;
2256 # Get the values from the hash.
2258 my $filter_id = $passedoptions->{"FilterID"};
2260 # Check if the filter exists before deleting.
2262 $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 );
2263 $filterdb_string_handle->execute();
2265 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2271 # Check to see if the filter really does exist.
2273 if (!$filter_exists){
2275 $error = "FilterDoesNotExist";
2280 # Delete the filter from the filter database.
2282 $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 );
2283 $filterdb_string_handle->execute();
2285 undef $filterdb_string_handle;
2289 #################################################################################
2290 # Template subroutines. #
2291 #################################################################################
2293 sub connecttemplate{
2294 #################################################################################
2295 # connecttemplate: Connect to the template database. #
2299 # $dbmodule->connecttemplate(missingignore); #
2301 # missingignore Ignore errror about database being missing. #
2302 #################################################################################
2308 my $ignoremissing = shift;
2310 # Check if the template database exists.
2312 my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2314 if ($templatedatabase_exists eq 1){
2316 $templatedb_exists = 0;
2318 if (!$ignoremissing){
2320 $error = "TemplateDatabaseDoesNotExist";
2327 # Check if the permission settings for the template database are valid.
2329 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2331 if ($templatedb_permissions eq 1){
2333 # The template database has invalid permissions set
2334 # so return an error value.
2336 if (!$ignoremissing){
2338 $error = "TemplateDatabaseInvalidPermissionsSet";
2345 # Connect to the template database.
2347 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2348 $template_database_handle->{unicode} = 1;
2349 $templatedb_loaded = 1;
2353 sub disconnecttemplate{
2354 #################################################################################
2355 # disconnecttemplate: Disconnect from the template database. #
2359 # $dbmodule->disconnecttemplate(); #
2360 #################################################################################
2362 # Disconnect the template database.
2364 if ($templatedb_loaded eq 1){
2366 undef $template_string_handle;
2367 $template_database_handle->disconnect();
2373 sub gettemplatelist{
2374 #################################################################################
2375 # gettemplatelist: Gets the list of templates. #
2379 # $dbmodule->gettemplatelist(); #
2380 #################################################################################
2385 $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 );
2386 $template_string_handle->execute();
2388 my @database_template;
2390 my $template_filename;
2392 while (@database_template = $template_string_handle->fetchrow_array()){
2394 # Get certain values from the array.
2396 $template_filename = $database_template[0];
2398 # Add the template to the list of templates.
2400 push(@templates_list, $template_filename);
2404 return @templates_list;
2408 sub gettemplateinfo{
2409 #################################################################################
2410 # gettemplateinfo: Get information on a template. #
2414 # $dbmodule->gettemplateinfo(options); #
2416 # options Specifies the following options in any order. #
2418 # TemplateFilename Specifies the template filename to use. #
2419 #################################################################################
2424 # Get the data passed to the subroutine.
2427 my ($passedoptions) = @_;
2432 my $template_filename;
2434 my $template_description;
2435 my $template_datemodified;
2436 my $template_layout;
2438 my $template_found = 0;
2440 my $filename = $passedoptions->{"TemplateFilename"};
2442 $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 );
2443 $template_string_handle->execute();
2445 while (@template_data = $template_string_handle->fetchrow_array()){
2447 # Get certain values from the array.
2449 $template_filename = $template_data[0];
2450 $template_name = $template_data[1];
2451 $template_description = $template_data[2];
2452 $template_layout = $template_data[3];
2453 $template_datemodified = $template_data[4];
2455 # Process them into the hash.
2458 "TemplateFilename" => $template_filename,
2459 "TemplateName" => $template_name,
2460 "TemplateDescription" => $template_description,
2461 "TemplateLayout" => $template_layout,
2462 "TemplateLastModified" => $template_datemodified
2465 $template_found = 1;
2469 if ($template_found eq 0){
2471 # The template was not found in the template database so
2472 # write an error value.
2474 $error = "TemplateDoesNotExist";
2484 #################################################################################
2485 # addtemplate: Adds a template to the template database. #
2489 # $dbmodule->addtemplate(); #
2491 # options Specifies the following options in any order. #
2493 # TemplateFilename Specifies the new template filename. #
2494 # TemplateName Specifies the new template name. #
2495 # TemplateDescription Specifies the new template description. #
2496 # TemplateLayout Specifies the new template layout. #
2497 #################################################################################
2502 # Get the data passed to the subroutine.
2505 my ($passedoptions) = @_;
2508 my $notemplatetable;
2511 my $template_filename = $passedoptions->{"TemplateFilename"};
2512 my $template_name = $passedoptions->{"TemplateName"};
2513 my $template_description = $passedoptions->{"TemplateDescription"};
2514 my $template_layout = $passedoptions->{"TemplateLayout"};
2516 # Check if the template database permissions are valid.
2518 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2519 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2521 if ($templatedb_permissions eq 1){
2523 if ($templatedb_exists eq 0){
2524 $error = "TemplateDatabaseInvalidPermissionsSet";
2530 # Check if the template already exists before adding.
2532 if ($templatedb_exists eq 0){
2534 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2536 if ($blankfile eq 0){
2538 $template_string_handle->execute();
2540 while (@page_exists = $template_string_handle->fetchrow_array()){
2542 $error = "TemplatePageExists";
2551 # Get the current date.
2553 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2555 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2557 # Check if certain values are undefined and if they
2558 # are then set them blank.
2560 if (!$template_name){
2562 $template_name = "";
2566 if (!$template_description){
2568 $template_description = "";
2572 if (!$template_layout){
2574 $template_layout = "";
2578 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2580 if ($directory_permissions eq 1 && $templatedb_exists){
2582 # The template database cannot be created because of invalid directory
2583 # permissions so return an error value.
2585 $error = "TemplateDatabaseUncreateable";
2590 # Check to see if a template can be added.
2592 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2593 \'' . $class->convert($template_filename) . '\',
2594 \'' . $class->convert($template_name) . '\',
2595 \'' . $class->convert($template_description) . '\',
2596 \'' . $class->convert($template_layout) . '\',
2597 \'' . $class->convert($template_date) . '\'
2598 )') or ( $notemplatetable = 1 );
2600 if (!$notemplatetable){
2602 $template_string_handle->execute();
2606 # Check to see if there is no template table and attempt to create one.
2608 if ($notemplatetable){
2610 # Create a template table.
2612 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2614 if ($directory_permissions eq 1){
2616 # The template database cannot be created because of invalid directory
2617 # permissions so return an error.
2619 $error = "TemplateDatabaseFileUncreateable";
2624 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2625 filename varchar(256) primary key,
2626 templatename varchar(512),
2627 templatedescription varchar(512),
2628 templatelayout text,
2629 datemodified datetime
2630 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2631 $template_string_handle->execute();
2633 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2634 \'' . $class->convert($template_filename) . '\',
2635 \'' . $class->convert($template_name) . '\',
2636 \'' . $class->convert($template_description) . '\',
2637 \'' . $class->convert($template_layout) . '\',
2638 \'' . $class->convert($template_date) . '\'
2639 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2640 $template_string_handle->execute();
2647 #################################################################################
2648 # deletetemplate: Deletes a template from the template database. #
2652 # $dbmodule->deletetemplate(options); #
2654 # options Specifies the following options in any order. #
2656 # TemplateFilename Specifies the template filename to delete. #
2657 #################################################################################
2662 # Get the data passed to the subroutine.
2665 my ($passedoptions) = @_;
2668 my $template_filename = $passedoptions->{"TemplateFilename"};
2669 my $template_count = 0;
2671 # Check if the template exists.
2673 $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 );
2674 $template_string_handle->execute();
2676 while (@pagedata = $template_string_handle->fetchrow_array()){
2682 if ($template_count eq 0){
2684 # No pages were returned so return an error value.
2686 $error = "TemplateDoesNotExist";
2691 # Delete the template from the template database.
2693 $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 );
2694 $template_string_handle->execute();
2699 #################################################################################
2700 # editttemplate: Edits a Kiriwrite template. #
2704 # $dbmodule->edittemplate(options); #
2706 # options Specifies the following options in any order. #
2708 # TemplateFilename Specifies the template filename to edit. #
2709 # NewTemplateFilename Specifies the new template filename. #
2710 # NewTemplateName Specifies the new template name. #
2711 # NewTemplateDescription Specifies the new template description. #
2712 # NewTemplateLayout Specifies the new template layout. #
2713 #################################################################################
2715 # Get the values passed.
2718 my ($passedoptions) = @_;
2719 my $template_found = 0;
2722 # Process the values passed.
2724 my $template_filename = $passedoptions->{"TemplateFilename"};
2725 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2726 my $new_template_name = $passedoptions->{"NewTemplateName"};
2727 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2728 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2730 # Check if the template database permissions are valid.
2732 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2733 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2735 if ($templatedb_permissions eq 1){
2737 if ($templatedb_exists eq 0){
2738 $error = "TemplateDatabaseInvalidPermissionsSet";
2744 # Check if the template exists.
2746 $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 );
2747 $template_string_handle->execute();
2749 while (@template_info = $template_string_handle->fetchrow_array()){
2751 $template_found = 1;
2755 # Check to see if the template was found and set an error value if
2758 if ($template_found eq 0){
2760 $error = "TemplateDoesNotExist";
2765 # Get the date and time.
2767 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2768 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2770 # Update the template information.
2772 $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2773 filename = \'' . $class->convert($new_template_filename) . '\',
2774 templatename = \'' . $class->convert($new_template_name) . '\',
2775 templatedescription = \'' . $class->convert($new_template_description) . '\',
2776 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2777 datemodified = \'' . $class->convert($templatenewdate) . '\'
2778 WHERE filename = \'' . $class->convert($template_filename) . '\'
2779 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2780 $template_string_handle->execute();
2784 #################################################################################
2785 # General subroutines. #
2786 #################################################################################
2789 #################################################################################
2790 # connect: Connect to the server. #
2794 # $dbmodule->connect(); #
2795 #################################################################################
2797 # This function is not needed in this database module.
2802 #################################################################################
2803 # connect: Disconnect from the server. #
2807 # $dbmodule->disconnect(); #
2808 #################################################################################
2810 # This function is not needed in this database module.
2812 undef $string_handle;