Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pages, change commit message #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added parser functions to replace page content if desired. Added para…
…meter to override default commit message.
kriegfrj committed Feb 22, 2016
commit 1c0bb492846cada9b2cba8d78dcf9548ad336e9c
1 change: 1 addition & 0 deletions AutoCreatePage.i18n.magic.php
Original file line number Diff line number Diff line change
@@ -11,4 +11,5 @@
/** English (English) */
$magicWords['en'] = array(
'createPage' => array( 0, 'createpageifnotex' ),
'createOrReplacePage' => array( 0, 'createorreplacepage' ),
);
55 changes: 38 additions & 17 deletions AutoCreatePage.php
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}

23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <link to 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.,