"""Gets all redirected templates and their target titles along with number of use and saves in a json file. Output example:"""
{"Str sub long":{"rd_title":"Str sub old","num":56839},"Find sources":{"rd_title":"Çavkanî bigere","num":48889},"If empty":{"rd_title":"Ifempty","num":35558},"Coord":{"rd_title":"Koord","num":33599},"WikidataCoord":{"rd_title":"WikidataKoord","num":32943},"Commonscat-b":{"rd_title":"Commonscat-biçûk","num":32566},}
importpymysqlimportosimportjson# Database connection details# Note: If you are using Toolforge, you may ignore the database username and passworddb_hostname_format="kuwiki.analytics.db.svc.wikimedia.cloud"# Hostname of the database serverdb_port=3306# Port number for the database server# db_username = "" # Add your actual database username credential (if not using Toolforge)# db_password = "" # Add your actual database password credential (if not using Toolforge)db_name_format="kuwiki_p"# Name of the target databasedb_connect_file="~/replica.my.cnf"# path to the "my.cnf" file# Create a connection to the databaseconnection=pymysql.connect(host=db_hostname_format,port=db_port,# user=db_username,# password=db_password,database=db_name_format,read_default_file=db_connect_file,# "my.cnf" file contains user and password and read these parameters from under the [client] section.charset='utf8')# Create a cursorcursor=connection.cursor()# Use the kuwiki_p databasecursor.execute("USE kuwiki_p;")# queryquery="""select page_title, rd_title, count(tl_from) as numfrom pagejoin linktarget on lt_title = page_title and lt_namespace = 10join templatelinks on tl_target_id = lt_idjoin redirect on rd_from = page_idwhere page_namespace = 10and page_is_redirect = 1group by page_titleORDER BY num DESC"""# Execute the querycursor.execute(query)# Fetch the resultsresults=cursor.fetchall()# Close the cursor and the database connectioncursor.close()connection.close()# Construct a dictionary to hold the dataredirected_template_mappings={}ifresults:forresultinresults:ku_page_title=result[0].decode('utf-8').replace("_"," ")# Assuming there's only one column in the resultrd_title=result[1].decode('utf-8').replace("_"," ")num=result[2]redirected_template_mappings[ku_page_title]={"rd_title":rd_title,"num":num}# Write the dictionary to a JSON fileoutput_file_path=os.path.join(os.getenv('HOMEPATH','/data/project/balyozbot/'),'redirected_template_mappings.json')withopen(output_file_path,'w',encoding='utf-8')asoutput_file:json.dump(redirected_template_mappings,output_file,ensure_ascii=False,indent=4)print(f"Results written to {output_file_path}.")else:print("No results found from the query.")