Page MenuHomePhabricator
Paste P6901

MassMessage to all Lua talk pages
ActivePublic

Authored by Lucas_Werkmeister_WMDE on Mar 27 2018, 10:19 AM.
#!/bin/bash
function queryNamespaces {
local apiEndpoint=${1:?API endpoint missing}
curl \
--silent \
--get \
--header 'Accept: application/json' \
--data-urlencode action=query \
--data-urlencode meta=siteinfo \
--data-urlencode siprop=namespaces \
--data-urlencode format=json \
--data-urlencode formatversion=2 \
"$apiEndpoint" |
jq '
.query.namespaces
'
}
function getNamespaceNameFromTitle {
local title=${1:?title missing}
if [[ $title =~ ^([^:]*):.*$ ]]; then
printf '%s' "${BASH_REMATCH[1]}"
else
printf '%s' "$title"
fi
}
function getNamespaceIdByName {
local namespacesJson=${1:?namespace information JSON missing} \
name=${2?namespace name missing}
printf '%s' "$namespacesJson" |
jq --raw-output "
.[] |
select(.name == \"$name\") |
.id
"
}
function getNamespaceNameById {
local namespacesJson=${1:?namespace information JSON missing} \
id=${2:?namespace ID missing}
printf '%s' "$namespacesJson" |
jq --raw-output "
.[] |
select(.id == $id) |
.name
"
}
query='
SELECT ?wiki ?page WHERE {
?normalSitelink schema:about wd:Q6811431;
schema:isPartOf ?wiki;
schema:name ?page.
}
'
curl \
--silent \
--get \
--header 'Accept: application/json' \
--data-urlencode query="$query" \
https://query.wikidata.org/sparql |
jq --raw-output '
.results.bindings |
.[] |
(.wiki.value + "\t" + .page.value)
' |
while IFS=$'\t' read -r wiki page; do
apiEndpoint="${wiki}w/api.php"
site=${wiki/'https://'/}
namespaceJson=$(queryNamespaces "$apiEndpoint")
if [[ $page =~ ^([^:]*):(.*)$ ]]; then
namespaceName=${BASH_REMATCH[1]}
titleText=${BASH_REMATCH[2]}
else
namespaceName=''
titleText=$page
fi
namespaceId=$(getNamespaceIdByName "$namespaceJson" "$namespaceName")
talkNamespaceId=$((namespaceId + 1))
talkNamespaceName=$(getNamespaceNameById "$namespaceJson" "$talkNamespaceId")
talkPage="$talkNamespaceName:$titleText"
printf '# {{target|page=%s|site=%s}}\n' "$talkPage" "$site"
done