forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_disambiguation.py
executable file
·1289 lines (1138 loc) · 49.4 KB
/
solve_disambiguation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Script to help a human solve disambiguations by presenting a set of options.
Specify the disambiguation page on the command line.
The program will pick up the page, and look for all alternative links,
and show them with a number adjacent to them. It will then automatically
loop over all pages referring to the disambiguation page,
and show 30 characters of context on each side of the reference to help you
make the decision between the alternatives. It will ask you to type the
number of the appropriate replacement, and perform the change.
It is possible to choose to replace only the link (just type the number) or
replace both link and link-text (type 'r' followed by the number).
Multiple references in one page will be scanned in order, but typing 'n'
(next) on any one of them will leave the complete page unchanged. To leave
only some reference unchanged, use the 's' (skip) option.
Command line options:
-pos:XXXX adds XXXX as an alternative disambiguation
-just only use the alternatives given on the command line, do not
read the page for other possibilities
-dnskip Skip links already marked with a disambiguation-needed
template (e.g., {{dn}})
-primary "primary topic" disambiguation (Begriffsklärung nach Modell 2).
That's titles where one topic is much more important, the
disambiguation page is saved somewhere else, and the important
topic gets the nice name.
-primary:XY like the above, but use XY as the only alternative, instead of
searching for alternatives in [[Keyword (disambiguation)]].
Note: this is the same as -primary -just -pos:XY
-file:XYZ reads a list of pages from a text file. XYZ is the name of the
file from which the list is taken. If XYZ is not given, the
user is asked for a filename. Page titles should be inside
[[double brackets]]. The -pos parameter won't work if -file
is used.
-always:XY instead of asking the user what to do, always perform the same
action. For example, XY can be "r0", "u" or "2". Be careful with
this option, and check the changes made by the bot. Note that
some choices for XY don't make sense and will result in a loop,
e.g. "l" or "m".
-main only check pages in the main namespace, not in the Talk,
Project, User, etc. namespaces.
-first Uses only the first link of every line on the disambiguation
page that begins with an asterisk. Useful if the page is full
of irrelevant links that are not subject to disambiguation.
You won't get all af them as options, just the first on each
line. For a moderated example see
https://en.wikipedia.org/wiki/Szerdahely
A really exotic one is
https://hu.wikipedia.org/wiki/Brabant_(egyértelműsítő lap)
-start:XY goes through all disambiguation pages in the category on your
wiki that is defined (to the bot) as the category containing
disambiguation pages, starting at XY. If only '-start' or
'-start:' is given, it starts at the beginning.
-min:XX (XX being a number) only work on disambiguation pages for which
at least XX are to be worked on.
To complete a move of a page, one can use:
python pwb.py solve_disambiguation -just -pos:New_Name Old_Name
"""
#
# (C) Pywikibot team, 2003-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import codecs
import os
import re
from contextlib import suppress
from itertools import chain
from typing import Generator
import pywikibot
from pywikibot import config
from pywikibot import editor as editarticle
from pywikibot import i18n, pagegenerators
from pywikibot.bot import (
HighlightContextOption,
ListOption,
OutputProxyOption,
SingleSiteBot,
StandardOption,
)
from pywikibot.exceptions import (
Error,
IsNotRedirectPageError,
IsRedirectPageError,
LockedPageError,
NoPageError,
PageSaveRelatedError,
)
from pywikibot.site import Namespace
from pywikibot.tools import first_lower, first_upper
from pywikibot.tools.formatter import SequenceOutputter
# Disambiguation Needed template
dn_template = {
'ar': '{{بحاجة لتوضيح}}',
'arz': '{{Disambiguation needed}}',
'en': '{{dn}}',
'fr': '{{Lien vers un homonyme}}',
}
# disambiguation page name format for "primary topic" disambiguations
# (Begriffsklärungen nach Modell 2)
primary_topic_format = {
'ar': '%s_(توضيح)',
'arz': '%s_(توضيح)',
'ca': '%s_(desambiguació)',
'cs': '%s_(rozcestník)',
'de': '%s_(Begriffsklärung)',
'en': '%s_(disambiguation)',
'fa': '%s_(ابهامزدایی)',
'fi': '%s_(täsmennyssivu)',
'hu': '%s_(egyértelműsítő lap)',
'ia': '%s_(disambiguation)',
'it': '%s_(disambigua)',
'lt': '%s_(reikšmės)',
'kk': '%s_(айрық)',
'ko': '%s_(동음이의)',
'nl': '%s_(doorverwijspagina)',
'no': '%s_(peker)',
'pl': '%s_(ujednoznacznienie)',
'pt': '%s_(desambiguação)',
'pfl': '%s_BKL',
'he': '%s_(פירושונים)',
'ru': '%s_(значения)',
'sr': '%s_(вишезначна одредница)',
'sv': '%s_(olika betydelser)',
'uk': '%s_(значення)',
'ur': '%s_(ضد ابہام)',
}
# List pages that will be ignored if they got a link to a disambiguation
# page. An example is a page listing disambiguations articles.
# Special chars should be encoded with unicode (\x##) and space used
# instead of _
ignore_title = {
'wikipedia': {
'ca': [
'Viquipèdia:Enllaços incorrectes a pàgines de desambiguació',
'Viquipèdia:Registre de pàgines de desambiguació òrfenes',
'.*Discussió:.+',
'.*Usuari:.+',
'.+/[aA]rxiu.*',
],
'cs': [
'Wikipedie:Rozcestníky',
'Diskuse k Wikipedii:Rozcestníky',
'Wikipedie:Údržbové seznamy/Nejvíce odkazované rozcestníky/seznam',
'Wikipedie:Seznam rozcestníků/první typ',
'Wikipedie:Seznam rozcestníků/druhý typ',
'Wikipedista:Zirland/okres',
],
'da': [
'Wikipedia:Links til sider med flertydige titler'
],
'de': [
'.+/[aA]rchiv.*',
'.+/Baustelle.*',
'.+/Index',
'.+/Spielwiese',
'.+/[tT]est.*',
'.*Diskussion:.+',
'Benutzer:.+/[Ll]og.*',
'Benutzer:C.Löser/.+',
'Benutzer:Katharina/Begriffsklärungen',
'Benutzer:Kirschblut/.+buchstabenkürzel',
'Benutzer:Mathias Schindler/.+',
'Benutzer:Noisper/Dingliste/[A-Z]',
'Benutzer:Professor Einstein.*',
'Benutzer:Sebbot/.+',
'Benutzer:SirJective/.+',
'Benutzer:Srbauer.*',
'Benutzer:SteEis.',
'Benutzer:Steindy.*',
'Benutzer:SrbBot.*',
'Benutzer:PortalBot/.+',
'Benutzer:Xqbot/.+',
'Lehnwort',
'Liste griechischer Wortstämme in deutschen Fremdwörtern',
'Liste von Gräzismen',
'Portal:Abkürzungen/.+',
'Portal:Astronomie/Moves',
'Portal:Astronomie/Index/.+',
'Portal:Hund',
'Portal:Hund/Beobachtungsliste',
'Portal:Marxismus',
'Portal:Täuferbewegung/Seitenindex',
'Wikipedia:Administratoren/Anfragen',
'Wikipedia:Archiv/.+',
'Wikipedia:Artikelwünsche/Ding-Liste/[A-Z]',
'Wikipedia:Begriffsklärung.*',
'Wikipedia:Bots/.+',
'Wikipedia:Interwiki-Konflikte',
'Wikipedia:ISBN-Suche',
'Wikipedia:Liste mathematischer Themen/BKS',
'Wikipedia:Liste mathematischer Themen/Redirects',
'Wikipedia:Meinungsbilder/.+',
'Wikipedia:Löschkandidaten/.+',
'Wikipedia:WikiProjekt Altertumswissenschaft/.+',
'Wikipedia:WikiProjekt Verwaiste Seiten/Begriffsklärungen',
'Wikipedia:Qualitätssicherung/.+',
'Vorlage:Infobox Weltraum',
'Vorlage:Navigationsleiste Raumfahrt',
],
'en': [
'Wikipedia:Links to disambiguating pages',
'Wikipedia:Disambiguation pages with links',
'Wikipedia:Multiple-place names \\([A-Z]\\)',
'Wikipedia:Non-unique personal name',
"User:Jerzy/Disambiguation Pages i've Editted",
'User:Gareth Owen/inprogress',
'TLAs from [A-Z][A-Z][A-Z] to [A-Z][A-Z][A-Z]',
'List of all two-letter combinations',
'User:Daniel Quinlan/redirects.+',
'User:Oliver Pereira/stuff',
'Wikipedia:French Wikipedia language links',
'Wikipedia:Polish language links',
'Wikipedia:Undisambiguated abbreviations/.+',
'List of acronyms and initialisms',
'Wikipedia:Usemod article histories',
'User:Pizza Puzzle/stuff',
'List of generic names of political parties',
'Talk:List of initialisms/marked',
'Talk:List of initialisms/sorted',
'Talk:Programming language',
'Talk:SAMPA/To do',
"Wikipedia:Outline of Roget's Thesaurus",
'User:Wik/Articles',
'User:Egil/Sandbox',
'Wikipedia talk:Make only links relevant to the context',
'Wikipedia:Common words, searching for which is not possible',
],
'fa': [
'ویکیپدیا:فهرست صفحات ابهامزدایی',
],
'fi': [
'Wikipedia:Luettelo täsmennyssivuista',
'Wikipedia:Luettelo (täsmennyssivuista)',
'Wikipedia:Täsmennyssivu',
],
'fr': [
'Wikipédia:Liens aux pages d’homonymie',
'Wikipédia:Homonymie',
'Wikipédia:Homonymie/Homonymes dynastiques',
'Wikipédia:Prise de décision, noms des membres '
'de dynasties/liste des dynastiens',
'Liste de toutes les combinaisons de deux lettres',
'Wikipédia:Log d’upload/.*',
'Sigles de trois lettres de [A-Z]AA à [A-Z]ZZ',
'Wikipédia:Pages sans interwiki,.'
],
'fy': [
'Wikipedy:Fangnet',
],
'hu': [
# hu:Wikipédia:Kocsmafal (egyéb)#Hol nem kell egyértelműsíteni?
# 2012-02-08
'Wikipédia:(?!Sportműhely/Eddigi cikkeink).*',
'.*\\(egyértelműsítő lap\\)$',
'.*[Vv]ita:.*',
'Szerkesztő:[^/]+$',
],
'ia': [
'Categoria:Disambiguation',
'Wikipedia:.+',
'Usator:.+',
'Discussion Usator:.+',
],
'it': [
'Aiuto:Disambigua/Disorfanamento',
'Discussioni utente:.+',
'Utente:Civvì/disorfanamento',
],
'kk': [
'Санат:Айрықты бет',
],
'ko': [
'위키백과:(동음이의) 문서의 목록',
'위키백과:동음이의어 문서의 목록',
],
'lt': [
'Wikipedia:Rodomi nukreipiamieji straipsniai',
],
'nl': [
'Gebruiker:.*',
'Overleg gebruiker:.+[aA]rchief.*',
'Overleg gebruiker:Pven',
'Portaal:.+[aA]rchief.*',
'Wikipedia:Humor en onzin.*',
"Wikipedia:Links naar doorverwijspagina's/Winkeldochters.*",
"Wikipedia:Project aanmelding bij startpagina's",
"Wikipedia:Wikiproject Roemeense gemeenten/Doorverwijspagina's",
'Categorie:Doorverwijspagina',
'Lijst van Nederlandse namen van pausen',
'Overleg Wikipedia:Discussie spelling 2005',
'Overleg Wikipedia:Doorverwijspagina',
'Overleg Wikipedia:Logboek.*',
'Wikipedia:Logboek.*',
'Overleg gebruiker:Sybren/test.*',
'Overleg gebruiker:([0-9][0-9]?[0-9]?\\.){3}[0-9][0-9]?[0-9]?',
'Overleg:Lage Landen (staatkunde)',
'Wikipedia:.*[aA]rchief.*',
'Wikipedia:Doorverwijspagina',
'Wikipedia:Lijst van alle tweeletter-combinaties',
'Wikipedia:Onderhoudspagina',
'Wikipedia:Ongelijke redirects',
'Wikipedia:Protection log',
'Wikipedia:Te verwijderen.*',
'Wikipedia:Top 1000 van meest bekeken artikelen',
'Wikipedia:Wikipedianen met een encyclopedisch artikel',
'Wikipedia:Woorden die niet als zoekterm gebruikt kunnen worden',
'Overleg gebruiker:Taka(/.*)?',
"Wikipedia:Links naar doorverwijspagina's/Artikelen",
'Wikipedia:Wikiproject/Redirects/.*',
'Wikipedia:Wikiproject/Muziek/Overzicht/.*',
"Wikipedia:Wikiproject/Roemeense gemeenten/Doorverwijspagina's",
'Overleg Wikipedia:Wikiproject/Redirects.*',
"Wikipedia:Links naar doorverwijspagina's/Amsterdamconstructie",
],
'pl': [
'Wikipedysta:.+',
'Dyskusja.+:.+',
],
'pt': [
'Usuário:.+',
'Usuário Discussão:.+',
'Discussão:.+',
'Lista de combinações de duas letras',
'Wikipedia:Lista de páginas de desambiguação.+',
'Wikipedia:Páginas para eliminar/.+',
],
'ru': [
'Категория:Disambig',
'Википедия:Страницы разрешения неоднозначностей',
'Википедия:Вики-уборка/Статьи без языковых ссылок',
'Википедия:Страницы с пометкой «(значения)»',
'Список общерусских фамилий',
],
'sr': [
'Википедија:Вишезначна одредница',
],
'ur': [
'زمرہ:ضد ابہام صفحات',
],
},
'memoryalpha': {
'en': [
'Memory Alpha:Links to disambiguating pages'
],
'de': [
'Memory Alpha:Liste der Wortklärungsseiten'
],
},
}
def correctcap(link, text: str) -> str:
"""Return the link capitalized/uncapitalized according to the text.
:param link: link page
:type link: pywikibot.Page
:param text: the wikitext that is supposed to refer to the link
:return: uncapitalized title of the link if the text links to the link
with an uncapitalized title, else capitalized
"""
linkupper = link.title()
linklower = first_lower(linkupper)
if f'[[{linklower}]]' in text or f'[[{linklower}|' in text:
return linklower
return linkupper
class ReferringPageGeneratorWithIgnore:
"""Referring Page generator, with an ignore manager."""
def __init__(
self,
page,
primary: bool = False,
minimum: int = 0,
main_only: bool = False
) -> None:
"""Initializer.
:type page: pywikibot.Page
"""
self.page = page
# if run with the -primary argument, enable the ignore manager
self.primaryIgnoreManager = PrimaryIgnoreManager(page, enabled=primary)
self.minimum = minimum
self.main_only = main_only
def __iter__(self) -> Generator[pywikibot.Page, None, None]:
"""Yield pages."""
# TODO: start yielding before all referring pages have been found
refs = list(self.page.getReferences(with_template_inclusion=False,
namespaces=0 if self.main_only
else None))
pywikibot.info(f'Found {len(refs)} references.')
# Remove ignorables
site = self.page.site
if site.family.name in ignore_title \
and site.lang in ignore_title[site.family.name]:
for ig in ignore_title[site.family.name][site.lang]:
for i in range(len(refs) - 1, -1, -1):
if re.match(ig, refs[i].title()):
pywikibot.log('Ignoring page ' + refs[i].title())
del refs[i]
elif self.primaryIgnoreManager.isIgnored(refs[i]):
del refs[i]
if len(refs) < self.minimum:
pywikibot.info(
f'Found only {len(refs)} pages to work on; skipping.')
return
pywikibot.info(f'Will work on {len(refs)} pages.')
yield from refs
class PrimaryIgnoreManager:
"""
Primary ignore manager.
If run with the -primary argument, reads from a file which pages should
not be worked on; these are the ones where the user pressed n last time.
If run without the -primary argument, doesn't ignore any pages.
"""
def __init__(self, disamb_page, enabled: bool = False) -> None:
"""Initializer.
:type disamb_page: pywikibot.Page
"""
self.disamb_page = disamb_page
self.enabled = enabled
self.ignorelist = set()
folder = config.datafilepath('disambiguations')
if os.path.exists(folder):
self._read_ignorelist(folder)
def _read_ignorelist(self, folder) -> None:
"""Read pages to be ignored from file.
:type folder: str
"""
filename = os.path.join(
folder, self.disamb_page.title(as_filename=True) + '.txt')
# The file is stored in the disambiguation/ subdir.
# Create if necessary.
with suppress(IOError), codecs.open(filename, 'r', 'utf-8') as f:
for line in f:
# remove trailing newlines and carriage returns
line = line.rstrip('\r\n')
# skip empty lines
if line:
self.ignorelist.add(line)
def isIgnored(self, ref_page) -> bool: # noqa: N802
"""Return if ref_page is to be ignored.
:type ref_page: pywikibot.Page
"""
return self.enabled and ref_page.title(as_url=True) in self.ignorelist
def ignore(self, page_titles) -> None:
"""Write pages to ignorelist.
:param page_titles: page titles to be ignored
:type page_titles: iterable
"""
# backward compatibility
if isinstance(page_titles, pywikibot.Page):
page_titles = [page_titles.title(as_url=True)]
if self.enabled:
# Skip this occurrence next time.
filename = config.datafilepath(
'disambiguations',
self.disamb_page.title(as_url=True) + '.txt')
# Open file for appending. If none exists, create a new one.
with suppress(IOError), codecs.open(filename, 'a', 'utf-8') as f:
f.write('\n'.join(page_titles) + '\n')
class AddAlternativeOption(OutputProxyOption):
"""Add a new alternative."""
def result(self, value) -> None:
"""Add the alternative and then list them."""
new_alternative = pywikibot.input('New alternative:')
self._outputter.sequence.append(new_alternative)
super().result(value)
class EditOption(StandardOption):
"""Edit the text."""
def __init__(self, option, shortcut, text, start, title) -> None:
"""Initializer.
:type option: str
:type shortcut: str
:type text: str
:type start: int
:type title: str
"""
super().__init__(option, shortcut)
self._text = text
self._start = start
self._title = title
@property
def stop(self) -> bool:
"""Return whether if user didn't press cancel and changed it."""
return self.new_text and self.new_text != self._text
def result(self, value) -> str:
"""Open a text editor and let the user change it."""
editor = editarticle.TextEditor()
self.new_text = editor.edit(self._text, jumpIndex=self._start,
highlight=self._title)
return super().result(value)
class ShowPageOption(StandardOption):
"""Show the page's contents in an editor."""
def __init__(self, option, shortcut, start, page) -> None:
"""Initializer."""
super().__init__(option, shortcut, stop=False)
self._start = start
if page.isRedirectPage():
page = page.getRedirectTarget()
self._page = page
def result(self, value) -> None:
"""Open a text editor and show the text."""
editor = editarticle.TextEditor()
editor.edit(self._page.text,
jumpIndex=self._start,
highlight=self._page.title())
class AliasOption(StandardOption):
"""An option allowing multiple aliases which also select it."""
def __init__(self, option, shortcuts, stop: bool = True) -> None:
"""Initializer."""
super().__init__(option, shortcuts[0], stop=stop)
self._aliases = frozenset(s.lower() for s in shortcuts[1:])
def test(self, value) -> bool:
"""Test aliases and combine it with the original test."""
return value.lower() in self._aliases or super().test(value)
class DisambiguationRobot(SingleSiteBot):
"""Disambiguation Bot."""
ignore_contents = {
'de': ('{{[Ii]nuse}}',
'{{[Ll]öschen}}',
),
'fi': ('{{[Tt]yöstetään}}',
),
'kk': ('{{[Ii]nuse}}',
'{{[Pp]rocessing}}',
),
'nl': ('{{wiu2}}',
'{{nuweg}}',
),
'ru': ('{{[Ii]nuse}}',
'{{[Pp]rocessing}}',
),
}
primary_redir_template = {
# First letter uppercase
'hu': 'Egyért-redir',
}
# refer -help message for complete options documentation
available_options = {
'always': None, # always perform the same action
'pos': [], # add possibilities as alternative disambig
'just': True, # just and only use the possibilities given with command
'dnskip': False, # skip already marked links
'primary': False, # primary topic disambig
'main': False, # only use main namespace
'first': False, # use first link only
'min': 0, # minimum number of pages on a disambig
}
def __init__(self, *args, **kwargs) -> None:
"""Initializer."""
super().__init__(*args, **kwargs)
self.ignores = set()
self.summary = None
self.dn_template_str = i18n.translate(self.site, dn_template)
def checkContents(self, text: str) -> str | None: # noqa: N802
"""
Check if the text matches any of the ignore regexes.
:param text: wikitext of a page
:return: None if none of the regular expressions
given in the dictionary at the top of this class matches
a substring of the text, otherwise the matched substring
"""
for ig in self.ignore_contents_regexes:
match = ig.search(text)
if match:
return match.group()
return None
def makeAlternativesUnique(self) -> None: # noqa: N802
"""Remove duplicate items from self.opt.pos.
Preserve the order of alternatives.
"""
seen = set()
self.opt.pos = [i for i in self.opt.pos
if i not in seen and not seen.add(i)]
def setup(self) -> None:
"""Compile regular expressions."""
self.ignore_contents_regexes = []
if self.site.lang in self.ignore_contents:
for ig in self.ignore_contents[self.site.lang]:
self.ignore_contents_regexes.append(re.compile(ig))
linktrail = self.site.linktrail()
self.trailR = re.compile(linktrail)
# The regular expression which finds links. Results consist of four
# groups:
# group title is the target page title, that is, everything before
# | or ].
# group section is the page section. It'll include the # to make life
# easier for us.
# group label is the alternative link title, that's everything
# between | and ].
# group linktrail is the link trail, that's letters after ]] which
# are part of the word.
# note: the definition of 'letter' varies from language to language.
self.linkR = re.compile(rf"""
\[\[ (?P<title> [^\[\]\|#]*)
(?P<section> \#[^\]\|]*)?
(\|(?P<label> [^\]]*))? \]\]
(?P<linktrail>{linktrail})""", flags=re.X)
@staticmethod
def firstlinks(page) -> Generator[str, None, None]:
"""Return a list of first links of every line beginning with `*`.
When a disambpage is full of unnecessary links, this may be useful
to sort out the relevant links. E.g. from line
`* [[Jim Smith (smith)|Jim Smith]] ([[1832]]-[[1932]]) [[English]]`
it returns only 'Jim Smith (smith)'
Lines without an asterisk at the beginning will be disregarded.
No check for page existence, it has already been done.
"""
reg = re.compile(r'\*.*?\[\[(.*?)(?:\||\]\])')
for line in page.text.splitlines():
found = reg.match(line)
if found:
yield found[1]
def firstize(self, page, links) -> list[pywikibot.Page]:
"""Call firstlinks and remove extra links.
This will remove a lot of silly redundant links from overdecorated
disambiguation pages and leave the first link of each asterisked
line only. This must be done if -first is used in command line.
"""
titles = {first_upper(t) for t in self.firstlinks(page)}
links = list(links)
for link in links[:]: # uses a copy because of remove!
if link.title() not in titles:
links.remove(link)
return links
def treat_links(self, ref_page, disamb_page) -> bool:
"""Resolve the links to disamb_page or its redirects.
:param disamb_page: the disambiguation page or redirect we don't want
anything to link to
:type disamb_page: pywikibot.Page
:param ref_page: a page linking to disamb_page
:type ref_page: pywikibot.Page
:return: Return whether continue with next page (True)
or next disambig (False)
"""
nochange = True
for page in chain(
(disamb_page,), disamb_page.getReferences(filter_redirects=True)
):
treat_result = self.treat_disamb_only(ref_page, page)
if treat_result == 'nextpage':
return True
if treat_result == 'nextdisambig':
return False
if treat_result == 'done':
nochange = False
if nochange:
pywikibot.info('No changes necessary in ' + ref_page.title())
return True
def treat_disamb_only(self, ref_page, disamb_page) -> str:
"""Resolve the links to disamb_page but don't look for its redirects.
:param disamb_page: the disambiguation page or redirect we don't want
anything to link to
:type disamb_page: pywikibot.Page
:param ref_page: a page linking to disamb_page
:type ref_page: pywikibot.Page
:return: "nextpage" if the user enters "n" to skip this page,
"nochange" if the page needs no change, and
"done" if the page is processed successfully
"""
# TODO: break this function up into subroutines!
self.current_page = ref_page
include = False
unlink_counter = 0
new_targets = []
try:
text = ref_page.get()
except IsRedirectPageError:
pywikibot.info(
f'{ref_page.title()} is a redirect to {disamb_page.title()}')
if disamb_page.isRedirectPage():
target = self.opt.pos[0]
if pywikibot.input_yn(
f'Do you want to make redirect {ref_page.title()} point '
f'to {target}?',
default=False, automatic_quit=False):
redir_text = f'#{self.site.redirect()} [[{target}]]'
try:
ref_page.put(redir_text, summary=self.summary,
asynchronous=True)
except PageSaveRelatedError as error:
pywikibot.info(f'Page not saved: {error.args}')
else:
choice = pywikibot.input_choice(
f'Do you want to work on pages linking to '
f'{ref_page.title()}?',
[('yes', 'y'), ('no', 'n'), ('change redirect', 'c')], 'n',
automatic_quit=False)
if choice == 'y':
gen = ReferringPageGeneratorWithIgnore(
ref_page, self.opt.primary, main_only=self.opt.main
)
gen = pagegenerators.PreloadingGenerator(gen)
for ref_page2 in gen:
# run until the user selected 'quit'
self.treat_links(ref_page2, ref_page)
elif choice == 'c':
text = ref_page.get(get_redirect=True)
include = 'redirect'
except NoPageError:
pywikibot.info(f'Page [[{ref_page.title()}]] does not seem to'
' exist?! Skipping.')
else:
ignore_reason = self.checkContents(text)
if ignore_reason:
pywikibot.info(f'\n\nSkipping {ref_page.title()} because it '
f'contains {ignore_reason}.\n\n')
else:
include = True
if include:
# save the original text so we can show the changes later
original_text = text
n_links = 0 # number of links
curpos = 0
dn = False
edited = False
# This loop will run until we have finished the current page
while True:
m = self.linkR.search(text, pos=curpos)
if not m:
# No additinal link found
if n_links == 0:
# No remaining links to change for this disambiguation
# title.
return 'nochange'
# There are links to change; stop loop and save page
break
# Ensure that next time around we will not find this same hit.
curpos = m.start() + 1
try:
foundlink = pywikibot.Link(m['title'], disamb_page.site)
foundlink.parse()
except Error:
continue
# ignore interwiki links
if foundlink.site != disamb_page.site:
continue
# Check whether the link found is to disamb_page.
try:
if foundlink.canonical_title() != disamb_page.title():
continue
except Error:
# must be a broken link
pywikibot.log('Invalid link [[{}]] in page [[{}]]'
.format(m['title'], ref_page.title()))
continue
n_links += 1 # new link found: increase link counter
# how many bytes should be displayed around the current link
context = 60
# check if there's a dn-template here already
if (self.opt.dnskip and self.dn_template_str
and self.dn_template_str[:-2] in text[
m.end():m.end() + len(self.dn_template_str) + 8]):
continue
edit = EditOption('edit page', 'e', text, m.start(),
disamb_page.title())
context_option = HighlightContextOption(
'more context', 'm', text, 60, start=m.start(),
end=m.end())
context_option.before_question = True
options = [ListOption(self.opt.pos, ''),
ListOption(self.opt.pos, 'r'),
StandardOption('skip link', 's'),
edit,
StandardOption('next page', 'n'),
StandardOption('next disambig', 'g'),
StandardOption('unlink', 'u')]
if self.dn_template_str:
# '?', '/' for old choice
options += [AliasOption(
f'tag template {self.dn_template_str}',
['t', '?', '/'])]
options += [context_option]
if not edited:
options += [ShowPageOption('show disambiguation page', 'd',
m.start(), disamb_page)]
options += [
OutputProxyOption('list', 'l',
SequenceOutputter(self.opt.pos)),
AddAlternativeOption('add new', 'a',
SequenceOutputter(self.opt.pos))]
if edited:
options += [StandardOption('save in this form', 'x')]
# TODO: Output context on each question
answer = pywikibot.input_choice('Option', options,
default=self.opt.always,
force=bool(self.opt.always))
if answer == 'x':
assert edited, 'invalid option before editing'
break
if answer == 's':
# skip this link and decrease link counter
n_links -= 1
continue
if answer == 'e':
text = edit.new_text
edited = True
curpos = 0
continue
if answer == 'n':
# skip this page
if self.opt.primary:
# If run with the -primary argument, skip this
# occurrence next time.
self.ignores.add(ref_page.title(as_url=True))
return 'nextpage'
if answer == 'g':
return 'nextdisambig'
# The link looks like this:
# [[page_title|link_text]]trailing_chars
page_title = m['title']
link_text = m['label']
if not link_text:
# or like this: [[page_title]]trailing_chars
link_text = page_title
if m['section'] is None:
section = ''
else:
section = m['section']
trailing_chars = m['linktrail']
if trailing_chars:
link_text += trailing_chars
if answer == 't':
assert self.dn_template_str
# small chunk of text to search
search_text = text[m.end():m.end() + context]
# figure out where the link (and sentence) ends, put note
# there
end_of_word_match = re.search(r'\s', search_text)
if end_of_word_match:
position_split = end_of_word_match.start(0)
else:
position_split = 0
# insert dab needed template
text = (text[:m.end() + position_split]
+ self.dn_template_str
+ text[m.end() + position_split:])
dn = True
continue
if answer == 'u':
# unlink - we remove the section if there's any
text = text[:m.start()] + link_text + text[m.end():]
unlink_counter += 1
continue
# else check that no option from above was missed
assert isinstance(answer, tuple), 'only tuple answer left.'
assert answer[0] in ['r', ''], 'only valid tuple answers.'
if answer[0] == 'r':
# we want to throw away the original link text
replaceit = link_text == page_title
else:
replaceit = include == 'redirect'
new_page_title = answer[1]
rep = pywikibot.Page(pywikibot.Link(new_page_title,
disamb_page.site))
new_page_title = rep.title()
if not (new_page_title[0].isupper()
or link_text[0].isupper()):
new_page_title = first_lower(new_page_title)
if new_page_title not in new_targets:
new_targets.append(new_page_title)
if replaceit and trailing_chars:
newlink = f'[[{new_page_title}{section}]]{trailing_chars}'
elif replaceit or (new_page_title == link_text
and not section):
newlink = f'[[{new_page_title}]]'
# check if we can create a link with trailing characters
# instead of a pipelink
elif (
(len(new_page_title) <= len(link_text))
and (first_upper(link_text[:len(new_page_title)])
== first_upper(new_page_title))
and (self.trailR.sub(
'', link_text[len(new_page_title):]) == '')
and (not section)
):