Tech
Tech-related questions about third-party wikis should be asked at mw:Project:Support desk. |
A place to talk about tech related to a Wikimedia wiki.
Have a technical wiki question? Ask here. This can include, for example:
- requests for new tools, scripts, and bots;
- help with CSS or JavaScript;
- API help;
- and data collection help (including database queries).
If you are not using the "New topic tool", such as the "Ask a question" button above, please sign manually using four tildes (~~~~
), or by clicking on the signature icon ; this will automatically generate your name and the current timestamp when you publish your changes.
Understanding Wikipedia titles batching API
[edit]With the MediaWiki API we can query the Wikipedia API. One of the fields is `titles` where one *or more* titles can be queried at the same time. Batching them together is recommended in high load scenarios to avoid multiple consecutive requests. Multiple titles should be separated by a pipe `|` character.
I am using the Wikipedia API to find "translations" of categories. Let's say I have an English category "Antiquity", I want to find the corresponding category in a different language. That is possible by querying the API for the prop `langlinks`.
I find that, indeed, I can find such one-on-one mappings of an English category if I do not use batching, but if I *do* use batching, I do not always get all of the results back. To illustrate, I have a list of English categories, and at each iteration I process one item more than before (starting with only one). With batching, it becomes clear that with larger lists (still well within the max. limit of 50 imposed by the API), the earlier categories are lost and not included anymore. When not using batching (batch size=1), this issue does not occur.
import requests
def get_translated_category(category_titles: str | list[str], target_lang: str, batch_size: int = 50) -> list[str]:
"""Fetch the translated equivalent of a Wikipedia category."""
endpoint = "https://en.wikipedia.org/w/api.php"
if isinstance(category_titles, str):
category_titles = [category_titles]
category_titles = [f"Category:{title}" for title in category_titles]
translated_categories = {}
# API is limited to 50 titles per request
for start_idx in range(0, len(category_titles), batch_size):
end_idx = start_idx + batch_size
batch_titles = category_titles[start_idx:end_idx]
params = {
"action": "query",
"format": "json",
"prop": "langlinks",
"titles": "|".join(batch_titles),
"lllimit": "max"
}
response = requests.get(endpoint, params=params)
data = response.json()
pages = data.get("query", {}).get("pages", {})
for page_data in pages.values():
title = page_data["title"].split(":")[-1]
if title in translated_categories:
print("We already found this category title!")
langlinks = page_data.get("langlinks", [])
for link in langlinks:
if link["lang"] == target_lang:
translated_categories[title] = link["*"].split(":")[-1]
return translated_categories
if __name__ == "__main__":
english_categories: list[str] = [
"Classical antiquity",
"Late antiquity",
"Latin-language literature",
"Roman Kingdom",
"Roman Republic",
"Roman Empire",
"Byzantine Empire",
"Latin language",
"Ancient Greek",
"Ancient Greece",
"Ancient Greek literature",
"Medieval history of Greece",
]
print("Batch size 50 (default)")
for idx in range(len(english_categories)):
categories = english_categories[:idx+1]
latin_categories = get_translated_category(categories, "la")
print(latin_categories)
print()
print("Batch size 1 (no batching)")
for idx in range(len(english_categories)):
categories = english_categories[:idx+1]
latin_categories = get_translated_category(categories, "la", batch_size=1)
print(latin_categories)
The output of the code above is:
# Batch size 50 (default)
{'Classical antiquity': 'Res classicae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin language': 'Lingua Latina', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin language': 'Lingua Latina', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}
# Batch size 1 (no batching)
{'Classical antiquity': 'Res classicae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua', 'Ancient Greek literature': 'Litterae Graecae antiquae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua', 'Ancient Greek literature': 'Litterae Graecae antiquae'}
It should be immediately clear that there is a difference between batching and not using batching and, more worrisome, that using batching leads to some items being discarded. I thought that perhaps this would be the case if categories are merged in Latin and have the same name, so the API resolves to only returning one of them, but as far as I can tell that is not the case.
How can I ensure that batching my requests (titles) together, I get the same results as firing individual requests with the Wikipedia API?
EDIT: after further investigation it would seem that the API does return results for all categories (the `pages ` variable) but for some reason the corresponding languages (`langlinks`) are not the same. BramVanroy (talk) 13:21, 12 February 2025 (UTC)
- You need to follow API continuation, e.g. using
continuation=True
when using mwapi. Lucas Werkmeister (talk) 14:00, 12 February 2025 (UTC)- Thank you Lucas, that was the missing link! BramVanroy (talk) 11:12, 17 February 2025 (UTC)
jQuery and dark mode styles
[edit]Hi, is there a pre-defined list of dark mode styles I can apply to jQuery? I'm trying to convert c:MediaWiki:Gadget-VisualFileChange.js to use dark mode styles, but I need to change styles for elements like .ui-widget. I know the correct thing would be to convert this to ooUI, but is there a short term fix I can apply? —Matrix (user page (@ commons) - talk?) 12:35, 15 February 2025 (UTC)
- It would be better to move the tool away from jQuery ui altogether. I don't think it is really suited to applying dark mode fixes to jQuery ui. Solutions that other projects have chosen is mostly to opt out the entire component from dark mode. —TheDJ (talk • contribs) 12:40, 12 March 2025 (UTC)
- I think that one solution could be to use Codex and/or Codex CSS design tokens with fallback colors for older skins (per Recommendations for night mode compatibility on Wikimedia wikis) like this:
background-color: var(--background-color-interactive, #DEF);
I do not know if there is nice ready made list CSS-class list for different design elements (forms, buttons etc). I tried to find but found just case-by-case examples. --Zache (talk) 08:04, 13 March 2025 (UTC)
API call to list article's previous titles/move history?
[edit]Can the MediaWiki Action API supply a list of an article's previous titles? E.g. for "Bindi" I'd like to get "Bindi (decoration)". My understanding is that Revisions only record edits associated with page moves, not the actual move itself. Thanks Matthew at catfishing (talk) 08:55, 17 February 2025 (UTC)
- @Matthew at catfishing , you can query them from logs like this, but as it filters the list by title you will get only one move per query and you need to use old title to query next move in list. --Zache (talk) 10:53, 13 March 2025 (UTC)
- Thanks @Zache! As far as I can tell, that API gets me the new title, if I have the old title. Your example picks up Tanja's 2006 move from Karpela to Saarela. But not the 2008 move from Saarela to the current title (which happens to also be Tanja Karpela). Is there any way of getting from current title to old title(s)? Matthew at catfishing (talk) 23:14, 14 March 2025 (UTC)
Request help for a module in Mon Wiktionary
[edit]In this wikt:mnw:မဝ်ဂျူ:it-headword how do I remove " s "? for an example, check out this wikt:mnw:augurato page. If possible, please edit this, thanks--𝓓𝓻.𝓘𝓷𝓽𝓸𝓫𝓮𝓼𝓪|𝒯𝒶𝓁𝓀 11:21, 17 February 2025 (UTC)
Lua error on quote template on tr.wiktionary
[edit]Hi, I had an encounter with error on quote-juornal template. I had asked for help which is includes admins and technical people on Turkish Wikimedia but at the end of the day this problem was not solved. I need this template now and more days. Can you help us to make this template work again? This is template. Satirdan kahraman (talk) 16:28, 28 February 2025 (UTC)
Mobile version
[edit]I can't choose different mobile and PC themes in my wiki, I hadn't got it in my settings. Soviet Motherland (talk) 08:55, 12 March 2025 (UTC)
- This page is for Wikimedia wikis only. If "your wiki" is your own MediaWiki installation, see mw:Project:Support_desk instead and read the "Post a new question" section. Thanks. AKlapper (WMF) (talk) 08:58, 12 March 2025 (UTC)
Visual editor not working on ca.wikipedia.org
[edit]Myself and others can't get visual editor to work at all. --Lluis tgn (talk) 10:11, 13 March 2025 (UTC)
- @Lluis tgn I cannot reproduce when going to https://ca.wikipedia.org/w/index.php?title=Filipines&veaction=edit for example. What does happen after which exact steps? What's the output in your web browser's console? See mw:Help:Locating broken scripts for more info. Thanks! AKlapper (WMF) (talk) 10:16, 13 March 2025 (UTC)
- Centralizing reports on task T388772 now. Thanks :) --Lluis tgn (talk) 10:59, 13 March 2025 (UTC)
Strange technical behaviour at Talk:Community Wishlist on mobile
[edit]When visiting Talk:Community Wishlist on Mobile, even when logged in the little person icon doesn't display in the top right corner and there is no page history or watchlist icon (I have noticed this for months). There is an "expand all" button in the three dots that fails. I am not sure if a transclusion is causing it or it is a MediaWiki bug. Incidentally, a working "expand all" button would be handy on c:COM:HD. Commander Keane (talk) 22:12, 18 March 2025 (UTC)
- @Commander Keane: At a first impression, sounds potentially similar to what was reported as phab:T383272 - does what's described in that task's description match what you're seeing? (In addition, purely out of curiosity, does refreshing the page on mobile help at all?) All the best, —a smart kitten[meow] 00:05, 19 March 2025 (UTC)
- Yes refreshing did fix it. I have seen the phab:T383272 behaviour on other pages and now I understand it is caused by special page transclusion. I guess Talk:Community Wishlist is the page I visit the most that has a special page included! Thanks for finding the phab task.
- I found phab:T358733 that has a gif of the expand all toggler bug. The task is open and medium.
- I am not sure why the toggler doesn't appear at Commons in the three dots (could be my local prefs?), but it doesn't work anyway so not a current issue.
- I will wait for bug fixes :-). Commander Keane (talk) 00:44, 19 March 2025 (UTC)