Open main menu

Extension:MakeArticle/Source

ExtensionEdit

  1 <?php
  2 
  3 /**
  4  * A parser hook to generate a form from a Template: so the user can populate the parameters of the
  5  * template and then hit a button to create a page with the name they have chosen consisting of
  6  * the template filled out with those parameters.
  7  *
  8  * @addtogroup Extensions
  9  *
 10  * Draws on the following extensions:
 11  *
 12  *   - createarticle by Lisa Ridley
 13  *   - PageCSS by Ævar Arnfjörð Bjarmason
 14  *
 15  * @author Julian Porter <julian.porter@porternet.org>
 16  * @copyright Copyright © 2010, Julian Porter
 17  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 18  */
 19 
 20 
 21 $wgHooks['ParserFirstCallInit'][]='wfMakeArticleInit';
 22 $wgExtensionCredits['parserhook'][] = array(
 23 					    'path' => __FILE__,
 24         'name' => 'MakeArticle (version 0.1 beta)',
 25         'author' => 'Julian Porter',
 26         'url' => 'http://www.mediawiki.org/User:julianporter',
 27         'description' => 'Makes an article based on a specified Template: with the <tt>&lt;makearticle&gt;</tt> tag'
 28 );
 29 
 30 $wgHooks['UnknownAction'][] = 'wfActionMakeArticle';
 31 $wgMakeArticleCSS="$IP/extensions/makearticle/Style.css";
 32 
 33 function wfMakeArticleInit(&$parser)
 34 {
 35   $parser->setHook('makearticle','wfMakeArticle');
 36   return true;
 37 }
 38 
 39 
 40 function wfMakeArticle($input)
 41 {
 42   $pageMaker=new MakeArticle($input);
 43   return $pageMaker->render();
 44 }
 45 
 46 class MakeArticle
 47 {
 48   private $template,$css,$templateText,$parameters;
 49 
 50   function __construct($input)
 51   {
 52     global $wgMakeArticleCSS;
 53 
 54     $this->parameters=array();
 55     $this->template=trim($input);
 56     $this->css=file_get_contents($wgMakeArticleCSS);
 57     if($this->css==false) MakeArticle::errorHandler("File Missing","Cannot find $wgMakeArticleCSS");
 58   }
 59 
 60   public function render()
 61   {
 62     $this->makeStyleSheet();
 63     $this->getTemplate();
 64     $this->getParameters();
 65     return $this->makeForm($template,$parameters);
 66   }
 67 
 68 
 69   private function makeStyleSheet()
 70   {
 71     global $wgParser;
 72     $css = htmlspecialchars( trim( Sanitizer::checkCss( $this->css ) ) );
 73     $data= '<style type="text/css"> <![CDATA[*/'.$css.'/*]]>*/ </style>';
 74     $wgParser->mOutput->addHeadItem($data);
 75   }
 76 
 77  
 78  private function getTemplate()
 79   {
 80     $title=Title::newFromText($this->template);
 81     $id=0;
 82     if(isset($title)) $id=$title->getArticleID();
 83     if(!$id) MakeArticle::errorHandler("Template Missing","Cannot find template $template");
 84     $article=Article::newFromID($id);
 85     $this->templateText=$article->fetchContent();
 86   }
 87 
 88   
 89   private function getParameters()
 90   {
 91     preg_match_all("#\{{3}\s*(.*?)\s*\}{3}#",$this->templateText,$matches,PREG_SET_ORDER);
 92     foreach($matches as $value) if(!in_array($value[1],$this->parameters)) $this->parameters[]=$value[1];
 93   }
 94 
 95 
 96   private function makeForm()
 97   {
 98     global $wgScript;
 99 
100     $action=htmlspecialchars($wgScript);
101     $html="";
102     $html.='<div class="mpForm"><h2>Populating template <span class="mpR">'.$this->template.'</span></h2><form name="makepage" action="'.$action.'" method="get">';
103     $html.='<input type="hidden" name="action" value="___makePage" />';
104     $html.='<table class="mpTable"><tr><td class="head">Title</td><td><textarea class="mpSingle" name="___title" ncols="40" nrows="2"></textarea></td></tr>';
105 
106     foreach($this->parameters as $param)
107     {
108       $html.='<tr><td class="mpHeader">'.$param.'</td><td><textarea name="cp_'.$param.'" ncols="40" nrows="10"></textarea></td></tr>';
109     }
110     $html.='</table><input type="hidden" name="___template" value="'.$this->template.'"><input type="submit" name="createarticle" class="mpButton" value="Create" /></form></div>';
111     
112     return $html;
113   }
114 
115   static function errorHandler($name,$message)
116   {
117     global $wgOut, $wTitle;
118 
119     $wgTitle=Title::newFromText(wfMsgForContent($name));
120     $wgOut->errorPage($name,$message);
121   }
122 
123 }
124   
125 
126 
127 function wfActionMakeArticle($action,$article)
128 {
129   global $wgRequest;
130     
131   if($action!='___makePage') return true;
132   $title=trim($wgRequest->getVal('___title'));
133   if($title=='') MakeArticle::errorHandler('Bad Page Title','Title is Null');
134   else
135     {
136       $pageTitle=Title::newFromText($title);
137       if(isset($pageTitle)&&$pageTitle->getArticleID()==0)
138 	{
139 
140 	  $values=$wgRequest->getValues();
141 	  $html='{{'.$wgRequest->getVal('___template');
142 	  foreach($values as $key => $value)
143 	    {
144 	      $prefix=substr($key,0,3);
145 	      $keyName=substr($key,3);
146 	      if($prefix=='cp_') $html.='|'.$keyName.'='.$value;
147 	    }
148 	  $html.='}}';
149 
150 	  //$preload=wfMakePageContents();
151 	  $article=new Article($pageTitle);
152 	  $status=$article->doEdit($html,"$title",EDIT_NEW);
153 	  if($status->isOK()) $article->doRedirect();
154 	  else MakeArticle::errorHandler('Error Creating Page',$status->getWikiText());
155 	}
156       else MakeArticle::errorHandler('Bad Page Title',"Page $title already exists");
157     }
158   return false;
159 }
160 ?>

StylesheetEdit

 1 .mpForm
 2 {
 3     margin: 0;
 4     padding: 0;
 5     width: 400px;
 6 }
 7 
 8 .mpForm h2
 9 {
10     text-align: center;
11     font-size: 16pt;
12     font-weight: bold;
13 }
14 
15 .mpR
16 {
17     color: red;
18 }
19 
20 .mpTable
21   {
22     border-collapse: collapse;
23     text-align: left;
24     width: 100%;
25   }
26 
27 .mpForm textarea
28   {
29       margin-left: 2px;
30       margin-right: 2px;
31       vertical-align: middle;
32       width: 99%;
33       height: 10em;
34 }
35 
36 .mpForm textarea.mpSingle
37 {
38     height: 1.5em;
39 }
40 
41 .mpHeader
42   {
43     width: 10em;
44   }
45 
46 .mpForm td
47 {
48     background-color: #f2f2f2;
49     border: 1px black solid;
50 }
51 
52 .mpButton
53 {
54     background-color: #ffe0e0;
55     position: absolute;
56     right: 10px;
57 }