diff --git a/onebot/plugins/urlinfo.py b/onebot/plugins/urlinfo.py index 09f8e93..d3af176 100644 --- a/onebot/plugins/urlinfo.py +++ b/onebot/plugins/urlinfo.py @@ -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 @@ -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 diff --git a/tests/test_plugin_urlinfo_mediawiki.py b/tests/test_plugin_urlinfo_mediawiki.py index 35ffad2..ada2185 100644 --- a/tests/test_plugin_urlinfo_mediawiki.py +++ b/tests/test_plugin_urlinfo_mediawiki.py @@ -21,9 +21,6 @@ def setUp(self): "username": "BotUser", "password": "BotPassword", }, - "en.wikipedia.org": { - "api_url": "https://en.wikipedia.org/w/api.php", - }, } } } @@ -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)