Files
git/contrib/mw-to-git/Git/Mediawiki.pm
brian m. carlson 702d8c1f3b Require Perl 5.26.0
Our platform support policy states that we require "versions of
dependencies which are generally accepted as stable and supportable,
e.g., in line with the version used by other long-term-support
distributions".  Of Debian, Ubuntu, RHEL, and SLES, the four most common
distributions that provide LTS versions, the version with mainstream
long-term security support with the oldest Perl is 5.26.0 in SLES 15.6.

This is a major upgrade, since Perl 5.8.1, according to the Perl
documentation, was released in September of 2003.  It brings a lot of
new features that we can choose to use, such as s///r to return the
modified string, the postderef functionality, and subroutine signatures,
although the latter was still considered experimental until 5.36.

This change was made with the following one-liner, which intentionally
excludes modifying the vendored modules we include to avoid conflicts:

    git grep -l 'use 5.008001' | grep -v 'LoadCPAN/' | xargs perl -pi -e 's/use 5.008001/require v5.26/'

Use require instead of use to avoid changing the behavior as the latter
enables features and the former does not.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
2024-10-23 16:16:36 -04:00

102 lines
2.7 KiB
Perl

package Git::Mediawiki;
require v5.26;
use strict;
use POSIX;
use Git;
BEGIN {
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
# Totally unstable API.
$VERSION = '0.01';
require Exporter;
@ISA = qw(Exporter);
@EXPORT = ();
# Methods which can be called as standalone functions as well:
@EXPORT_OK = qw(clean_filename smudge_filename connect_maybe
EMPTY HTTP_CODE_OK HTTP_CODE_PAGE_NOT_FOUND);
}
# Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
use constant SLASH_REPLACEMENT => '%2F';
# Used to test for empty strings
use constant EMPTY => q{};
# HTTP codes
use constant HTTP_CODE_OK => 200;
use constant HTTP_CODE_PAGE_NOT_FOUND => 404;
sub clean_filename {
my $filename = shift;
$filename =~ s{@{[SLASH_REPLACEMENT]}}{/}g;
# [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
# Do a variant of URL-encoding, i.e. looks like URL-encoding,
# but with _ added to prevent MediaWiki from thinking this is
# an actual special character.
$filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
# If we use the uri escape before
# we should unescape here, before anything
return $filename;
}
sub smudge_filename {
my $filename = shift;
$filename =~ s{/}{@{[SLASH_REPLACEMENT]}}g;
$filename =~ s/ /_/g;
# Decode forbidden characters encoded in clean_filename
$filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf('%c', hex($1))/ge;
return substr($filename, 0, NAME_MAX-length('.mw'));
}
sub connect_maybe {
my $wiki = shift;
if ($wiki) {
return $wiki;
}
my $remote_name = shift;
my $remote_url = shift;
my ($wiki_login, $wiki_password, $wiki_domain);
$wiki_login = Git::config("remote.${remote_name}.mwLogin");
$wiki_password = Git::config("remote.${remote_name}.mwPassword");
$wiki_domain = Git::config("remote.${remote_name}.mwDomain");
$wiki = MediaWiki::API->new;
$wiki->{config}->{api_url} = "${remote_url}/api.php";
if ($wiki_login) {
my %credential = (
'url' => $remote_url,
'username' => $wiki_login,
'password' => $wiki_password
);
Git::credential(\%credential);
my $request = {lgname => $credential{username},
lgpassword => $credential{password},
lgdomain => $wiki_domain};
if ($wiki->login($request)) {
Git::credential(\%credential, 'approve');
print {*STDERR} qq(Logged in mediawiki user "$credential{username}".\n);
} else {
print {*STDERR} qq(Failed to log in mediawiki user "$credential{username}" on ${remote_url}\n);
print {*STDERR} ' (error ' .
$wiki->{error}->{code} . ': ' .
$wiki->{error}->{details} . ")\n";
Git::credential(\%credential, 'reject');
exit 1;
}
}
return $wiki;
}
1; # Famous last words