AKModel.protocol_migration module

Migration of AK protocols from HedgeDoc pads to MediaWiki pages

For each AK, the protocol (referenced by protocol_link) is downloaded from HedgeDoc as markdown, converted to MediaWiki syntax using pandoc (bundled with pypandoc_binary) and written to the wiki page of the AK (referenced by link), but only if that wiki page is still empty (or does not exist yet).

Protocols may be stored on any HedgeDoc instance. Since protocol links are entered by users, downloads are restricted to public addresses (see fetch_public()). Which wikis are supported is configured in the settings, see PROTOCOL_MIGRATION_WIKIS and PROTOCOL_MIGRATION_WIKI_CREDENTIALS.

class AKModel.protocol_migration.MigrationStatus(*values)[source]

Bases: TextChoices

Possible outcomes of the migration of a single AK protocol (in the order they should be shown to the user)

class AKModel.protocol_migration.MigrationResult(ak: AK, status: MigrationStatus, detail: str = '')[source]

Bases: object

Result of the migration of a single AK protocol

property success: bool

Was the migration of this protocol successful?

__init__(ak: AK, status: MigrationStatus, detail: str = '') → None
exception AKModel.protocol_migration.MigrationError(status: MigrationStatus, detail: str = '')[source]

Bases: Exception

Raised when the migration of a single protocol cannot be completed

__init__(status: MigrationStatus, detail: str = '')[source]
AKModel.protocol_migration.get_hedgedoc_download_url(protocol_link: str) → str | None[source]

Determine the URL to download the markdown source of a HedgeDoc note

Parameters:

protocol_link – link to the note, e.g., https://md.example.com/abc123?both#heading

Returns:

download URL (e.g., https://md.example.com/abc123/download) or None if the link cannot point to a HedgeDoc note. Since the server downloads from these links (which are entered by users), only https links on the default port and without credentials are accepted

AKModel.protocol_migration.get_wiki_page_title(wiki_link: str) → tuple[dict, str] | None[source]

Determine the wiki and title of the page a wiki link points to

Parameters:

wiki_link – link to the page, e.g., https://wiki.example.com/wiki/Some_Page or https://wiki.example.com/w/index.php?title=Some_Page

Returns:

tuple of wiki configuration and page title or None if the link does not point to a page in one of the configured wikis

AKModel.protocol_migration.resolve_public_address(hostname: str) → str[source]

Resolve a hostname to an IP address, but only if all its addresses are publicly reachable

Protocol links are entered by users, hence downloads must not reach the server itself or internal networks

Raises:

MigrationError – if the hostname cannot be resolved or resolves to a non-public address

class AKModel.protocol_migration.PinnedAddressAdapter(hostname: str, **kwargs)[source]

Bases: HTTPAdapter

Transport adapter for connections to a fixed (previously checked) IP address that still uses the hostname for TLS (server name indication and certificate validation)

__init__(hostname: str, **kwargs)[source]
AKModel.protocol_migration.fetch_public(url: str) → Response[source]

Send a GET request to an https URL (without following redirects), but only if its host is publicly reachable

The connection is made to exactly the checked address (instead of resolving the hostname again), so the check cannot be circumvented by DNS answers changing in between (DNS rebinding). The response is streamed, i.e., the caller has to read its content and close it

Raises:

MigrationError – if the host is not publicly reachable or the request fails

AKModel.protocol_migration.download_protocol(download_url: str) → str[source]

Download the markdown source of a HedgeDoc note

Raises:

MigrationError – if the note cannot be downloaded (e.g., because it does not exist or is not public) or the response does not look like the markdown source of a note

AKModel.protocol_migration.convert_to_mediawiki(markdown: str) → str[source]

Convert (GitHub flavored) markdown to MediaWiki syntax using pandoc

Raises:

MigrationError – if pandoc is not available or the conversion fails

class AKModel.protocol_migration.MediaWikiClient(wiki: dict, username: str = '', password: str = '')[source]

Bases: object

Minimal client for the MediaWiki action API

Authentication uses bot passwords (see Special:BotPasswords in the wiki), the login is performed lazily on the first request (reading may require it, too). Logins are stored in the cache to reuse them across requests (see restore_session()), expired logins are detected and renewed automatically.

__init__(wiki: dict, username: str = '', password: str = '')[source]
restore_session()[source]

Reuse the login of a previous request (if still cached) instead of logging in again

login()[source]

Log in (if not already done) and retrieve CSRF token needed for editing

The resulting session is stored in the cache, see restore_session()

get_page(title: str) → tuple[bool, str, str | None, str][source]

Retrieve the current state of a page

Returns:

tuple of (page exists?, current content, timestamp of current revision, timestamp of this request (needed to detect deletions of the page before writing))

Raises:

MigrationError – if the title is invalid or does not refer to a regular page, or the wiki cannot be reached or returns an unexpected response

write_page(title: str, content: str, summary: str, *, exists: bool, base_timestamp: str | None, start_timestamp: str | None)[source]

Write the given content to a page that is currently empty or missing

Edit conflicts (i.e., someone else creating, filling or deleting the page in the meantime) are detected by MediaWiki and reported as errors instead of overwriting the page

AKModel.protocol_migration.migrate_protocol(ak: AK, wiki_clients: dict[str, MediaWikiClient]) → MigrationResult[source]

Migrate the protocol of a single AK from HedgeDoc to MediaWiki

Parameters:
  • ak – AK to migrate the protocol for

  • wiki_clients – cache of wiki clients (by API URL) to reuse logins across multiple AKs

Returns:

result of the migration

AKModel.protocol_migration.migrate_protocols(aks) → list[MigrationResult][source]

Migrate the protocols of the given AKs from HedgeDoc to MediaWiki

Parameters:

aks – AKs to migrate the protocols for

Returns:

list of results (one per AK)