1
1
<?php
2
2
3
+ use MediaWiki \Hook \ParserFirstCallInitHook ;
4
+ use MediaWiki \Storage \Hook \RevisionDataUpdatesHook ;
5
+
3
6
/**
4
7
* This extension provides a parser function #createpageifnotex that can be used to create
5
8
* additional auxiliary pages when a page is saved. New pages are only created if they do
22
25
* @file
23
26
*/
24
27
25
- class AutoCreatePage {
28
+ class AutoCreatePage implements ParserFirstCallInitHook, RevisionDataUpdatesHook {
26
29
27
- public static function onParserFirstCallInit ( $ parser ) {
30
+ public function onParserFirstCallInit ( $ parser ) {
28
31
$ parser ->setFunctionHook ( 'createPage ' , [ self ::class, 'createPageIfNotExisting ' ] );
29
32
}
30
33
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 () );
33
36
}
34
37
35
38
/**
36
39
* Handles the parser function for creating pages that don't exist yet,
37
40
* filling them with the given default content. It is possible to use <nowiki>
38
41
* 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
39
46
*/
40
47
public static function createPageIfNotExisting ( $ parser , $ newPageTitleText , $ newPageContent ) {
41
48
global $ egAutoCreatePageMaxRecursion , $ egAutoCreatePageIgnoreEmptyTitle ,
@@ -78,19 +85,23 @@ public static function createPageIfNotExisting( $parser, $newPageTitleText, $new
78
85
* Creates pages that have been requested by the create page parser function. This is done only
79
86
* after the safe is complete to avoid any concurrent article modifications.
80
87
* 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
81
93
*/
82
- private static function doCreatePages ( $ article , $ editInfo , $ changed ) {
94
+ private static function doCreatePages ( $ sourceTitle , $ output ) {
83
95
global $ egAutoCreatePageMaxRecursion ;
84
96
85
- $ createPageData = $ editInfo -> output ->getExtensionData ( 'createPage ' );
97
+ $ createPageData = $ output ->getExtensionData ( 'createPage ' );
86
98
if ( $ createPageData === null ) {
87
99
return true ; // no pages to create
88
100
}
89
101
90
102
// Prevent pages to be created by pages that are created to avoid loops:
91
103
$ egAutoCreatePageMaxRecursion --;
92
104
93
- $ sourceTitle = $ article ->getTitle ();
94
105
$ sourceTitleText = $ sourceTitle ->getPrefixedText ();
95
106
96
107
foreach ( $ createPageData as $ pageTitleText => $ pageContentText ) {
@@ -102,13 +113,12 @@ private static function doCreatePages( $article, $editInfo, $changed ) {
102
113
$ pageContent = ContentHandler::makeContent ( $ pageContentText , $ sourceTitle );
103
114
$ newWikiPage ->doEditContent ( $ pageContent ,
104
115
"Page created automatically by parser function on page [[ $ sourceTitleText]] " ); // TODO i18n
105
-
106
116
// wfDebugLog( 'createpage', "CREATED PAGE " . $pageTitle->getText() . " Text: " . $pageContent );
107
117
}
108
118
}
109
119
110
120
// Reset state. Probably not needed since parsing is usually done here anyway:
111
- $ editInfo -> output ->setExtensionData ( 'createPage ' , null );
121
+ $ output ->setExtensionData ( 'createPage ' , null );
112
122
$ egAutoCreatePageMaxRecursion ++;
113
123
114
124
return true ;
0 commit comments