Extension:ExtensionCheck

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
ExtensionCheck
Release status: stable
ExtensionCheck.png
Implementation Parser extension
Description Adds a parser function to check whether an extension is installed on the wiki
Author(s) Nathan Larson (Leucostictetalk)
Latest version 1.0.0 (2013-12-06)
MediaWiki 1.23+
PHP 5.3+
Database changes No
License GNU General Public License 2.0 or later
Download See the code section
Example wiki.mises.org
Hooks used
ParserFirstCallInit
Translate the ExtensionCheck extension if it is available at translatewiki.net
Check usage and version matrix.

The ExtensionCheck extension adds a parser function to check whether an extension is installed on the local wiki or on Wikipedia.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/ExtensionCheck/ExtensionCheck.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage[edit]

{{#wikipediaextensioncheck: ...}}[edit]

Use siprop=extensions to get the list of extensions installed on Wikipedia. Then put them in "MediaWiki:WikipediaExtensions" with a pipe in front of each item like this:


|Abuse Filter
|AccountAudit
|AntiBot
|AntiSpoof
|AntiSpoof for CentralAuth
|ApiSandbox
...

Now you can use:

Cite is {{#wikipediaextensioncheck: Cite|installed|not installed}} on the English Wikipedia.

{{#extensioncheck: ...}}[edit]

The "Cite" extension is {{#extensioncheck: Cite|installed|not installed}} on the local wiki.

Code[edit]

ExtensionCheck.php[edit]

<?php
/**
 * ExtensionCheck MediaWiki extension.
 *
 * This extension adds a parser function to check whether an extension is installed on the wiki
 *
 * Written by Nathan Larson
 * http://www.mediawiki.org/wiki/User:Leucosticte
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Extensions
 */

# Alert the user that this is not a valid entry point to MediaWiki if the user tries to access the
# extension file directly.
if (!defined('MEDIAWIKI')) {
	die( 'This file is a MediaWiki extension. It is not a valid entry point' );
}

$wgExtensionCredits['parserhook'][] = array(
	'path' => __FILE__,
	'name' => 'ExtensionCheck',
	'author' => 'Nathan Larson',
	'url' => 'https://www.mediawiki.org/wiki/Extension:ExtensionCheck',
	'descriptionmsg' => 'extensioncheck-desc',
	'version' => '1.0.0',
);

// Specify the function that will initialize the parser function.
$wgHooks['ParserFirstCallInit'][] = 'ExtensionCheckSetupParserFunction';
$wgHooks['ParserFirstCallInit'][] = 'ExtensionCheckSetupWikipediaParserFunction';

// Allow translation of the parser function name
$wgExtensionMessagesFiles['ExtensionCheck'] = __DIR__ . '/ExtensionCheck.i18n.php';
$wgExtensionCheckWikipediaExtensionsPage = 'MediaWiki:WikipediaExtensions';

// Tell MediaWiki that the parser function exists.
function ExtensionCheckSetupParserFunction( &$parser ) {
   $parser->setFunctionHook( 'extensioncheck', 'ExtensionCheckRenderParserFunction' );

   // Return true so that MediaWiki continues to load extensions.
   return true;
}

function ExtensionCheckSetupWikipediaParserFunction( &$parser ) {
   $parser->setFunctionHook( 'wikipediaextensioncheck', 'ExtensionCheckRenderWikipediaParserFunction' );

   // Return true so that MediaWiki continues to load extensions.
   return true;
}

// Render the output of the parser function.
function ExtensionCheckRenderParserFunction( $parser, $param1 = '', $param2 = '', $param3 = '' ) {

   // The input parameters are wikitext with templates expanded.
   // The output should be wikitext too.
   global $wgExtensionCredits;
   $output = $param3;
   foreach ( $wgExtensionCredits as $credits ) {
	foreach ( $credits as $credit ) {
		if ( $credit['name'] == $param1 ) {
			$output = $param2;
		}
	}
   }
   return $output;
}

function ExtensionCheckRenderWikipediaParserFunction( $parser, $param1 = '', $param2 = '', $param3 = '' ) {
   // The input parameters are wikitext with templates expanded.
   // The output should be wikitext too.
   $extensions = array();
   $output = $param3;
   global $wgExtensionCheckWikipediaExtensionsPage, $wgVersion;
   $title = Title::newFromText( $wgExtensionCheckWikipediaExtensionsPage );
	if ( $title->exists() ) {
		$pageId = $title->getArticleID();
		$article = Article::newFromId( $pageId );
		$page = $article->getPage();
		if ( version_compare( $wgVersion, '1.21', '<' ) ) {
			$contents = $page->getRevision()->getRawText();
		} else {
			$content = $page->getRevision()->getContent( Revision::RAW );
			$contents = ContentHandler::getContentText( $content );
		}
		if ( $contents ) {
			$contentsArr = explode ( "\n", $contents );
			foreach ( $contentsArr as $line ) {
				// Ignore lines that don't start with |
				if ( substr ( $line, 0, 1 ) == '|' ) {
					// Chop off that |
					$line = substr ( $line, 1, strlen( $line ) - 1);
					$key = trim ( $line );
					if ( $key == $param1 ) {
						$output = $param2;
					}
				}
			}
		}
	}
   return $output;
}

ExtensionCheck.i18n.php[edit]

<?php
/**
 * @since 1.0.0
 *
 * @file
 *
 * @licence GNU GPL
 * @author Nathan Larson (Leucosticte)
 */

$magicWords = array();

/** English
 * @author Nathan Larson (Leucosticte)
 */
$magicWords['en'] = array(
   'extensioncheck' => array( 0, 'extensioncheck' ),
   'wikipediaextensioncheck' => array( 0, 'wikipediaextensioncheck' ),
);

$messages = array();

/** English
 * @author Leucosticte
 */
$messages['en'] = array(
    'extensioncheck-desc' => 'Adds a parser function to check whether an extension is installed on the wiki'
);