From dc05fc51b5f743a85f8bda554b9189b596e19bbd Mon Sep 17 00:00:00 2001 From: Tobias Oetterer Date: Mon, 6 Nov 2023 20:26:55 +0100 Subject: [PATCH] fix incompatibility with MW REL1_39 (#9) * 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 --- README.md | 8 +++-- extension.json | 3 ++ i18n/en.json | 7 ++++- src/AutoCreatePageHooks.php | 58 ++++++++++++++++++++----------------- 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3f318c9..486e56f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/extension.json b/extension.json index cee0393..14bbc72 100644 --- a/extension.json +++ b/extension.json @@ -32,6 +32,9 @@ "AutoCreatePageIgnoreEmptyTitle": { "value": false }, + "AutoCreatePageIgnoreEmptyContent": { + "value": false + }, "AutoCreatePageMaxRecursion": { "value": 1 }, diff --git a/i18n/en.json b/i18n/en.json index af24a3a..5e5593e 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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]]" } diff --git a/src/AutoCreatePageHooks.php b/src/AutoCreatePageHooks.php index 2176de8..511d4ad 100644 --- a/src/AutoCreatePageHooks.php +++ b/src/AutoCreatePageHooks.php @@ -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++; } @@ -64,22 +62,28 @@ 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 ''; } @@ -87,7 +91,7 @@ public static function createPageIfNotExisting( Parser $parser, string $newPageT $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 ''; }