Extension:AutoCreateTalkPage

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
AutoCreateTalkPage

Release status: stable

Implementation Page action
Description When a page is created in the Main namespace, a corresponding talk page is also created.
Author(s) Timneu22 (wikipedia), Timneu22
Latest version 1.0.0.1 (8 April 2010)
MediaWiki 1.15.2, but probably works with most versions before this.
License GPL
Download None. Source code is on this page.
Hooks used
ArticleInsertComplete

Translate the AutoCreateTalkPage extension if it is available at translatewiki.net

The AutoCreateTalkPage extension does just what the name implies: it automatically creates a talk page. This uses the ArticleInsertComplete hook, and determines if the page being created is in the Main namespace; if it is, this extension automatically creates the corresponding talk page.

Versions[edit | edit source]

The extension was written on a MediaWiki at version 1.15.2. The code uses some very basic functions that have probably existed forever, so it is quite likely that this works with just about any older version (at least since 1.6, when the ArticleInsertComplete hook was introduced). Feel free to try, but don't complain if it fails, which is unlikely.

Configuration[edit | edit source]

  1. Copy the code block below into a file, AutoCreateTalkPage.php, and put that file in the extensions directory.
  2. Add the following line to LocalSettings.php:
require_once( "$IP/extensions/AutoCreateTalkPage.php" );

Customization[edit | edit source]

When a new page is created, the defaults are to add the "talk page" template to the page, with an edit summary of Page automatically created by extension AutoCreateTalkPage. These can be changed; to customize the text, you can edit these lines in the file:

## THESE COULD BE CUSTOMIZED AS NECESSARY BY THE USER
$text = "{{talk page}}";
$summary = "Page automatically created by extension AutoCreateTalkPage";

I could have (should have?) created variables for those, but this is such a simple extension it didn't seem right to make things more complicated than they are.

Future features[edit | edit source]

This is a simple extension; I do not plan to work on it after this. If you require things like...

  • Other namespaces
  • Custom messages for other namespaces
  • Checks for permissions

... well these are all good ideas, but you should be able to modify the code to your needs. Again, this is a quite simple extension; hopefully you'll be able to modify it as necessary if it doesn't currently do things you need.

Code[edit | edit source]

<?php
if ( ! defined( 'MEDIAWIKI' ) )
    die();
 
/**
 * Extension to create talk page automatically after page in main namespace is created
 *
 * @author Tim Neumark <tim@timneumark.com>
 * @page creation extensions
 * @copyright © 2010 Tim Neumark
 * @license GNU General Public Licence 2.0 or later
 */
 
$wgExtensionCredits['other'][] = array(
	'path' => __FILE__,
	'name'           => 'Auto Create Talk',
	'version'        => '1.0.0.0',
	'author'         => 'Tim Neumark',
	'url'            => 'http://www.mediawiki.org/wiki/Extension:AutoCreateTalkPage',
	'description'    => 'Creates talk page automatically when page created in main namespace',
);
 
$wgHooks['ArticleInsertComplete'][] = 'AutoCreateTalkPage';
 
/**
 * Hook on page creation
 */
function AutoCreateTalkPage( &$article, &$user, $text, $summary, $minoredit, 
	$watchthis, $sectionanchor, &$flags, $revision ) {
 
	// if it's a redirect, nothing to create
        # apparently this doesn't work when the page is created?
	#if ( $article->mIsRedirect ) 
	#	return true;
        # so here's a hack way to do it. compare the text and
        # see if it starts with #redirect
	if ( strpos( strtolower( $text ), "#redirect" ) === 0 )
		return true;
 
	// we only do this for the main namespace
	if ( $article->mTitle->getNamespace() !== 0 )
		return true;
 
	$talkTitle = Title::makeTitle( NS_TALK, $article->mTitle );
 
	// nothing to do if it already exists. it is possible (but usually rare)
	// that a talk page exists without the primary article.
	if ( $talkTitle->exists() )
		return true;
 
	## THESE COULD BE CUSTOMIZED AS NECESSARY BY THE USER
	$text = "{{talk page}}";
	$summary = "Page automatically created by extension AutoCreateTalkPage";
 
	#now create the page	
	$talkPage = new Article( $talkTitle );
	$talkPage->doEdit( $text, $summary, EDIT_NEW );
	return true;
}

See also[edit | edit source]