summaryrefslogtreecommitdiffstats
path: root/extensions/SiteMapIndex/Extension.pm
blob: c606702ae0793aad1dcc8d803749e014b8f23629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Sitemap Bugzilla Extension.
#
# The Initial Developer of the Original Code is Everything Solved, Inc.
# Portions created by the Initial Developer are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Max Kanat-Alexander <mkanat@bugzilla.org>
#   Dave Lawrence <dkl@mozilla.com>

package Bugzilla::Extension::SiteMapIndex;

use 5.10.1;
use strict;
use warnings;

use base qw(Bugzilla::Extension);

our $VERSION = '1.0';

use Bugzilla::Constants qw(bz_locations ON_WINDOWS);
use Bugzilla::Util qw(get_text);
use Bugzilla::Install::Filesystem;

use Bugzilla::Extension::SiteMapIndex::Constants;
use Bugzilla::Extension::SiteMapIndex::Util;

use DateTime;
use IO::File;
use POSIX;

#########
# Pages #
#########

sub template_before_process {
  my ($self, $args) = @_;
  my ($vars, $file) = @$args{qw(vars file)};

  return if $file ne 'global/header.html.tmpl';
  return unless (exists $vars->{bug} || exists $vars->{bugs});
  my $bugs = exists $vars->{bugs} ? $vars->{bugs} : [$vars->{bug}];
  return if ref $bugs ne 'ARRAY';

  foreach my $bug (@$bugs) {
    if (!bug_is_ok_to_index($bug)) {
      $vars->{sitemap_noindex} = 1;
      last;
    }
  }
}

sub page_before_template {
  my ($self, $args) = @_;
  my $page = $args->{page_id};

  if ($page =~ m{^sitemap/sitemap\.}) {
    my $map = generate_sitemap(__PACKAGE__->NAME);
    print Bugzilla->cgi->header('text/xml');
    print $map;
    exit;
  }
}

################
# Installation #
################

sub install_before_final_checks {
  my ($self) = @_;
  if (!Bugzilla->localconfig->{urlbase}) {
    print STDERR get_text('sitemap_no_urlbase'), "\n";
    return;
  }
  if (Bugzilla->params->{'requirelogin'}) {
    print STDERR get_text('sitemap_requirelogin'), "\n";
    return;
  }

  return if (Bugzilla->localconfig->{urlbase} ne 'https://bugzilla.mozilla.org/');
}

sub install_filesystem {
  my ($self, $args) = @_;
  my $create_dirs  = $args->{'create_dirs'};
  my $recurse_dirs = $args->{'recurse_dirs'};
  my $htaccess     = $args->{'htaccess'};

  # Create the sitemap directory to store the index and sitemap files
  my $sitemap_path = bz_locations->{'datadir'} . "/" . __PACKAGE__->NAME;

  $create_dirs->{$sitemap_path} = Bugzilla::Install::Filesystem::DIR_CGI_WRITE
    | Bugzilla::Install::Filesystem::DIR_ALSO_WS_SERVE;

  $recurse_dirs->{$sitemap_path} = {
    files => Bugzilla::Install::Filesystem::CGI_WRITE
      | Bugzilla::Install::Filesystem::DIR_ALSO_WS_SERVE,
    dirs => Bugzilla::Install::Filesystem::DIR_CGI_WRITE
      | Bugzilla::Install::Filesystem::DIR_ALSO_WS_SERVE
  };

  # Create a htaccess file that allows the sitemap files to be served out
  $htaccess->{"$sitemap_path/.htaccess"} = {
    perms    => Bugzilla::Install::Filesystem::WS_SERVE,
    contents => <<EOT
# Allow access to sitemap files created by the SiteMapIndex extension
<FilesMatch ^sitemap.*\\.xml(.gz)?\$>
  Allow from all
</FilesMatch>
Deny from all
EOT
  };
}

sub before_robots_txt {
  my ($self, $args) = @_;
  $args->{vars}{SITEMAP_URL} = Bugzilla->localconfig->{urlbase} . SITEMAP_URL;
}

__PACKAGE__->NAME;