Skip to content

Commit 5dfeeda

Browse files
committed
Fixed for usage in MW 1.37
- use Hook interfaces and HookHandlers - register for RevisionDataUpdates hook instead of ArticleEditUpdates (which has been removed in MW 1.37)
1 parent 1597e3d commit 5dfeeda

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

AutoCreatePage.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use MediaWiki\Hook\ParserFirstCallInitHook;
4+
use MediaWiki\Storage\Hook\RevisionDataUpdatesHook;
5+
36
/**
47
* This extension provides a parser function #createpageifnotex that can be used to create
58
* additional auxiliary pages when a page is saved. New pages are only created if they do
@@ -22,20 +25,24 @@
2225
* @file
2326
*/
2427

25-
class AutoCreatePage {
28+
class AutoCreatePage implements ParserFirstCallInitHook, RevisionDataUpdatesHook {
2629

27-
public static function onParserFirstCallInit( $parser ) {
30+
public function onParserFirstCallInit( $parser ) {
2831
$parser->setFunctionHook( 'createPage', [ self::class, 'createPageIfNotExisting' ] );
2932
}
3033

31-
public function onArticleEditUpdates( $wikiPage, $editInfo, $changed ) {
32-
self::doCreatePages( $wikiPage, $editInfo, $changed );
34+
public function onRevisionDataUpdates( $title, $renderedRevision, &$updates ) {
35+
return self::doCreatePages( $title, $renderedRevision->getRevisionParserOutput() );
3336
}
3437

3538
/**
3639
* Handles the parser function for creating pages that don't exist yet,
3740
* filling them with the given default content. It is possible to use &lt;nowiki&gt;
3841
* in the default text parameter to insert verbatim wiki text.
42+
* @param Parser $parser
43+
* @param string $newPageTitleText
44+
* @param string $newPageContent
45+
* @return string
3946
*/
4047
public static function createPageIfNotExisting( $parser, $newPageTitleText, $newPageContent ) {
4148
global $egAutoCreatePageMaxRecursion, $egAutoCreatePageIgnoreEmptyTitle,
@@ -78,19 +85,23 @@ public static function createPageIfNotExisting( $parser, $newPageTitleText, $new
7885
* Creates pages that have been requested by the create page parser function. This is done only
7986
* after the safe is complete to avoid any concurrent article modifications.
8087
* Note that article is, in spite of its name, a WikiPage object since MW 1.21.
88+
* @param Title $sourceTitle
89+
* @param ParserOutput $output
90+
* @return bool
91+
* @throws MWContentSerializationException
92+
* @throws MWException
8193
*/
82-
private static function doCreatePages( $article, $editInfo, $changed ) {
94+
private static function doCreatePages( $sourceTitle, $output ) {
8395
global $egAutoCreatePageMaxRecursion;
8496

85-
$createPageData = $editInfo->output->getExtensionData( 'createPage' );
97+
$createPageData = $output->getExtensionData( 'createPage' );
8698
if ( $createPageData === null ) {
8799
return true; // no pages to create
88100
}
89101

90102
// Prevent pages to be created by pages that are created to avoid loops:
91103
$egAutoCreatePageMaxRecursion--;
92104

93-
$sourceTitle = $article->getTitle();
94105
$sourceTitleText = $sourceTitle->getPrefixedText();
95106

96107
foreach ( $createPageData as $pageTitleText => $pageContentText ) {
@@ -102,13 +113,12 @@ private static function doCreatePages( $article, $editInfo, $changed ) {
102113
$pageContent = ContentHandler::makeContent( $pageContentText, $sourceTitle );
103114
$newWikiPage->doEditContent( $pageContent,
104115
"Page created automatically by parser function on page [[$sourceTitleText]]" ); // TODO i18n
105-
106116
// wfDebugLog( 'createpage', "CREATED PAGE " . $pageTitle->getText() . " Text: " . $pageContent );
107117
}
108118
}
109119

110120
// Reset state. Probably not needed since parsing is usually done here anyway:
111-
$editInfo->output->setExtensionData( 'createPage', null );
121+
$output->setExtensionData( 'createPage', null );
112122
$egAutoCreatePageMaxRecursion++;
113123

114124
return true;

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ EXTENSION := AutoCreatePage
33
MW_VERSION ?= 1.35
44

55
EXTENSION_FOLDER := /var/www/html/extensions/$(EXTENSION)
6-
IMAGE_NAME := $(shell echo $(EXTENSION) | tr A-Z a-z}):test-$(MW_VERSION)-$(SMW_VERSION)
6+
IMAGE_NAME := $(shell echo $(EXTENSION) | tr A-Z a-z}):test-$(MW_VERSION)
77
PWD := $(shell bash -c "pwd -W 2>/dev/null || pwd")# this way it works on Windows and Linux
88
DOCKER_RUN_ARGS := --rm -v $(PWD)/coverage:$(EXTENSION_FOLDER)/coverage -w $(EXTENSION_FOLDER) $(IMAGE_NAME)
99
docker_run := docker run $(DOCKER_RUN_ARGS)

extension.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
"description": "Provides a parser function to create additional wiki pages with default content when saving a page.",
1010
"license-name": "GPL-2.0+",
1111
"requires": {
12-
"MediaWiki": ">= 1.31"
12+
"MediaWiki": ">= 1.35"
1313
},
1414
"AutoloadClasses": {
1515
"AutoCreatePage": "AutoCreatePage.php"
1616
},
1717
"Hooks": {
18-
"ParserFirstCallInit": [
19-
"AutoCreatePage::onParserFirstCallInit"
20-
],
21-
"ArticleEditUpdates": [
22-
"AutoCreatePage::onArticleEditUpdates"
23-
]
18+
"ParserFirstCallInit": "main",
19+
"RevisionDataUpdates": "main"
20+
},
21+
"HookHandlers": {
22+
"main": {
23+
"class": "AutoCreatePage"
24+
}
2425
},
2526
"config": {
2627
"AutoCreatePageMaxRecursion": 1,

0 commit comments

Comments
 (0)