mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-27 17:50:06 +00:00
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:
parent
db4691da48
commit
a414aba445
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue