Skip to content

Commit 18a9546

Browse files
committed
Initial version of extension (experimental)
1 parent 3e72c77 commit 18a9546

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

CreatePage.i18n.magic.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Magic words
5+
*
6+
* @file
7+
*/
8+
9+
$magicWords = array();
10+
11+
/** English (English) */
12+
$magicWords['en'] = array(
13+
'createPage' => array( 0, 'createpageifnotex' ),
14+
);

CreatePage.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/**
4+
* This extension provides a parser function #createpageifnotex that can be used to create
5+
* additional auxiliary pages when a page is saved. New pages are only created if they do
6+
* not exist yet. The function takes two parameters: (1) the title of the new page,
7+
* (2) the text to be used on the new page. It is possible to use &lt;nowiki&gt; tags in the
8+
* text to inserst wiki markup more conveniently.
9+
*
10+
* The created page is attributed to the user who made the edit. The original idea for this
11+
* code was edveloped by Daniel Herzig at AIFB Karlsruhe. In his code, there were some further
12+
* facilities to show a message to the user about the pages that have been auto-created. This
13+
* is not implemented here yet (the basic way of doing this would be to insert some custom
14+
* HTML during 'OutputPageBeforeHTML').
15+
*
16+
* The code restricts the use of the parser function to MediaWiki content namespaces. So
17+
* templates, for example, cannot create new pages by accident. Also, the code prevents created
18+
* pages from creating further pages to avoid (unbounded) chains of page creations.
19+
*
20+
* @author Markus Kroetzsch
21+
* @author Daniel Herzig
22+
* @file
23+
*/
24+
25+
if ( !defined( 'MEDIAWIKI' ) ) {
26+
die( 'Not an entry point.' );
27+
}
28+
29+
/**
30+
* This is set to false during page creation to avoid recursive creation of pages.
31+
*/
32+
$gEnablePageCreation = true;
33+
34+
$GLOBALS['wgExtensionCredits']['other'][] = array(
35+
'name' => 'Create Page',
36+
'version' => '0.4',
37+
'author' => '[http://korrekt.org Markus Krötzsch], Daniel Herzig',
38+
'url' => ' ',
39+
'description' => 'Provides a parser function to create additional wiki pages with default content when saving a page.', //TODO i18n
40+
'license-name' => 'GPL-2.0+'
41+
);
42+
43+
$GLOBALS['wgExtensionMessagesFiles']['CreatePageMagic'] = dirname(__FILE__) . '/CreatePage.i18n.magic.php';
44+
45+
$GLOBALS['wgExtensionFunctions'][] = function() {
46+
47+
$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function ( \Parser &$parser ) {
48+
49+
$parser->setFunctionHook( 'createPage', function( $parser ) {
50+
return createPageIfNotExisting( func_get_args() );
51+
} );
52+
53+
};
54+
55+
$GLOBALS['wgHooks']['ArticleEditUpdates'][] = 'doCreatePages';
56+
};
57+
58+
/**
59+
* Handles the parser function for creating pages that don't exist yet,
60+
* filling them with the given default content. It is possible to use &lt;nowiki&gt;
61+
* in the default text parameter to insert verbatim wiki text.
62+
*/
63+
function createPageIfNotExisting( array $rawParams ) {
64+
global $wgContentNamespaces, $gEnablePageCreation;
65+
66+
if ( !$gEnablePageCreation ) {
67+
return "Error: auto-created pages cannot create other pages."; //TODO i18n
68+
}
69+
70+
if ( isset( $rawParams[2] ) && isset( $rawParams[1] ) && isset( $rawParams[2] ) ) {
71+
$parser = $rawParams[0];
72+
$newPageTitleText = $rawParams[1];
73+
$newPageContent = $rawParams[2];
74+
}
75+
76+
if ( empty( $newPageTitleText ) ) {
77+
return "Error: this function must be given a valid title text for the page to be created."; //TODO i18n
78+
}
79+
80+
$sourcetitle = $parser->getTitle();
81+
82+
// Create pages only if in contentnamespace (not for templates, forms etc.)
83+
if ( !in_array( $sourcetitle->getNamespace(), $wgContentNamespaces ) ) {
84+
return "";
85+
}
86+
87+
// Get the raw text of $newPageContent as it was before stripping <nowiki>:
88+
$newPageContent = $parser->mStripState->unstripNoWiki( $newPageContent );
89+
90+
// Store data in the parser output for later use:
91+
$createPageData = $parser->getOutput()->getExtensionData( 'createPage' );
92+
if ( is_null( $createPageData ) ) {
93+
$createPageData = array();
94+
}
95+
$createPageData[$newPageTitleText] = $newPageContent;
96+
$parser->getOutput()->setExtensionData( 'createPage', $createPageData );
97+
98+
return "";
99+
}
100+
101+
/**
102+
* Creates pages that have been requested by the creat page parser function. This is done only
103+
* after the safe is complete to avoid any concurrent article modifications.
104+
* Note that article is, in spite of its name, a WikiPage object since MW 1.21.
105+
*/
106+
function doCreatePages( &$article, &$editInfo, $changed ) {
107+
global $gEnablePageCreation;
108+
109+
$createPageData = $editInfo->output->getExtensionData( 'createPage' );
110+
if ( is_null( $createPageData ) ) {
111+
return true; // no pages to create
112+
}
113+
114+
// Prevent pages to be created by pages that are created to avoid loops:
115+
$gEnablePageCreation = false;
116+
117+
$sourceTitle = $article->getTitle();
118+
$sourceTitleText = $sourceTitle->getPrefixedText();
119+
120+
foreach ( $createPageData as $pageTitleText => $pageContentText ) {
121+
$pageTitle = Title::newFromText( $pageTitleText );
122+
// wfDebugLog( 'createpage', "CREATE " . $pageTitle->getText() . " Text: " . $pageContent );
123+
124+
if ( !is_null( $pageTitle ) && !$pageTitle->isKnown() && $pageTitle->canExist() ){
125+
$newWikiPage = new WikiPage( $pageTitle );
126+
$pageContent = ContentHandler::makeContent( $pageContentText, $sourceTitle );
127+
$newWikiPage->doEditContent( $pageContent,
128+
"Page created automatically by parser function on page [[$sourceTitleText]]" ); //TODO i18n
129+
130+
// wfDebugLog( 'createpage', "CREATED PAGE " . $pageTitle->getText() . " Text: " . $pageContent );
131+
}
132+
}
133+
134+
// Reset state. Probably not needed since parsing is usually done here anyway:
135+
$editInfo->output->setExtensionData( 'createPage', null );
136+
$gEnablePageCreation = true;
137+
138+
return true;
139+
}
140+

0 commit comments

Comments
 (0)