lib/PurpleWiki/Page.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 174 CC 21 SCAR 32.1
CC
21
Cov
0%
GetPageOrEditLink
72.0
FreeToNormal
30.0
exists
12.0
CRAP 24.9
1 # PurpleWiki::Page.pm
2 # vi:ai:sw=4:ts=4:et:sm
3 #
4 # $Id: Page.pm 444 2004-08-05 08:14:37Z eekim $
5 #
6 # Copyright (c) Blue Oxen Associates 2002-2003. All rights reserved.
7 #
8 # This file is part of PurpleWiki. PurpleWiki is derived from:
9 #
10 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
11 # AtisWiki v0.3 (c) Markus Denker 1998
12 # CVWiki CVS-patches (c) Peter Merel 1997
13 # The Original WikiWikiWeb (c) Ward Cunningham
14 #
15 # PurpleWiki is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the
27 # Free Software Foundation, Inc.
28 # 59 Temple Place, Suite 330
29 # Boston, MA 02111-1307 USA
30  
31 package PurpleWiki::Page;
32  
33 use PurpleWiki::Config;
34 use PurpleWiki::Database::Page;
35  
36 # mappings between PurpleWiki code and code within useMod
37  
38 # $Id: Page.pm 444 2004-08-05 08:14:37Z eekim $
39  
40 our $MainPage;
41 our $VERSION;
42 $VERSION = sprintf("%d", q$Id: Page.pm 444 2004-08-05 08:14:37Z eekim $ =~ /\s(\d+)\s/);
43  
44 sub exists {
45 my $id = shift;
46 my $config = PurpleWiki::Config->instance();
47  
48 $id =~ s|^/|$MainPage/| if defined($MainPage);
49 if ($config->FreeLinks) {
50 $id = FreeToNormal($id, $config);
51 }
52 my $page = new PurpleWiki::Database::Page('id' => $id);
53 return $page->pageExists();
54 }
55  
56 sub siteExists {
57 my $site = shift;
58 my $config = PurpleWiki::Config->instance();
59 my $status;
60 my $data;
61  
62 ($status, $data) = PurpleWiki::Database::ReadFile($config->InterFile);
63 return undef if (!$status);
64 my %interSite = split(/\s+/, $data);
65 return $interSite{$site};
66 }
67  
68 sub getWikiWordLink {
69 my $id = shift;
70  
71 my $results;
72 $results = GetPageOrEditLink($id, '');
73 return _makeURL($results);
74 }
75  
76 sub getInterWikiLink {
77 my $id = shift;
78
79 my $results;
80 $results = (InterPageLink($id, $config))[0];
81 return $results ? _makeURL($results) : '';
82 }
83  
84 sub getFreeLink {
85 my $id = shift;
86  
87 my $results;
88 $results = (GetPageOrEditLink($id, ''))[0];
89 return _makeURL($results);
90 }
91  
92 sub _makeURL {
93 my $string = shift;
94 return ($string =~ /\"([^\"]+)\"/)[0];
95 }
96  
97 # FIXME: this is hackery
98 sub GetPageOrEditLink {
99 my ($id, $name) = @_;
100 my (@temp);
101 my $config = PurpleWiki::Config->instance();
102  
103 if ($name eq "") {
104 $name = $id;
105 if ($config->FreeLinks) {
106 $name =~ s/_/ /g;
107 }
108 }
109 # FIXME: this is not right. There are times when
110 # the / is there but MainPage is not set.
111 $id =~ s|^/|$MainPage/| if defined($MainPage);
112 if ($config->FreeLinks) {
113 $id = FreeToNormal($id);
114 }
115 my $page = new PurpleWiki::Database::Page('id' => $id);
116 if ($page->pageExists()) { # Page file exists
117 return GetPageLinkText($id, $name);
118 }
119 if ($config->FreeLinks) {
120 if ($name =~ m| |) { # Not a single word
121 $name = "[$name]"; # Add brackets so boundaries are obvious
122 }
123 }
124 return $name . GetEditLink($id, "?");
125 }
126  
127 sub FreeToNormal {
128 my $id = shift;
129 my $config = PurpleWiki::Config->instance();
130  
131 $id =~ s/ /_/g;
132 $id =~ s/[\r\n]/_/g;
133 $id = ucfirst($id);
134 if (index($id, '_') > -1) { # Quick check for any space/underscores
135 $id =~ s/__+/_/g;
136 $id =~ s/^_//;
137 $id =~ s/_$//;
138 if ($config->UseSubpage) {
139 $id =~ s|_/|/|g;
140 $id =~ s|/_|/|g;
141 }
142 }
143 if ($config->FreeUpper) {
144 # Note that letters after ' are *not* capitalized
145 if ($id =~ m|[-_.,\(\)/][a-z]|) { # Quick check for non-canonical case
146 $id =~ s|([-_.,\(\)/])([a-z])|$1 . uc($2)|ge;
147 }
148 }
149 return $id;
150 }
151  
152 sub GetPageLinkText {
153 my ($id, $name) = @_;
154 my $config = PurpleWiki::Config->instance();
155  
156 # FIXME: this is not right. There are times when
157 # the / is there but MainPage is not set.
158 $id =~ s|^/|$MainPage/| if defined($MainPage);
159 if ($config->FreeLinks) {
160 $id = FreeToNormal($id);
161 $name =~ s/_/ /g;
162 }
163 return ScriptLink($id, $name);
164 }
165  
166 sub ScriptLink {
167 my ($action, $text) = @_;
168 my $config = PurpleWiki::Config->instance();
169  
170 my $scriptName = $config->ScriptName;
171 return "<a href=\"$scriptName?$action\">$text</a>";
172 }
173  
174 sub GetEditLink {
175 my ($id, $name) = @_;
176 my $config = PurpleWiki::Config->instance();
177  
178 if ($config->FreeLinks) {
179 $id = FreeToNormal($id);
180 $name =~ s/_/ /g;
181 }
182 return ScriptLink("action=edit&amp;id=$id", $name);
183 }
184  
185 sub InterPageLink {
186 my ($id) = @_;
187 my ($name, $site, $remotePage, $url, $punct);
188  
189 ($id, $punct) = SplitUrlPunct($id);
190  
191 $name = $id;
192 ($site, $remotePage) = split(/:/, $id, 2);
193 $url = siteExists($site);
194 return ("", $id . $punct) if ($url eq "");
195 $remotePage =~ s/&amp;/&/g; # Unquote common URL HTML
196 $url .= $remotePage;
197 return ("<a href=\"$url\">$name</a>", $punct);
198 }
199  
200 sub SplitUrlPunct {
201 my ($url) = @_;
202 my ($punct);
203  
204 if ($url =~ s/\"\"$//) {
205 return ($url, ""); # Delete double-quote delimiters here
206 }
207 $punct = "";
208 ($punct) = ($url =~ /([^a-zA-Z0-9\/\xc0-\xff]+)$/);
209 $url =~ s/([^a-zA-Z0-9\/\xc0-\xff]+)$//;
210 return ($url, $punct);
211 }
212  
213 1;