Skip to content

Commit

Permalink
fix incompatibility with MW REL1_39 (#9)
Browse files Browse the repository at this point in the history
* fix incompatibility with MW REL1_39

WikiPage:doEditContent has been removed, page update is being
refactored.

Please check out

  https://github.com/wikimedia/mediawiki/blob/master/docs/pageupdater.md

This commit modifies AutoCreatePageHooks::onRevisionDataUpdates to take
care of these changes.

* add message support
  • Loading branch information
oetterer authored Nov 6, 2023
1 parent a4416e7 commit dc05fc5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ LocalSettings.php.
`$wgAutoCreatePageMaxRecursion`: The maximum recursion depth to which calls of
`createpageifnotex` are executed on created pages. Default: 1.

`$wgAutoCreatePageIgnoreEmptyContent`: If invocations of `createpageifnotex`
should silently ignore missing page content. Default: false (will put an error
message on the wiki page).

`$wgAutoCreatePageIgnoreEmptyTitle`: If invocations of `createpageifnotex`
should be silently ignored. Default: false (will put an error message on the
wiki page).
should silently ignore missing page title. Default: false (will put an error
message on the wiki page).

`$wgAutoCreatePageNamespaces`: The list of namespaces in which calls of
`createpageifnotex` are executed. If your call originates from a page not in
Expand Down
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"AutoCreatePageIgnoreEmptyTitle": {
"value": false
},
"AutoCreatePageIgnoreEmptyContent": {
"value": false
},
"AutoCreatePageMaxRecursion": {
"value": 1
},
Expand Down
7 changes: 6 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
"Universal Omega"
]
},
"autocreatepage-desc": "Provides a parser function to create additional wiki pages with default content when saving a page."
"autocreatepage-desc": "Provides a parser function to create additional wiki pages with default content when saving a page.",
"autocreatepage-error-empty-content": "Error: For a page to be created, page content must be provided.",
"autocreatepage-error-empty-title": "Error: this function must be given a valid title text for the page to be created. $1",
"autocreatepage-error-missing-hook-parameter": "Hook invoked with missing parameters.",
"autocreatepage-error-recursion-level-exceeded": "Error: Recursion level for auto-created pages exceeded.",
"autocreatepage-revision-comment": "Page created automatically by parser function on page [[$1]]"
}
58 changes: 31 additions & 27 deletions src/AutoCreatePageHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,39 @@ class AutoCreatePageHooks {
public static function onRevisionDataUpdates( Title $title, RenderedRevision $renderedRevision, array &$updates ) {
global $wgAutoCreatePageMaxRecursion;

$wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
$wikiPage = $wikiPageFactory->newFromTitle( $title );
$output = $renderedRevision->getRevisionParserOutput();
$createPageData = $output->getExtensionData( 'createPage' );

$options = $wikiPage->makeParserOptions( RequestContext::getMain() );
$output = $wikiPage->getParserOutput( $options );
$edit = new PreparedEdit();

$edit->parserOutputCallback = static function () use ( $output ) {
return $output;
};

$createPageData = $edit->getOutput()->getExtensionData( 'createPage' );
if ( is_null( $createPageData ) ) {
return true; // no pages to create
}

// Prevent pages to be created by pages that are created to avoid loops:
$wgAutoCreatePageMaxRecursion--;

$sourceTitle = $wikiPage->getTitle();
$sourceTitleText = $sourceTitle->getPrefixedText();
$sourceTitleText = $title->getPrefixedText();

foreach ( $createPageData as $pageTitleText => $pageContentText ) {
$pageTitle = Title::newFromText( $pageTitleText );

if ( !is_null( $pageTitle ) && !$pageTitle->isKnown() && $pageTitle->canExist() ){
$newWikiPage = $wikiPageFactory->newFromTitle( $pageTitle );
$pageContent = ContentHandler::makeContent( $pageContentText, $sourceTitle );
$newWikiPage->doEditContent( $pageContent,
"Page created automatically by parser function on page [[$sourceTitleText]]" ); //TODO i18n
$newWikiPage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $pageTitle );
$pageContent = ContentHandler::makeContent( $pageContentText, $pageTitle );

// WikiPage:doEditContent has been removed, page update is being refactored.
// please check out https://github.com/wikimedia/mediawiki/blob/master/docs/pageupdater.md
// the following takes care of this change for REL1_39.
$updater = $newWikiPage->newPageUpdater( $renderedRevision->getRevision()->getUser() );
$updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN, $pageContent );
$updater->setRcPatrolStatus( RecentChange::PRC_PATROLLED );
$comment = CommentStoreComment::newUnsavedComment(
wfMessage( 'autocreatepage-revision-comment', $sourceTitleText )->inContentLanguage()->text()
);
$updater->saveRevision( $comment );
}
}

// Reset state. Probably not needed since parsing is usually done here anyway:
$edit->getOutput()->setExtensionData( 'createPage', null );
$output->setExtensionData( 'createPage', null );
$wgAutoCreatePageMaxRecursion++;
}

Expand All @@ -64,30 +62,36 @@ public static function onParserFirstCallInit( Parser $parser ) {
* @param string $newPageTitleText
* @param string $newPageContent
* @return string
*
* @throws MWException
*/
public static function createPageIfNotExisting( Parser $parser, string $newPageTitleText, string $newPageContent ) {
global $wgAutoCreatePageMaxRecursion, $wgAutoCreatePageIgnoreEmptyTitle,
$wgAutoCreatePageNamespaces, $wgContentNamespaces;
$wgAutoCreatePageNamespaces, $wgContentNamespaces, $wgAutoCreatePageIgnoreEmptyContent;

if ( $wgAutoCreatePageMaxRecursion <= 0 ) {
return 'Error: Recursion level for auto-created pages exeeded.'; //TODO i18n
}

if ( !isset( $parser ) || !isset( $newPageTitleText ) || !isset( $newPageContent ) ) {
throw new MWException( 'Hook invoked with missing parameters.' );
return wfMessage( 'autocreatepage-error-recursion-level-exceeded' )->inContentLanguage()->text();
}

if ( empty( $newPageTitleText ) ) {
if ( $wgAutoCreatePageIgnoreEmptyTitle === false ) {
return 'Error: this function must be given a valid title text for the page to be created.'; //TODO i18n
return wfMessage( 'autocreatepage-error-empty-title' )->inContentLanguage()->text();
} else {
return '';
}
}

if ( !isset( $newPageContent ) ) {
if ( $wgAutoCreatePageIgnoreEmptyContent === false ) {
return wfMessage( 'autocreatepage-error-empty-content' )->inContentLanguage()->text();
} else {
return '';
}
}

$namespaces = $wgAutoCreatePageNamespaces ?: $wgContentNamespaces;
// Create pages only if the page calling the parser function is within defined namespaces
if ( !in_array( $parser->getTitle()->getNamespace(), $namespaces ) ) {
if ( !in_array( $parser->getPage()->getNamespace(), $namespaces ) ) {
return '';
}

Expand Down

0 comments on commit dc05fc5

Please sign in to comment.