Extension:UploadCSV/UploadCSV.php

\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) { if ($handle = fopen($UploadFile, "r")) {

// search for pagetitle $headerline = fgets($handle); $headers = preg_split($sepPattern,trim($headerline)); unset ($colPageTitle); foreach ($headers as $key => $header) { $headers[$key] = trim($header); if ( stripos($header, "pagetitle") === 0 ) $colPageTitle = $key; }

// Loop through the each line of the file if we have a pagetitle column if (isset($colPageTitle)) { $cRow = 0; $sRow = $this->curRow; if (!is_numeric($this->curRow)) $this->curRow = 0; while ($dataline = fgets($handle)) { $cRow += 1;

// Process the line if it's not before the start-line if ($cRow >= $this->curRow) { $this->curRow = $cRow; $data = preg_split($sepPattern,trim($dataline)); $curInfo += 1; if (is_array($data)) { $pageTitle = trim($data[$colPageTitle]); if (!empty($pageTitle)) { $ret = $this->MakeArticle($pageTitle, $template, $headers, $data); if ($ret !== true) $wgOut->AddWikiText("Error creating article: $ret"); }							}						}

// check, if we are through if (feof($handle) || ($this->curRow >= $endRow && $endRow > 0)) { $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+1) . '";								document.userForm.wpendRow.value = "' . (2*$this->curRow-$sRow+1) . '";								 '); unset($UploadFile); $bolFinished = true; $this->curRow = 0; break; }					}				}				else $errormsg = "One column needs to be defined as \"pagetitle\"."; fclose ($handle); }			elseif ($UploadFile) $errormsg = "Couldn't open file."; }		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 (0 && $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, $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; $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; } }