*/ /** * All DokuWiki plugins to extend the admin function * need to inherit from this class */ class admin_plugin_csvtodwpages extends AdminPlugin { /** * Version: see lang.php */ protected $params; protected $paramcount; private $nl = "\n"; /** * access for managers */ function forAdminOnly() { return false; } /** * return sort order for position in admin menu */ function getMenuSort() { return 1; } /** * handle user request */ function handle() { global $INPUT; global $params; global $paramcount; $fieldnames = array('csvdw_namespace','csvdw_delim','csvdw_pagename','csvdw_csvdata','csvdw_csvtemplate','csvdw_overwrite','csvdw_dummyrun' ,'csvdw_trim'); if ($INPUT->has('csvdw_submit') && checkSecurityToken()) { foreach ($fieldnames as $key) { if (!blank($INPUT->param($key))){ $paramcount++; $params[$key] = $INPUT->param($key); } } } } /** * output appropriate html */ function html() { global $params; global $paramcount; $fieldcount = 0; // update the label when the namespace is changed echo ""; echo $this->locale_xhtml('intro') . $this->nl; echo '
' . $this->nl; echo '' . $this->nl; echo '' . $this->nl; formSecurityToken(); echo '
' . $this->nl; echo '
' . $this->nl; echo '

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

' . $this->nl; echo '' . $this->nl; $fieldcount++; echo '
' . $this->nl; echo '

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

' . $this->nl; echo '' . $this->nl; $fieldcount++; echo '
' . $this->nl; echo '

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

' . $this->nl; echo '' . $this->nl; $fieldcount++; echo '
' . $this->nl; echo '

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

' . $this->nl; echo '

' . $this->nl; $fieldcount++; echo '

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

' . $this->nl; echo '

' . $this->nl; $fieldcount++; echo '
' . $this->nl; echo '

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

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

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

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

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

' . $this->nl; echo '_field('csvdw_trim','true',true).'/> ' . $this->nl; echo ' --- ' . $this->nl; echo '_field('csvdw_trim','false',true).'/>' . $this->nl; $fieldcount++; echo '
' . $this->nl; // required fields warning echo (is_numeric($paramcount) && $paramcount !== $fieldcount) ? "".$this->getLang('csvdw_allfields')."
" : '' . $this->nl; echo '' . $this->nl; 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 echo '
Incorrect number of form parameters received. We\'re screwed!
Needed '.$fieldcount.', got '.$paramcount.'
' . $this->nl; } } /** * 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; global $ID; $resolver = new PageResolver($ID); $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) { echo $this->getLang('csvdw_dummyrunnotice') . $this->nl; } echo $this->getLang('csvdw_explodednotice') . $this->nl; 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($resolver->resolveId($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($resolver->resolveId($namespace.':'.$cleanpagename), $thispage, $this->getLang('csvdw_summary')); $filecreated = page_exists($resolver->resolveId($namespace.':'.$cleanpagename)); } if ($filecreated == FALSE) { echo $this->getLang('csvdw_errornotice'); } else { echo $this->getLang('csvdw_creatednoticep'); } } echo wikiFN($resolver->resolveId($namespace.':'.$cleanpagename))."
" . $this->nl; } } // show an example template page if this is a dummy run // (uses the data from the last processed csv row) if ($dummyrun) { echo $this->getLang('csvdw_examplepagenotice') . $this->nl; echo nl2br($thispage) . $this->nl; } echo "


" . $this->nl; // create index page $filecreated = FALSE; if ($overwrite_files !== TRUE && page_exists($resolver->resolveId($namespace.':'.$indexpage))) { echo $this->getLang('csvdw_skipnoticei'); // msg($this->getLang('csvdw_skipnoticei')); } else { if ($dummyrun) { $filecreated = TRUE; } else { saveWikiText($resolver->resolveId($namespace.':'.$indexpage), $indexpagecontent, $this->getLang('csvdw_summary')); $filecreated = page_exists($resolver->resolveId($namespace.':'.$indexpage)); } if ($filecreated == FALSE) { echo $this->getLang('csvdw_errornotice'); } else { echo $this->getLang('csvdw_creatednoticei'); } } // echo wikiFN(resolve_id($namespace, $indexpage))."
" . $this->nl; echo wikiFN($resolver->resolveId($namespace.':'.$indexpage))."
" . $this->nl; } echo "
" . $this->nl; // null is used as the first param to wl() as it's the only way to get the URL // to work on both our dev server (excludes the doku.php in url) and the live // server (includes doku.php in url) echo 'Go to ' . $namespace . ':' . $indexpage . '
' . $this->nl; } 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 (!blank($params[$field])) { $val = is_array($params[$field]) ? $params[$field][0] : $params[$field]; } if ($isradio) { if ($val==$default) { return 'checked'; } return ''; } return !blank($val) ? $val : $default; } }