diff --git a/AutoCreatePage.i18n.magic.php b/AutoCreatePage.i18n.magic.php index ab99a63..9c4ae06 100644 --- a/AutoCreatePage.i18n.magic.php +++ b/AutoCreatePage.i18n.magic.php @@ -11,4 +11,5 @@ /** English (English) */ $magicWords['en'] = array( 'createPage' => array( 0, 'createpageifnotex' ), + 'createOrReplacePage' => array( 0, 'createorreplacepage' ), ); diff --git a/AutoCreatePage.php b/AutoCreatePage.php index 9d01ca2..a1c716e 100644 --- a/AutoCreatePage.php +++ b/AutoCreatePage.php @@ -47,7 +47,14 @@ $GLOBALS['wgHooks']['ParserFirstCallInit'][] = function ( \Parser &$parser ) { $parser->setFunctionHook( 'createPage', function( $parser ) { - return createPageIfNotExisting( func_get_args() ); + return createPage( false, func_get_args() ); + } ); + + }; + $GLOBALS['wgHooks']['ParserFirstCallInit'][] = function ( \Parser &$parser ) { + + $parser->setFunctionHook( 'createOrReplacePage', function( $parser ) { + return createPage( true, func_get_args() ); } ); }; @@ -56,11 +63,15 @@ }; /** - * Handles the parser function for creating pages that don't exist yet, - * filling them with the given default content. It is possible to use <nowiki> + * Handles the parser function for creating/editing pages, + * filling them with the given content. It is possible to use <nowiki> * in the default text parameter to insert verbatim wiki text. + * + * @param $canReplace true indicates that the content will be replaced with a new + * revision if the page already exists, false indicates that the + * existing page will be left untouched. */ -function createPageIfNotExisting( array $rawParams ) { +function createPage( $canReplace, array $rawParams ) { global $wgContentNamespaces, $gEnablePageCreation; if ( !$gEnablePageCreation ) { @@ -71,6 +82,7 @@ function createPageIfNotExisting( array $rawParams ) { $parser = $rawParams[0]; $newPageTitleText = $rawParams[1]; $newPageContent = $rawParams[2]; + $newPageMessage = $rawParams[3]; } if ( empty( $newPageTitleText ) ) { @@ -88,9 +100,12 @@ function createPageIfNotExisting( array $rawParams ) { // Store data in the parser output for later use: $createPageData = $parser->getOutput()->getExtensionData( 'createPage' ); if ( is_null( $createPageData ) ) { - $createPageData = array(); + $createPageData = array( + false => array(), + true => array() + ); } - $createPageData[$newPageTitleText] = $newPageContent; + $createPageData[$canReplace][$newPageTitleText] = array( $newPageContent, $newPageMessage ); $parser->getOutput()->setExtensionData( 'createPage', $createPageData ); return ""; @@ -115,17 +130,23 @@ function doCreatePages( &$article, &$editInfo, $changed ) { $sourceTitle = $article->getTitle(); $sourceTitleText = $sourceTitle->getPrefixedText(); - foreach ( $createPageData as $pageTitleText => $pageContentText ) { - $pageTitle = Title::newFromText( $pageTitleText ); - // wfDebugLog( 'createpage', "CREATE " . $pageTitle->getText() . " Text: " . $pageContent ); - - if ( !is_null( $pageTitle ) && !$pageTitle->isKnown() && $pageTitle->canExist() ){ - $newWikiPage = new WikiPage( $pageTitle ); - $pageContent = ContentHandler::makeContent( $pageContentText, $sourceTitle ); - $newWikiPage->doEditContent( $pageContent, - "Page created automatically by parser function on page [[$sourceTitleText]]" ); //TODO i18n - - // wfDebugLog( 'createpage', "CREATED PAGE " . $pageTitle->getText() . " Text: " . $pageContent ); + foreach ( $createPageData as $canReplace => $pageData ) { + foreach ( $pageData as $pageTitleText => $pageUpdate ) { + list( $pageContentText, $pageComment ) = $pageUpdate; + $pageTitle = Title::newFromText( $pageTitleText ); + // wfDebugLog( 'createpage', "CREATE " . $pageTitle->getText() . " Text: " . $pageContent ); + + if ( is_null( $pageTitle ) ) { + continue; + } + $isKnown = $pageTitle->isKnown(); + if ( !is_null( $pageTitle ) && ( $canReplace || !$isKnown ) && $pageTitle->canExist() ) { + $newWikiPage = new WikiPage( $pageTitle ); + $pageContent = ContentHandler::makeContent( $pageContentText, $sourceTitle ); + $message = isset( $pageComment ) ? $pageComment : 'Page ' . ( $isKnown ? 'upd' : 'cre' ) . "ated automatically by parser function on page [[$sourceTitleText]]"; //TODO i18n + $newWikiPage->doEditContent( $pageContent, $message ); + // wfDebugLog( 'createpage', "CREATED PAGE " . $pageTitle->getText() . " Text: " . $pageContent ); + } } } diff --git a/README.md b/README.md index 8a094f4..cf2b7ee 100644 --- a/README.md +++ b/README.md @@ -24,18 +24,31 @@ displays all relevant data, but this time with English labels). Both of these uses were pioneered by the [AIFB Portal Wiki](http://www.aifb.kit.edu/). +Another usage of this function is for bulk-importing data (using an extension like +ExternalData) and then bulk-generating wiki pages from it. Particularly useful for +creating multiple semantic pages in Semantic MediaWiki or Cargo based on the contents +of external data. + Usage ----- -The extension provides one parser function, `createpageifnotex` that takes a page -title and a text to use when creating the page. Example: +The extension provides two parser functions: +* `createpageifnotex` - creates a new page but will not overwrite an existing page. +* `createorreplacepage` - will either create a new page if it doesn't exist, or else create a new revision for a page if it already exists. + +Both functions take two or three positional parameters: +1. a page title; +2. text to use when creating the page; +3. (optional) the text to use for the commit message. The default is a message that says *Page created/updated automatically by parser function on page *. + +An example: `{{#createpageifnotex:Test page 1|The content of test page 1}}` -Pages are only created if they don't exist yet. The author of the new page is the -user who saved the edit to the page that contained the parser function call. Pages -are only created when saving an edit, never when viewing a page. +The author of the new page/revision is the user who saved the edit to the page +that contained the parser function call. Pages are only created when +saving an edit, never when viewing a page. Pages are only created if the parser function is used on a page in one of MediaWiki's content namespaces. Nothing will happen if the parser function is called on, e.g.,