The Commons Upload wizard needs some parameters that come from the Wikipedia page but can't be determined inside the template that generates the link to the upload wizard. This can be solved with a redirector script that the Wikipedia link points to and which generates the parameters and redirects to the Upload Wizard.
The script must be installed on Tools Lab.
The script should do the following steps:
- Read the GET parameters "lat", "lon", "campaign", "pagename" and "id".
- Validate that campaign, pagename and id parameters are not empty, otherwise display error page.
- Validate with the commons API that the campaign exists (cache positive commons api results for at least 1 day).
- Use the Wikipedia API to download the wikipedia page source with the "pagename" parameter (maybe cache the page text for 10 minutes).
- Use the JSON template configuration to determine supported template names and the name of the ID template parameter ("ID" or "Nummer").
- Iterate through the templates on the page, looking for valid configured template names until you find then one whose ID parameter matches the GET "id" parameter. If none is found, display error page.
- Check if ID matches the pattern for valid IDs, check if the ID is unique on the page (not used in other valid templates). If ID is not unique, display error page. If ID is not valid, store the invalid ID.
- Determine most precise category: Try the "Commonscat" parameter of the matched template, look for the "Commonscat" template in the "Weblinks" section of the page source, fall back to querying the API for the categories of the page, checking recursively against one of the sub-categories of "Liste (Kulturdenkmale in Deutschland)" (Cache the category tree).
- Build the redirect URL. Parameters are explained below. Parameters must be URL-encoded correctly.
- Log redirect URL to file (can be used for statistics later)
- Redirect to URL (I suggest HTTP status "301 Moved Permanently").
URL for valid IDs (caps are placeholders):
https://commons.wikimedia.org/wiki/Special:UploadWizard?campaign=CAMPAIGN&categories=hiddencat,CATEGORY&fields[]=VALID_ID&lat=LAT&lon=LON&objref=de|PAGENAME|VALID_ID
URL for invalid IDs (caps are placeholders):
https://commons.wikimedia.org/wiki/Special:UploadWizard?campaign=CAMPAIGN&categories=hiddencat,CATEGORY&lat=LAT&lon=LON&objref=de|PAGENAME|INVALID_ID
You can have a look at the existing Python bot code for inspiration.