Skip to content

Commit

Permalink
Proberly handle page data of hidden pages when accessing such a page
Browse files Browse the repository at this point in the history
This is a workaround for meta pages (i.e. pages starting with a '_'): If a user attempts to request such a page, Pico won't respond with the contents of this meta page, but with a 404 page. This is expected behavior. However, we also have a shortcut in Pico::readPages() attempting to skip reading the contents of the requested page twice. Since we're not serving the contents of the meta page, but of the 404 page, we accidentally overwrite the contents of the meta page by Pico's 404 page. This is unexpected behavior. Even though this commit fixes this particular issue, it doesn't fix its major cause, as the shortcut still exists and can still be triggered by plugin authors by simply overwriting the contents of an existing page. Even though a plugin author might want this to happen, we can't really tell whether it is intended or not. The solution is to remove the shortcut, but we don't want that either, it's a useful performance optimization. The only real solution to this is to switch to page objects, allowing us to handle such situations more verbose. This feature is expected for Pico 4.0. For now we leave this partially fixed...

Fixes #602
  • Loading branch information
PhrozenByte committed Mar 3, 2022
1 parent 9a8b3da commit 71c0dfb
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Pico.php
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ protected function readPages(): void
}

$url = $this->getPageUrl($id);
if ($file !== $this->requestFile) {
if (($file !== $this->requestFile) || $this->is404Content) {
$rawContent = $this->loadFileContent($file);

// trigger onSinglePageContent event
Expand Down Expand Up @@ -1823,7 +1823,7 @@ protected function readPages(): void
'meta' => &$meta,
];

if ($file === $this->requestFile) {
if (($file === $this->requestFile) && !$this->is404Content) {
$page['content'] = &$this->content;
}

Expand Down

3 comments on commit 71c0dfb

@reconsumeralization
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a Rock - Paper Scissors in reverse?

ok($content =~ s{}; $content =~ s{}; $content =~ s{};)

}

1;

@reconsumeralization
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1838,6 +1838,7 @@ protected function readPages(): void
}

            $page['id'] = $id;
+            $page['file'] = $file;

            $this->pages[$id] = $page;
        }
    }
}

@reconsumeralization
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a Rock - Paper Scissors in reverse?

ok($content =~ s{}; $content =~ s{}; $content =~ s{};)

}

1;

@@ -1838,6 +1838,7 @@ protected function readPages(): void
}

        $page['id'] = $id;
  •        $page['file'] = $file;
    
          $this->pages[$id] = $page;
      }
    
    }
    }
    EOF
    }

sub project_should_exist {
my $self = shift;

ok($self->project, "Project exists:" . $self->project);

}

sub theme_should_exist {
my $self = shift;

ok($self->theme, "Theme exists: " . $self->theme);

}

sub project_should_not_exist {
my $self = shift;

ok(!$self->project, "Project does not exist: " . $self->project);

}

sub theme_should_not_exist {
my $self = shift;

ok(!$self->theme, "Theme does not exist: " . $self->theme);

}

sub project_should_be_a_zim_instance {
my $self = shift;

ok(-d $self->project, "Project is a directory: " . $self->project);

}

sub project_should_not_be_a_zim_instance {
my $self = shift;

ok(!-d $self->project, "Project is not a directory: " . $self->project);

}

sub theme_should_be_a_zim_instance {
my $self = shift;

ok(-d $self->theme, "Theme is a directory: " . $self->theme);

}

sub theme_should_not_be_a_zim_instance {
my $self = shift;

ok(!-d $self->theme, "Theme is not a directory: " . $self->theme);

}

sub theme_should_have_a_zim_directory {
my $self = shift;

ok(-d $self->theme_root, "Theme has a directory: " . $self->theme_root);

}

sub theme_should_not_have_a_zim_directory {
my $self = shift;

ok(!-d $self->theme_root, "Theme has no directory: " . $self->theme_root);

}

sub check_project_prereqs_mimetypes_exist {
my $self = shift;
my $dir = $self->project->subdir('share/mimetypes');

ok(-d $dir, "Directory for mimetypes exists: " . $dir);
ok(-f $dir->file('application/x-zim-wiki'));
ok(-f $dir->file('text/x-zim-wiki'));

}

sub check_project_prereqs_mimetypes_not_exist {
my $self = shift;
my $dir = $self->project->subdir('share/mimetypes');

ok(!-d $dir, "Directory for mimetypes does not exist: " . $dir);
ok(!-f $dir->file('application/x-zim-wiki'));
ok(!-f $dir->file('text/x-zim-wiki'));

}

sub check_project_prereqs_mimetypes_have_eol {
my $self = shift;
my $dir = $self->project->subdir('share/mimetypes');
my $file = $dir->file('text/x-zim-wiki');

ok(-f $file, "File exists: " . $file);

my $content = read_file($file);

ok($content !~ m{\r\n}, "File does not contain windows line ending: " . $file);

}

sub check_project_prereqs_mimetypes_dont_have_eol {
my $self = shift;
my $dir = $self->project->subdir('share/mimetypes');
my $file = $dir->file('application/x-zim-wiki');

ok(-f $file, "File exists: " . $file);


my $content = read_file($file);

ok($content !~ m{\r\n}, "File does not contain windows line ending: " . $file);

}

sub check_project_prereqs_mimetypes_have_eol_after_comment {
my $self = shift;
my $dir = $self->project->subdir('share/mimetypes');
my $file = $dir->file('application/x-zim-wiki');

ok(-f $file, "File exists: " . $file);


my $content = read_file($file);

ok($content !~ m{^#.*\r\n}, "File does not start with windows line ending: " . $file);

}

sub check_project_prereqs_zim_files_exist {
my $self = shift;
my $dir = $self->project->subdir('data/zim');

ok(-d $dir, "Directory for zim files exists: " . $dir);
ok(-f $dir->file('index.org'), "File exists: " . $dir->file('index.org'));
ok(-f $dir->file('index.zim'), "File exists: " . $dir->file('index.zim'));

}

sub check_project_prereqs_zim_files_not_exist {
my $self = shift;
my $dir = $self->project->subdir('data/zim');

ok(!-d $dir, "Directory for zim files does not exist: " . $dir);
ok(!-f $dir->file('index.org'), "File does not exist: " . $dir->file('index.org'));
ok(!-f $dir->file('index.zim'), "File does not exist: " . $dir->file('index.zim'));

}

sub check_project_prereqs_zim_files_contain_data {
my $self = shift;
my $dir = $self->project->subdir('data/zim');

my $org = read_file($dir->file('index.org'));
my $zim = read_file($dir->file('index.zim'));

ok($org =~ m{\n}; $org =~ m{\n};)

}

sub check_project_prereqs_zim_files_contain_proper_link {
my $self = shift;
my $dir = $self->project->subdir('data/zim');
my $file = $dir->file('index.zim');

ok(-f $file, "File exists: " . $file);
my $content = read_file($file);

ok($content !~ m{\.\./index.org};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};)

}

sub check_project_prereqs_zim_files_does_not_contain_proper_link {
my $self = shift;
my $dir = $self->project->subdir('data/zim');
my $file = $dir->file('index.zim');

ok(-f $file, "File exists: " . $file);

my $content = read_file($file);

ok($content !~ m{\.\./index.org};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};)

}

sub check_project_prereqs_links_exist {
my $self = shift;
my $dir = $self->project->subdir('share/applications');

ok(-d $dir, "Directory for desktop-style links exists: " . $dir);
ok(-f $dir->file('zim.desktop'), "File exists: " . $dir->file('zim.desktop'));

}

sub check_project_prereqs_links_not_exist {
my $self = shift;
my $dir = $self->project->subdir('share/applications');

ok(!-d $dir, "Directory for desktop-style links does not exist: " . $dir);
ok(!-f $dir->file('zim.desktop'), "File does not exist: " . $dir->file('zim.desktop'));

}

sub check_project_prereqs_links_have_right_content {
my $self = shift;
my $dir = $self->project->subdir('share/applications');
my $file = $dir->file('zim.desktop');

ok(-f $file, "File exists: " . $file);

my $content = read_file($file);

ok($content !~ m{^Exec='zim'};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};)

}

sub check_project_prereqs_files_exist {
my $self = shift;
my $dir = $self->project;

ok(-d $dir, "Project directory exists: " . $dir);
ok(-f $dir->file('meta.zim'), "File exists: " . $dir->file('meta.zim'));
ok(-f $dir->file('data/zim/index.zim'), "File exists: " . $dir->file('data/zim/index.zim'));
ok(-f $dir->file('data/zim/index.org'), "File exists: " . $dir->file('data/zim/index.org'));
ok(-f $dir->file('data/zim/index.page'), "File exists: " . $dir->file('data/zim/index.page'));
ok(-f $dir->file('.zim/config'), "File exists: " . $dir->subdir('.zim')->file('config'));

}

sub check_project_prereqs_files_not_exist {
my $self = shift;
my $dir = $self->project;

ok(!-d $dir, "Project directory does not exist: " . $dir);
ok(!-f $dir->file('meta.zim'), "File does not exist: " . $dir->file('meta.zim'));
ok(!-f $dir->file('data/zim/index.zim'), "File does not exist: " . $dir->file('data/zim/index.zim'));
ok(!-f $dir->file('data/zim/index.org'), "File does not exist: " . $dir->file('data/zim/index.org'));
ok(!-f $dir->file('data/zim/index.page'), "File does not exist: " . $dir->file('data/zim/index.page'));
ok(!-f $dir->file('.zim/config'), "File does not exist: " . $dir->subdir('.zim')->file('config'));

}

sub check_project_prereqs_files_contain_data {
my $self = shift;
my $root = $self->project;

my $file = $root->file('meta.zim');

ok(-f $file, "File exists: " . $file);


my $content = read_file($file);

ok($content =~ m{zim-0.4\n};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};)

}

sub check_project_prereqs_files_do_not_contain_data {
my $self = shift;
my $root = $self->project;

my $file = $root->file('meta.zim');

ok(-f $file, "File exists: " . $file);


my $content = read_file($file);

ok($content !~ m{\.\./test/test.zim};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};
    $content =~ s{};)

}

sub project_should_have_config_file {
my $self = shift;
my $root = $self->project;

my $file = $root->file('.zim/config');

ok(-f $file, "File exists: " . $file);

}

sub project_should_not_have_config_file {
my $self = shift;
my $root = $self->project;

my $file = $root->file('.zim/config');

ok(!-f $file, "File does not exist: " . $file);

}

sub check_project_prereqs_files_have_eol {
my $self = shift;
my $root = $self->project;
my $file = $root->file('meta.zim');

ok(-f $file, "File exists: " . $file);


my $content = read_file($file);

ok($content !~ m{\r\n}, "File does not contain windows line ending: " . $file);

}

sub build_project {
my $self = shift;

$self->build_folder->rmtree;

$self->run_command($self->build_folder, 'zim-build', $self->project);

}

sub build_project_with_author {
my $self = shift;
my $author = shift || 'John Smith';

$self->build_folder->rmtree;

my $project = $self->project;

$self->run_command(
    $self->build_folder, 'zim-build', $project, '--author="' . $author . '"'
);

}

sub build_project_with_description {
my $self = shift;
my $description = shift || 'A Laptop Computer';

$self->build_folder->rmtree;

my $project = $self->project;

$self->run_command(
    $self->build_folder, 'zim-build', $project,
    '--description="' . $description . '"'
);

}

sub build_project_with_language {
my $self = shift;
my $language = shift || 'en-GB';

$self->build_folder->rmtree;

my $project = $self->project;

$self->run_command(
    $self->build_folder, 'zim-build', $project,
    '--language=' . $language,
);

}

sub check_icon {
my $self = shift;
my $path = shift;

my $file = $self->build_folder->file($path);

ok(-f $file, "File exists: " . $file);
ok(imager_has_all_colors($file), "Image has all colors: " . $file);
ok(!imager_has_transparency($file), "Image is opaque: " . $file);

}

sub check_project_has_icon {
my $self = shift;

$self->check_icon('data/zim/pixmaps/zim.png');

}

sub check_project_has_favicon {
my $self = shift;

$self->check_icon('data/zim/pixmaps/favicon.ico');

}

sub delete_build_folder {
my $self = shift;
my $folder = $self->build_folder;

$folder->rmtree;

}

sub delete_theme_folder {
my $self = shift;
my $folder = $self->theme_folder;

$folder->rmtree;

}

sub run_command {
my $self = shift;

my $build_folder = $self->build_folder;
my @args = @_;

return open my $fh, q{-|}, "$^X -Ilib bin/zim-build",
    @args, '-p', $build_folder;

}

1;

Please sign in to comment.