1 #################################################################################
2 # Kiriwrite Output Module - Normal Output Module. #
3 # Outputs the pages exactly as previous versions of Kiriwrite (before 0.5.0). #
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 Modules::Output::Normal;
26 # Enable strict and use warnings.
30 use Encode qw(decode_utf8);
32 use Modules::System::Common;
34 # Set the following values.
36 our $VERSION = "0.5.0";
39 my $error_message = "";
40 my $language_name = "";
43 tie(%pages, "Tie::IxHash");
46 #################################################################################
47 # new: Create an instance of Modules::Output::Normal #
51 # $dbmodule = Modules::Output::Normal->new(); #
52 #################################################################################
54 # Get the perl module name.
59 return bless($self, $class);
64 #################################################################################
65 # initialise: Initialises the output module. #
69 # $outputmodule->initialise(); #
70 #################################################################################
75 #################################################################################
76 # loadsettings: Loads some settings for the output module. #
80 # $outputmodule->loadsettings(language, optionshash); #
82 # language Specifies the language to use. #
83 # datetime Specifies the date and time format. #
84 # optionshash Specifies the hash that contains the options for the module. #
85 #################################################################################
88 my $passed_lang = shift;
92 $language_name = $passed_lang;
94 # Check for any blank variables.
96 $optionshash{outputmodule_seperatedirdatabase} = "off" if !$optionshash{outputmodule_seperatedirdatabase};
101 #################################################################################
102 # getoptions: Gets the options that will be used. #
106 # %options = $outputmodule->getoptions(); #
107 #################################################################################
109 my (%options, $options);
110 tie(%options, "Tie::IxHash");
112 $options{seperatedirdatabase}{type} = "checkbox";
113 $options{seperatedirdatabase}{string} = languagestrings("seperatedirdatabase");
119 #################################################################################
120 # addpage: Adds a page. #
124 # $outputmodule->addpage({ Page => "index.html", Data => $pagedata, #
125 # Title => $title, Section => $section, #
126 # LastModified => $lastmodified, #
127 # Database => $database }); #
129 # Page Specifies the page name to use. #
130 # Data Specifies the data of the page. #
131 # Title Specifies the title of the page. #
132 # Section Specifies the section of the page. #
133 # LastModified Specifies when the page was last modified. #
134 # Database Specifies the name of the database it came from. #
135 #################################################################################
137 # Not needed for this module.
144 #################################################################################
145 # outputpage: Outputs a page. #
149 # $outputmodule->outputpage({ Page => "index.html", Data => $pagedata, #
150 # Title => $title, Section => $section, #
151 # LastModified => $lastmodified, #
152 # Database => $database }); #
154 # Page Specifies the page name to use. #
155 # Data Specifies the data of the page. #
156 # Title Specifies the title of the page. #
157 # Section Specifies the section of the page. #
158 # LastModified Specifies when the page was last modified. #
159 # Database Specifies the name of the database it came from. #
160 #################################################################################
165 my $page_filename = $options->{"Page"};
166 my $page_data = $options->{"Data"};
167 my $page_database = $options->{"Database"};
169 my $page_filename_length = 0;
170 my $page_filename_seek = 0;
171 my $page_filename_dircount = 0;
172 my $page_filename_permissions = 0;
173 my $page_filename_directory = "";
174 my $page_filename_char = "";
175 my $page_filename_exists = "";
176 my $page_directory_name = "";
177 my $page_directory_path = "";
181 # Check if the use seperate directory option is enabled and add the
182 # database name to the start of the filename.
184 if ($optionshash{outputmodule_seperatedirdatabase} eq "on"){
185 ($page_database) = $page_database =~ m/^(.*)$/g;
186 $page_filename = $page_database . "/" . $page_filename;
189 # Process the page filename and check what directories
190 # need to be created.
192 $page_filename_length = int(length($page_filename));
196 $page_filename_char = substr($page_filename, $page_filename_seek, 1);
198 # Check if a forward slash appears and add it to
199 # the list of directories array.
201 if ($page_filename_char eq '/'){
203 # Append the directory name to the list of
206 $pagedirectories[$page_filename_dircount] = $page_filename_directory;
207 $page_filename_directory = "";
208 $page_filename_char = "";
209 $page_filename_dircount++;
213 # Append the character to the directory/filename.
215 $page_filename_directory = $page_filename_directory . $page_filename_char;
219 $page_filename_seek++;
221 } until ($page_filename_length eq $page_filename_seek);
223 foreach $page_directory_name (@pagedirectories){
225 # Check if the directory name is undefined and if it
226 # is then set it blank.
228 if (!$page_directory_name){
229 $page_directory_name = "";
232 if (!$page_directory_path){
233 $page_directory_path = "";
236 # Check if the directory exists and create
237 # the directory if it doesn't exist.
239 $page_directory_path = $page_directory_path . '/' . $page_directory_name;
241 mkdir($main::kiriwrite_config{"directory_data_output"} . '/' . $page_directory_path);
245 # Check if the file already exists and if it does then check
246 # the permissions of the file and return an error if the
247 # permissions set are invalid.
249 $page_filename_exists = kiriwrite_fileexists($main::kiriwrite_config{"directory_data_output"} . '/' . $page_filename);
251 if ($page_filename_exists eq 0){
253 # The page filename exists, so check if the permissions given are
256 $page_filename_permissions = kiriwrite_filepermissions($main::kiriwrite_config{"directory_data_output"} . '/' . $page_filename, 1, 1);
258 if ($page_filename_permissions eq 1){
260 # The file has invalid permissions set.
263 $error_message = languagestrings("invalidpermissionset");
270 # Write the file to the output directory.
272 ($page_filename) = $page_filename =~ m/^(.*)$/g;
273 ($main::kiriwrite_config{"directory_data_output"}) = $main::kiriwrite_config{"directory_data_output"} =~ m/^(.*)$/g;
275 open($filehandle_page, "> ", $main::kiriwrite_config{"directory_data_output"} . '/' . $page_filename) or ($error_flag = 1, $error_message = $!, return);
277 binmode $filehandle_page, ':utf8';
278 print $filehandle_page $page_data;
279 close($filehandle_page);
292 #################################################################################
293 # outputall: Outputs all pages. #
297 # $outputmodule->outputall({ FinishedProcessing => 1 }); #
299 # FinishedProcessing Indicate that this should be the last time outputall is #
300 # going to be called. #
301 #################################################################################
303 # Not needed for this module.
310 #################################################################################
311 # clearpages: Clears all of the pages. #
315 # $outputmodule->clearpages(); #
316 #################################################################################
320 #################################################################################
321 # errorflag: Returns an error flag (if any). #
325 # $errorflag = $outputmodule->errorflag(); #
326 #################################################################################
333 #################################################################################
334 # errormessage: Returns an error message (if any). #
338 # $errormessage = $outputmodule->errormessage(); #
339 #################################################################################
341 return $error_message;
346 #################################################################################
347 # clearflag: Clears the error message flag and the error message itself. #
351 # $outputmodule->clearflag(); #
352 #################################################################################
360 #################################################################################
361 # languagestrings: Language strings that are used in this module. #
365 # $string = $outputmodule->languagestrings("langstring"); #
366 #################################################################################
368 my $langstring = shift;
371 my ($language_strings, %language_strings);
374 if ($language_name eq "en-GB" or !$language_name){
376 # Language strings for English (British) language.
378 $language_strings{seperatedirdatabase} = "Seperate directory for each database.";
379 $language_strings{invalidpermissionset} = "Invalid file permissions set.";
383 # Invalid language so use English (British) as default.
385 $language_strings{seperatedirdatabase} = "Seperate directory for each database.";
386 $language_strings{invalidpermissionset} = "Invalid file permissions set.";
390 $language_string = $language_strings{$langstring};
394 $language_string =~ s/%s/$item/;
398 return $language_string;
403 #################################################################################
404 # finish: Close anything that was open. #
408 # $outputmodule->finish(); #
409 #################################################################################