Skip to content

Commit

Permalink
Draft: Fix moin-nonexistent class in itemlinks starting with '+'
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichB22 committed Jun 26, 2024
1 parent 704e08c commit b0f9354
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/moin/constants/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright: 2011 MoinMoin:ThomasWaldmann
# Copyright: 2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand Down Expand Up @@ -69,3 +70,6 @@
NO_LOCK = 0 # false, someone else holds lock for current item
LOCKED = 1 # true, current user has obtained or renewed lock
LOCK = "lock"

# Valid views allowed for itemlinks
VALID_ITEMLINK_VIEWS = ["+meta", "+history", "+download", "+highlight"]
19 changes: 15 additions & 4 deletions src/moin/converters/link.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright: 2008 MoinMoin:BastianBlank
# Copyright: 2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -10,6 +11,7 @@

from flask import g as flaskg

from moin.constants.misc import VALID_ITEMLINK_VIEWS
from moin.utils.interwiki import is_known_wiki, url_for_item
from moin.utils.iri import Iri
from moin.utils.mime import type_moin_document
Expand Down Expand Up @@ -182,18 +184,27 @@ def handle_wiki_links(self, elem, input, to_tag=ConverterBase._tag_xlink_href):
elem.set(to_tag, link)

def handle_wikilocal_links(self, elem, input, page, to_tag=ConverterBase._tag_xlink_href):
view_name = ""
if input.path:
# this can be a relative path, make it absolute:
path = input.path
item_name = str(input.path)
# Remove view from item_name before searching
if item_name.startswith("+"):
view_name = item_name.split("/")[0]
if view_name in VALID_ITEMLINK_VIEWS:
item_name = item_name.split(f"{view_name}/")[1]
if page:
path = self.absolute_path(path, page.path)
item_name = str(path)
# this can be a relative path, make it absolute:
item_name = str(self.absolute_path(Iri(path=item_name).path, page.path))
if not flaskg.storage.has_item(item_name):
# XXX these index accesses slow down the link converter quite a bit
elem.set(moin_page.class_, "moin-nonexistent")
else:
item_name = str(page.path[1:]) if page else ""
endpoint, rev, query = self._get_do_rev(input.query)

if view_name == "+meta": # TODO: add other views
endpoint = "frontend.show_item_meta"

url = url_for_item(item_name, rev=rev, endpoint=endpoint)
if not page:
url = url[1:]
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
<li class="list-group-item">Item Links:&nbsp;
{%- if meta['itemlinks'] -%}
{%- for item in meta['itemlinks']|sort -%}
<a href="{{ url_for('frontend.show_item', item_name=item) }}" {% if not theme_supp.item_exists(item) %}class="moin-nonexistent"{% endif %}>{{ item }}</a>
<a href="{{ url_for('frontend.show_item', item_name=item) }}" {% if not theme_supp.itemlink_exists(item) %}class="moin-nonexistent"{% endif %}>{{ item }}</a>
{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{%- else -%}
Expand Down
17 changes: 16 additions & 1 deletion src/moin/themes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright: 2003-2010 MoinMoin:ThomasWaldmann
# Copyright: 2008 MoinMoin:RadomirDopieralski
# Copyright: 2010 MoinMoin:DiogenesAugusto
# Copyright: 2023 MoinMoin project
# Copyright: 2023-2024 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -26,6 +26,7 @@
from moin import wikiutil, user
from moin.constants.keys import USERID, ADDRESS, HOSTNAME, REVID, ITEMID, NAME_EXACT, ASSIGNED_TO, NAME, NAMESPACE
from moin.constants.contenttypes import CONTENTTYPES_MAP, CONTENTTYPE_MARKUP, CONTENTTYPE_TEXT, CONTENTTYPE_MOIN_19
from moin.constants.misc import VALID_ITEMLINK_VIEWS
from moin.constants.namespaces import NAMESPACE_DEFAULT, NAMESPACE_USERS, NAMESPACE_ALL
from moin.constants.rights import SUPERUSER
from moin.search import SearchForm
Expand Down Expand Up @@ -591,6 +592,20 @@ def item_exists(self, itemname):
"""
return self.storage.has_item(itemname)

def itemlink_exists(self, itemlink):
"""
Check whether the item pointed to by the given itemlink exists or not
:rtype: boolean
:returns: whether item pointed to by the link exists or not
"""
item_name = itemlink
if itemlink.startswith("+"):
view_name = itemlink.split("/")[0]
if view_name in VALID_ITEMLINK_VIEWS:
item_name = itemlink.split(f"{view_name}/")[1]
return self.storage.has_item(item_name)

def variables_css(self):
"""
Check whether this theme has a variables.css file
Expand Down

0 comments on commit b0f9354

Please sign in to comment.