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