#!/usr/bin/perl # TmNetWiki version 0.4 (September 11, 2002) # Copyright (C) 2002 Adam Brate # # Based on the GPLed UseModWiki 0.92 # Copyright (C) 2000-2001 Clifford A. Adams # or # Based on the GPLed AtisWiki 0.3 (C) 1998 Markus Denker # # ...which was based on # the LGPLed CVWiki CVS-patches (C) 1997 Peter Merel # and The Original WikiWikiWeb (C) Ward Cunningham # (code reused with permission) # Email and ThinLine options by Jim Mahoney # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc. # 59 Temple Place, Suite 330 # Boston, MA 02111-1307 USA package Wiki::page; use strict; # needs subs Wiki::file: CreateDir, WriteStringToFile # Wiki::error: ReportError, ReadFileOrDie, # Wiki::translate: T # Wiki::utility: FreeToNormal # needs consts $Now, $PageDir, $OpenPageName # needs vars %Page use base 'Exporter'; use Wiki::consts; use Wiki::file; use Wiki::error; use Wiki::translate; use Wiki::utility; #if ($^V and $^V gt v5.6.0) { # our @EXPORT; #else { use vars qw(@EXPORT); #} @EXPORT = qw( OpenNewPage GetPageFile GetLockedPageFile PageIsLocked GetPageDirectory OpenPage GetPageCache SetPageCache SavePage UpdatePageVersion CreatePageDir IsRedirect ); sub OpenNewPage { my ($id) = @_; my $page = _openNewPage($id); %Page = %{$page}; return $page; } # _openNewPage doesn't change any globals sub _openNewPage { my ($id) = @_; my $page = { version => 3, revision => 0, tscreate => $Now, ts => $Now }; return $page; } sub GetPageFile { my ($id) = @_; return "$Consts->{PageDir}/" . &GetPageDirectory($id) . "/$id.db"; } sub GetLockedPageFile { my ($id) = @_; return "$Consts->{PageDir}/" . &GetPageDirectory($id) . "/$id.lck"; } # A subpage will return as locked if either it or its parent # is locked... sub PageIsLocked { my ($id) = @_; if ($id =~ m|(.*)/|) { # subpage return (-f &GetLockedPageFile($id) || -f &GetLockedPageFile($1)); } return -f &GetLockedPageFile($id); } sub GetPageDirectory { my ($id) = @_; if ($id =~ /^([a-zA-Z])/) { return uc($1); } return "other"; } sub OpenPage { my ($id) = @_; my ($fname, $data,$page); if ($OpenPageName eq $id) { $page = \%Page; return $page; } %Section = (); %Text = (); $page = &_openPage($id); $OpenPageName = $id; %Page = %{$page}; return $page; } sub _openDefaultPage { my ($id) = @_; my $page = {}; $page = &_openPage($id); &OpenDefaultText(); } # _openPage doesn't change any globals sub _openPage { my ($id) = @_; my ($fname, $data,$page); $page = {}; $fname = &GetPageFile($id); if (-f $fname) { $data = &ReadFileOrDie($fname); %{$page} = split(/$Consts->{FS1}/, $data, -1); # -1 keeps trailing null fields } else { $page = &_openNewPage($id); } if ($page->{'version'} != 3) { &UpdatePageVersion(); # only returns error } return $page; } sub GetPageCache { my ($name) = @_; return $Page{"cache_$name"}; } sub SetPageCache { my ($name, $data) = @_; $Page{"cache_$name"} = $data; } # Always call SavePage within a lock. sub SavePage { my $file = &GetPageFile($OpenPageName); $Page{'revision'} += 1; # Number of edited times $Page{'ts'} = $Now; # Updated every edit &CreatePageDir($PageDir, $OpenPageName); &WriteStringToFile($file, join($Consts->{FS1}, %Page)); } sub UpdatePageVersion { &ReportError(T('Bad page version (or corrupt page).')); } sub CreatePageDir { my ($dir, $id) = @_; my $subdir; &CreateDir($dir); # Make sure main page exists $subdir = $dir . "/" . &GetPageDirectory($id); &CreateDir($subdir); if ($id =~ m|([^/]+)/|) { # 1-level subpages... $subdir = $subdir . "/" . $1; &CreateDir($subdir); } } # this is horribly inefficient: #sub IsRedirect { # my ($id) = @_; # my ($openPage,$isRedirect); # if ($id ne $OpenPageName) { # $openPage = $OpenPageName; # &OpenPage($id); # } # $isRedirect = (substr($Text{'text'}, 0, 10) eq '#REDIRECT '); # if ($id ne $OpenPageName) { # &OpenPage($OpenPageName); # } #} # so, let's cut around # Returns the redirect text if IsRedirect, else 0. # probably should be in utility? # Will this work incorrectly if the page title is '0'? Probably. sub IsRedirect { my ($id) = @_; my (%page,%section,$data,%text,$fname); if ($id eq $OpenPageName) { if (substr($Text{'text'}, 0, 10) eq '#REDIRECT ') { if (($Consts->{FreeLinks}) && ($Text{'text'} =~ /\#REDIRECT\s+\[\[(.+)\]\]/)) { return &FreeToNormal($1); } else { $Text{'text'} =~ /\#REDIRECT\s+(\S+)/; return $1; } } return 0; } # if OpenPage, OpenText didn't mess w/globals...arg. $fname = &GetPageFile($id); if (-f $fname) { $data = &ReadFileOrDie($fname); %page = split(/$Consts->{FS1}/, $data, -1); # -1 keeps trailing null fields %section = split(/$Consts->{FS2}/, $page{'text_default'}, -1); %text = split(/$Consts->{FS3}/, $section{'data'}, -1); if (substr($text{'text'}, 0, 10) eq '#REDIRECT ') { if (($Consts->{FreeLinks}) && ($text{'text'} =~ /\#REDIRECT\s+\[\[(.+)\]\]/)) { return &FreeToNormal($1); } else { $text{'text'} =~ /\#REDIRECT\s+(\S+)/; return $1; } } return 0; } # page doesn't exist return 0; } 1;