2

I need to replicate exactly, Wikipedia's behavior regarding the conversion of [[links]] in the page body to URLs that open the articles they refer to.

The link syntax and its behavior (conversion) are described here. While that article only mentions a few very basic modifications being applied, I think that's not the whole story.

I tried searching the MediaWiki API reference, but haven't found the particular piece of code I was looking for. Is anyone familiar enough with the code to know which PHP function (presumably related to editing/submitting) I might be talking about?

Adrian
  • 2,276
  • 2
  • 19
  • 25

1 Answers1

5

There is two parts to this:

  1. Parsing the link syntax.
  2. Generating a URL from a page title.

Parsing the link is done by the Parser class, which does all wikitext processing. Links can be more complex that just [[Foo]], e.g. [[Foo#bar|something]], where Foo is the target page, #bar is a section, and "something" is the "surface text" of the resulting link. For generating a URL, you need the target page and section. To generate a link (in HTML), you also need the surface text (if not given, it's the same as the target page + section). There is currently no easy way to just parse a link, without full wikitext processing.

If you already have the target page title (and maybe a section id), you have two choices of getting a URL for it: the old school (monolithic) way, or the new style (service based) way.

The new style method is:

  • Use MediaWikiTitleCodec::parseTitle to generate a TitleValue object from the string(s) you have.
  • Use MediaWikiPageLinkRenderer::getPageURL to get the page URL for a given TitleValue object.

The old school method is:

  • Use Title::newFromText() to creae a Title object from the string(s) you have.
  • Use Title::getFullURL() to get the page URL.

The old style is easier, because it doesn't require you to create service objects. It relies on global state and has all dependencies hardcoded. The new style uses simple dependency injection, which allows all parts to be swapped out individually and be tested independently - but it requires a little more code.

brightbyte
  • 971
  • 4
  • 10
  • Thank you brightbyte, that's exactly what I was looking for. I'm already browsing the code sections in question. I need to replicated the same behavior in another language, thus my interest. – Adrian Feb 15 '15 at 15:08
  • There are several bot/client frameworks for MediaWiki, like pywikibot or JWBF, see https://de.wikipedia.org/wiki/Wikipedia:Bots#MediaWiki-Bot-Frameworks . They are bound to have functions for converting page titles to full URLs. Perhaps have a look there. – brightbyte Feb 16 '15 at 18:19