Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions onebot/plugins/urlinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ def _process_url_mediawiki(
) -> Optional[list[str]]:
parsed = urlparse(url)
hostname = parsed.hostname

# Auto-detect Wikipedia URLs (any language edition)
if hostname and hostname.endswith(".wikipedia.org"):
return self._process_wikipedia(session, parsed)

if hostname not in self.mediawiki_sites:
return None

Expand Down Expand Up @@ -401,6 +406,21 @@ def _process_url_mediawiki(

return None

def _process_wikipedia(
self, session: requests.Session, parsed: ParseResult
) -> Optional[list[str]]:
"""Handle public Wikipedia URLs without requiring config."""
title = self._mediawiki_get_title(parsed)
if not title:
return None

api_url = f"https://{parsed.hostname}/w/api.php"
try:
return self._mediawiki_request_info(session, api_url, title)
except Exception as e:
self.log.error("Wikipedia error: %s", e)
return None

def _mediawiki_get_title(self, parsed_url: ParseResult) -> Optional[str]:
"""Extract MediaWiki page title from URL."""
title = None
Expand Down
42 changes: 39 additions & 3 deletions tests/test_plugin_urlinfo_mediawiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ def setUp(self):
"username": "BotUser",
"password": "BotPassword",
},
"en.wikipedia.org": {
"api_url": "https://en.wikipedia.org/w/api.php",
},
}
}
}
Expand Down Expand Up @@ -54,6 +51,45 @@ def test_wikipedia(self):
result = self.plugin._process_url(session, url)
self.assertEqual(result, expected)

@patch("socket.getaddrinfo")
def test_wikipedia_auto_detect_without_config(self, mock_getaddrinfo):
"""Wikipedia URLs work without explicit mediawiki_sites config."""
mock_getaddrinfo.return_value = [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("198.35.26.96", 443))
]
session = MagicMock()
url = "https://nl.wikipedia.org/wiki/Internet_Relay_Chat"

def side_effect(url, **kwargs):
params = kwargs.get("params", {})
if params.get("prop") == "extracts|info":
return MagicMock(
ok=True,
json=lambda: {
"query": {
"pages": {
"42": {
"pageid": 42,
"title": "Internet Relay Chat",
"extract": "IRC is een chatprotocol.",
}
}
}
},
)
return MagicMock(ok=True, json=lambda: {})

session.get.side_effect = side_effect

result = self.plugin._process_url(session, url)
self.assertEqual(
result,
["\u201cInternet Relay Chat\u201d", "\u2014 IRC is een chatprotocol."],
)
# Verify the API was called with the correct nl.wikipedia.org URL
call_args = session.get.call_args_list[-1]
self.assertIn("nl.wikipedia.org", call_args[0][0])

@patch("socket.getaddrinfo")
def test_mediawiki_login_and_fetch(self, mock_getaddrinfo):
# Allow wiki.example.com (public IP)
Expand Down
Loading