$value) { if (in_array($key, array('csvdw_namespace','csvdw_delim','csvdw_pagename','csvdw_csvdata','csvdw_csvtemplate','csvdw_overwrite','csvdw_dummyrun' ,'csvdw_trim'))){ // if (!empty($value) || is_numeric($value)) { if (!blank($value)) { $paramcount++; $params[$key] = $value; } } } } } } /** * output appropriate html */ function html() { global $params; global $paramcount; $fieldcount = 0; // update the label when the namespace is changed echo ""; ptln( $this->locale_xhtml('intro') ); ptln( '
' ); ptln( '' ); ptln( '' ); formSecurityToken(); ptln( '
' ); ptln( '
' ); ptln( '

'.$this->getLang('csvdw_namespace').'

' ); ptln( '' ); $fieldcount++; ptln( '
' ); ptln( '

'.$this->getLang('csvdw_pagename').'

' ); ptln( '' ); $fieldcount++; ptln( '
' ); ptln( '

'.$this->getLang('csvdw_delim').'

' ); ptln( '' ); $fieldcount++; ptln( '
' ); ptln( '

'.$this->getLang('csvdw_csvtemplate').'

' ); ptln( '

' ); $fieldcount++; ptln( '

'.$this->getLang('csvdw_csvdata').'

' ); ptln( '

' ); $fieldcount++; ptln( '
' ); ptln( '

'.sprintf($this->getLang('csvdw_overwrite'), $this->_field('csvdw_namespace','csvexploded')).'

' ); ptln( '_field('csvdw_overwrite','true',true).'/> '); ptln( ' --- ' ); ptln( '_field('csvdw_overwrite','false',true).'/>' ); $fieldcount++; ptln( '
' ); ptln( '

'.$this->getLang('csvdw_dummyrun').'

' ); ptln( '_field('csvdw_dummyrun','true',true).'/> ' ); ptln( ' --- ' ); ptln( '_field('csvdw_dummyrun','false',true).'/>' ); $fieldcount++; ptln( '
' ); ptln( '

'.$this->getLang('csvdw_trim').'

' ); ptln( '_field('csvdw_trim','true',true).'/> ' ); ptln( ' --- ' ); ptln( '_field('csvdw_trim','false',true).'/>' ); $fieldcount++; ptln( '
' ); // required fields warning ptln( (is_numeric($paramcount) && $paramcount !== $fieldcount) ? "".$this->getLang('csvdw_allfields')."
" : '' ); ptln( '' ); echo '
'.$this->getLang('csvdw_ver').'
'; echo '
'; echo '
'; // if all form values recieved, let's explode! if ($paramcount == $fieldcount) { $this->_csvexploder(); } elseif ($paramcount > $fieldcount) { // something weird going on ptln( '
Incorrect number of form parameters received. We\'re screwed!
Needed '.$fieldcount.', got '.$paramcount.'
'); } } /** * take form values and explode the submitted csvdata into dokuwiki pages based on submitted csvtemplate */ function _csvexploder() { global $conf; // get dokuwiki conf array global $params; global $paramcount; $namespace = strtolower($params['csvdw_namespace']); // which namespace to create pages within $pagename_column = $params['csvdw_pagename']; // which column(s) in the csv file used for the pagename $indexpage = (empty($conf['start'])) ? 'start' : $conf['start']; // the dokuwiki index page name $csvdata = $params['csvdw_csvdata']; // the dokuwiki page in which you pasted the csv data $csvtemplate = $params['csvdw_csvtemplate']; // the dokuwiki page you created holding the template $csvdelim = $params['csvdw_delim'][0]; // the csv delimiter character (single character) $overwrite_files = ($params['csvdw_overwrite'][0] == 'true') ? TRUE : FALSE; // set this to TRUE to overwrite existing pages $dummyrun = ($params['csvdw_dummyrun'][0] == 'true') ? TRUE : FALSE; // is this just a dummy run? $trimspaces = ($params['csvdw_trim'][0] == 'true') ? TRUE : FALSE; // is this just a dummy run? $indexpagecontent = $this->getLang('csvdw_indexpagecontent'); // template for autogenerated index page // $dr = rtrim( $_SERVER['DOCUMENT_ROOT'], "/" ) .'/'; // get field names from first row $csvfieldnames = str_getcsv(strtok($csvdata, PHP_EOL), $csvdelim); // if we have some csvfieldnames lets rock'n'roll if ($csvfieldnames !== FALSE) { if ($dummyrun) { ptln( $this->getLang('csvdw_dummyrunnotice') ); } ptln( $this->getLang('csvdw_explodednotice') ); if (array_count_values($csvfieldnames) >= 1) { while (($line = strtok(PHP_EOL)) !== FALSE) { $line = str_getcsv($line, $csvdelim); if (!empty(array_filter($line))) { // get clean copy of the template $thispage = $csvtemplate; // set the page name for the index page text $pagename = $this->_get_filename($pagename_column, $line); // clean output filename to dokuwiki standards $cleanpagename = cleanID($pagename); $indexpagecontent .= " * [[$cleanpagename|$pagename]]\r\n"; // replace the template [placeholder] names with content foreach ($csvfieldnames as $key => $field) { $thispage = str_replace('['.strtolower($field).']', $trimspaces ? trim($line[$key]) : $line[$key], $thispage); } // create wiki page $filecreated = FALSE; if ($overwrite_files !== TRUE && page_exists(resolve_id($namespace, $cleanpagename))) { echo $this->getLang('csvdw_skipnoticep'); // msg($this->getLang('csvdw_skipnotice')); } else { if ($dummyrun) { $filecreated = TRUE; } else { // we are assuming that if the file exists after saveWikiText() then it was created // Unfortunately saveWikiText() doesn't provide a return value to indicate if it succeeded or failed. // This leaves it possible that a page existed previously but we were unable to overwrite it. // The DW notice system will put a notice in the exploded file list to indicate the failure though. // see io_writeWikiPage() and _io_writeWikiPage_action() in .\dokuwiki\inc\io.php saveWikiText(resolve_id($namespace, $cleanpagename), $thispage, $this->getLang('csvdw_summary')); $filecreated = page_exists(resolve_id($namespace, $cleanpagename)); } if ($filecreated == FALSE) { echo $this->getLang('csvdw_errornotice'); } else { echo $this->getLang('csvdw_creatednoticep'); } } ptln( wikiFN(resolve_id($namespace, $cleanpagename))."
" ); } } // show an example template page if this is a dummy run // (uses the data from the last processed csv row) if ($dummyrun) { ptln( $this->getLang('csvdw_examplepagenotice') ); ptln( nl2br($thispage) ); } ptln( "


" ); // create index page $filecreated = FALSE; if ($overwrite_files !== TRUE && page_exists(resolve_id($namespace, $indexpage))) { echo $this->getLang('csvdw_skipnoticei'); // msg($this->getLang('csvdw_skipnoticei')); } else { if ($dummyrun) { $filecreated = TRUE; } else { saveWikiText(resolve_id($namespace, $indexpage), $indexpagecontent, $this->getLang('csvdw_summary')); $filecreated = page_exists(resolve_id($namespace, $indexpage)); } if ($filecreated == FALSE) { echo $this->getLang('csvdw_errornotice'); } else { echo $this->getLang('csvdw_creatednoticei'); } } ptln( wikiFN(resolve_id($namespace, $indexpage))."
" ); } ptln( "
" ); // ptln( 'Go to '.$namespace.' index page.' ); ptln( 'Go to '.$namespace.' index page.' ); } strtok('',''); // release strtok memory } /** * Helper to build a unique pagename from specified csv column data * * Will combine multiple column data together if comma separated list * is supplied. Each field will be separated with underscores _ * * @param $pagecols string Single int value or comma separated list eg. 0,3 * @param $line array An array containing the csv values for the current row * @return string A page name combining all row values separated with _ eg. youngstown_oh */ protected function _get_filename($pagecols='', &$line) { $pagename = ''; $pagecols = explode(',', $pagecols); // combine column data into filename foreach ($pagecols as $col) { $pagename .= trim($line[$col]).'_'; } return rtrim($pagename, "_"); } /** * Helper to repopulate form field values from POST data * * @param $field = fieldname * @param $default = default value to return if no value present * @param $isradio = flag indicating if field is a radiobutton - return 'checked' instead of value * @return string : form field value or default * @return array : first element of array if incoming field was an array (multivalue field) */ protected function _field($field, $default='', $isradio=false) { global $params; global $paramcount; $val = ''; if (!empty($params[$field])) { $val = is_array($params[$field]) ? $params[$field][0] : $params[$field]; } if ($isradio) { if ($val==$default) { return 'checked'; } return ''; } return !empty($val) ? $val : $default; } }