Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johannesloetzsch/910155f3ba70b6582906 to your computer and use it in GitHub Desktop.
Save johannesloetzsch/910155f3ba70b6582906 to your computer and use it in GitHub Desktop.
patch for using namespaces with git-mediawiki
patch works on:
* https://github.com/git/git/raw/9742fb7e53b2f3bd85b5f01e563aee2cad7e77b8/contrib/mw-to-git/git-remote-mediawiki.perl
* https://packages.debian.org/jessie/git-mediawiki
source:
* https://gist.github.com/anarcat/f821fa285c6b8b6b16a5
improvement:
* allows to get everything from main-namespace + selected namespaces/categories/pages
usage:
* git clone mediawiki::http://example.org/mediawiki -c 'remote.origin.namespaces=MAIN Howto Faq Todo'
--- git-remote-mediawiki.orig
+++ git-remote-mediawiki
@@ -18,6 +18,7 @@
use Git::Mediawiki qw(clean_filename smudge_filename connect_maybe
EMPTY HTTP_CODE_OK);
use DateTime::Format::ISO8601;
+use Scalar::Util;
use warnings;
# By default, use UTF-8 to communicate with Git and the user
@@ -64,6 +65,16 @@
my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.${remotename}.categories"));
chomp(@tracked_categories);
+# Just like @tracked_categories, but for MediaWiki namespaces.
+# "MAIN" is removed from @tracked_namespaces, but will be set as $main_namespace
+my $tracked_namespaces_str = run_git("config --get-all remote.${remotename}.namespaces");
+$tracked_namespaces_str =~ s/ ?MAIN ?//g;
+my @tracked_namespaces = split(/[ \n]/, $tracked_namespaces_str);
+chomp(@tracked_namespaces);
+
+# $main_namespace allows to get everything from main-namespace + selected namespaces/categories/pages (ignore $user_defined)
+my $main_namespace = run_git("config --get-all remote.${remotename}.namespaces") =~ /(^|[ ])MAIN([ ]|$)/;
+
# Import media files on pull
my $import_media = run_git("config --get --bool remote.${remotename}.mediaimport");
chomp($import_media);
@@ -257,6 +268,23 @@
return;
}
+sub get_mw_tracked_namespaces {
+ my $pages = shift;
+ foreach my $local_namespace (@tracked_namespaces) {
+ my $mw_pages = $mediawiki->list( {
+ action => 'query',
+ list => 'allpages',
+ apnamespace => get_mw_namespace_id($local_namespace),
+ aplimit => 'max' } )
+ || die $mediawiki->{error}->{code} . ': '
+ . $mediawiki->{error}->{details} . "\n";
+ foreach my $page (@{$mw_pages}) {
+ $pages->{$page->{title}} = $page;
+ }
+ }
+ return;
+}
+
sub get_mw_all_pages {
my $pages = shift;
# No user-provided list, get the list of pages from the API.
@@ -320,7 +348,11 @@
$user_defined = 1;
get_mw_tracked_categories(\%pages);
}
- if (!$user_defined) {
+ if (@tracked_namespaces) {
+ $user_defined = 1;
+ get_mw_tracked_namespaces(\%pages);
+ }
+ if (!$user_defined || $main_namespace) {
get_mw_all_pages(\%pages);
}
if ($import_media) {
@@ -1264,7 +1296,6 @@
sub get_mw_namespace_id {
$mediawiki = connect_maybe($mediawiki, $remotename, $url);
my $name = shift;
-
if (!exists $namespace_id{$name}) {
# Look at configuration file, if the record for that namespace is
# already cached. Namespaces are stored in form:
@@ -1332,7 +1363,12 @@
sub get_mw_namespace_id_for_page {
my $namespace = shift;
if ($namespace =~ /^([^:]*):/) {
- return get_mw_namespace_id($namespace);
+ my ($ns, $id) = split(/:/, $namespace);
+ if (Scalar::Util::looks_like_number($id)) {
+ return get_mw_namespace_id($ns);
+ } else{
+ return
+ }
} else {
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment