Skip to content

Commit

Permalink
import19 lacks support for UserGroup and WikiDict conversion; fixes #…
Browse files Browse the repository at this point in the history
…1595

Added 2 macros to display usergroup and wikidict attributes.
  • Loading branch information
RogerHaase committed Feb 21, 2024
1 parent 149ea0f commit 87fba6b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/moin/cli/migration/moin19/import19.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ def ImportMoin19(data_dir=None, markup_out=None, namespace=None):
# bumping modified time makes global and item history views more useful
meta[MTIME] = meta[MTIME] + 1
meta[COMMENT] = 'Converted moin 1.9 markup to ' + markup_out + ' markup'
if meta[NAME][0].endswith('Group') and meta[USERGROUP]:
msg = 'Moin1.x user list moved to User Group metadata; item content ignored. Use ShowUserGroup macro. '
meta[COMMENT] = msg + meta[COMMENT]
if meta[NAME][0].endswith('Dict') and meta[WIKIDICT]:
msg = 'Moin1.x Wiki Dict data moved to Wiki Dict metadata; item content ignored. Use ShowWikiDict macro. '
meta[COMMENT] = msg + meta[COMMENT]
meta[CONTENTTYPE] = CONTENTTYPE_MARKUP_OUT[markup_out]
del meta[DATAID]
out.seek(0)
Expand Down Expand Up @@ -457,6 +463,11 @@ def __init__(self, item, revno, path, target_namespace):
meta[TAGS].append(TEMPLATE)
else:
meta[TAGS] = [TEMPLATE]
if meta[NAME][0].endswith('Group'):
meta[USERGROUP] = self._parse_acl_list(content)
if meta[NAME][0].endswith('Dict'):
meta[WIKIDICT] = self._parse_wikidict(content)

# if this revision matches a custom namespace defined in wikiconfig,
# then modify the meta data for namespace and name
for custom_namespace in custom_namespaces:
Expand Down Expand Up @@ -514,6 +525,48 @@ def _process_data(self, meta, data):
data = process_categories(meta, data, self.backend.item_category_regex)
return data

def _parse_acl_list(self, content):
"""
Return ACL list extracted from item's content
"""
ret = []
first_user = True
start = 'not a hit yet'
lines = content.splitlines()
for line in lines:
if first_user:
parts = line.split('*')
if len(parts) == 2 and parts[1].startswith(' '):
# only select lines with consistent indentation
start = parts[0] + '* '
first_user = False
if line.startswith(start):
parts = line.split('*')
if len(parts) == 2 and parts[1].startswith(' '):
username = parts[1].strip()
# link conversion may have enclosed all links with [[...]], maybe a leading +/-: "+[[UserName]]"
if username.startswith('[[') and username.endswith(']]'):
ret.append(username[2:-2])
elif username.startswith('+[[') or username.startswith('-[[') and username.endswith(']]'):
username.replace('[', '')
ret.append(username[:-2])
else:
ret.append(username)
return ret

def _parse_wikidict(self, content):
"""
Return a dict of key, value pairs: {key: val,...}
"""
ret = {}
lines = content.splitlines()
for line in lines:
line = line.strip()
parts = line.split(':: ')
if len(parts) == 2:
ret[parts[0]] = parts[1]
return ret


def migrate_itemlinks(dom, namespace, itemlinks2chg):
""" Walk the DOM tree and change itemlinks to users namespace
Expand Down
22 changes: 22 additions & 0 deletions src/moin/macros/ShowUserGroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright: 2024 MoinMoin:RogerHaase
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Show contents of UserGroup attribute in metadata.
"""

from moin.macros._base import MacroBlockBase, fail_message
from moin.items import Item
from moin.constants.keys import USERGROUP
from moin.i18n import _


class Macro(MacroBlockBase):
def macro(self, content, arguments, page_url, alternative):
url = str(page_url.path)[1:]
try:
item = Item.create(url)
return item.meta[USERGROUP]
except KeyError:
msg = _('ShowUserGroup macro failed - metadata lacks "usergroup" attribute.')
return fail_message(msg)
22 changes: 22 additions & 0 deletions src/moin/macros/ShowWikiDict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright: 2024 MoinMoin:RogerHaase
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Show contents of WikiDict attribute in metadata.
"""

from moin.macros._base import MacroBlockBase, fail_message
from moin.items import Item
from moin.constants.keys import WIKIDICT
from moin.i18n import _


class Macro(MacroBlockBase):
def macro(self, content, arguments, page_url, alternative):
url = str(page_url.path)[1:]
try:
item = Item.create(url)
return item.meta[WIKIDICT]
except KeyError:
msg = _('ShowWikiDict macro failed - metadata lacks "wikidict" attribute.')
return fail_message(msg)
10 changes: 10 additions & 0 deletions src/moin/macros/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
from moin.utils import iri
from moin.items import Item
from moin.utils.tree import html
from moin.i18n import _
from werkzeug.exceptions import abort
from moin.utils.tree import moin_page, xlink
Expand Down Expand Up @@ -61,6 +62,15 @@ def get_item_names(name='', startswith='', kind='files', skiptag=''):
return item_names


def fail_message(msg, severity='error'):
"""
Return an error message in admonition-like format.
Severity may be: attention, caution, danger, error, hint, important, note, or tip.
"""
return html.div(attrib={html.class_: '{0} moin-nowiki'.format(severity)}, children=msg)


class MacroBase:
"""
Macro base class.
Expand Down

0 comments on commit 87fba6b

Please sign in to comment.