Expose page categories to Lua

Added `categories` property in the lua title class which fetches page
categories. Analogous to getContent() which fetches page content. This
increments the expensive function count.

This enables category-specific editnotices (T85372) and other use-cases.

Bug: T50175
Change-Id: Ie8e0762c537374e6086abe9f9916b8200582776a
This commit is contained in:
Siddharth VP 2023-05-14 00:42:03 +05:30 committed by jenkins-bot
parent db4691da48
commit a414aba445
2 changed files with 36 additions and 0 deletions

View file

@ -28,6 +28,7 @@ class TitleLibrary extends LibraryBase {
'getExpensiveData' => [ $this, 'getExpensiveData' ],
'getUrl' => [ $this, 'getUrl' ],
'getContent' => [ $this, 'getContent' ],
'getCategories' => [ $this, 'getCategories' ],
'getFileInfo' => [ $this, 'getFileInfo' ],
'protectionLevels' => [ $this, 'protectionLevels' ],
'cascadingProtection' => [ $this, 'cascadingProtection' ],
@ -337,6 +338,35 @@ class TitleLibrary extends LibraryBase {
return [ $content ? $content->serialize() : null ];
}
/**
* @internal
* @param string $text
* @return string[][]
*/
public function getCategories( $text ) {
$this->checkType( 'getCategories', 1, $text, 'string' );
$title = Title::newFromText( $text );
if ( !$title ) {
return [ [] ];
}
$page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
$this->incrementExpensiveFunctionCount();
$parserOutput = $this->getParser()->getOutput();
if ( $title->equals( $this->getTitle() ) ) {
$parserOutput->setOutputFlag( ParserOutputFlags::VARY_REVISION );
}
// Record in templatelinks, so edits cause the page to be refreshed
$parserOutput->addTemplate( $title, $title->getArticleID(), $title->getLatestRevID() );
$categoryTitles = $page->getCategories();
$categoryNames = [];
foreach ( $categoryTitles as $title ) {
$categoryNames[] = $title->getText();
}
return [ self::makeArrayOneBased( $categoryNames ) ];
}
/**
* Handler for getFileInfo
* @internal

View file

@ -301,6 +301,12 @@ local function makeTitleObject( data )
end
return data.redirectTarget
end
if k == 'categories' then
if data.categories == nil then
data.categories = php.getCategories( data.prefixedText )
end
return data.categories
end
return data[k]
end,