git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/5] git-remote-mediawiki: split get_mw_pages into smaller functions

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Jun 27, 2012, 09:10 UTC
Message-ID
<1340788220-10084-5-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1340788220-10084-1-git-send-email-Matthieu.Moy@imag.fr>
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 contrib/mw-to-git/git-remote-mediawiki | 106 +++++++++++++++++++--------------
 1 file changed, 62 insertions(+), 44 deletions(-)
diff --git a/contrib/mw-to-git/git-remote-mediawiki b/contrib/mw-to-git/git-remote-mediawiki
index 3405772..76d8824 100755
--- a/contrib/mw-to-git/git-remote-mediawiki
+++ b/contrib/mw-to-git/git-remote-mediawiki
@@ -258,6 +258,64 @@ sub mw_connect_maybe {
 	}
 }
 
+## Functions for listing pages on the remote wiki
+sub get_mw_tracked_pages {
+	my $pages = shift;
+	my @some_pages = @tracked_pages;
+	while (@some_pages) {
+		my $last = 50;
+		if ($#some_pages < $last) {
+			$last = $#some_pages;
+		}
+		my @slice = @some_pages[0..$last];
+		get_mw_first_pages(\@slice, $pages);
+		@some_pages = @some_pages[51..$#some_pages];
+	}
+}
+
+sub get_mw_tracked_categories {
+	my $pages = shift;
+	foreach my $category (@tracked_categories) {
+		if (index($category, ':') < 0) {
+			# Mediawiki requires the Category
+			# prefix, but let's not force the user
+			# to specify it.
+			$category = "Category:" . $category;
+		}
+		my $mw_pages = $mediawiki->list( {
+			action => 'query',
+			list => 'categorymembers',
+			cmtitle => $category,
+			cmlimit => 'max' } )
+			|| die $mediawiki->{error}->{code} . ': '
+				. $mediawiki->{error}->{details};
+		foreach my $page (@{$mw_pages}) {
+			$pages->{$page->{title}} = $page;
+		}
+	}
+}
+
+sub get_mw_all_pages {
+	my $pages = shift;
+	# No user-provided list, get the list of pages from the API.
+	my $mw_pages = $mediawiki->list({
+		action => 'query',
+		list => 'allpages',
+		aplimit => 'max'
+	});
+	if (!defined($mw_pages)) {
+		print STDERR "fatal: could not get the list of wiki pages.\n";
+		print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
+		print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
+		exit 1;
+	}
+	foreach my $page (@{$mw_pages}) {
+		$pages->{$page->{title}} = $page;
+	}
+}
+
+# queries the wiki for a set of pages. Meant to be used within a loop
+# querying the wiki for slices of page list.
 sub get_mw_first_pages {
 	my $some_pages = shift;
 	my @some_pages = @{$some_pages};
@@ -286,6 +344,7 @@ sub get_mw_first_pages {
 	}
 }
 
+# Get the list of pages to be fetched according to configuration.
 sub get_mw_pages {
 	mw_connect_maybe();
 
@@ -295,55 +354,14 @@ sub get_mw_pages {
 		$user_defined = 1;
 		# The user provided a list of pages titles, but we
 		# still need to query the API to get the page IDs.
-
-		my @some_pages = @tracked_pages;
-		while (@some_pages) {
-			my $last = 50;
-			if ($#some_pages < $last) {
-				$last = $#some_pages;
-			}
-			my @slice = @some_pages[0..$last];
-			get_mw_first_pages(\@slice, \%pages);
-			@some_pages = @some_pages[51..$#some_pages];
-		}
+		get_mw_tracked_pages(\%pages);
 	}
 	if (@tracked_categories) {
 		$user_defined = 1;
-		foreach my $category (@tracked_categories) {
-			if (index($category, ':') < 0) {
-				# Mediawiki requires the Category
-				# prefix, but let's not force the user
-				# to specify it.
-				$category = "Category:" . $category;
-			}
-			my $mw_pages = $mediawiki->list( {
-				action => 'query',
-				list => 'categorymembers',
-				cmtitle => $category,
-				cmlimit => 'max' } )
-			    || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};
-			foreach my $page (@{$mw_pages}) {
-				$pages{$page->{title}} = $page;
-			}
-		}
+		get_mw_tracked_categories(\%pages);
 	}
 	if (!$user_defined) {
-		# No user-provided list, get the list of pages from
-		# the API.
-		my $mw_pages = $mediawiki->list({
-			action => 'query',
-			list => 'allpages',
-			aplimit => 500,
-		});
-		if (!defined($mw_pages)) {
-			print STDERR "fatal: could not get the list of wiki pages.\n";
-			print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
-			print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
-			exit 1;
-		}
-		foreach my $page (@{$mw_pages}) {
-			$pages{$page->{title}} = $page;
-		}
+		get_mw_all_pages(\%pages);
 	}
 	return values(%pages);
 }
-- 
1.7.11.5.g0c7e058.dirty
Previous: Matthieu MoyNext: Matthieu Moy
Message 13 of 18 in “git-remote-mediawiki: support File: import and export”
  1. 0/5 git-remote-mediawiki: support File: import and exportMatthieu Moy, Jun 26, 2012
  2. 1/5 git-remote-mediawiki: don't compute the diff when getting commit messageMatthieu Moy, Jun 26, 2012
  3. Junio C HamanoJun 26, 2012
  4. Matthieu MoyJun 27, 2012
  5. 2/5 git-remote-mediawiki: don't "use encoding 'utf8';"Matthieu Moy, Jun 26, 2012
  6. 3/5 git-remote-mediawiki: send "File:" attachments to a remote wikiMatthieu Moy, Jun 26, 2012
  7. 4/5 git-remote-mediawiki: split get_mw_pages into smaller functionsMatthieu Moy, Jun 26, 2012
  8. 5/5 git-remote-mediawiki: import "File:" attachmentsMatthieu Moy, Jun 26, 2012
  9. 0/5 git-remote-mediawiki: support File: import and exportMatthieu Moy, Jun 27, 2012
  10. 1/5 git-remote-mediawiki: don't compute the diff when getting commit messageMatthieu Moy, Jun 27, 2012
  11. 2/5 git-remote-mediawiki: don't "use encoding 'utf8';"Matthieu Moy, Jun 27, 2012
  12. 3/5 git-remote-mediawiki: send "File:" attachments to a remote wikiMatthieu Moy, Jun 27, 2012
  13. 4/5 git-remote-mediawiki: split get_mw_pages into smaller functionsMatthieu Moy, Jun 27, 2012
  14. 5/5 git-remote-mediawiki: import "File:" attachmentsMatthieu Moy, Jun 27, 2012
  15. 5/5 git-remote-mediawiki: import "File:" attachmentsMatthieu Moy, Jun 27, 2012
  16. git-remote-mediawiki: import "File:" attachmentsMatthieu Moy, Jul 4, 2012
  17. Junio C HamanoJul 5, 2012
  18. Matthieu MoyJul 5, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.