Skip to content

Commit

Permalink
Item13897: First HTML page generated.
Browse files Browse the repository at this point in the history
Most of the core still doesn't function properly but it's kinda milestone
as the new Foswiki::App finally does something meaningful.

- expandStandardEscapes() is now a object method of Foswiki::Macros. Moved
from Foswiki.pm

- Foswiki::If::OP_*, Foswiki::Render::* modules are adapted to the new model.

- A number of macros have been adapted. Only those involved into generation
of Main.WebHome.

- Attributes attach, renderer, and zones are moved into Foswiki::App from
Foswiki.pm.

- Methods deepWebList(), inlineAlert(), setCacheControl(),
writeCompletePage() have been moved from Foswiki.pm

- Attribute urlHost and method getScriptUrl() have been moved into
Foswiki::Config. Same is planned for other getXxxUrl() methods.

- Foswiki.pm can now export urlEncode, urlDecode, make_params,
load_package.  It is planned to have all Foswiki.pm API exported for the
core and plugins code.
  • Loading branch information
vrurg committed Apr 13, 2016
1 parent 74b0669 commit aa07ddc
Show file tree
Hide file tree
Showing 56 changed files with 869 additions and 861 deletions.
565 changes: 1 addition & 564 deletions core/lib/Foswiki.pm

Large diffs are not rendered by default.

359 changes: 356 additions & 3 deletions core/lib/Foswiki/App.pm

Large diffs are not rendered by default.

28 changes: 10 additions & 18 deletions core/lib/Foswiki/Attach.pm
Expand Up @@ -16,7 +16,7 @@ use Unicode::Normalize;

use Moo;
use namespace::clean;
extends qw(Foswiki::Object);
extends qw(Foswiki::AppObject);

BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
Expand All @@ -27,17 +27,9 @@ BEGIN {

our $MARKER = "\0";

has session => (
is => 'rw',
clearer => 1,
required => 1,
weak_ref => 1,
isa => Foswiki::Object::isaCLASS( 'session', 'Foswiki', noUndef => 1 ),
);

=begin TML
---++ ClassMethod new(session => $session)
---++ ClassMethod new(app => $app)
Constructor.
Expand Down Expand Up @@ -93,7 +85,7 @@ sub renderMetaData {
my @attachments = $topicObject->find('FILEATTACHMENT');
return '' unless @attachments;

my $templates = $this->session->templates;
my $templates = $this->app->templates;
$templates->readTemplate($tmplname);

my $rows = '';
Expand Down Expand Up @@ -142,15 +134,15 @@ Generate a version history table for a single attachment
sub formatVersions {
my ( $this, $topicObject, %attrs ) = @_;

my $users = $this->session->users;
my $users = $this->app->users;

$attrs{name} =
Foswiki::Sandbox::untaint( $attrs{name},
\&Foswiki::Sandbox::validateAttachmentName );

my $revIt = $topicObject->getRevisionHistory( $attrs{name} );

my $templates = $this->session->templates;
my $templates = $this->app->templates;
$templates->readTemplate('attachtables');

my $header = $templates->expandTemplate('ATTACH:versions:header');
Expand Down Expand Up @@ -204,7 +196,7 @@ s/%R_(\w+)%/_expandRowAttrs( $this, $1, $topicObject, $info, $attachmentNum, $is
sub _expandAttrs {
my ( $this, $attr, $topicObject, $info, $attachmentNum ) = @_;
my $file = $info->{name} || '';
my $users = $this->session->users;
my $users = $this->app->users;

require Foswiki::Time;

Expand All @@ -222,7 +214,7 @@ sub _expandAttrs {
return $1;
}
elsif ( $attr eq 'URL' ) {
return $this->session->getScriptUrl(
return $this->app->cfg->getScriptUrl(
0, 'viewfile', $topicObject->web, $topicObject->topic,
rev => $info->{version} || undef,
filename => $file
Expand Down Expand Up @@ -302,7 +294,7 @@ sub _expandRowAttrs {
sub _cUID {
my ( $this, $info ) = @_;

my $users = $this->session->users;
my $users = $this->app->users;
my $user = $info->{author} || $info->{user} || 'UnknownUser';
my $cUID;
if ($user) {
Expand Down Expand Up @@ -368,7 +360,7 @@ sub getAttachmentLink {

my $fileLink = '';
my $imgSize = '';
my $prefs = $this->session->prefs;
my $prefs = $this->app->prefs;

if ( $attName =~ m/\.(gif|jpg|jpeg|png)$/i ) {

Expand Down Expand Up @@ -425,7 +417,7 @@ sub getAttachmentLink {

require Foswiki::Time;
$fileLink = Foswiki::Time::formatTime( $fileTime, $fileLink );
$fileLink = Foswiki::expandStandardEscapes($fileLink);
$fileLink = $this->app->macros->expandStandardEscapes($fileLink);

return $fileLink;
}
Expand Down
169 changes: 164 additions & 5 deletions core/lib/Foswiki/Config.pm
@@ -1,5 +1,13 @@
# See bottom of file for license and copyright information

=begin TML
---+!! package Foswiki::Config
Class representing configuration data.
=cut

package Foswiki::Config;
use v5.14;

Expand All @@ -11,7 +19,7 @@ use POSIX qw(locale_h);
use Unicode::Normalize;
use Cwd qw( abs_path );
use Try::Tiny;
use Foswiki ();
use Foswiki qw(urlEncode urlDecode make_params);

use Moo;
use namespace::clean;
Expand Down Expand Up @@ -45,27 +53,96 @@ my %remap = (
'{RCS}{WorkAreaDir}' => '{Store}{WorkAreaDir}'
);

=begin TML
---++ Attribute data
Contains configuration hash. =%Foswiki::cfg= is an alias to this attribute.
=cut

has data => (
is => 'rw',
lazy => 1,
clearer => 1,
default => sub { {} },
);

# What files we read the config from in the order of reading.
=begin TML
---++ Attribute files
What files we read the config from in the order of reading.
=cut

has files => (
is => 'rw',
default => sub { [] },
);

# failed keeps the name of the failed config or spec file.
=begin TML
---++ Attribute failedConfig
Keeps the name of the failed config or spec file.
=cut

has failedConfig => ( is => 'rw', );
has bootstrapMessage => ( is => 'rw', );
has noExpand => ( is => 'rw', default => 0, );
has noSpec => ( is => 'rw', default => 0, );
has configSpec => ( is => 'rw', default => 0, );
has noLocal => ( is => 'rw', default => 0, );

# Configuration shortcut attributes.

=begin TML
---++ Attribute urlHost
=cut

has urlHost => (
is => 'rw',
lazy => 1,
default => sub {
my $this = shift;

#{urlHost} is needed by loadSession..
my $url = $this->app->request->url;
my $cfg = $this->app->cfg;
my $urlHost;
if ( $url
&& !$cfg->data->{ForceDefaultUrlHost}
&& $url =~ m{^([^:]*://[^/]*).*$} )
{
$urlHost = $1;

if ( $cfg->data->{RemovePortNumber} ) {
$urlHost =~ s/\:[0-9]+$//;
}

# If the urlHost in the url is localhost, this is a lot less
# useful than the default url host. This is because new CGI("")
# assigns this host by default - it's a default setting, used
# when there is nothing better available.
if ( $urlHost =~ m/^(https?:\/\/)localhost$/i ) {
my $protocol = $1;

#only replace localhost _if_ the protocol matches the one specified in the DefaultUrlHost
if ( $cfg->data->{DefaultUrlHost} =~ m/^$protocol/i ) {
$urlHost = $cfg->data->{DefaultUrlHost};
}
}
}
else {
$urlHost = $cfg->data->{DefaultUrlHost};
}
ASSERT($urlHost) if DEBUG;
return $urlHost;
},
);

=begin TML
---++ ClassMethod new([noExpand => 0/1][, noSpec => 0/1][, configSpec => 0/1][, noLoad => 0/1])
Expand Down Expand Up @@ -876,6 +953,88 @@ sub setBootstrap {
push( @{ $this->data->{BOOTSTRAP} }, @BOOTSTRAP );
}

=begin TML
---++ ObjectMethod getScriptUrl( $absolute, $script, $web, $topic, ... ) -> $scriptURL
Returns the URL to a Foswiki script, providing the web and topic as
"path info" parameters. The result looks something like this:
"http://host/foswiki/bin/$script/$web/$topic".
* =...= - an arbitrary number of name,value parameter pairs that will
be url-encoded and added to the url. The special parameter name '#' is
reserved for specifying an anchor. e.g.
=getScriptUrl('x','y','view','#'=>'XXX',a=>1,b=>2)= will give
=.../view/x/y?a=1&b=2#XXX=
If $absolute is set, generates an absolute URL. $absolute is advisory only;
Foswiki can decide to generate absolute URLs (for example when run from the
command-line) even when relative URLs have been requested.
The default script url is taken from {ScriptUrlPath}, unless there is
an exception defined for the given script in {ScriptUrlPaths}. Both
{ScriptUrlPath} and {ScriptUrlPaths} may be absolute or relative URIs. If
they are absolute, then they will always generate absolute URLs. if they
are relative, then they will be converted to absolute when required (e.g.
when running from the command line, or when generating rss). If
$script is not given, absolute URLs will always be generated.
If either the web or the topic is defined, will generate a full url (including web and topic). Otherwise will generate only up to the script name. An undefined web will default to the main web name.
As required by RFC3986, the returned URL will only contain the
allowed characters -A-Za-z0-9_.~!*\'();:@&=+$,/?%#[]
=cut

sub getScriptUrl {
my ( $this, $absolute, $script, $web, $topic, @params ) = @_;

my $app = $this->app;

$absolute ||=
( $app->inContext('command_line') || $app->inContext('absolute_urls') );

# SMELL: topics and webs that contain spaces?

my $url;
if ( defined $this->data->{ScriptUrlPaths} && $script ) {
$url = $this->data->{ScriptUrlPaths}{$script};
}
unless ( defined($url) ) {
$url = $this->data->{ScriptUrlPath};
if ($script) {
$url .= '/' unless $url =~ m/\/$/;
$url .= $script;
if (
rindex( $url, $this->data->{ScriptSuffix} ) !=
( length($url) - length( $this->data->{ScriptSuffix} ) ) )
{
$url .= $this->data->{ScriptSuffix} if $script;
}
}
}

if ( $absolute && $url !~ /^[a-z]+:/ ) {

# See http://www.ietf.org/rfc/rfc2396.txt for the definition of
# "absolute URI". Foswiki bastardises this definition by assuming
# that all relative URLs lack the <authority> component as well.
$url = $this->urlHost . $url;
}

if ($topic) {
( $web, $topic ) = $app->request->normalizeWebTopicName( $web, $topic );

$url .= urlEncode( '/' . $web . '/' . $topic );

}
elsif ($web) {
$url .= urlEncode( '/' . $web );
}
$url .= make_params(@params);

return $url;
}

# Preset values that are hard-coded and not coming from external sources.
sub _populatePresets {
my $this = shift;
Expand Down Expand Up @@ -1030,10 +1189,10 @@ sub _guessDefaults {

# Windows default tmpdir is the C: root use something sane.
# Configure does a better job, it should be run.
$Foswiki::cfg{TempfileDir} = $Foswiki::cfg{WorkingDir};
$this->data->{TempfileDir} = $this->data->{WorkingDir};
}
else {
$Foswiki::cfg{TempfileDir} = File::Spec->tmpdir();
$this->data->{TempfileDir} = File::Spec->tmpdir();
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/lib/Foswiki/Form.pm
Expand Up @@ -586,8 +586,9 @@ sub renderForEdit {

my $dv = $fieldDef->getDefaultValue($value);
if ( defined($dv) ) {
$dv = $topicObject->expandMacros($dv);
$value = Foswiki::expandStandardEscapes($dv); # Item2837
$dv = $topicObject->expandMacros($dv);
$value =
$app->macros->expandStandardEscapes($dv); # Item2837
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/Form/FieldDefinition.pm
Expand Up @@ -297,8 +297,8 @@ sub populateMetaFromQueryData {
# Note: we test for 'defined' because value can also be 0 (zero)
$value = $query->param( $this->name );
$value = '' unless defined $value;
if ( $this->session->inContext('edit') ) {
$value = Foswiki::expandStandardEscapes($value);
if ( $this->app->inContext('edit') ) {
$value = $this->app->macros->expandStandardEscapes($value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/Func.pm
Expand Up @@ -3073,7 +3073,7 @@ sub normalizeWebTopicName {

#my( $web, $topic ) = @_;
ASSERT($Foswiki::app) if DEBUG;
return $Foswiki::app->normalizeWebTopicName(@_);
return $Foswiki::app->request->normalizeWebTopicName(@_);
}

=begin TML
Expand Down Expand Up @@ -3146,7 +3146,7 @@ The set of tokens that is expanded is described in System.FormatTokens.
=cut

sub decodeFormatTokens {
return Foswiki::expandStandardEscapes(@_);
return $Foswiki::app->macros->expandStandardEscapes(@_);
}

=begin TML
Expand Down

0 comments on commit aa07ddc

Please sign in to comment.