< Extension:MakeArticle

Extension

<?php

/**
 * A parser hook to generate a form from a Template: so the user can populate the parameters of the
 * template and then hit a button to create a page with the name they have chosen consisting of
 * the template filled out with those parameters.
 *
 * @addtogroup Extensions
 *
 * Draws on the following extensions:
 *
 *   - createarticle by Lisa Ridley
 *   - PageCSS by Ævar Arnfjörð Bjarmason
 *
 * @author Julian Porter <julian.porter@porternet.org>
 * @copyright Copyright © 2010, Julian Porter
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */


$wgHooks['ParserFirstCallInit'][]='wfMakeArticleInit';
$wgExtensionCredits['parserhook'][] = array(
					    'path' => __FILE__,
        'name' => 'MakeArticle (version 0.1 beta)',
        'author' => 'Julian Porter',
        'url' => 'http://www.mediawiki.org/User:julianporter',
        'description' => 'Makes an article based on a specified Template: with the <tt>&lt;makearticle&gt;</tt> tag'
);

$wgHooks['UnknownAction'][] = 'wfActionMakeArticle';
$wgMakeArticleCSS="$IP/extensions/makearticle/Style.css";

function wfMakeArticleInit(&$parser)
{
  $parser->setHook('makearticle','wfMakeArticle');
  return true;
}


function wfMakeArticle($input)
{
  $pageMaker=new MakeArticle($input);
  return $pageMaker->render();
}

class MakeArticle
{
  private $template,$css,$templateText,$parameters;

  function __construct($input)
  {
    global $wgMakeArticleCSS;

    $this->parameters=array();
    $this->template=trim($input);
    $this->css=file_get_contents($wgMakeArticleCSS);
    if($this->css==false) MakeArticle::errorHandler("File Missing","Cannot find $wgMakeArticleCSS");
  }

  public function render()
  {
    $this->makeStyleSheet();
    $this->getTemplate();
    $this->getParameters();
    return $this->makeForm($template,$parameters);
  }


  private function makeStyleSheet()
  {
    global $wgParser;
    $css = htmlspecialchars( trim( Sanitizer::checkCss( $this->css ) ) );
    $data= '<style type="text/css"> <![CDATA[*/'.$css.'/*]]>*/ </style>';
    $wgParser->mOutput->addHeadItem($data);
  }

 
 private function getTemplate()
  {
    $title=Title::newFromText($this->template);
    $id=0;
    if(isset($title)) $id=$title->getArticleID();
    if(!$id) MakeArticle::errorHandler("Template Missing","Cannot find template $template");
    $article=Article::newFromID($id);
    $this->templateText=$article->fetchContent();
  }

  
  private function getParameters()
  {
    preg_match_all("#\{{3}\s*(.*?)\s*\}{3}#",$this->templateText,$matches,PREG_SET_ORDER);
    foreach($matches as $value) if(!in_array($value[1],$this->parameters)) $this->parameters[]=$value[1];
  }


  private function makeForm()
  {
    global $wgScript;

    $action=htmlspecialchars($wgScript);
    $html="";
    $html.='<div class="mpForm"><h2>Populating template <span class="mpR">'.$this->template.'</span></h2><form name="makepage" action="'.$action.'" method="get">';
    $html.='<input type="hidden" name="action" value="___makePage" />';
    $html.='<table class="mpTable"><tr><td class="head">Title</td><td><textarea class="mpSingle" name="___title" ncols="40" nrows="2"></textarea></td></tr>';

    foreach($this->parameters as $param)
    {
      $html.='<tr><td class="mpHeader">'.$param.'</td><td><textarea name="cp_'.$param.'" ncols="40" nrows="10"></textarea></td></tr>';
    }
    $html.='</table><input type="hidden" name="___template" value="'.$this->template.'"><input type="submit" name="createarticle" class="mpButton" value="Create" /></form></div>';
    
    return $html;
  }

  static function errorHandler($name,$message)
  {
    global $wgOut, $wTitle;

    $wgTitle=Title::newFromText(wfMsgForContent($name));
    $wgOut->errorPage($name,$message);
  }

}
  


function wfActionMakeArticle($action,$article)
{
  global $wgRequest;
    
  if($action!='___makePage') return true;
  $title=trim($wgRequest->getVal('___title'));
  if($title=='') MakeArticle::errorHandler('Bad Page Title','Title is Null');
  else
    {
      $pageTitle=Title::newFromText($title);
      if(isset($pageTitle)&&$pageTitle->getArticleID()==0)
	{

	  $values=$wgRequest->getValues();
	  $html='{{'.$wgRequest->getVal('___template');
	  foreach($values as $key => $value)
	    {
	      $prefix=substr($key,0,3);
	      $keyName=substr($key,3);
	      if($prefix=='cp_') $html.='|'.$keyName.'='.$value;
	    }
	  $html.='}}';

	  //$preload=wfMakePageContents();
	  $article=new Article($pageTitle);
	  $status=$article->doEdit($html,"$title",EDIT_NEW);
	  if($status->isOK()) $article->doRedirect();
	  else MakeArticle::errorHandler('Error Creating Page',$status->getWikiText());
	}
      else MakeArticle::errorHandler('Bad Page Title',"Page $title already exists");
    }
  return false;
}
?>

Stylesheet

.mpForm
{
    margin: 0;
    padding: 0;
    width: 400px;
}

.mpForm h2
{
    text-align: center;
    font-size: 16pt;
    font-weight: bold;
}

.mpR
{
    color: red;
}

.mpTable
  {
    border-collapse: collapse;
    text-align: left;
    width: 100%;
  }

.mpForm textarea
  {
      margin-left: 2px;
      margin-right: 2px;
      vertical-align: middle;
      width: 99%;
      height: 10em;
}

.mpForm textarea.mpSingle
{
    height: 1.5em;
}

.mpHeader
  {
    width: 10em;
  }

.mpForm td
{
    background-color: #f2f2f2;
    border: 1px black solid;
}

.mpButton
{
    background-color: #ffe0e0;
    position: absolute;
    right: 10px;
}
This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.