[ Index ] |
|
Code source de Serendipity 1.2 |
1 <?php # $Id: voodoopad.inc.php 1 2005-04-16 06:39:31Z timputnam $ 2 # Copyright (c) 2003-2005, Tim Putnam 3 4 /***************************************************************** 5 * VoodooPad Importer, by Tim Putnam 6 * http://deepbluesea.fracsoft.com * 7 *****************************************************************/ 8 9 // These are used by the XML parser 10 class element{ 11 var $name = ''; 12 var $attributes = array(); 13 var $data = ''; 14 var $depth = 0; 15 } 16 $elements = $stack = array(); 17 $count = $depth = 0; 18 19 // Language, language... 20 switch ($serendipity['lang']) { 21 case 'en': 22 default: 23 @define('IMPORTER_VOODOO_FILEPROMPT', 'VoodooPad XML file'); 24 @define('IMPORTER_VOODOO_CREATEINTRALINKSPROMPT', 'Recreate intra-links?'); 25 @define('IMPORTER_VOODOO_WIKINAMEPROMPT','Wiki name'); 26 @define('IMPORTER_VOODOO_KEYPREFIXPROMPT','Prefix for static page DB key'); 27 @define('IMPORTER_VOODOO_UPDATEEXISTINGPROMPT','Update existing entries?'); 28 @define('IMPORTER_VOODOO_CREATINGPAGE','Creating page'); 29 @define('IMPORTER_VOODOO_UPDATINGPAGE','Updating page'); 30 @define('IMPORTER_VOODOO_NOTUPDATING','Not updating'); 31 @define('IMPORTER_VOODOO_RECORDURL','Recording link URL'); 32 @define('IMPORTER_VOODOO_WRITEINTRALINKS','Writing intra-links..'); 33 @define('IMPORTER_VOODOO_REQUIREMENTFAIL', 'This importer requires the Static Pages plugin to be installed. All static pages are currently scanned for a match.'); 34 break; 35 } 36 37 class Serendipity_Import_VoodooPad extends Serendipity_Import { 38 var $info = array('software' => 'VoodooPad'); 39 var $data = array(); 40 var $inputFields = array(); 41 var $force_recode = false; 42 43 function Serendipity_Import_VoodooPad($data) { 44 $this->data = $data; 45 $this->inputFields = array( 46 array('text' => IMPORTER_VOODOO_FILEPROMPT, 47 'type' => 'file', 48 'name' => 'voodooPadXML'), 49 array('text' => IMPORTER_VOODOO_CREATEINTRALINKSPROMPT, 50 'type' => 'bool', 51 'name' => 'shouldWriteLinks', 52 'default' => 'true'), 53 array('text' => IMPORTER_VOODOO_WIKINAMEPROMPT, 54 'type' => 'input', 55 'name' => 'wikiName', 56 'default' => ''), 57 array('text' => IMPORTER_VOODOO_KEYPREFIXPROMPT, 58 'type' => 'input', 59 'name' => 'keyPrefix', 60 'default' => '' ), 61 array('text' => IMPORTER_VOODOO_UPDATEEXISTINGPROMPT, 62 'type' => 'bool', 63 'name' => 'updateExisting', 64 'default' => 'true' ) ); 65 } 66 67 function getImportNotes(){ 68 return IMPORTER_VOODOO_REQUIREMENTFAIL; 69 } 70 71 function validateData() { 72 return sizeof($_FILES['serendipity']['tmp_name']['import']['voodooPadXML']); 73 } 74 75 function getInputFields() { 76 return $this->inputFields; 77 } 78 79 function import() { 80 global $serendipity; 81 global $elements; 82 83 // Dependency on static pages 84 if (!class_exists('serendipity_event_staticpage')) { 85 die(IMPORTER_VOODOO_REQUIREMENTFAIL . '<br/>'); 86 } 87 88 // The selected file 89 $file = $_FILES['serendipity']['tmp_name']['import']['voodooPadXML']; 90 91 // Create a parser and set it up with the callbacks 92 $xml_parser = xml_parser_create(''); 93 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0); 94 xml_set_element_handler($xml_parser, "start_element_handler", "end_element_handler"); 95 xml_set_character_data_handler($xml_parser, "character_data_handler"); 96 97 // Feed the contents of the file into the parser 98 if (!file_exists($file)) { 99 die(sprintf(DOCUMENT_NOT_FOUND, htmlspecialchars($file))); 100 } 101 102 if(!($handle = fopen($file, "r"))) { 103 die(sprintf(SKIPPING_FILE_UNREADABLE, htmlspecialchars($file))); 104 } 105 106 while($contents = fread($handle, 4096)) { 107 xml_parse($xml_parser, $contents, feof($handle)); 108 } 109 110 fclose($handle); 111 xml_parser_free($xml_parser); 112 113 // Maintain a list of the aliases and their links 114 $aliases = array(); 115 116 // Now have a list of elements referenceable by id 117 // so loop through building and/or updating page objects 118 while(list($key_a) = each($elements)) { 119 $name = $elements[$key_a]->name; 120 121 switch ($name) { 122 case 'data': // <data> indicates the start of the VoodooPad entry, so create page object 123 $thispage = array(); 124 break; 125 126 case 'key': // This is the unique identifier of the page 127 $mykey = serendipity_makeFilename($elements[$key_a]->data); 128 $mykey = basename($this->data['keyPrefix']) . $mykey; 129 130 // Pluck out the existing one if its there 131 $page = serendipity_db_query("SELECT * 132 FROM {$serendipity['dbPrefix']}staticpages 133 WHERE filename = '" . serendipity_db_escape_string($mykey.'.htm') . "' 134 LIMIT 1", true, 'assoc'); 135 if (is_array($page)) { 136 $thispage =& $page; 137 if (empty($thispage['timestamp'])) { 138 $thispage['timestamp'] = time(); 139 } 140 } 141 142 $thispage['filename'] = $mykey.'.htm'; 143 // Thanks for pointing this out to me and not just fixing it, I'm learning. 144 $thispage['permalink'] = $serendipity['serendipityHTTPPath'] . 'index.php?serendipity[subpage]=' . $mykey; 145 break; 146 147 case 'alias': // The title and the string used to match links 148 $thispage['articleformattitle'] = $this->data['wikiName']; 149 $thispage['pagetitle'] = $mykey; 150 $thispage['headline'] = $elements[$key_a]->data; 151 break; 152 153 case 'content': // The content of a voodoopad entry 154 case 'path': // The path of a url string 155 $thispage['content'] = $elements[$key_a]->data; 156 157 // If its a content link list it for referencing with the page permalink 158 if ( $name == 'content' ){ 159 $aliases[$thispage['headline']] = $thispage['permalink']; 160 161 // Either replace or insert depending on previous existence 162 if (!isset($thispage['id'])) { 163 echo '<br/>'.IMPORTER_VOODOO_CREATINGPAGE.': '. $mykey; 164 serendipity_db_insert('staticpages', $thispage); 165 $serendipity["POST"]["staticpage"] = serendipity_db_insert_id("staticpages", 'id'); 166 } elseif ($this->data['updateExisting'] == 'true') { 167 echo '<br/>'.IMPORTER_VOODOO_UPDATINGPAGE.': '. $mykey; 168 serendipity_db_update("staticpages", array("id" => $thispage["id"]), $thispage); 169 } else { 170 echo '<br/>'.IMPORTER_VOODOO_NOTUPDATING.': '. $mykey; 171 } 172 } else { 173 // If its a url, the content is the link instead 174 echo '<br/>'.IMPORTER_VOODOO_RECORDURL.': '.$thispage['headline']; 175 $aliases[$thispage['headline']] = $thispage['content']; 176 } 177 break; 178 } 179 } 180 181 // Now rewrite the permalinks 182 echo '<br/>'; 183 if ($this->data['shouldWriteLinks'] == 'true') { 184 Serendipity_Import_VoodooPad::write_links($aliases); 185 } 186 return true; 187 } 188 189 function write_links($aliases) { 190 // Here we run through the static pages database and put in cross links 191 // around the keywords in the text 192 global $serendipity; 193 194 // **TODO** Change this to pull out only entries for the current wiki 195 echo '<br/><p>'.IMPORTER_VOODOO_WRITEINTRALINKS.'</p>'; 196 197 $pages= &serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}staticpages ORDER BY pagetitle DESC"); 198 199 foreach ($pages as $thispage) { 200 // Parse the content string 201 foreach ($aliases as $alias => $permalink) { 202 $thispage['content'] = Serendipity_Import_VoodooPad::wikify($alias, $permalink, $thispage['content']); 203 } 204 205 for ($counter = 0; $counter <= 12; $counter+=1) { 206 unset ($thispage[$counter]); 207 } 208 209 // Write back to the database 210 serendipity_db_update("staticpages", array("id" => $thispage["id"]), $thispage); 211 } 212 213 echo DONE . '<br />'; 214 } 215 216 // Search and replace avoiding content of links 217 // **TODO** Fix this to avoid short links screwing up longer links 218 function wikify($alias, $link, $txt) { 219 $r = preg_split('((>)|(<))', $txt, -1, PREG_SPLIT_DELIM_CAPTURE); 220 $ns = ''; 221 for ($i = 0; $i < count($r); $i++) { 222 if ($r[$i] == "<") { 223 $i+=2; 224 continue; 225 } 226 $r[$i] = eregi_replace(sql_regcase($alias), '<a href="'.$link.'">'.$alias.'</a>', $r[$i]); 227 } 228 229 return join("", $r); 230 } 231 } 232 233 // XML Parser callbacks 234 function start_element_handler($parser, $name, $attribs){ 235 global $elements, $stack, $count, $depth; 236 237 $id = $count; 238 $element = new element; 239 $elements[$id] = $element; 240 $elements[$id]->name = $name; 241 242 while(list($key, $value) = each($attribs)) { 243 $elements[$id]->attributes[$key] = $value; 244 } 245 246 $elements[$id]->depth = $depth; 247 array_push($stack, $id); 248 249 $count++; 250 $depth++; 251 } 252 253 function end_element_handler($parser, $name){ 254 global $stack, $depth; 255 256 array_pop($stack); 257 258 $depth--; 259 } 260 261 function character_data_handler($parser, $data){ 262 global $elements, $stack; 263 264 $elements[$stack[count($stack)-1]]->data .= $data; 265 } 266 267 return 'Serendipity_Import_VoodooPad'; 268 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Nov 24 09:00:37 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |