Jump to content

Project:WikiProject Extensions/Projects/Page Drive/After

From mediawiki.org
MediaWiki extensions manual
Example Fake Extension
Release status: experimental
Implementation Parser function, Extended syntax
Description Used as an example for page drive.
Author(s) user:example
MediaWiki tested on 1.15, 1.16, 1.17, 1.18
License GPLv2
Download Code
$wgExample['enable']
Translate the WikiProject Extensions/Projects/Page Drive/After extension if it is available at translatewiki.net

Purpose

[edit]

The purpose of this extension is to enable conditional execution of a template.

The extension is especially useful if one relies on a complex 'stack' of templates to render a page and would rather make this as generic as possible. The conditional inclusion of a specified page enables, for example, definition of one master template which includes various 'header' / 'footer' pages depending on the namespace of inclusion.

Usage

[edit]
{{#fakearticle:test article|condition}}

To setup this other feature, add to your LocalSettings.php:

$wgExample['enable'] = true;

Example

[edit]
 {{#fakearticle:Lorem ipsum|condition}}

The above example serves no purpose than to be used for this before/after page.

Source Code

[edit]
<?php

/**
 * Sample Fake Extension
 * @package MediaWiki
 * @subpackage Extensions
 *
 * This extension does nothing
 */
$wgExtensionCredits['parserhook'][] = array(
  'name'    => "ConditionalTemplate [http://www.bluecortex.com]",
  'version' => '$LastChangedRevision: 202 $',
  'author'  => 'Jean-Lou Dupont [http://www.bluecortex.com]' 
);

$wgExtensionFunctions[]         = 'efCondTemplateSetup';
$wgHooks['LanguageGetMagic'][]  = 'efCondTemplateGetMagic';

function efCondTemplateGetMagic( &$magicWords, $langCode ) 
{
  $magicWords['template'] = array( 0, 'template' );
  return true;
}

function efCondTemplateSetup()
{
  global $wgParser;
  $wgParser->setFunctionHook( 'template', 'efCondTemplateExec' );	
}

function efCondTemplateExec(&$parser, $page, $cond = false )
{
  return ($cond ? efCondTemplateLoadPage($page) : '' );
}

#
# LoadPage function
# 
function efCondTemplateLoadPage( $p )
{
  $title = Title::newFromText( $p );
  if ( $title->getArticleID() == 0 )
    $text = "<b>[[".$p.']]</b>';
  else
  {
    $article = new Article( $title );
    $text = $article->getContent();
  }
	
  return $text;
}
if ($wgExample['enable']) {
        $unique_sql = " AND rc_id=
                (SELECT MAX(rc_id) 
                FROM $recentchanges tmp 
                WHERE tmp.rc_cur_id=rc.rc_cur_id) ";
}
$sql = "SELECT rc_id, rev_id AS revid, rev_page AS page, rev_user_text as user, rc_id AS rcid 
        FROM $recentchanges rc, $revision 
        WHERE rc_this_oldid=rev_id AND rev_deleted=0 $unique_sql
        ORDER BY rev_id DESC LIMIT $count;";