{"thread":{"id":"30808","subject":"[PATCHv4 1/2] git-remote-mediawiki: import \"File:\" attachments","startedAt":"2012-06-14T12:26:37Z","lastAt":"2012-06-14T17:47:58Z","messageCount":3,"participants":["Pavel Volek","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"193648","messageId":"1339676798-6286-1-git-send-email-Pavel.Volek@ensimag.imag.fr","threadId":"30808","inReplyTo":null,"subject":"[PATCHv4 1/2] git-remote-mediawiki: import \"File:\" attachments","fromName":"Pavel Volek","fromEmail":"pavel.volek@ensimag.imag.fr","sentAt":"2012-06-14T12:26:37Z","receivedAt":"2012-06-14T12:26:37Z","isPatch":false,"sender":{"key":"pavel.volek@ensimag.imag.fr","avatar":null},"body":"The current version of the git-remote-mediawiki supports only import and export\nof the pages, doesn't support import and export of file attachments which are\nalso exposed by MediaWiki API. This patch adds the functionality to import file\nattachments and description pages for these files.\n\nSigned-off-by: Pavel Volek <Pavel.Volek@ensimag.imag.fr>\nSigned-off-by: NGUYEN Kim Thuat <Kim-Thuat.Nguyen@ensimag.imag.fr>\nSigned-off-by: ROUCHER IGLESIAS Javier <roucherj@ensimag.imag.fr>\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n contrib/mw-to-git/git-remote-mediawiki | 299 +++++++++++++++++++++++++++++----\n 1 file changed, 266 insertions(+), 33 deletions(-)\n\ndiff --git a/contrib/mw-to-git/git-remote-mediawiki b/contrib/mw-to-git/git-remote-mediawiki\nindex c18bfa1..a51e9ab 100755\n--- a/contrib/mw-to-git/git-remote-mediawiki\n+++ b/contrib/mw-to-git/git-remote-mediawiki\n@@ -13,9 +13,6 @@\n #\n # Known limitations:\n #\n-# - Only wiki pages are managed, no support for [[File:...]]\n-#   attachments.\n-#\n # - Poor performance in the best case: it takes forever to check\n #   whether we're up-to-date (on fetch or push) or to fetch a few\n #   revisions from a large wiki, because we use exclusively a\n@@ -71,6 +68,9 @@ chomp(@tracked_pages);\n my @tracked_categories = split(/[ \\n]/, run_git(\"config --get-all remote.\". $remotename .\".categories\"));\n chomp(@tracked_categories);\n \n+# Import media files too.\n+my $import_media = run_git(\"config --get --bool remote.\". $remotename .\".mediaimport\");\n+\n my $wiki_login = run_git(\"config --get remote.\". $remotename .\".mwLogin\");\n # TODO: ideally, this should be able to read from keyboard, but we're\n # inside a remote helper, so our stdin is connect to git, not to a\n@@ -86,6 +86,9 @@ my $shallow_import = run_git(\"config --get --bool remote.\". $remotename .\".shall\n chomp($shallow_import);\n $shallow_import = ($shallow_import eq \"true\");\n \n+# Cache for MediaWiki namespace ids.\n+my %namespace_id;\n+\n # Dumb push: don't update notes and mediawiki ref to reflect the last push.\n #\n # Configurable with mediawiki.dumbPush, or per-remote with\n@@ -183,25 +186,34 @@ sub get_mw_first_pages {\n \n \tmy $pages = shift;\n \n-\t# pattern 'page1|page2|...' required by the API\n-\tmy $titles = join('|', @some_pages);\n+\tmy $batch = 50;\n+\twhile (@some_pages) {\n+\t\tif ($#some_pages < $batch) {\n+\t\t\t$batch = $#some_pages;\n+\t\t}\n+\t\tmy @slice = @some_pages[0..$batch];\n \n-\tmy $mw_pages = $mediawiki->api({\n-\t\taction => 'query',\n-\t\ttitles => $titles,\n-\t});\n-\tif (!defined($mw_pages)) {\n-\t\tprint STDERR \"fatal: could not query the list of wiki pages.\\n\";\n-\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n-\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n-\t\texit 1;\n-\t}\n-\twhile (my ($id, $page) = each(%{$mw_pages->{query}->{pages}})) {\n-\t\tif ($id < 0) {\n-\t\t\tprint STDERR \"Warning: page $page->{title} not found on wiki\\n\";\n-\t\t} else {\n-\t\t\t$pages->{$page->{title}} = $page;\n+\t\t# pattern 'page1|page2|...' required by the API\n+\t\tmy $titles = join('|', @slice);\n+\n+\t\tmy $mw_pages = $mediawiki->api({\n+\t\t\taction => 'query',\n+\t\t\ttitles => $titles,\n+\t\t});\n+\t\tif (!defined($mw_pages)) {\n+\t\t\tprint STDERR \"fatal: could not query the list of wiki pages.\\n\";\n+\t\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n+\t\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n+\t\t\texit 1;\n+\t\t}\n+\t\twhile (my ($id, $page) = each(%{$mw_pages->{query}->{pages}})) {\n+\t\t\tif ($id < 0) {\n+\t\t\t\tprint STDERR \"Warning: page $page->{title} not found on wiki\\n\";\n+\t\t\t} else {\n+\t\t\t\t$pages->{$page->{title}} = $page;\n+\t\t\t}\n \t\t}\n+\t\t@some_pages = @some_pages[($batch+1)..$#some_pages];\n \t}\n }\n \n@@ -214,16 +226,10 @@ sub get_mw_pages {\n \t\t$user_defined = 1;\n \t\t# The user provided a list of pages titles, but we\n \t\t# still need to query the API to get the page IDs.\n+\t\tget_mw_first_pages(\\@tracked_pages, \\%pages);\n \n-\t\tmy @some_pages = @tracked_pages;\n-\t\twhile (@some_pages) {\n-\t\t\tmy $last = 50;\n-\t\t\tif ($#some_pages < $last) {\n-\t\t\t\t$last = $#some_pages;\n-\t\t\t}\n-\t\t\tmy @slice = @some_pages[0..$last];\n-\t\t\tget_mw_first_pages(\\@slice, \\%pages);\n-\t\t\t@some_pages = @some_pages[51..$#some_pages];\n+\t\tif ($import_media) {\n+\t\t\tget_mw_pages_for_linked_mediafiles(\\@tracked_pages, \\%pages);\n \t\t}\n \t}\n \tif (@tracked_categories) {\n@@ -240,10 +246,16 @@ sub get_mw_pages {\n \t\t\t\tlist => 'categorymembers',\n \t\t\t\tcmtitle => $category,\n \t\t\t\tcmlimit => 'max' } )\n-\t\t\t    || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};\n+\t\t\t\t|| die $mediawiki->{error}->{code} . ': '\n+\t\t\t\t\t. $mediawiki->{error}->{details};\n \t\t\tforeach my $page (@{$mw_pages}) {\n \t\t\t\t$pages{$page->{title}} = $page;\n \t\t\t}\n+\n+\t\t\tif ($import_media) {\n+\t\t\t\tmy @titles = map $_->{title}, @{$mw_pages};\n+\t\t\t\tget_mw_pages_for_linked_mediafiles(\\@titles, \\%pages);\n+\t\t\t}\n \t\t}\n \t}\n \tif (!$user_defined) {\n@@ -252,7 +264,7 @@ sub get_mw_pages {\n \t\tmy $mw_pages = $mediawiki->list({\n \t\t\taction => 'query',\n \t\t\tlist => 'allpages',\n-\t\t\taplimit => 500,\n+\t\t\taplimit => 'max'\n \t\t});\n \t\tif (!defined($mw_pages)) {\n \t\t\tprint STDERR \"fatal: could not get the list of wiki pages.\\n\";\n@@ -263,10 +275,203 @@ sub get_mw_pages {\n \t\tforeach my $page (@{$mw_pages}) {\n \t\t\t$pages{$page->{title}} = $page;\n \t\t}\n+\n+\t\tif ($import_media) {\n+\t\t\t# Attach list of all pages for media files from the API,\n+\t\t\t# they are in a different namespace, only one namespace\n+\t\t\t# can be queried at the same moment\n+\t\t\tmy $mw_pages = $mediawiki->list({\n+\t\t\t\taction => 'query',\n+\t\t\t\tlist => 'allpages',\n+\t\t\t\tapnamespace => get_mw_namespace_id(\"File\"),\n+\t\t\t\taplimit => 'max'\n+\t\t\t});\n+\t\t\tif (!defined($mw_pages)) {\n+\t\t\t\tprint STDERR \"fatal: could not get the list of pages for media files.\\n\";\n+\t\t\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n+\t\t\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n+\t\t\t\texit 1;\n+\t\t\t}\n+\t\t\tforeach my $page (@{$mw_pages}) {\n+\t\t\t\t$pages{$page->{title}} = $page;\n+\t\t\t}\n+\t\t}\n \t}\n \treturn values(%pages);\n }\n \n+sub get_mw_pages_for_linked_mediafiles {\n+\tmy $titles = shift;\n+\tmy @titles = @{$titles};\n+\tmy $pages = shift;\n+\n+\t# The query is split in small batches because of the MW API limit of\n+\t# the number of links to be returned (500 links max).\n+\tmy $batch = 10;\n+\twhile (@titles) {\n+\t\tif ($#titles < $batch) {\n+\t\t\t$batch = $#titles;\n+\t\t}\n+\t\tmy @slice = @titles[0..$batch];\n+\n+\t\t# pattern 'page1|page2|...' required by the API\n+\t\tmy $mw_titles = join('|', @slice);\n+\n+\t\t# Media files could be included or linked from\n+\t\t# a page, get all related\n+\t\tmy $query = {\n+\t\t\taction => 'query',\n+\t\t\tprop => 'links|images',\n+\t\t\ttitles => $mw_titles,\n+\t\t\tplnamespace => get_mw_namespace_id(\"File\"),\n+\t\t\tpllimit => 'max'\n+\t\t};\n+\t\tmy $result = $mediawiki->api($query);\n+\n+\t\twhile (my ($id, $page) = each(%{$result->{query}->{pages}})) {\n+\t\t\tmy @titles;\n+\t\t\tif (defined($page->{links})) {\n+\t\t\t\tmy @link_titles = map $_->{title}, @{$page->{links}};\n+\t\t\t\tpush(@titles, @link_titles);\n+\t\t\t}\n+\t\t\tif (defined($page->{images})) {\n+\t\t\t\tmy @image_titles = map $_->{title}, @{$page->{images}};\n+\t\t\t\tpush(@titles, @image_titles);\n+\t\t\t}\n+\t\t\tif (@titles) {\n+\t\t\t\tget_mw_first_pages(\\@titles, \\%{$pages});\n+\t\t\t}\n+\t\t}\n+\n+\t\t@titles = @titles[($batch+1)..$#titles];\n+\t}\n+}\n+\n+# Return MediaWiki id for a canonical namespace name.\n+# Ex.: \"File\", \"Project\".\n+# Looks for the namespace id in the local configuration\n+# variables, if it is not found asks MW API.\n+sub get_mw_namespace_id {\n+\tmw_connect_maybe();\n+\tmy $name = shift;\n+\n+\tif (!exists $namespace_id{$name}) {\n+\t\t# Look at configuration file, if the record for that namespace is\n+\t\t# already stored. Namespaces are stored in form:\n+\t\t# \"Name_of_namespace:Id_namespace\", ex.: \"File:6\".\n+\t\tmy @temp = split(/[ \\n]/, run_git(\"config --get-all remote.\"\n+\t\t\t\t\t\t. $remotename .\".namespaces\"));\n+\t\tchomp(@temp);\n+\t\tforeach my $ns (@temp) {\n+\t\t\tmy ($n, $s) = split(/:/, $ns);\n+\t\t\t$namespace_id{$n} = $s;\n+\t\t}\n+\t}\n+\n+\tif (!exists $namespace_id{$name}) {\n+\t\t# NS not found => get namespace id from MW and store it in\n+\t        # configuration file.\n+\t        my $query = {\n+\t                action => 'query',\n+\t                meta => 'siteinfo',\n+\t                siprop => 'namespaces'\n+\t        };\n+\t        my $result = $mediawiki->api($query);\n+\n+\t        while (my ($id, $ns) = each(%{$result->{query}->{namespaces}})) {\n+\t                if (defined($ns->{canonical}) && ($ns->{canonical} eq $name)) {\n+\t                        run_git(\"config --add remote.\". $remotename\n+\t\t\t\t\t.\".namespaces \". $name .\":\". $ns->{id});\n+\t\t\t\t$namespace_id{$name} = $ns->{id};\n+\t                }\n+\t        }\n+\t}\n+\n+\tif (exists $namespace_id{$name}) {\n+\t\treturn $namespace_id{$name};\n+\t} else {\n+\t\tdie \"No such namespace $name on MediaWiki.\";\n+\t}\n+}\n+\n+sub get_mw_mediafile_for_page_revision {\n+\t# Name of the file on Wiki, with the prefix.\n+\tmy $mw_filename = shift;\n+\tmy $timestamp = shift;\n+\tmy %mediafile;\n+\n+\t# Search if on MediaWiki exists a media file with given\n+\t# timestamp. In that case download the file.\n+\tmy $query = {\n+\t\taction => 'query',\n+\t\tprop => 'imageinfo',\n+\t\ttitles => $mw_filename,\n+\t\tiistart => $timestamp,\n+\t\tiiend => $timestamp,\n+\t\tiiprop => 'timestamp|archivename|url',\n+\t\tiilimit => 1\n+\t};\n+\tmy $result = $mediawiki->api($query);\n+\n+\tmy ($fileid, $file) = each ( %{$result->{query}->{pages}} );\n+\t# If not defined it means there is no revision of the file for\n+\t# given timestamp.\n+\tif (defined($file->{imageinfo})) {\n+\t\t# Get real name of media file.\n+\t\tmy $filename;\n+\t\tif (index($mw_filename, 'File:') == 0) {\n+\t\t\t$filename = substr $mw_filename, 5;\n+\t\t} else {\n+\t\t\t$filename = substr $mw_filename, 6;\n+\t\t}\n+\t\t$mediafile{title} = $filename;\n+\n+\t\tmy $fileinfo = pop(@{$file->{imageinfo}});\n+\t\t$mediafile{timestamp} = $fileinfo->{timestamp};\n+\t\t# If this is an old version of the file, the file has to be\n+\t\t# obtained from the archive. Otherwise it can be downloaded\n+\t\t# by MediaWiki API download() function.\n+\t\tif (defined($fileinfo->{archivename})) {\n+\t\t\t$mediafile{content} = download_mw_mediafile_from_archive($fileinfo->{url});\n+\t\t} else {\n+\t\t\t$mediafile{content} = download_mw_mediafile($mw_filename);\n+\t\t}\n+\t}\n+\treturn %mediafile;\n+}\n+\n+sub download_mw_mediafile_from_archive {\n+\tmy $url = shift;\n+\tmy $file;\n+\n+\tmy $ua = LWP::UserAgent->new;\n+\tmy $response = $ua->get($url);\n+\tif ($response->code) {\n+\t\t$file = $response->decoded_content;\n+\t} else {\n+\t\tprint STDERR \"Error downloading a file from archive.\\n\";\n+\t}\n+\n+\treturn $file;\n+}\n+\n+sub download_mw_mediafile {\n+\tmy $filename = shift;\n+\n+\t$mediawiki->{config}->{files_url} = $url;\n+\n+\tmy $file_content = $mediawiki->download( { title => $filename } );\n+\tif (!defined($file_content)) {\n+\t\tprint STDERR \"\\tFile \\'$filename\\' could not be downloaded.\\n\";\n+\t\texit 1;\n+\t} elsif ($file_content eq \"\") {\n+\t\tprint STDERR \"\\tFile \\'$filename\\' does not exist on the wiki.\\n\";\n+\t\texit 1;\n+\t} else {\n+\t\treturn $file_content;\n+\t}\n+}\n+\n sub run_git {\n \topen(my $git, \"-|:encoding(UTF-8)\", \"git \" . $_[0]);\n \tmy $res = do { local $/; <$git> };\n@@ -466,6 +671,13 @@ sub import_file_revision {\n \tmy %commit = %{$commit};\n \tmy $full_import = shift;\n \tmy $n = shift;\n+\tmy $mediafile_import = shift;\n+\tmy $mediafile;\n+\tmy %mediafile;\n+\tif ($mediafile_import) {\n+\t\t$mediafile = shift;\n+\t\t%mediafile = %{$mediafile};\n+\t}\n \n \tmy $title = $commit{title};\n \tmy $comment = $commit{comment};\n@@ -485,6 +697,10 @@ sub import_file_revision {\n \tif ($content ne DELETED_CONTENT) {\n \t\tprint STDOUT \"M 644 inline $title.mw\\n\";\n \t\tliteral_data($content);\n+\t\tif ($mediafile_import) {\n+\t\t\tprint STDOUT \"M 644 inline $mediafile{title}\\n\";\n+\t\t\tliteral_data($mediafile{content});\n+\t\t}\n \t\tprint STDOUT \"\\n\\n\";\n \t} else {\n \t\tprint STDOUT \"D $title.mw\\n\";\n@@ -580,6 +796,7 @@ sub mw_import_ref {\n \n \t\t$n++;\n \n+\t\tmy $page_title = $result->{query}->{pages}->{$pagerevid->{pageid}}->{title};\n \t\tmy %commit;\n \t\t$commit{author} = $rev->{user} || 'Anonymous';\n \t\t$commit{comment} = $rev->{comment} || '*Empty MediaWiki Message*';\n@@ -596,9 +813,25 @@ sub mw_import_ref {\n \t\t}\n \t\t$commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp);\n \n-\t\tprint STDERR \"$n/\", scalar(@revisions), \": Revision #$pagerevid->{revid} of $commit{title}\\n\";\n+\t\t# Differentiates classic pages and media files.\n+\t\tmy @prefix = split (\":\", $page_title);\n \n-\t\timport_file_revision(\\%commit, ($fetch_from == 1), $n);\n+\t\tmy %mediafile;\n+\t\tif ($prefix[0] eq \"File\" || $prefix[0] eq \"Image\") {\n+\t\t\t# The name of the file is the same as the media page.\n+\t\t\tmy $filename = $page_title;\n+\t\t\t%mediafile = get_mw_mediafile_for_page_revision($filename, $rev->{timestamp});\n+\t\t}\n+\t\t# If this is a revision of the media page for new version\n+\t\t# of a file do one common commit for both file and media page.\n+\t\t# Else do commit only for that page.\n+\t\tprint STDERR \"$n/\", scalar(@revisions), \": Revision #$pagerevid->{revid} of $commit{title}\\n\";\n+\t\tif (%mediafile) {\n+\t\t\tprint STDERR \"\\tDownloading file $mediafile{title}, version $mediafile{timestamp}\\n\";\n+\t\t\timport_file_revision(\\%commit, ($fetch_from == 1), $n, 1, \\%mediafile);\n+\t\t} else {\n+\t\t\timport_file_revision(\\%commit, ($fetch_from == 1), $n, 0);\n+\t\t}\n \t}\n \n \tif ($fetch_from == 1 && $n == 0) {\n-- \n1.7.10.2.552.gaa3bb87\n"},{"id":"193649","messageId":"1339676798-6286-2-git-send-email-Pavel.Volek@ensimag.imag.fr","threadId":"30808","inReplyTo":"1339676798-6286-1-git-send-email-Pavel.Volek@ensimag.imag.fr","subject":"[PATCHv4 2/2] git-remote-mediawiki: refactoring get_mw_pages function","fromName":"Pavel Volek","fromEmail":"pavel.volek@ensimag.imag.fr","sentAt":"2012-06-14T12:26:38Z","receivedAt":"2012-06-14T12:26:38Z","isPatch":false,"sender":{"key":"pavel.volek@ensimag.imag.fr","avatar":null},"body":"Splits the code in the get_mw_pages function into three separate functions.\nOne for getting list of all pages and all file attachments, second for pages\nin category specified in configuration file and files related to these pages\nand the last function to get from MW a list of specified pages with related\nfile attachments.\n\nSigned-off-by: Pavel Volek <Pavel.Volek@ensimag.imag.fr>\nSigned-off-by: NGUYEN Kim Thuat <Kim-Thuat.Nguyen@ensimag.imag.fr>\nSigned-off-by: ROUCHER IGLESIAS Javier <roucherj@ensimag.imag.fr>\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n contrib/mw-to-git/git-remote-mediawiki | 120 ++++++++++++++++++---------------\n 1 file changed, 67 insertions(+), 53 deletions(-)\n\ndiff --git a/contrib/mw-to-git/git-remote-mediawiki b/contrib/mw-to-git/git-remote-mediawiki\nindex a51e9ab..866bd6f 100755\n--- a/contrib/mw-to-git/git-remote-mediawiki\n+++ b/contrib/mw-to-git/git-remote-mediawiki\n@@ -224,80 +224,94 @@ sub get_mw_pages {\n \tmy $user_defined;\n \tif (@tracked_pages) {\n \t\t$user_defined = 1;\n-\t\t# The user provided a list of pages titles, but we\n-\t\t# still need to query the API to get the page IDs.\n-\t\tget_mw_first_pages(\\@tracked_pages, \\%pages);\n-\n-\t\tif ($import_media) {\n-\t\t\tget_mw_pages_for_linked_mediafiles(\\@tracked_pages, \\%pages);\n-\t\t}\n+\t\tget_mw_tracked_pages(\\%pages);\n \t}\n \tif (@tracked_categories) {\n \t\t$user_defined = 1;\n-\t\tforeach my $category (@tracked_categories) {\n-\t\t\tif (index($category, ':') < 0) {\n-\t\t\t\t# Mediawiki requires the Category\n-\t\t\t\t# prefix, but let's not force the user\n-\t\t\t\t# to specify it.\n-\t\t\t\t$category = \"Category:\" . $category;\n-\t\t\t}\n-\t\t\tmy $mw_pages = $mediawiki->list( {\n-\t\t\t\taction => 'query',\n-\t\t\t\tlist => 'categorymembers',\n-\t\t\t\tcmtitle => $category,\n-\t\t\t\tcmlimit => 'max' } )\n-\t\t\t\t|| die $mediawiki->{error}->{code} . ': '\n-\t\t\t\t\t. $mediawiki->{error}->{details};\n-\t\t\tforeach my $page (@{$mw_pages}) {\n-\t\t\t\t$pages{$page->{title}} = $page;\n-\t\t\t}\n-\n-\t\t\tif ($import_media) {\n-\t\t\t\tmy @titles = map $_->{title}, @{$mw_pages};\n-\t\t\t\tget_mw_pages_for_linked_mediafiles(\\@titles, \\%pages);\n-\t\t\t}\n-\t\t}\n+\t\tget_mw_tracked_categories(\\%pages);\n \t}\n \tif (!$user_defined) {\n-\t\t# No user-provided list, get the list of pages from\n-\t\t# the API.\n+\t\tget_mw_all_pages(\\%pages);\n+\t}\n+\treturn values(%pages);\n+}\n+\n+sub get_mw_all_pages {\n+\tmy $pages = shift;\n+\t# No user-provided list, get the list of pages from the API.\n+\tmy $mw_pages = $mediawiki->list({\n+\t\taction => 'query',\n+\t\tlist => 'allpages',\n+\t\taplimit => 'max'\n+\t});\n+\tif (!defined($mw_pages)) {\n+\t\tprint STDERR \"fatal: could not get the list of wiki pages.\\n\";\n+\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n+\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n+\t\texit 1;\n+\t}\n+\tforeach my $page (@{$mw_pages}) {\n+\t\t$pages->{$page->{title}} = $page;\n+\t}\n+\n+\tif ($import_media) {\n+\t\t# Attach list of all pages for media files from the API,\n+\t\t# they are in a different namespace, only one namespace\n+\t\t# can be queried at the same moment\n \t\tmy $mw_pages = $mediawiki->list({\n \t\t\taction => 'query',\n \t\t\tlist => 'allpages',\n+\t\t\tapnamespace => get_mw_namespace_id(\"File\"),\n \t\t\taplimit => 'max'\n \t\t});\n \t\tif (!defined($mw_pages)) {\n-\t\t\tprint STDERR \"fatal: could not get the list of wiki pages.\\n\";\n+\t\t\tprint STDERR \"fatal: could not get the list of pages for media files.\\n\";\n \t\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n \t\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n \t\t\texit 1;\n \t\t}\n \t\tforeach my $page (@{$mw_pages}) {\n-\t\t\t$pages{$page->{title}} = $page;\n+\t\t\t$pages->{$page->{title}} = $page;\n+\t\t}\n+\t}\n+}\n+\n+sub get_mw_tracked_pages {\n+\tmy $pages = shift;\n+\t# The user provided a list of pages titles, but we\n+\t# still need to query the API to get the page IDs.\n+\tget_mw_first_pages(\\@tracked_pages, \\%{$pages});\n+\n+\tif ($import_media) {\n+\t\tget_mw_pages_for_linked_mediafiles(\\@tracked_pages, \\%{$pages});\n+\t}\n+}\n+\n+sub get_mw_tracked_categories {\n+\tmy $pages = shift;\n+\tforeach my $category (@tracked_categories) {\n+\t\tif (index($category, ':') < 0) {\n+\t\t\t# Mediawiki requires the Category\n+\t\t\t# prefix, but let's not force the user\n+\t\t\t# to specify it.\n+\t\t\t$category = \"Category:\" . $category;\n+\t\t}\n+\t\tmy $mw_pages = $mediawiki->list( {\n+\t\t\taction => 'query',\n+\t\t\tlist => 'categorymembers',\n+\t\t\tcmtitle => $category,\n+\t\t\tcmlimit => 'max' } )\n+\t\t\t|| die $mediawiki->{error}->{code} . ': '\n+\t\t\t\t. $mediawiki->{error}->{details};\n+\t\tforeach my $page (@{$mw_pages}) {\n+\t\t\t$pages->{$page->{title}} = $page;\n \t\t}\n \n \t\tif ($import_media) {\n-\t\t\t# Attach list of all pages for media files from the API,\n-\t\t\t# they are in a different namespace, only one namespace\n-\t\t\t# can be queried at the same moment\n-\t\t\tmy $mw_pages = $mediawiki->list({\n-\t\t\t\taction => 'query',\n-\t\t\t\tlist => 'allpages',\n-\t\t\t\tapnamespace => get_mw_namespace_id(\"File\"),\n-\t\t\t\taplimit => 'max'\n-\t\t\t});\n-\t\t\tif (!defined($mw_pages)) {\n-\t\t\t\tprint STDERR \"fatal: could not get the list of pages for media files.\\n\";\n-\t\t\t\tprint STDERR \"fatal: '$url' does not appear to be a mediawiki\\n\";\n-\t\t\t\tprint STDERR \"fatal: make sure '$url/api.php' is a valid page.\\n\";\n-\t\t\t\texit 1;\n-\t\t\t}\n-\t\t\tforeach my $page (@{$mw_pages}) {\n-\t\t\t\t$pages{$page->{title}} = $page;\n-\t\t\t}\n+\t\t\tmy @titles = map $_->{title}, @{$mw_pages};\n+\t\t\tget_mw_pages_for_linked_mediafiles(\\@titles, \\%{$pages});\n \t\t}\n \t}\n-\treturn values(%pages);\n }\n \n sub get_mw_pages_for_linked_mediafiles {\n-- \n1.7.10.2.552.gaa3bb87\n"},{"id":"193668","messageId":"7vhaudeqep.fsf@alter.siamese.dyndns.org","threadId":"30808","inReplyTo":"1339676798-6286-1-git-send-email-Pavel.Volek@ensimag.imag.fr","subject":"Re: [PATCHv4 1/2] git-remote-mediawiki: import \"File:\" attachments","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-06-14T17:47:58Z","receivedAt":"2012-06-14T17:47:58Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"It appears to me that 2/2 should come before 1/2 so that addition of\nattachment support will build on a cleaned up codebase.\n\nAlso a part of 1/2 that teaches the get_mw_first_pages function not\nto feed too many titles at once may be better done as a separate\npatch that is done before addition of attachment support.  There may\nbe other things 1/2 does that should be separated into a series of\npreparatory clean-up patches, but I didn't read it very carefully.\n"}]}