def api_archwiki(): """ Return an API instance with anonymous connection to wiki.archlinux.org """ # NOTE: anonymous, will be very slow for big data! api_url = "https://wiki.archlinux.org/api.php" index_url = "https://wiki.archlinux.org/index.php" ssl_verify = True session = API.make_session(ssl_verify=ssl_verify) return API(api_url, index_url, session)
def _init_mw_database(self): # create database and mediawiki user master_url = sa.engine.url.URL("postgresql+psycopg2", username=self._postgresql_proc.user, host=self._postgresql_proc.host, port=self._postgresql_proc.port) self._master_db_engine = sa.create_engine(master_url, isolation_level="AUTOCOMMIT") conn = self._master_db_engine.connect() r = conn.execute("SELECT count(*) FROM pg_user WHERE usename = '{}'".format(_mw_db_user)) if r.fetchone()[0] == 0: conn.execute("CREATE USER {} WITH PASSWORD '{}'".format(_mw_db_user, _mw_db_password)) conn.execute("CREATE DATABASE {} WITH OWNER {}".format(_mw_db_name, _mw_db_user)) conn.close() # execute MediaWiki's tables.sql mw_url = sa.engine.url.URL("postgresql+psycopg2", database=_mw_db_name, username=_mw_db_user, password=_mw_db_password, host=self._postgresql_proc.host, port=self._postgresql_proc.port) # use NullPool, so that we don't have to recreate the engine when we drop the database self.db_engine = sa.create_engine(mw_url, poolclass=sa.pool.NullPool) tables = open(os.path.join(self._mw_nginx_proc.server_root, "maintenance/postgres/tables.sql")) with self.db_engine.begin() as conn: conn.execute(tables.read()) # create a wiki-scripts user in MediaWiki cmd = [ "php", "--php-ini", _php_ini, "maintenance/createAndPromote.php", "--sysop", _mw_api_user, _mw_api_password, ] subprocess.run(cmd, cwd=self._mw_nginx_proc.server_root, check=True) # construct the API object for the new user wiki-scripts in the database api_url = "http://{host}:{port}/api.php".format(host=self.hostname, port=self.port) index_url = "http://{host}:{port}/index.php".format(host=self.hostname, port=self.port) self.api = API(api_url, index_url, API.make_session()) self.api.login(_mw_api_user, _mw_api_password) # save the database as a template for self.clear() with self._master_db_engine.begin() as conn: conn.execute("SELECT pg_terminate_backend(pg_stat_activity.pid) " "FROM pg_stat_activity WHERE pg_stat_activity.datname = '{}'" .format(_mw_db_name)) conn.execute("CREATE DATABASE {} WITH TEMPLATE {} OWNER {}" .format(_mw_db_name + "_template", _mw_db_name, _mw_db_user))