Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Numinix MediaWiki Demo
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Manual/Importing XML dumps
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== On Python ==== <!--T:161--> </translate> <syntaxhighlight lang="python"> from pathlib import Path from lxml import etree import re from pywikibot import Link, Site """ The exported XML preserves local namespace names. To import into a project in another language, you need to convert them to canonical names. Otherwise the pages will be imported as originals without recognizing their namespace. XML contains namespace numbers, but mapping them can be problematic. Because they can differ between projects. For example, in ru.wikisource NS Page is number 104, while in uk.wikisource it is 252. To solve these, the Link() class from pywikibot is used. The Link() required the language code and family of project. XML does not contain these as separate values. Instead, they are in another format, like `enwikisource` or 'commonswiki', which needs to be parsed. """ xml_folder = 'PATH_TO_XML_DUMP' for xml_file in Path(xml_folder).glob('*.xml'): root = etree.parse(xml_file).getroot() ns = {'': root.nsmap[None]} # xml namespace # Parsing language code and family of the project dbname = root.find('.//siteinfo/dbname', ns).text m = re.search(r'^(\w+?)(wiki|wikisource|wikiquote|wikinews|wikibooks)$', dbname) if m: site = Site(code=m.group(1), fam=m.group(2)) elif 'commons' in dbname: site = Site('commons') elif 'mediawikiwiki' in dbname: site = Site('mediawiki') else: print("Site didn't recognized.") break wiki = [] for page in root.findall('.//page', ns): title_source = page.find('title', ns).text title = Link(title_source, site).ns_title().replace(' ', '_') revision_id = page.find('.//revision/id', ns).text print(f'{title}, revision id: {revision_id}') text = page.find('.//revision/text', ns).text wiki.append("\n'''%s'''\n%s\n" % (title, text)) Path(f'out_{xml_file.stem}.wiki').write_text('\n'.join(wiki)) </syntaxhighlight> <translate>
Summary:
Please note that all contributions to Numinix MediaWiki Demo may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Project:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)