ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/ValBot/Python/check_interwiki_links.py
(Generate patch)

Comparing ValBot/Python/check_interwiki_links.py (file contents):
Revision 1180 by iritscen, Fri Apr 28 00:54:21 2023 UTC vs.
Revision 1192 by iritscen, Mon Sep 16 23:08:26 2024 UTC

# Line 12 | Line 12 | import os
12   from urllib.parse import urljoin
13  
14   import pywikibot
15 + import bs4
16   import re
17   import requests # for listing members with dir()
18  
# Line 32 | Line 33 | debug = 0
33   pages_checked = 0
34   iw_found = 0
35   errors_issued = 0
36 + name_printed = 0
37 +
38 + # Prints the name of a page on which something occurred, if it has not been printed before
39 + def possibly_print(page_name):
40 +   global debug
41 +   global name_printed
42 +  
43 +   if not name_printed and not debug:
44 +      pywikibot.stdout('')
45 +      pywikibot.stdout('From page "{}":'.format(page_name))
46 +      name_printed = 1
47 +
48 + # Search a page for the section specified in the link
49 + def find_section(page_text, page_name, page_slug, prefix, print_result):
50 +   global errors_issued
51 +
52 +   # Isolate section link
53 +   target_page_name, anchor_name = page_slug.split('#')
54 +   target_page_name_human = target_page_name.replace('_', ' ')
55 +  
56 +   # Convert dot-notation hex entities to proper characters
57 +   anchor_name = anchor_name.replace('.22', '"')
58 +   anchor_name = anchor_name.replace('.27', '\'')
59 +   anchor_name = anchor_name.replace('.28', '(')
60 +   anchor_name = anchor_name.replace('.29', ')')
61 +  
62 +   # Read linked page to see if it really has this anchor link
63 +   soup = BeautifulSoup(page_text, 'html.parser')
64 +   found_section = False
65 +   for the_tag in soup.findAll('span'): # search for span with ID matching the section name
66 +      tag_name = the_tag.get('id', None)
67 +      if tag_name == anchor_name:
68 +         found_section = True
69 +         break
70 +   if found_section == False:
71 +      for the_tag in soup.findAll('div'): # search for div with ID matching the section name
72 +         tag_name = the_tag.get('id', None)
73 +         if tag_name == anchor_name:
74 +            found_section = True
75 +            break
76 +   if found_section == False:
77 +      for the_tag in soup.findAll('h2'): # search for h2 with ID matching the section name
78 +         tag_name = the_tag.get('id', None)
79 +         if tag_name == anchor_name:
80 +            found_section = True
81 +            break
82 +   if found_section == False:
83 +      for the_tag in soup.findAll('h3'): # search for h3 with ID matching the section name
84 +         tag_name = the_tag.get('id', None)
85 +         if tag_name == anchor_name:
86 +            found_section = True
87 +            break
88 +   if found_section == False:
89 +      for the_tag in soup.findAll('h4'): # search for h4 with ID matching the section name
90 +         tag_name = the_tag.get('id', None)
91 +         if tag_name == anchor_name:
92 +            found_section = True
93 +            break
94 +   if found_section == False:
95 +      possibly_print(page_name)
96 +      pywikibot.stdout('   ERROR: Could not find section "{0}" on {1} page "{2}".'.format(anchor_name, prefix, target_page_name_human))
97 +      errors_issued = errors_issued + 1
98 +   elif print_result == True:
99 +      pywikibot.stdout('   The section "{0}" was found on {1} page "{2}".'.format(anchor_name, prefix, target_page_name_human))
100 +
101 + # For a link that redirected us to another page, extract the name of the target page from
102 + # the target page's source
103 + def find_canonical_link(page_text, page_name, page_slug, prefix, prefix_url):
104 +   # Extract link from this markup which contains name of redirected-to page:
105 +   # <link rel="canonical" href="https://en.wikipedia.org/wiki/Page_name"/>
106 +   canonical_name = page_text.split('<link rel="canonical" href="')[-1]
107 +   prefix_length = len(prefix_url)
108 +   canonical_name = canonical_name[prefix_length:]
109 +   tag_end = canonical_name.find('">')
110 +  
111 +   if tag_end == -1:
112 +      pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect page, but this script could not isolate the target page name.'.format(prefix, page_slug))
113 +      errors_issued = errors_issued + 1
114 +   else:
115 +      canonical_name = canonical_name[:tag_end]
116 +      if len(canonical_name) > 100:
117 +         # Certain things can cause the trim to fail; report error and avoid slamming the
118 +         # output with massive page source from a failed trim
119 +         pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect to "{2}…" (string overflow).'.format(prefix, page_slug, canonical_name[:100]))
120 +         errors_issued = errors_issued + 1
121 +      else:
122 +         canonical_name = canonical_name.replace('_', ' ')
123 +         if '#' in page_slug:
124 +            _, anchor_name = page_slug.split('#')
125 +            pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}#{3}", which is a valid page. Checking section link….'.format(prefix, page_slug, canonical_name, anchor_name))
126 +            find_section(page_text, page_name, page_slug, prefix, True)
127 +         else:
128 +            pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}", which is a valid page.'.format(prefix, page_slug, canonical_name))
129 +
130 + # Test an interwiki link and look for a section link if applicable
131 + def test_interwiki_link(prefix, prefix_url, iw_url, page_name, page_slug):
132 +   global errors_issued
133 +  
134 +   response = fetch(iw_url)
135 +
136 +   # One way we tell that a redirect occurred is by checking fetch's history, as it
137 +   # automatically follows redirects. This will catch formal redirects which come from pages
138 +   # such as Special:PermanentLink.
139 +   if response.history != []:
140 +      possibly_print(page_name)
141 +        
142 +      if page_slug.startswith('WP:') and page_slug == page_slug.upper():
143 +         pywikibot.stdout('   Got redirection code "{0}" for {1} link "{2}". This appears to be a deliberate use of a Wikipedia shortcut. Checking the target page….'.format(response.history[0], prefix, page_slug))
144 +         find_canonical_link(response.text, page_name, page_slug, prefix, prefix_url)
145 +      else:
146 +         permalink1 = 'Special:PermanentLink/'.lower()
147 +         permalink2 = 'Special:Permalink/'.lower()
148 +         page_slug_lower = page_slug.lower()
149 +         if page_slug_lower.startswith(permalink1) or page_slug_lower.startswith(permalink2):
150 +            pywikibot.stdout('   Got redirection code "{0}" for {1} permanent revision link "{2}". Checking the target page….'.format(response.history[0], prefix, page_slug))
151 +            find_canonical_link(response.text, page_name, page_slug, prefix, prefix_url)
152 +         else:
153 +            pywikibot.stdout('   ERROR: Unrecognized type of redirection (code "{0}") for {1} link "{2}". You should check the link manually.'.format(response.history[0], prefix, page_slug))
154 +            errors_issued = errors_issued + 1
155 +   elif response.status_code != 200:
156 +      possibly_print(page_name)
157 +      pywikibot.stdout('   ERROR: Got response code {0} for {1} link "{2}". The page may not exist.'.format(response.status_code, prefix, page_slug))
158 +      errors_issued = errors_issued + 1
159 +   # However the usual way that a redirect occurs is that MediaWiki redirects us sneakily
160 +   # using JavaScript, while returning code OK 200 as if the link was correct; this happens
161 +   # when a redirect page is accessed. We must detect these soft redirects by looking at the
162 +   # page source to find the redirect note inserted at the top of the page for the reader.
163 +   elif 'Redirected from <a' in response.text:
164 +      possibly_print(page_name)
165 +      pywikibot.stdout('   Got silently redirected by {0} link "{1}". Checking the target page….'.format(prefix, page_slug))
166 +      find_canonical_link(response.text, page_name, page_slug, prefix, prefix_url)
167 +   elif '#' in page_slug:
168 +      find_section(response.text, page_name, page_slug, prefix, False)
169  
170   # Searches the given page text for interwiki links
171   def scan_for_interwiki_links(page_text, page_name):
172     global debug
173     global pages_checked
174     global iw_found
175 <   global errors_issued
175 >   global name_printed
176     pages_checked = pages_checked + 1
177 <   cur = 0
177 >   cur_prefix = 0
178     name_printed = 0
179  
180     for prefix in interwiki_prefixes:
181        # Isolate strings that start with "[[prefix:" and end with "|" or "]"
182 <      iw_link = "\[\[" + prefix + ":[^|\]]*(\||\])"
182 >      iw_link = r"\[\[" + prefix + r":[^|\]]*(\||\])"
183        for match in re.finditer(iw_link, page_text):
184           # Extract just the page title from this regex match
185           s = match.start() + 2 + len(prefix) + 1
186           e = match.end() - 1
187  
188 <         # Sometimes we used a space char. instead of a '_', so fix that before querying
189 <         page_title = page_text[s:e].replace(' ', '_')
188 >         # Commonly we use spaces instead of underscores, so fix that before querying
189 >         page_slug = page_text[s:e].replace(' ', '_')
190  
191 <         # Use only spaces for title when printing it
192 <         page_title_human = page_title.replace('_', ' ')
191 >         # But use spaces for title when printing it
192 >         page_title_human = page_slug.replace('_', ' ')
193           if debug: pywikibot.stdout('   Validating {0} link "{1}"'.format(prefix, page_title_human))
194           iw_found = iw_found + 1
195  
196           # Construct full URL for the particular wiki
197 <         iw_url = interwiki_urls[cur] + page_title
197 >         iw_url = interwiki_urls[cur_prefix] + page_slug
198  
199           # Adjust URL if this is a foreign-language WP link
200 <         if re.match("^[a-zA-Z]{2}:", page_title):
201 <            lang_code = page_title[0:2] + "."
200 >         if re.match("^[a-zA-Z]{2}:", page_slug):
201 >            lang_code = page_slug[0:2] + "."
202              # "wp:" is the Wikipedia: namespace, not a language
203              if lang_code != "wp." and lang_code != "WP.":
204                 iw_url = iw_url.replace('en.', lang_code)
205 <               iw_url = iw_url.replace(page_title[0:3], '')
205 >               iw_url = iw_url.replace(page_slug[0:3], '')
206  
207           # Test the URL
208 <         response = fetch(iw_url)
208 >         test_interwiki_link(prefix, interwiki_urls[cur_prefix], iw_url, page_name, page_slug)
209 >      cur_prefix = cur_prefix + 1
210  
211 <         # One way we tell that a redirect occurred is by checking the history
212 <         if response.history != []:
213 <            if not name_printed and not debug:
214 <               pywikibot.stdout('From page "{}":'.format(page_name))
215 <               name_printed = 1
216 <            if page_title.startswith('WP:') and page_title == page_title.upper():
217 <               pywikibot.stdout('   ERROR: Got redirection code ({0}) for {1} link "{2}", but this appears to be a deliberate use of a Wikipedia shortcut. You should check the link manually.'.format(response.history[0], prefix, page_title))
218 <            else:
219 <               pywikibot.stdout('   ERROR: Got redirection code ({0}) for {1} link "{2}". You should check the link manually.'.format(response.history[0], prefix, page_title))
220 <            errors_issued = errors_issued + 1
221 <         elif response.status_code != 200:
222 <            if not name_printed and not debug:
223 <               pywikibot.stdout('From page "{}":'.format(page_name))
224 <               name_printed = 1
225 <            pywikibot.stdout('   ERROR: Got response code {0} for {1} link "{2}". The page may not exist.'.format(response.status_code, prefix, page_title))
226 <            errors_issued = errors_issued + 1
227 <         # The usual way that a redirect occurs is that MediaWiki redirects us sneakily
228 <         # using JavaScript, while returning code OK 200 as if the link was correct; we
229 <         # must detect this from the page source
230 <         elif 'Redirected from <a' in response.text:
231 <            if not name_printed and not debug:
97 <               pywikibot.stdout('From page "{}":'.format(page_name))
98 <               name_printed = 1
99 <            # Extract link from this source which contains name of redirected-to page:
100 <            # <link rel="canonical" href="https://en.wikipedia.org/wiki/Page_name"/>
101 <            canonical_name = response.text.split('<link rel="canonical" href="')[-1]
102 <            prefix_length = len(interwiki_urls[cur])
103 <            canonical_name = canonical_name[prefix_length:]
104 <            tag_end = canonical_name.find('"/>')
105 <            if tag_end == -1:
106 <               pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect page, but this script could not isolate the target page name.', format(prefix, page_title))
107 <            else:
108 <               canonical_name = canonical_name[:tag_end]
109 <               if len(canonical_name) > 100:
110 <                 # Certain things can cause the trim to fail; here we avoid slamming
111 <                 # the output with massive page source from a failed trim
112 <                 pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect to "{2}…" (string trimmed to 100 chars).'.format(prefix, page_title, canonical_name[:100]))
113 <               else:
114 <                 canonical_name = canonical_name.replace('_', ' ')
115 <                 pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect to "{2}".'.format(prefix, page_title, canonical_name))
116 <            errors_issued = errors_issued + 1
117 <         elif '#' in page_title:
118 <            # Isolate section link
119 <            target_page_name, anchor_name = page_title.split('#')
120 <            
121 <            # Convert dot-notation hex entities to proper characters
122 <            anchor_name = anchor_name.replace('.22', '"')
123 <            anchor_name = anchor_name.replace('.27', '\'')
124 <            anchor_name = anchor_name.replace('.28', '(')
125 <            anchor_name = anchor_name.replace('.29', ')')
126 <            
127 <            # Read linked page to see if it really has this anchor link
128 <            soup = BeautifulSoup(response.text, 'html.parser')
129 <            found_section = False
130 <            for span_tag in soup.findAll('span'):
131 <               span_name = span_tag.get('id', None)
132 <               if span_name == anchor_name:
133 <                  found_section = True
134 <                  break
135 <            if found_section == False:
136 <               if not name_printed and not debug:
137 <                  pywikibot.stdout('From page "{}":'.format(page_name))
138 <                  name_printed = 1
139 <               target_page_name_human = target_page_name.replace('_', ' ')
140 <               pywikibot.stdout('   ERROR: Could not find section "{0}" on {1} page "{2}".'.format(anchor_name, prefix, target_page_name_human))
141 <               errors_issued = errors_issued + 1
142 <      cur = cur + 1
211 > # Print a wrap-up message
212 > def print_summary():
213 >   global pages_checked
214 >   global iw_found
215 >   global errors_issued
216 >
217 >   page_str = "pages"
218 >   if pages_checked == 1:
219 >      page_str = "page"
220 >
221 >   link_str = "links"
222 >   if iw_found == 1:
223 >      link_str = "link"
224 >
225 >   pywikibot.stdout('Checked {0} {1} and found {2} interwiki {3}.'.format(pages_checked, page_str, iw_found, link_str))
226 >
227 >   error_str = "errors were"
228 >   if errors_issued == 1:
229 >      error_str = "error was"
230 >
231 >   pywikibot.stdout('{0} {1} encountered in validating these links.'.format(errors_issued, error_str))
232  
233 + # Main function
234   def main(*args):
235     global debug
236     search_cat = ''
237     search_page = ''
238  
239 +   # Process arguments
240     local_args = pywikibot.handle_args(args)
150   genFactory = pagegenerators.GeneratorFactory()
151
241     for arg in local_args:
242        if arg.startswith('-cat:'):
243           search_cat = arg[5:]
# Line 157 | Line 246 | def main(*args):
246        elif arg == '-dbg':
247           debug = 1
248        else:
249 <         pywikibot.stdout('Unknown argument "{}".'.format(arg))
249 >         pywikibot.stdout('Unknown argument "{}". Exiting.'.format(arg))
250           return
251  
163   site = pywikibot.Site()
164
252     #pywikibot.stdout('The members of the requests.models.Response class are:')
253     #pywikibot.stdout(format(dir(requests.models.Response)))
254 <
254 >   #return
255 >  
256 >   # Check specified page or loop through specified category and check all pages
257 >   site = pywikibot.Site()
258     if search_cat != '':
259        cat_obj = pywikibot.Category(site, search_cat)
260        generator = pagegenerators.CategorizedPageGenerator(cat_obj, recurse=True)
# Line 176 | Line 266 | def main(*args):
266        if debug: pywikibot.stdout('Checking page "{}"'.format(page.title()))
267        scan_for_interwiki_links(page.text, page.title())
268  
269 <   global pages_checked
270 <   global iw_found
181 <   global errors_issued
182 <
183 <   page_str = "pages"
184 <   if pages_checked == 1:
185 <      page_str = "page"
186 <
187 <   link_str = "links"
188 <   if iw_found == 1:
189 <      link_str = "link"
190 <
191 <   pywikibot.stdout('Checked {0} {1} and found {2} interwiki {3}.'.format(pages_checked, page_str, iw_found, link_str))
192 <
193 <   error_str = "errors were"
194 <   if errors_issued == 1:
195 <      error_str = "error was"
196 <
197 <   pywikibot.stdout('{0} {1} encountered in validating these links.'.format(errors_issued, error_str))
269 >   # Print the results
270 >   print_summary()
271  
272   if __name__ == '__main__':
273     main()

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)