Topic on Project:Support desk

How to access page name and URL from within LocalSettings.php?

5
Sampson2023 (talkcontribs)

I am using a $wgHooks to insert some meta information in the <HEAD> section of the page.

How can I access the page's title and URL from within LocalSettings.php?

Bawolff (talkcontribs)

It can vary depending on hook but generally:

RequestContext::getMain()->getTitle()->getFullUrl()

RequestContext::getMain()->getTitle()->getPrefixedText()


(In a future version you might have to use \MediaWiki\Context\RequestContext::getMain()->getTitle()->getFullUrl() instead)

Sampson2023 (talkcontribs)

That's amazing, it worked! Many thanks :-)

How about getting the revision timestamp, in the same way?

Bawolff (talkcontribs)

Its more complicated... you'd have to do something like MediaWiki\MediawikiServices->getRevisionLookop()->getRevisionByTitle( RequestContext::getMain()->getTitle() )->getTimestamp();

The timestamp would be in machine format so you would need to format it with RequestContext::getMain()->getLanguage()->timeanddate() if you want it based on user preference, or wfTimestamp() if you need a specific format.


I didn't test any of this, so it might be slightly off, but should be roughly right.

There are docs for mediawiki classes at https://doc.wikimedia.org/mediawiki-core/master/php/ the classes i have mentioned are RequestContext, Title, RevisionRecord, RevisionLookup, Language

Sampson2023 (talkcontribs)

Brilliant, I got it working, thank you so much!

Reply to "How to access page name and URL from within LocalSettings.php?"