Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P20224
unlink-incubatorwiki.py
Active
Public
Actions
Authored by
LucasWerkmeister
on Feb 7 2022, 8:47 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
Wikidata
Referenced Files
F34944757: unlink-incubatorwiki.py
Feb 7 2022, 8:47 PM
2022-02-07 20:47:11 (UTC+0)
Subscribers
None
#!/usr/bin/env python3
import
json
import
mwapi
import
random
bot_username
=
'Lucas Werkmeister@unlink-incubatorwiki_(T284211)'
bot_password
=
'redacted'
edit_group_hash
=
'{:x}'
.
format
(
random
.
randrange
(
0
,
2
**
48
))
edit_group
=
f
'[[:toolforge:editgroups/b/CB/{edit_group_hash}|details]]'
session
=
mwapi
.
Session
(
'https://www.wikidata.org'
,
user_agent
=
'unlink-incubatorwiki (mail@lucaswerkmeister.de; T284211)'
,
formatversion
=
1
)
# mwapi assumes error contents are in '*'
lgtoken
=
session
.
get
(
action
=
'query'
,
meta
=
'tokens'
,
type
=
'login'
)[
'query'
][
'tokens'
][
'logintoken'
]
session
.
post
(
action
=
'login'
,
lgname
=
bot_username
,
lgpassword
=
bot_password
,
lgtoken
=
lgtoken
)
token
=
session
.
get
(
action
=
'query'
,
meta
=
'tokens'
,
type
=
[
'csrf'
])[
'query'
][
'tokens'
][
'csrftoken'
]
def
unlink_wiki
(
item_id
,
site_id
):
item
=
session
.
get
(
action
=
'wbgetentities'
,
ids
=
[
item_id
])[
'entities'
][
item_id
]
baserevid
=
item
[
'lastrevid'
]
del
item
[
'pageid'
],
item
[
'ns'
],
item
[
'title'
],
item
[
'lastrevid'
],
item
[
'modified'
]
try
:
del
item
[
'sitelinks'
][
site_id
]
except
KeyError
:
print
(
f
'No {site_id} sitelink found in {item_id}.'
)
return
revid
=
session
.
post
(
**
{
'action'
:
'wbeditentity'
,
'id'
:
item_id
,
'baserevid'
:
baserevid
,
'clear'
:
''
,
'data'
:
json
.
dumps
(
item
),
'summary'
:
f
'removing broken {site_id} sitelink ([[phabricator:T284211|T284211]], {edit_group})'
,
'assert'
:
'user'
,
'token'
:
token
,
})[
'entity'
][
'lastrevid'
]
print
(
f
'Edited {item_id}, revision {revid}.'
)
site_id
=
'incubatorwiki'
# SELECT CONCAT("['", GROUP_CONCAT(CONCAT('Q', ips_item_id) SEPARATOR "', '"), "']") FROM wb_items_per_site WHERE ips_site_id = 'incubatorwiki';
item_ids
=
[
'Q4654925'
,
'Q5296'
,
'Q3055357'
,
'Q15117218'
,
'Q4847311'
,
'Q4608595'
,
'Q6064718'
,
'Q5462890'
,
'Q5056'
,
'Q6865035'
,
'Q5826993'
,
'Q12573042'
,
'Q5620924'
,
'Q5683132'
,
'Q10971517'
,
'Q8129125'
,
'Q11722212'
,
'Q18180967'
,
'Q18180953'
,
'Q18180959'
,
'Q5412328'
,
'Q5462387'
,
'Q27989948'
,
'Q4021091'
,
'Q14330485'
,
'Q21474049'
,
'Q105273306'
,
'Q86746352'
,
'Q110573516'
,
'Q2201314'
,
'Q29579'
,
'Q20979815'
,
'Q3540'
,
'Q956'
,
'Q148'
,
'Q11725'
,
'Q8686'
,
'Q11736'
,
'Q1880'
,
'Q20981262'
,
'Q52'
,
'Q25679189'
,
'Q131276'
,
'Q2018526'
,
'Q5592'
,
'Q10884'
,
'Q98713577'
,
'Q24905'
,
'Q110311413'
,
'Q84299498'
,
'Q59698'
]
errors
=
[]
for
item_id
in
item_ids
:
try
:
unlink_wiki
(
item_id
,
site_id
)
except
mwapi
.
errors
.
APIError
as
e
:
print
(
f
'Got error editing {item_id}.'
)
if
not
errors
:
print
(
'(All errors will be printed at the end. Continuing meanwhile.)'
)
errors
.
append
(
e
)
for
error
in
errors
:
print
(
error
)
Event Timeline
LucasWerkmeister
created this paste.
Feb 7 2022, 8:47 PM
2022-02-07 20:47:11 (UTC+0)
LucasWerkmeister
mentioned this in
T284211: Uncaught TypeError: $(...).siteselector(...).getId is not a function
.
Log In to Comment