mwn icon indicating copy to clipboard operation
mwn copied to clipboard

Redirects to not-existant articles handled inconsistently

Open JN-Jones opened this issue 1 year ago • 2 comments

I have a simple article that redirects to a missing article. When creating the page instance and immediately calling getRedirectTarget or isRedirect the call throws a MwnErrorMissingPage error because the api (obviously) returns that the target doesn't exist. However when first calling text on the page instance, getRedirectTarget uses the text of the article to determine the (intended) redirect target and correctly returns it. Imho the (externally observed) behaviour shouldn't change based on other calls I might've done before, ideally returning the target either way.

While checking the function I've seen that there's also a regex in place, however that didn't work at all for me and it also doesn't consider localizations (eg in a german wiki #weiterleitung and #redirect are valid).

JN-Jones avatar Aug 01 '24 18:08 JN-Jones

All valid points. Unfortunately, the read operations in MwnPage are a mess. In addition, many of the methods in this class use action=parse which invokes the MediaWiki parser instead of reading pre-parsed data from the db. All of this needs to be resolved in a v3 release as they involve breaking changes. For now, I'd suggest using lower-level methods, bot.read() or bot.query(), to get the data directly.

siddharthvp avatar Aug 01 '24 19:08 siddharthvp

For now I've switched back to an implementation from an old bot of mine which simply uses a regex instead of the library provided ones:

  private getRedirectTargetFromText = async () => {
    const text = await this.page.text();
    const match = text.match(/\s*#(redirect|weiterleitung)\s*\[\[([^|]*)(\|.*)?\]\]/is);
    if (match !== null && match.length > 3) {
      return match[2];
    }

    return false;
  };

Obviously currently only handles english and german, which would need to be adjusted, but maybe this would be an idea? At least in the wiki the bot is currently running this catches all cases.

JN-Jones avatar Aug 01 '24 20:08 JN-Jones