1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 */ 6 7/* ----- Settings ----- */ 8 9define('DOKU_INC', realpath(dirname(__FILE__).'/../../../').'/'); 10define('DISCUSSION_NS', 'discussion'); 11 12/* ----- Main ----- */ 13 14// conversion script should only be run once 15if (@file_exists(dirname(__FILE__).'/convert_completed')) 16die('Conversion already completed.'); 17 18require_once(DOKU_INC.'inc/init.php'); 19 20$files = getDiscussionPages(); 21$n = 0; 22 23foreach ($files as $file) { 24 if (convertDiscussionPage($file)) { 25 echo $file['id'].'<br />'; 26 $n++; 27 } 28} 29 30if ($n > 0) { 31 io_saveFile(dirname(__FILE__).'/convert_completed', ''); 32 echo '<br />Successfully converted '.$n.' discussion pages to new comments meta files.'; 33} else { 34 echo 'No discussion pages found.'; 35} 36 37/* ----- Functions ----- */ 38 39/** 40 * returns a list of all discussion pages in the wiki 41 */ 42function getDiscussionPages() { 43 global $conf; 44 45 $data = array(); 46 search($data, $conf['datadir'], 'search_discussionpages', array()); 47 return $data; 48} 49 50/** 51 * function for the search callback 52 */ 53function search_discussionpages(&$data, $base, $file, $type, $lvl, $opts) { 54 global $conf; 55 56 if ($type == 'd') return true; // recurse into directories 57 if (!preg_match('#'.preg_quote('/'.DISCUSSION_NS.'/', '#').'#u', $file)) return false; 58 if (!preg_match('#\.txt$#', $file)) return false; 59 60 $id = pathID(str_replace(DISCUSSION_NS.'/', '', $file)); 61 $data[] = array( 62 'id' => $id, 63 'old' => $conf['datadir'].$file, 64 'new' => metaFN($id, '.comments') 65 ); 66 return true; 67} 68 69/** 70 * this converts individual discussion pages to .comment meta files 71 */ 72function convertDiscussionPage($file) { 73 74 // read the old file 75 $data = io_readFile($file['old'], false); 76 77 // handle file with no comments yet 78 if (trim($data) == '') { 79 io_saveFile($file['new'], serialize(array('status' => 1, 'number' => 0))); 80 @unlink($file['old']); 81 return true; 82 } 83 84 // break it up into pieces 85 $old = explode('----', $data); 86 87 // merge with possibly already existing (newer) comments 88 $comments = array(); 89 if (@file_exists($file['new'])) 90 $comments = unserialize(io_readFile($file['old'], false)); 91 92 // set general info 93 if (!isset($comments['status'])) $comments['status'] = 1; 94 $comments['number'] += count($old); 95 96 foreach ($old as $comment) { 97 98 // prepare comment data 99 if (strpos($comment, '<sub>') !== false) { 100 $in = '<sub>'; 101 $out = ':</sub>'; 102 } else { 103 $in = '//'; 104 $out = ': //'; 105 } 106 list($meta, $raw) = array_pad(explode($out, $comment, 2),2, ''); 107 $raw = trim($raw); 108 109 // skip empty comments 110 if (!$raw) { 111 $comments['number']--; 112 continue; 113 } 114 115 list($mail, $meta) = array_pad(explode($in, $meta, 2),2, ''); 116 list($name, $strd) = array_pad(explode(', ', $meta, 2),2, ''); 117 $date = strtotime($strd); 118 if ($date == -1) $date = time(); 119 if ($mail) { 120 list($mail) = array_pad(explode(' |', $mail, 2),2, ''); 121 $mail = substr(strrchr($mail, '>'), 1); 122 } 123 $cid = md5($name.$date); 124 125 // render comment 126 $xhtml = p_render('xhtml', p_get_instructions($raw), $info); 127 128 // fill in the converted comment 129 $comments['comments'][$cid] = array( 130 'user' => array( 131 'name' => hsc($name), 132 'mail' => hsc($mail)), 133 'date' => array('created' => $date), 134 'show' => true, 135 'raw' => $raw, 136 'xhtml' => $xhtml, 137 'replies' => array() 138 ); 139 } 140 141 // save the new file 142 io_saveFile($file['new'], serialize($comments)); 143 144 // remove the old file 145 @unlink($file['old']); 146 147 return true; 148} 149// vim:ts=4:sw=4:et:enc=utf-8: 150