Changeset 1198


Ignore:
Timestamp:
Sep 21, 2025, 11:50:56 PM (33 hours ago)
Author:
iritscen
Message:

ValBot: Corrected last attempt at printing correct page name when a redirect occurs, and generally straightened out code a bit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ValBot/Python/check_interwiki_links.py

    r1197 r1198  
    11# Check Interwiki Links
    22# by iritscen@yahoo.com
    3 # Looks at each link on a page (or all the pages in a category) which uses a registered
    4 # interwiki prefix and loads the linked page, verifying that it exists and that any section
    5 # link, if present, is valid as well. The output will use the word "ERROR" when it cannot
    6 # validate the interwiki link.
     3# Looks at each link on a page (or all the pages in a category) which uses a registered interwiki prefix and loads the linked page, verifying that it exists and that
     4# any section link, if present, is valid as well. The output will use the word "ERROR" when it cannot validate the interwiki link.
    75# Recommended viewing width:
    8 # |---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---|
     6# |---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----|
    97
    108import bs4
     
    2220
    2321class IWLink:
    24    def __init__(self, iw_prefix, prefix_url, full_url, page_name, page_slug, curl_response):
     22   def __init__(self, iw_prefix, prefix_url, full_url, page_name, page_name_only, page_slug, hosting_page, curl_response):
    2523      self.iw_prefix = iw_prefix # e.g. "wp"
    2624      self.prefix_url = prefix_url # e.g. "https://en.wikipedia.org/wiki/"
    27       self.full_url = full_url # e.g. "https://en.wikipedia.org/wiki/Easter_egg"
    28       self.page_name = page_name # "Easter egg"
    29       self.page_slug = page_slug # "Easter_egg"
     25      self.full_url = full_url # e.g. "https://en.wikipedia.org/wiki/Marathon_(series)#Rampancy"
     26      self.page_name = page_name # "Marathon (series)#Rampancy"
     27      self.page_name_only = page_name # "Marathon (series)"
     28      self.page_slug = page_slug # "Marathon_(series)#Rampancy"
     29      self.hosting_page = hosting_page # "Easter eggs"; page where the link was found
    3030      self.curl_response = curl_response # a class defined in the Requests library
    3131
     
    4444
    4545# Prints the name of a page on which something occurred, if it has not been printed before
    46 def possibly_print(page_name):
     46def possibly_print(the_link):
    4747   global debug
    4848   global name_printed
     
    5050   if not name_printed and not debug:
    5151      pywikibot.stdout('')
    52       pywikibot.stdout('From page "{}":'.format(page_name))
     52      pywikibot.stdout('From page "{}":'.format(the_link.hosting_page))
    5353      name_printed = 1
    5454
     
    5858
    5959   # Isolate section link
    60    target_page_name, anchor_name = the_link.page_slug.split('#')
    61    target_page_name_human = target_page_name.replace('_', ' ')
     60   _, anchor_name = the_link.page_slug.split('#')
    6261   
    6362   # Convert dot-notation hex entities to proper characters
     
    8079   # Tell user what we found
    8180   if found_section == False:
    82       possibly_print(the_link.page_name)
    83       pywikibot.stdout('   ERROR: Could not find section "{0}" on {1} page "{2}".'.format(anchor_name, the_link.iw_prefix, target_page_name_human))
     81      possibly_print(the_link)
     82      pywikibot.stdout('   ERROR: Could not find section "{0}" on {1} page "{2}".'.format(anchor_name, the_link.iw_prefix, the_link.page_name))
    8483      errors_issued = errors_issued + 1
    8584   elif print_result == True:
    86       pywikibot.stdout('   The section "{0}" was found on {1} page "{2}".'.format(anchor_name, the_link.iw_prefix, target_page_name_human))
    87 
    88 # For a link that redirected us to another page, extract the name of the target page from
    89 # the target page's source
     85      pywikibot.stdout('   The section "{0}" was found on {1} page "{2}".'.format(anchor_name, the_link.iw_prefix, the_link.page_name))
     86
     87# For a link that redirected us to another page, extract the name of the target page from the target page's source
    9088def find_canonical_link(the_link):
    9189   # Extract link from this markup which contains name of redirected-to page:
     
    102100      canonical_name = canonical_name[:tag_end]
    103101      if len(canonical_name) > 100:
    104          # Certain things can cause the trim to fail; report error and avoid slamming the
    105          # output with massive page source from a failed trim
     102         # Certain things can cause the trim to fail; report error and avoid slamming the output with massive page source from a failed trim
    106103         pywikibot.stdout('   ERROR: The {0} link "{1}" is a redirect to "{2}…" (string overflow).'.format(the_link.iw_prefix, the_link.page_slug, canonical_name[:100]))
    107104         errors_issued = errors_issued + 1
    108105      else:
    109          canonical_name = canonical_name.replace('_', ' ')
     106         the_link.page_name = canonical_name.replace('_', ' ')
    110107         if '#' in the_link.page_slug:
    111             _, anchor_name = the_link.page_slug.split('#')
    112             pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}#{3}", which is a valid page. Checking for section on that page….'.format(the_link.iw_prefix, the_link.page_slug, canonical_name, anchor_name))
    113             the_link.page_slug = the_link.page_slug.replace(the_link.page_name, canonical_name) # update page slug so that find_section() uses the right page name in its messages
     108            the_link.page_name_only, _ = the_link.page_slug.split('#')
     109            pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}", which is a valid page. Checking for section on that page….'.format(the_link.iw_prefix, the_link.page_name_only, the_link.page_name))
    114110            find_section(the_link, True)
    115111         else:
    116             pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}", which is a valid page.'.format(the_link.iw_prefix, the_link.page_slug, canonical_name))
     112            pywikibot.stdout('   The {0} link "{1}" is a redirect to "{2}", which is a valid page.'.format(the_link.iw_prefix, the_link.page_slug, the_link.page_name))
    117113
    118114# Test an interwiki link and look for a section link if applicable
     
    123119   the_link.curl_response = fetch(the_link.full_url)
    124120
    125    # One way we tell that a redirect occurred is by checking fetch's history, as it
    126    # automatically follows redirects. This will catch formal redirects which come from pages
    127    # such as Special:PermanentLink.
     121   # One way we tell that a redirect occurred is by checking fetch's history, as it automatically follows redirects. This will catch formal redirects which come from
     122   # pages such as Special:PermanentLink.
    128123   if the_link.curl_response.history != []:
    129       possibly_print(the_link.page_name)
     124      possibly_print(the_link)
    130125     
    131126      # If linked page is in all caps, e.g. WP:BEANS, it's likely a deliberate use of a redirect
     
    144139            errors_issued = errors_issued + 1
    145140   elif the_link.curl_response.status_code != 200:
    146       possibly_print(the_link.page_name)
     141      possibly_print(the_link)
    147142      pywikibot.stdout('   ERROR: Got response code {0} for {1} link "{2}". The page may not exist.'.format(the_link.curl_response.status_code, the_link.iw_prefix, the_link.page_slug))
    148143      errors_issued = errors_issued + 1
    149    # However the usual way that a redirect occurs is that MediaWiki redirects us sneakily
    150    # using JavaScript, while returning code OK 200 as if the link was correct; this happens
    151    # when a redirect page is accessed. We must detect these soft redirects by looking at the
    152    # page source to find the redirect note inserted at the top of the page for the reader.
     144   # However the usual way that a redirect occurs is that MediaWiki redirects us sneakily using JavaScript, while returning code OK 200 as if the link was correct; this
     145   # happens when a redirect page is accessed. We must detect these soft redirects by looking at the page source to find the redirect note inserted at the top of the
     146   # page for the reader.
    153147   elif 'Redirected from <a' in the_link.curl_response.text:
    154148      unintended_redirects_found = unintended_redirects_found + 1
    155       possibly_print(the_link.page_name)
     149      possibly_print(the_link)
    156150      pywikibot.stdout('   WARNING: Got silently redirected by {0} link "{1}". Checking the target page….'.format(the_link.iw_prefix, the_link.page_slug))
    157       find_canonical_link(the_link)
     151      find_canonical_link(the_link) # calls find_section() at end
    158152   elif '#' in the_link.page_slug:
    159153      find_section(the_link, False)
     
    173167      iw_link = r"\[\[" + prefix + r":[^|\]]*(\||\])"
    174168      for match in re.finditer(iw_link, page_text):
    175          the_link = IWLink(prefix, interwiki_urls[cur_prefix], "", page_name, "", "")
     169         the_link = IWLink(prefix, interwiki_urls[cur_prefix], "", "", "", "", page_name, "")
    176170     
    177171         # Extract just the page title from this regex match
     
    179173         e = match.end() - 1
    180174
    181          # Commonly we use spaces instead of underscores, so fix that before querying
     175         # Use underscores in slug used to constructed URL, but retain spaces for printable name
    182176         the_link.page_slug = page_text[s:e].replace(' ', '_')
    183 
    184          # But use spaces for title when printing it
    185          page_title_human = the_link.page_slug.replace('_', ' ')
    186          if debug: pywikibot.stdout('   Validating {0} link "{1}"'.format(the_link.iw_prefix, page_title_human))
     177         the_link.page_name = page_text[s:e]
     178         if debug: pywikibot.stdout('   Validating {0} link "{1}"'.format(the_link.iw_prefix, the_link.page_name))
    187179         iw_found = iw_found + 1
    188180
Note: See TracChangeset for help on using the changeset viewer.