Extension:UploadCSV/UploadCSV.php

 This is an MediaWiki-extension. To enable it, put require_once("$IP/extensions/UploadCSV/UploadCSV.php"); near the bottom of your LocalSettings.php  'Special:UploadCSV',        'description' => 'Mass creation of articles from a csv-file.',        'url' => 'http://www.mediawiki.org/wiki/Extension:UploadCSV',        'author' => 'Gunter Schmidt',        'version' => $Ext_Version );

/* ToDo - smart edit of existing articles, only replace template - multiple templates on a page (maybe template given on the dataline, will need a template key) - translation - documentation

require_once "$IP/includes/SpecialPage.php"; require_once( dirname( __FILE__ ) . '/UploadCSV.i18n.php' );
 * 1) Internationalisation file

function wfUploadCSVStart { global $wgMessageCache, $ExtensionTitles;

// Load messages, so we have a title in Special:SpecialPages UploadCSVPage::loadTitles;

SpecialPage::addPage(new UploadCSVPage); }

class UploadCSVPage extends SpecialPage { public $Specialpage_Name='UploadCSV', $MsgPrefix; //keep this within the class or it will mess with other extensions! public $curRow; function UploadCSVPage { SpecialPage::SpecialPage($this->Specialpage_Name); $this->MsgPrefix = strtolower($this->Specialpage_Name). '_';       }

function execute( $par ) { global $wgRequest, $wgOut, $wgUser, $csvInfo; $MsgPrefix = $this->MsgPrefix; //Debug info $hidden = "text"; // hidden or text

self::loadMessages; $this->setHeaders;

#$wgOut->setPagetitle("Gunters Extension"); // only necessary if you do want a different title than in the Special:Specialpages list $wgOut->addHTML( wfMsg( $MsgPrefix. 'intro' ) ); // Set form variables $template = $wgRequest->getText('wpTemplate'); if (empty($template)) $template = "SAP-Jobstep"; #else $template = "ok"; $UploadFile = $wgRequest->getText('wpUploadFile'); $curInfo = $wgRequest->getText('wpcurInfo'); $curAction = $wgRequest->getText('wpcurAction'); if (empty($curAction)) $curAction = 'start'; $this->curRow = $wgRequest->getText('wpcurRow'); if (empty($this->curRow)) $this->curRow = 1; $endRow = $wgRequest->getText('wpendRow'); if (empty($endRow) || !is_numeric($endRow)) $endRow = 0;

#echo " c: $curAction"; if ($curAction != 'start') { $curUploadFile = $wgRequest->getText('wpcurUploadFile'); $UploadFile = $curUploadFile; }               // build form $wgOut->addHTML("action\" method=\"post\" > " .						" ".

wfElement( 'input', array( 'type' => 'submit', 'name' => 'create', 'value' => wfMsgHtml( 'uploadbtn' ) )).

// hidden elements wfElement( 'input', array( 'type' => "$hidden", 'name' => 'wpcurUploadFile', 'size' => '20', 'maxlength' => '150', 'value' => $curUploadFile )) .						wfElement( 'input', array( 'type' => "$hidden", 'name' => 'wpcurInfo', 'size' => '20', 'value' => $curInfo )) .						" \n" );

// do the works // if ($this->curRow > 1) // $wgOut->addHTML("Finished uploading data for article no. " . ($this->curRow + 1) . "."); // $wgOut->AddHTML ('							// var jscount = document.userForm.wpcurRow.value;							// document.write("Uploading data for article no2. " + // jscount + // ".Ende");							// '); unset($errormsg); $editAllowed = $wgUser->isAllowed('edit'); if ($editAllowed) { $UploadFile = $_FILES['wpUploadFile']['tmp_name']; if (!empty($template) && !empty($UploadFile)) { // load template $tvars = $this->LoadTemplate($template); if ( is_array ($tvars) ) { // Load csv-file if (is_file ($UploadFile)) { $handle = fopen ($UploadFile, "r"); // search for pagetitle $headerline = fgets($handle, 4096); $headers = explode(';', $headerline); unset ($colPageTitle); foreach ($headers as $key => $header) { $headers[$key] = trim($header); if ( stripos($header, "pagetitle") === 0 ) $colPageTitle = $key; }							// do we have a page title? if (isset ($colPageTitle)) { //all checks done //find titles corresponding to template titles #print " "; #print_r ($tvars); foreach ($tvars as $tkey => $tvar) { foreach ($headers as $key => $header) { if (empty($header)) break 1; #$tvarkey[$tkey] = -1; if (stripos($header, $tvar) === 0) { #unset($tvarkey[$header]); $tvarCol[$tkey] = $key; #print " $header, $tvar, $key"; break 1; }									}								}								#print " "; #print_r ($tvarkey); //now read lines $cRow = 0; if (!is_numeric($this->curRow)) $this->curRow = 0; while (!feof($handle)) { $dataline = fgets($handle, 4096); #echo " $dataline"; $cRow += 1; if ($cRow >= $this->curRow) { #echo " crow: $cRow, $this->curRow"; $data = explode(';', $dataline); $this->curRow = $cRow + 1; $curInfo += 1; $wgOut->addHTML('											if (document.userForm.wpcurAction.value == "start") {												document.userForm.wpcurInfo.value = 1; }											else { document.userForm.wpcurInfo.value = ' . $curInfo . '}											document.userForm.wpcurAction.value = "running";											document.userForm.wpcurUploadFile.value = "' . addslashes($UploadFile) . '";											document.userForm.wpcurRow.value = "' . $this->curRow . '";											 '); if (is_array($data)) { $pageTitle = trim($data[$colPageTitle]); if (!empty($pageTitle)) { #echo " $pageTitle"; $ret = $this->MakeArticle($pageTitle, $template, $tvars, $tvarCol, $data); if ($ret !== true) $wgOut->AddWikiText("Error creating article: $ret"); break 1; // pause extension and update text }										}									}								}								// check, if we are through if (feof($handle) || ($this->curRow > $endRow && $endRow > 0)) { $this->curRow = 0; unset($UploadFile); $bolFinished = true; $wgOut->addHTML('										document.userForm.wpcurAction.value = "start";										document.userForm.wpcurUploadFile.value = "";										document.userForm.wpcurRow.value = "1";										document.userForm.wpcurInfo.value = "0";										 '); }										//alert("Upload finished!");

}							else $errormsg = "One column needs to be defined as \"pagetitle\"."; fclose ($handle); }						else $errormsg = "File not found."; }					else $errormsg = $tvars; }				#else print "empty!"; }				else $errormsg = "Not allowed to edit. Please log in first."; if (isset($errormsg)) $wgOut->AddWikiText("Error: $errormsg");

// Output $output = " ". wfMsg( $MsgPrefix . 'lastmessage' ); #$wgOut->addHTML( $output );

//automatically repeat function until upload is finished if ($this->curRow > 1 && !empty($UploadFile)) { #echo " c: $this->curRow"; #sleep (2); $wgOut->addHTML('						document.userForm.submit;						 '); } 				elseif ($bolFinished) $txt = wfMsg( $MsgPrefix . 'finished' ); $txt = ereg_replace('\$1', utf8_encode($curInfo), $txt); $wgOut->addHTML(" $txt");

}		function MakeArticle($pageTitle, $templateName, $tvars, $tvarCol, $data, $summary='') { global $wgOut; // $pageTitle: Article name // $templateName: Full name of template, including Template: // $tvars: array of template variables // $tvarCol: array of column numbers corresponding to $tvars // $data: array of data // $summary: article change summary information $MsgPrefix = $this->MsgPrefix; $summary .= " (auto upload csv)"; $summary = trim($summary); // get template title without "template:" if (preg_match("/:(.*)/", $templateName, $ret ) ) { $templateTitle = $ret[1]; #print " $templateTitle"; }				else $templateTitle = $templateName; //create content $content = ''. "\r\n". time. "\r\n"; #print "$content"; //create article $titleNew = title::newFromText(htmlentities($pageTitle)); $articleNew = new Article( $titleNew ); if( $titleNew->getArticleID == 0 ) { $mode = EDIT_NEW; $ctext = 'created'; }				else { $mode = EDIT_UPDATE; $ctext = 'changed'; // smart edit, only replace template $t = explode('{{', $articleNew->getContent); //all template starts echo " "; print_r ($t); echo " "; if (count($t) > 1) { //find our template, for now expect only one $start = true; $tc = ''; foreach ( $t AS $key => $x ) {							$xl = ltrim($x); if (stripos($xl, $templateTitle)=== 0 ) { // $foundkey = $key; // now we need the end, but there might be templates within the template or following templates $start = false; $te = explode('}}', $x); print_r($te); echo " "; if ( count($te) == 2 ) { //no included templates, if there are, count = 1 $tc .= substr($content,2). $te[1]; }								else $tc .= implode('', $te); // foreach ( $te as $key2 => $y ) { // }							}							else $tc .= $x. '{{'; // rebuild content }						$content = $tc; // $y = explode ( '}}', $x , 2 ) ; // echo " "; // print_r ($y); // if ( count ( $y ) == 2 ) // {							// $z = $y[0] ; // $z = explode ( '|', $z ) ; // $tn = array_shift ( $z ) ; // $t[$key] = '{{'. $x ; // }						// else if ( $key != 0 ) $t[$key] = '{{'. $x ; // else $t[$key] = $x ; }					else $content = implode('', $t). $content; //no template found, so we add to the end #if (count($t) == 2) { //simple, only one template // $content_old = $articleNew->getContent; // $reg = '//'; // $reg = '/.*?(' . $templateTitle . '.*?)B/'; // $reg = '/Sofort([^}]*})/'; // echo " "; // if (preg_match($reg, $content_old, $regs)) // print_r ($regs); // else print "Fehler $content_old $reg"; }				// create/update article $createSuccess = $articleNew->doEdit( $content, $summary, $mode ); #| EDIT_FORCE_BOT //sleep (2); // wait between mass creation if ($createSuccess) { $cr = $this->curRow - 1; $txt = 	wfMsg( $MsgPrefix . $ctext ); $txt = ereg_replace('\$1', utf8_encode($cr), $txt); $txt = ereg_replace('\$2', utf8_encode($pageTitle), $txt); $wgOut->addHTML(" $txt "); return true; }		}

function LoadTemplate($templateName='') { global $namespaceNames, $wgOut; //accept only templates, with or without "template:" // echo " Temp: ". Namespace::getCanonicalName(NS_TEMPLATE). ", " . NS_TEMPLATE ; // $tName = str_replace($namespaceNames[NS_TEMPLATE] . ':', '', $templateName); // $tName = $namespaceNames[NS_TEMPLATE]. ':' . $tName; // if ($tName <> $templateName) // $wgOut->addHTML('						// document.userForm.wpTemplate.value = "' . $tName . '";						// '); #print "Template: $templateName"; $title = title::newFromText($templateName); $pageID = $title->getArticleID; //Maybe the string template: was not given if ($pageID == 0) { $title = title::newFromText($templateName, NS_TEMPLATE); $pageID = $title->getArticleID; }				if ($pageID) { #print "page: $pageID"; $article = new Article( $title ); $text = $article->getContent; $reg = '//'; $varExist = preg_match_all($reg, $text, $tvars, PREG_PATTERN_ORDER); #print_r ($tvars); if ($varExist) { // Variables may be used multiple times in an template, we need to delete all double entries. $ret = array_unique($tvars[1]); return $ret; //return array of template vars; does not check on 					} else return "novars"; }				else return "notemplate"; }

function loadMessages { self::loadMessagesS( 'messages' ); }

function loadTitles { self::loadMessagesS( 'titles' ); }		function loadMessagesS( $MsgType ) { // using a function to save some memory instead of a global for the messages global $wgMessageCache; $Specialpage_Name='UploadCSV'; //needs to be defined here, because it is not always called in an object context $extensionName = strtolower($Specialpage_Name);

//Need to change the function name accordingly $Messages = SpecialUploadCSV_Messages( $MsgType, $Specialpage_Name );

if ( count ($Messages) == 0 ) return; foreach ( $Messages as $lang => $langMessages ) { foreach ( $langMessages as $key => $langMessage ) $wgMessageCache->addMessage( $key, utf8_encode( $langMessage ), $lang); }			return; } }

?>