* @based_on "userhistory" plugin by Ondra Zara * @based_on "pagemove" plugin by Gary Owen */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'admin.php'); require_once(DOKU_INC.'inc/search.php'); class admin_plugin_querychangelog extends DokuWiki_Admin_Plugin { var $show_form = true; var $errors = array(); var $opts = array(); var $changes = array(); /** * function constructor */ function admin_plugin_querychangelog(){ // enable direct access to language strings $this->setupLocale(); } /** * return some info */ function getInfo(){ return array( 'author' => 'Vincent de Lagabbe', 'email' => 'vincent@delagabbe.com', 'date' => '2009-05-05', 'name' => 'QueryChangelog', 'desc' => $this->lang['desc'], 'url' => 'http://wiki.splitbrain.org/plugin:querychangelog', ); } function forAdminOnly(){ return false; } function getMenuText() { return $this->lang['menu']; } /** * return sort order for position in admin menu */ function getMenuSort() { return 999; } /** * handle user request * * @author Vincent de Lagabbe */ function handle() { global $lang; global $ID; global $opts; global $conf; $opts['ns'] = getNS($ID); $opts['startd'] = isset($_REQUEST['startd']) ? $_REQUEST['startd'] : "YYYY-MM-DD HH:MM"; $opts['endd'] = isset($_REQUEST['endd']) ? $_REQUEST['endd'] : "YYYY-MM-DD HH:MM"; if (isset($_REQUEST['base_ns'])) { if (!checkSecurityToken()) return; // get begining date $date_from = 0; if ($_REQUEST['qcsd'] == '') { $date_from = $this->_qc_getstamp($opts['startd']); if (!$date_from) { $this->errors[] = $this->lang['qc_err_date']; } } // get ending date $date_to = time(); if ($_REQUEST['qced'] == '') { $date_to = $this->_qc_getstamp($opts['endd']); if (!$date_to) { $this->errors[] = $this->lang['qc_err_date']; } } if ($date_from && $date_to - $date_from <= 0) { $this->errors[] = $this->lang['qc_err_period']; } // get namespace $opts['base_ns'] = $_REQUEST['base_ns']; $base_dir = $this->_ns2path($opts['base_ns']); if (count($this->errors) == 0) { // get users if (!in_array(".", $_REQUEST['qcusers'])) { $opts['users'] = $_REQUEST['qcusers']; } $opts['major_only'] = $_REQUEST['qcmo'] == ''; $opts['date_from'] = $date_from; $opts['date_to'] = $date_to; $this->changes = $this->_getChanges($base_dir); $this->show_form = false; } if ($_REQUEST['as_csv']) { $this->_generate_csv(); } } } /** * output appropriate html * * @author Vincent de Lagabbe */ function html() { global $lang; global $conf; global $ID; global $opts; global $auth; ptln(''); if ($this->show_form) { ptln($this->locale_xhtml('querychangelog')); $this->_qc_form(); } else { $href = wl($ID).'?do=admin&page='.$this->getPluginName(); ptln('

['.$this->lang['qc_back'].']

'); // Display the query settings $start_date = $opts['date_from'] == 0 ? $this->lang['qc_begining'] : strftime($conf['dformat'], $opts['date_from']); ptln($this->lang['qc_res_from'].': '.$start_date.'
'); ptln($this->lang['qc_res_to'].': '.strftime($conf['dformat'], $opts['date_to']).'
'); ptln($this->lang['qc_res_ns'].': '.$opts['base_ns'].'
'); $str_list = ""; if (isset($opts['users'])) { $user_list = $auth->retrieveUsers(); foreach ($user_list as $user => $info) { if (in_array($user, $opts['users'])) { $str_list .= ($info['name'].', '); } } $str_list = substr($str_list, 0, -2); } else { $str_list = $this->lang['qc_res_all']; } ptln($this->lang['qc_res_users'].': '.$str_list.'
'); ptln('

'.$this->lang['qc_res_title'].'

'); // Display the changelog if (count($this->changes) == 0) { ptln($this->lang['qc_res_nc'].'

'); } else { $this->_show_changes(); } } ptln(''); } /** * show the query changelog form * * @author Gary Owen * @author Vincent de Lagabbe */ function _qc_form() { global $ID; global $lang; global $conf; global $opts; global $auth; ptln('

'); ptln(' '); ptln('
'); formSecurityToken(); ptln('
'); // output hidden values to ensure dokuwiki will return back to this plugin ptln(' '); ptln(' '); ptln(' '); ptln(' '); //Show any errors if (count($this->errors) > 0) { ptln (''); } // Start and End dates Selection ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); // NS Selection $namesp = array( 0 => '' ); //Include root in namespace list search($namesp,$conf['metadir'],'search_namespaces',$opts); sort($namesp); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); // User(s) selection ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); // Major changes / all changes switch ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); ptln( ' '); // Submit ptln( ' '); ptln( ' '); ptln( ' '); ptln( '
'); foreach($this->errors as $error) ptln ($error.'
'); ptln ('
' ? 'CHECKED' : '').'>
'.$this->lang['qc_begining'].'
 
' ? 'CHECKED' : '').'>
'.$this->lang['qc_now'].'
 
 
 
 
 
' ? 'checked="checked"' : '').'">
 
'); ptln( '
'); ptln( '
'); ptln( '
'); } /** * Convert namespace to its corresponding path in meta */ function _ns2path($ns) { global $conf ; if ($ns == ':' || $ns == '') return $conf ['metadir'] ; $ns = trim ($ns, ':') ; $path = $conf ['metadir'] . '/' . str_replace (':', '/', $ns) ; return $path ; } /** * Convert the user input date into a UNIX timestamps. * * @author Vincent de Lagabbe */ function _qc_getstamp($str_date) { $date = array(); $tstamp = false; if (0 == preg_match('/([0-9]{4})-([01][0-9])-([0-3][0-9]) ([0-2][0-9]):([0-6][0-9])/', $str_date, $date)) { return false; } $tstamp = mktime($date[4], $date[5], 0, $date[2], $date[3], $date[1]); return $tstamp; } /** * Get the changelogs entries according to the user query * * @author Ondra Zara * @author Vincent de Lagabbe */ function _getChanges($base_dir, $user = 0) { global $conf; global $opts; function globr($dir, $pattern) { $files = glob($dir.'/'.$pattern); foreach (glob($dir.'/*', GLOB_ONLYDIR) as $subdir) { $subfiles = globr($subdir, $pattern); $files = array_merge($files, $subfiles); } return $files; } $changes = array(); $alllist = globr($base_dir, '*.changes'); $skip = array('_comments.changes', '_dokuwiki.changes'); for ($i = 0; $i < count($alllist); $i++) { $fullname = $alllist[$i]; if (in_array(basename($fullname), $skip)) { continue; } $f = file($fullname); for ($j = 0; $j < count($f); $j++) { $change = parseChangelogLine($f[$j]); if ($opts['major_only'] && $change['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) { continue; } elseif (isset($opts['users']) && !in_array($change['user'], $opts['users'])) { continue; } if ($change['date'] > $opts['date_from'] && $change['date'] < $opts['date_to']) { $changes[] = $change; } } /* for all lines */ } /* for all files */ function cmp($a,$b) { $time1 = $a['date']; $time2 = $b['date']; if ($time1 == $time2) { return 0; } return ($time1 < $time2 ? 1 : -1); } uasort($changes, 'cmp'); return $changes; } /** * Display the changes as DK * * @author Ondra Zara * @author Vincent de Lagabbe */ function _show_changes() { global $conf; global $lang; ptln('
'); ptln(''); ptln('
'); } /** * Generate CSV data from the list of changes * * @author Emmanuel BenoƮt */ function _generate_csv() { global $conf; global $lang; global $auth; global $ID; $titles = []; $users = []; $xhtml_renderer = p_get_renderer('xhtml'); header('Content-type: text/csv;charset=utf-8'); header('Content-Disposition: attachment; filename="changes.csv"'); $fd = fopen('php://output', 'w'); fputcsv($fd, [ 'date', 'time', 'minor', 'pageid', 'pagetitle', 'userid', 'username', 'ip' ]); foreach($this->changes as $change){ $id = $change['id']; $user = $change['user']; if (!array_key_exists($id, $titles)) { resolve_pageid(getNS($id), $id, $exists, $change['date'], true); $titles[$id] = p_get_first_heading($id); } if (!array_key_exists($user, $users)) { $users[$user] = $auth->getUserData($user); } fputcsv($fd, [ strftime('%y-%m-%d', $change['date']), strftime('%T', $change['date']), $change['type'] === 'e' ? 'y' : 'n', $id, $titles[$id], $user, $users[$user]['name'], $change['ip'], ]); } fclose($fd); die; } }