Jump to content

Extension:PageCreator

From mediawiki.org
MediaWiki extensions manual
PageCreator
Release status: unmaintained
Implementation Variable
Description Provides variables for retrieving the creator of a page and the time stamp of page creation
Author(s)
Latest version 0.3 (2022-12-30)
MediaWiki 1.35
Database changes No
License GPL
Download see the code section

The PageCreator extension provides two variables:

  • {{PAGECREATOR}} which returns the page's creator user name
  • {{CREATIONTIMESTAMP}} which returns the page's Creation TimeStamp.

Installation

[edit]
  • Copy the code into a file and place the file(s) in a directory called PageCreator in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'PageCreator' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code

[edit]

1.35+

[edit]
extension.json
{
	"name": "PageCreator",
	"version": "0.4.0",
	"author": [
		"Pierro78",
		"Seb35"
	],
	"url": "https://www.mediawiki.org/wiki/Extension:PageCreator",
	"description": "Provides variables for retrieving the creator of a page an the time stamp of page creation",
	"license-name": "GPL",
	"type": "parserhook",
	"requires": {
		"MediaWiki": ">= 1.35.0"
	},
	"config": {
	},
	"ExtensionMessagesFiles": {
		"PageCreatorMagic": "PageCreator.i18n.magic.php"
	},
	"MessagesDirs": {
	},
	"Hooks": {
		"ParserGetVariableValueSwitch": "PageCreator::wfPppAssignAValue",
		"MagicWordwgVariableIDs": "PageCreator::wfPppDeclareVarIds"
	},
	"AutoloadClasses": {
		"PageCreator": "GetPageCreator.php"
	},
	"AutoloadNamespaces": {
	},
	"manifest_version": 2
}
GetPageCreator.php
<?php

use MediaWiki\MediaWikiServices;

/**
 * Step 1: choose a magic word ID
 *
 * Storing the chosen ID in a constant is not required, but still good
 * programming practice - it makes searching for all occurrences of the magic
 * word ID a bit easier.
 * Note that the name of the constant and the value it is assigned don't have
 * to have anything to do with each other.
 */
define( 'PPP_PAGECREATOR', 'PAGECREATOR' );
define( 'PPP_CREATIONTIMESTAMP', 'CREATIONTIMESTAMP' );


class PageCreator {

	/**
	 * Step 3: assign a value to our variable
	 */
	static function wfPppAssignAValue( &$parser, &$cache, &$magicWordId, &$ret ) {

		if( $magicWordId !== PPP_PAGECREATOR && $magicWordId !== PPP_CREATIONTIMESTAMP ) {
			return true;
		}

		$record = MediaWikiServices::getInstance()->getRevisionLookup()->getFirstRevision( $parser->getTitle() );

		if( $magicWordId === PPP_PAGECREATOR ) {
			$user = $record ? $record->getUser() : $parser->getUserIdentity();
			$ret = $user ? $user->getName() : '';
			return true;
		} elseif( $magicWordId === PPP_CREATIONTIMESTAMP ) {
			$ret = $record ? $record->getTimestamp() : '';
			return true;
		}

		return true;
	}

	/**
	 * Step 4: register wiki markup words associated with
	 *	 PPP_PAGECREATOR as a variable and not some
	 *	 other type of magic word
	 */
	static function wfPppDeclareVarIds( &$customVariableIds ) {
		// $customVariableIds is where MediaWiki wants to store its list of custom
		// variable IDs. We oblige by adding ours:
		$customVariableIds[] = PPP_PAGECREATOR;
		$customVariableIds[] = PPP_CREATIONTIMESTAMP;

		// must do this or you will silence every MagicWordwgVariableIds hook
		// registered after this!
		return true;
	}
}
PageCreator.i18n.magic.php
<?php
/**
 * Step 1: choose a magic word ID
 * Step 2: define some words to use in wiki markup
 */

$magicWords = [];

/** English (English) */
$magicWords['en'] = [
	'PAGECREATOR' => [ 0, 'PAGECREATOR' ],
	'CREATIONTIMESTAMP' => [ 0, 'CREATIONTIMESTAMP' ],
];