File matomo.spec of Package matomo

#
# spec file for package matomo
#
# Copyright (c) 2020 SUSE LLC
# Copyright (c) 2024 openSUSE contributors
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

%if 0%{?suse_version}
%define ap_usr wwwrun
%define ap_grp www
%else
%define ap_usr nobody
%define ap_grp nogroup
%endif

%define php_version 8
# %%(%%{_bindir}/php -r 'echo PHP_VERSION;' | cut -d "." -f1)

Name:           matomo
Version:        5.0.3
Release:        0
Summary:        Web analytics platform
License:        GPL-3.0-or-later
Group:          Productivity/Networking/Web/Utilities
URL:            http://matomo.org/
Source0:        %{name}-%{version}.tar.gz
Source1:        %{name}-%{version}.tar.gz.asc
Source2:        %{name}.conf
Source3:        %{name}.logrotate
Source4:        %{name}-README.SUSE
Source11:       %{name}-archive.service
Source12:       %{name}-archive.timer
Source13:       %{name}.my.cnf
Source14:       %{name}-tmpfile.conf
Source15:       %{name}-php-fpm.conf
Source16:       %{name}-php-fpm.default.conf
Source17:       %{name}-nginx.conf 
Source99:       %{name}.rpmlintrc
Patch0:         matomo-ignore_excluded_files.patch
BuildArch:      noarch
%if 0%{?suse_version} >= 1500
Requires(pre):  user(%ap_usr)
Requires(pre):  group(%ap_grp)
%else
Requires(pre):  aaa_base
%endif
BuildRequires:  apache2-devel
BuildRequires:  coreutils
BuildRequires:  cron
BuildRequires:  fdupes
BuildRequires:  logrotate
BuildRequires:  mariadb
BuildRequires:  php8-fpm >= %{php_version}
BuildRequires:  unzip
BuildRequires:  pkgconfig(systemd)
BuildRequires:  php8-cli
BuildRequires:  php8-pdo
BuildRequires:  php8-iconv
BuildRequires:  php8-mbstring
Requires:       apache2
Requires:       logrotate
Requires:       %{name}-import_logs >= %{version}
Requires:       php8-cli
Requires:       php8-ctype
Requires:       php8-curl
Requires:       php8-dom
Requires:       php8-gd
Requires:       php8-iconv
Requires:       php8-intl
Requires:       php8-mbstring
Requires:       php8-mysql
Requires:       php8-pdo
Requires:       php8-tokenizer
Requires:       php8-xmlreader
Requires:       php-xmlwriter
Requires:       php8-zlib
Requires(pre):  php8
Recommends:     php8-geoip
Recommends:     php8-openssl
Recommends:     php8-redis
Recommends:     apache2-mod_geoip
Recommends:     mod_php_any >= %{php_version}
Recommends:     mariadb
Conflicts:      piwik
BuildRequires:  systemd
%{?systemd_requires}
%define apxs %{_bindir}/apxs
%define ap_sysconfdir %(%{apxs} -q SYSCONFDIR || /etc/apache2)
%define ap_serverroot %(%{apxs} -q PREFIX || /srv/www)


%description
Matomo, formerly Piwik, is a web analytics platform that gives
insights into a website's visitors and marketing campaigns, so the
strategy and online experience of visitors may be optimized.

%package php-fpm
Summary:        PHP FPM configuration for %{name}
Group:          Productivity/Networking/Web/Utilities
Requires:       php8-fpm >= %{php_version}
Requires(pre):  php8-fpm >= %{php_version}
%{?systemd_requires}

%description php-fpm
This package contains the PHP FPM configuration file to run
Matomo with php-fpm.

Please remember to setup php-fpm correctly (by creating a working
/etc/php%{php_version}/fpm/php-fpm.conf first).

%package nginx
Summary:        Nginx configuration for running Matomo
Group:          Productivity/Networking/Web/Utilities
Requires:       %{name}-php-fpm >= %{version}
Requires(pre):  nginx
Requires:       nginx
%{?systemd_requires}

%description nginx
This package contains a small nginx configuration that should help you get your
own Matomo instance running and start collecting your own analytics.

Please read through the %{_sysconfdir}/nginx/vhosts.d/matomo.conf and adjust
it to your needs. Please remember that you also need to check/configure the 
%{name}-php-fpm package and enable and run php-fpm. 

%package import_logs
Summary:        Logfile importer for installation on remote hosts
Group:          Productivity/Networking/Web/Utilities
Requires:       python(abi) >= 2.6
Requires:       python(abi) < 3.0

%description import_logs
This package contains the import_logs.py script, which can be run on a remote
host to import logfiles on the matomo instance.

%package tests
Summary:       Unit tests for Matomo
Group:         Productivity/Networking/Web/Utilities
Requires:      php8-phpunit
Requires(pre): %{name} = %{version}

%description tests
Matomo comes with unit tests, integration tests, system tests, Javascript tests
and UI tests.

This package contains the needed test directory (not needed in a productive environment) 
and requires the needed dependencies for debugging and testing.


%prep
%setup -q  -n %{name}
%patch0 -p1
# remove unwanted files
find . -type f "(" -name .htaccess -o -name .travis.sh -o -name .gitkeep ")" -delete
#find . -name ".git*" -exec rm -Rf "{}" "+"
find . -type f "(" -name "*.c" -o -name "*.h" -o -name "*.js.orig" ")" -delete
# remove doc from subdirs
find plugins vendor libs -type f "(" -iname README.md -o -iname CHANGELOG.md -iname CONTRIBUTING.md -iname UPGRADE.md ")" -delete
rm -rf vendor/szymach/c-pchart/doc
# env-script-interpreter
find . -type f -exec sed -i -e 's|\/usr\/bin\/env php|\/usr\/bin\/php|g' {} +
find . -type f -exec sed -i -e 's|\/usr\/bin\/env bash|\/bin\/bash|g' {} +
#
# disable the auto updater, it can't work properly with the new, more secure permissions and is a bad idea on a RPM based setup anyways.
#
sed -i '/enable_auto_update/s/1$/0/' config/global.ini.php
#
# regenerate the manifest file
#
find ./ -type f -printf '%s ' -exec md5sum {} \; \
		| grep -E -v 'config/manifest.inc.php|vendor/autoload.php|vendor/composer/autoload_real.php' \
		| grep -E -v ' ./LEGALNOTICE| ./LICENSE|misc/How to install Matomo.html' \
		| grep -E -v ' ./CHANGELOG.md| ./CONTRIBUTING.md| ./PRIVACY.md| ./README.md| ./SECURITY.md' \
		| grep -E -v 'tests/README.md' \
		| grep -E -v 'js/piwik.min.js' \
		| sed '1,$ s/\([0-9]*\) \([a-z0-9]*\) *\.\/\(.*\)/\t\t"\3" => array("\1", "\2"),/;' \
		| sort \
		| sed '1 s/^/<?php\n\/\/ This file is automatically generated during the Matomo build process \
namespace Piwik;\nclass Manifest {\n\tstatic $files=array(\n/; $ s/$/\n\t);\n}/' \
		> ./config/manifest.inc.php


%build
# nothing to build

%install
# make directories
install -d -m0755 %{buildroot}/%{ap_serverroot}/%{name}
install -d -m0755 %{buildroot}/%{ap_serverroot}/%{name}/tmp
install -d -m0755 %{buildroot}/%{_sysconfdir}/%{name}
# copy src from build to buildroot
install -D -m644 %{SOURCE4} %{buildroot}/%{_defaultdocdir}/%{name}/README.SUSE
mv LEGALNOTICE %{buildroot}/%{_defaultdocdir}/%{name}
mv LICENSE %{buildroot}/%{_defaultdocdir}/%{name}
mv "misc/How to install Matomo.html" %{buildroot}/%{_defaultdocdir}/%{name}
mv *md %{buildroot}/%{_defaultdocdir}/%{name}
cp -dR * %{buildroot}/%{ap_serverroot}/%{name}
# install matomo.conf to apache conf.d
mkdir -p %{buildroot}/%{ap_sysconfdir}/conf.d
sed -e 's|__matomo_web__|%{ap_serverroot}/%{name}|g' \
	-e 's|__matomo_conf__|%{_sysconfdir}/%{name}|g' \
	-e 's|__matomo_log__|/var/log/%{name}|g' \
	%{SOURCE2} > %{buildroot}/%{ap_sysconfdir}/conf.d/%{name}.conf
# install php-fpm configuration
mkdir -p %{buildroot}/etc/php%{php_version}/fpm/php-fpm.d
sed -e 's|__matomo_web__|%{ap_serverroot}/%{name}|g' \
	-e 's|__matomo_conf__|%{_sysconfdir}/%{name}|g' \
	-e 's|__matomo_log__|/var/log/%{name}|g' \
%{SOURCE15} > %{buildroot}%{_sysconfdir}/php%{php_version}/fpm/php-fpm.d/matomo-php-fpm.conf
install -m 0644 %{SOURCE16} %{buildroot}%{_sysconfdir}/php%{php_version}/fpm/php-fpm.conf
# install nginx configuration
install -D -m 0644 %{SOURCE17} %{buildroot}%{_sysconfdir}/nginx/vhosts.d/matomo.conf
sed -i 's|__matomo_web__|%{ap_serverroot}/%{name}|g' %{buildroot}%{_sysconfdir}/nginx/vhosts.d/matomo.conf
# install logrotate
install -D -m0644 %{SOURCE3} %{buildroot}/%{_sysconfdir}/logrotate.d/%{name}
# move config to etc/matomo and make symlink
mv %{buildroot}/%{ap_serverroot}/%{name}/config/* \
	%{buildroot}/%{_sysconfdir}/%{name}
rm -d %{buildroot}/%{ap_serverroot}/%{name}/config
ln -s %{_sysconfdir}/%{name} %{buildroot}/%{ap_serverroot}/%{name}/config
# install cronscript and systemd-timer
install -d -m 0755 %{buildroot}/var/log/%{name}
install -D -m 0644 %{SOURCE11} %{buildroot}%{_unitdir}/%{name}-archive.service
install -D -m 0644 %{SOURCE12} %{buildroot}%{_unitdir}/%{name}-archive.timer
install -D -m 0644 %{SOURCE14} %{buildroot}%{_tmpfilesdir}/%{name}.conf
sed -i -e 's|@ap_serverroot@|%{ap_serverroot}|g' %{buildroot}%{_unitdir}/%{name}-archive.service
# install changes for mariadb
install -D -m0644 %{SOURCE13} %{buildroot}/%{_sysconfdir}/my.cnf.d/%{name}.my.cnf
# touch config.ini.php (ghost)
# touch %{buildroot}%{_sysconfdir}/%{name}/config.ini.php

%fdupes %{buildroot}/%{_prefix}
%fdupes %{buildroot}/srv

%pre
%service_add_pre matomo-archive.timer matomo-archive.service

%post
%service_add_post matomo-archive.timer matomo-archive.service apache2.service
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf
if [ $1 -gt 1 ]; then
  # Update matomo if this is an upgrade $1 == 2
  echo "matomo: Update matomo:core..."
  su wwwrun -s /bin/sh -c "%{_bindir}/php %{ap_serverroot}/%{name}/console config:set 'Tracker.record_statistics="0"'" || :
  su wwwrun -s /bin/sh -c "%{_bindir}/php %{ap_serverroot}/%{name}/console config:set 'General.maintenance_mode="1"'" || :
  su wwwrun -s /bin/sh -c "%{_bindir}/php %{ap_serverroot}/%{name}/console core:update --yes" || :
  su wwwrun -s /bin/sh -c "%{_bindir}/php %{ap_serverroot}/%{name}/console config:set 'General.maintenance_mode="0"'" || :
  su wwwrun -s /bin/sh -c "%{_bindir}/php %{ap_serverroot}/%{name}/console config:set 'Tracker.record_statistics="1"'" || :
  :
fi

%preun
%service_del_preun matomo-archive.timer matomo-archive.service

%postun
%service_del_postun matomo-archive.timer matomo-archive.service apache2.service

%pre php-fpm
%service_add_pre php-fpm.service

%post php-fpm
%service_add_post php-fpm.service

%preun php-fpm
%service_del_preun php-fpm.service

%postun php-fpm
%service_del_postun php-fpm.service

%pre nginx
%service_add_pre nginx.service

%post nginx
%service_add_post nginx.service

%preun nginx
%service_del_preun nginx.service

%postun nginx
%service_del_postun nginx.service

%files
%defattr(0644,root,root,0755)
%doc %dir %{_defaultdocdir}/%{name}
%doc %{_defaultdocdir}/%{name}/*
%dir %{ap_serverroot}/%{name}
%dir %{ap_serverroot}/%{name}/plugins
%config(noreplace) %{ap_sysconfdir}/conf.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%config(noreplace) %{_sysconfdir}/my.cnf.d/%{name}.my.cnf
%{ap_serverroot}/%{name}/*
%attr(0755,root,root) %{ap_serverroot}/%{name}/console
%{_unitdir}/%{name}-archive.service
%{_unitdir}/%{name}-archive.timer
%{_tmpfilesdir}/%{name}.conf
%exclude %dir %{ap_serverroot}/%{name}/misc/log-analytics
%exclude %{ap_serverroot}/%{name}/misc/log-analytics/*
%exclude %dir %{ap_serverroot}/%{name}/tests
%exclude %{ap_serverroot}/%{name}/tests/*
#
# defattr change
#
%defattr(0640,root,%{ap_grp},0750)
%dir %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/%{name}/environment
%config(noreplace) %{_sysconfdir}/%{name}/environment/*.php
#
# defattr change
#
%defattr(0640,%{ap_usr},%{ap_grp},0750)
# below is needed to update the Javascript Tracker with plugin tracker additions
%{ap_serverroot}/%{name}/matomo.js
%{ap_serverroot}/%{name}/piwik.js
# ^^
# below is needed to allow Matomo (Matomo Tag Manager) to write container_* files
%dir %{ap_serverroot}/%{name}/js
# ^^
# below is needed as Matomo wants to write it's own configuration file
%ghost %config(noreplace) %{_sysconfdir}/%{name}/config.ini.php
%config %{_sysconfdir}/%{name}/global.ini.php
%config %{_sysconfdir}/%{name}/global.php
%config %{_sysconfdir}/%{name}/manifest.inc.php
# ^^
%ghost /run/%{name}_sessions
%dir %{ap_serverroot}/%{name}/tmp
%dir /var/log/%{name}

%files php-fpm
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/php%{php_version}/fpm/php-fpm.conf
%config(noreplace) %{_sysconfdir}/php%{php_version}/fpm/php-fpm.d/matomo-php-fpm.conf

%files nginx
%defattr(-,root,root,-)
%dir %{_sysconfdir}/nginx
%dir %{_sysconfdir}/nginx/vhosts.d
%config(noreplace) %{_sysconfdir}/nginx/vhosts.d/matomo.conf

%files import_logs
%defattr(-,root,root,-)
%license %{ap_serverroot}/%{name}/misc/log-analytics/LICENSE.txt
%doc %{ap_serverroot}/%{name}/misc/log-analytics/*.{md,txt}
%doc %{ap_serverroot}/%{name}/misc/log-analytics/LEGALNOTICE
%dir %{ap_serverroot}/%{name}/misc/log-analytics
%{ap_serverroot}/%{name}/misc/log-analytics/import_logs.py

%files tests
%defattr(-,root,root,-)
%dir %{ap_serverroot}/%{name}/tests
%{ap_serverroot}/%{name}/tests/*

%changelog
openSUSE Build Service is sponsored by