Coverage for mindsdb / integrations / handlers / mediawiki_handler / mediawiki_handler.py: 0%
35 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1from mediawikiapi import MediaWikiAPI
3from mindsdb.integrations.handlers.mediawiki_handler.mediawiki_tables import PagesTable
4from mindsdb.integrations.libs.api_handler import APIHandler
5from mindsdb.integrations.libs.response import (
6 HandlerStatusResponse as StatusResponse,
7)
9from mindsdb.utilities import log
10from mindsdb_sql_parser import parse_sql
12logger = log.getLogger(__name__)
15class MediaWikiHandler(APIHandler):
16 """
17 The MediaWiki handler implementation.
18 """
20 name = 'mediawiki'
22 def __init__(self, name: str, **kwargs):
23 """
24 Initialize the handler.
25 Args:
26 name (str): name of particular handler instance
27 **kwargs: arbitrary keyword arguments.
28 """
29 super().__init__(name)
31 self.kwargs = kwargs
33 self.connection = None
34 self.is_connected = False
36 pages_data = PagesTable(self)
37 self._register_table("pages", pages_data)
39 def connect(self):
40 """
41 Set up the connection required by the handler.
42 Returns
43 -------
44 StatusResponse
45 connection object
46 """
47 if self.is_connected is True:
48 return self.connection
50 self.connection = MediaWikiAPI()
52 self.is_connected = True
54 return self.connection
56 def check_connection(self) -> StatusResponse:
57 """
58 Check connection to the handler.
59 Returns:
60 HandlerStatusResponse
61 """
63 response = StatusResponse(False)
65 try:
66 self.connect()
67 response.success = True
68 except Exception as e:
69 logger.error('Error connecting to MediaWiki!')
70 response.error_message = str(e)
72 self.is_connected = response.success
74 return response
76 def native_query(self, query: str) -> StatusResponse:
77 """Receive and process a raw query.
78 Parameters
79 ----------
80 query : str
81 query in a native format
82 Returns
83 -------
84 StatusResponse
85 Request status
86 """
87 ast = parse_sql(query)
88 return self.query(ast)