1<?php 2/** 3 * ZWIdoku Plugin: Export DokuWiki acrticle to the ZWI format. 4 * 5 * @license LGPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author S.Chekanov (KSF) 7 * 8 */ 9########################## start configuration ############################### 10 11# add name of your encyclopedia (or sandbox) 12$wgMzwiName="sandbox"; 13# 14# action tab to make "Export to ZWI" 15$wgMzwiTab=true; 16# Submission webpage to Encyclosphere 17# Set to 0 to trigger download and nothing more 18# Set to 1 to create ZWI in extension/ZWIMaker/tmp and do not do anything 19# Set to 2 to create ZWI in extension/ZWIMaker/tmp and submit to the Encyclosphre 20$wgMzwiSubmit=1; 21# 22# if false, then anybody after login can send ZWI file to Encyclosphere 23# if true, then only authors or contributors can send the article to Encyclosphere 24$wgMzwiAuthorApprove=false; 25# 26# Password for the submission 27$wgMzwiPassword='0123456789'; 28# 29# Encyclosphere submission URL if select 1 30$wgMzwiSubmitUrl="https://encycloreader.org/upload/"; 31# 32# what about the license? 33$wgMzwiLicense='CC BY-SA 3.0'; 34# 35# Current URL of your wiki 36$CURRENT_URL="https://enhub.org/dokuwiki/"; 37# 38############################ end configuration ############################# 39 40 41if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); 42if(!defined('DOKU_LIB')) define('DOKU_LIB',realpath(dirname(__FILE__).'/../../').'/'); 43if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_LIB.'plugins/'); 44//define('DOKU_MAIN',realpath(dirname(__FILE__).'/../../../').'/'); 45 46// must be run within Dokuwiki 47if(!defined('DOKU_INC')) die("Cannot find installed DokuWiki"); 48 49require_once(DOKU_INC.'/inc/init.php'); 50require_once(DOKU_INC.'/inc/template.php'); 51 52 53$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 54$newlink = str_replace("lib/plugins/zwidoku/getEncyc.php?id=", "doku.php?id=", "$actual_link"); 55 56$tmp=explode("getEncyc.php", $actual_link); 57$ext_path = trim( $tmp[0] ); 58 59 60require("Html2Text.php"); 61require("ShortDescription.php"); 62 63 64 function texifyReference($reference) { 65 $patterns[ 0] = '/[áâ�| åä]/ui'; 66 $patterns[ 1] = '/[ðéêèë]/ui'; 67 $patterns[ 2] = '/[íîìï]/ui'; 68 $patterns[ 3] = '/[óôòøõö]/ui'; 69 $patterns[ 4] = '/[úûùü]/ui'; 70 $patterns[ 5] = '/æ/ui'; 71 $patterns[ 6] = '/ç/ui'; 72 $patterns[ 7] = '/�~_/ui'; 73 $patterns[ 8] = '/\\s/'; 74 $patterns[ 9] = '/#/'; 75 $patterns[10] = '/[^A-Za-z0-9\\-:]/'; 76 $replacements[ 0] = 'a'; 77 $replacements[ 1] = 'e'; 78 $replacements[ 2] = 'i'; 79 $replacements[ 3] = 'o'; 80 $replacements[ 4] = 'u'; 81 $replacements[ 5] = 'ae'; 82 $replacements[ 6] = 'c'; 83 $replacements[ 7] = 'ss'; 84 $replacements[ 8] = '-'; 85 $replacements[ 9] = ':'; 86 $replacements[10] = '_'; 87 88 return preg_replace($patterns, $replacements, $reference); 89 } 90 91 92 93$ID = cleanID(getID()); 94//die("ID=".$ID); 95$onlyCode = $INPUT->str('onlyCode'); 96$insideTag = $INPUT->str('insideTag'); 97$title=tpl_pagetitle(null,true); 98$titletext=tpl_pagetitle(null,true); 99 100 101 if (empty($conf['useacl']) || auth_quickaclcheck($ID) >= AUTH_READ) { 102 $file = rawWiki($ID); 103 $articleWiki = $file; 104 105 $fileLastMod = wikiFN($ID); 106 $lastMod = @filemtime($fileLastMod); // from page 107 $NS=getNS($ID); 108 109 // get revisions 110 $filepath = str_replace(":", "/", $ID); 111 // list all revisions 112 $path=DOKU_INC."data/attic/". $filepath."*"; 113 $files = glob($path); 114 115 //print_r($files); 116 117 $changes="data/meta/".$filepath .".changes"; 118 $indexed="data/meta/".$filepath .".indexed"; 119 $meta="data/meta/".$filepath .".meta"; 120 121 $dir_changes=DOKU_INC."data/meta/".$filepath .".changes"; 122 $dir_indexed=DOKU_INC."data/meta/".$filepath .".indexed"; 123 $dir_meta=DOKU_INC."data/meta/".$filepath .".meta"; 124 125 //echo $dir_changes; 126 127 //if ($onlyCode) 128 // $file = preg_replace('/[\s\S]*<code>/m', '', preg_replace('/<\/code>[\s\S]*/m', '', $file)); 129 //if ($insideTag) 130 // $file = preg_replace('/[\s\S]*<' . $insideTag . '[^>]*>/m', '', preg_replace('/<\/' . $insideTag . '>[\s\S]*/m', '', $file)); 131 132 //print $file; 133 // file_put_contents("tmp/article.txt", $file); 134 135 136$pieces = explode(":", $ID); 137 138$filename="article"; 139if (count($pieces)>1) 140 $filename=texifyReference($pieces[1]); // no spaces etc.. 141 142$filename=DOKU_INC."data/cache/".$filename . ".zwi"; 143$zipfilename=$filename; 144// clear 145unlink( $filename ); 146 147//$filename="/tmp/".$filename . ".wik"; 148//echo $filename; 149//die("OK"); 150 151$zip = new ZipArchive; 152$isZipOpen=$zip->open($filename, ZipArchive::CREATE); 153 154if ($isZipOpen === TRUE) 155{ 156 // Add files to the zip file 157 //$zip->addFile('test.txt'); 158 //$zip->addFile('test.pdf'); 159 // Add random.txt file to zip and rename it to newfile.txt 160 //$zip->addFile('random.txt', 'newfile.txt'); 161 // Add a file new.txt file to zip using the text specified 162 $zip->addFromString('article.dokuwiki', $articleWiki); 163 164 // XHTML for embedding 165 $xx=$CURRENT_URL."/doku.php?id=".$ID."&do=export_xhtmlbody"; 166 $body_content = file_get_contents($xx); 167 168 169 # push all revisions too 170 $xr=0; 171 foreach ($files as $xfile) { 172 $destination = str_replace(DOKU_INC, "", $xfile); 173 //print($destination); 174 $zip->addFile($xfile, $destination); 175 $xr=$xr+1; 176 }; 177 178 // metadata 179 $zip->addFile($dir_changes, $changes); 180 $zip->addFile($dir_indexed, $indexed); 181 $zip->addFile($dir_meta, $meta); 182 183 184 // css 185 $css_file=DOKU_INC."/lib/plugins/zwidoku/files/common.css"; 186 $zip->addFile($css_file, "data/css/common.css"); 187 $css_file=DOKU_INC."/lib/plugins/zwidoku/files/fonts.css"; 188 $zip->addFile($css_file, "data/css/fonts.css"); 189 $css_file=DOKU_INC."/lib/plugins/zwidoku/files/doku.css"; 190 $zip->addFile($css_file, "data/css/doku.css"); 191 192 193 $page_title=tpl_pagetitle(null,true); 194 //$page_info=tpl_pageinfo(true); 195 196 //die("OK"); 197 198 // list used plugins for consistency 199 foreach(glob(DOKU_INC."lib/plugins/*", GLOB_ONLYDIR) as $dir) { 200 $usedplugins[] = basename($dir); 201 } 202 203 204 $zip->addFromString('plugins.json', json_encode($usedplugins)); 205 206 207 $DESC = new ShortDescription($articleWiki,$articleTXT); 208 $description=$DESC->getDescription(); 209 210 $Lang="en"; 211 212 // remove styles 213 $html_tmp=preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/is', "$1$3", $body_content); 214 $html_tmp=strip_tags($html_tmp, '<b>'); // remove bold, so it will not move to upper case; 215 $html_tmp=strip_tags($html_tmp, '<i>'); // remove italic 216 $html2TextConverter = new \Html2Text\Html2Text($html_tmp); 217 $articleTXT = $html2TextConverter->getText(); 218 219 220 $content_array= array(); 221 $content_array["article.dokuwiki"]=sha1($file); //$revision->getSha1(); 222 $content_array["article.html"]=sha1($body_content); 223 $content_array["article.txt"]=sha1($articleTXT); 224 225 $generator="DokuWiki"; 226 227 // ZWI -specific metadata for the current revision 228 $tt['ZWIversion'] = "1.3"; 229 $tt['Primary'] = "article.dokuwiki"; 230 $tt['Title'] = $title; 231 $tt['Namespace'] = $NS; 232 $tt['LastModified'] = dformat($INFO['lastmod']); 233 $tt['TimeCreated'] = dformat($INFO['lastmod']); 234 $tt['Location'] = $fileLastMod; 235 $tt['Revisions'] = $xr; 236 $tt['Publisher']=$wgMzwiName; 237 $tt['Content'] = $content_array; 238 $tt['GeneratorName'] = $generator; 239 240 // short description 241 // article information 242 $pagemeta=pageinfo(); 243 244 // do not expose pass 245 $pagemeta["userinfo"]["pass"]=""; 246 $tt['PageInfo'] = $pagemeta; 247 //$tt['Creator']=$pagemeta['meta'][; 248 //$tt['Contributors']=$pagemeta['contributor']; 249 // $mresult = array_merge($tt, $pagemeta); 250 //$mresult=$tt; 251 252 $tt['Lang'] = $Lang; 253 //$tt['Comment'] = $title->getEditNotices (); 254 $tt['Rating'] = ""; 255 $tt['License'] = $wgMzwiLicense; 256 $tt['Description']=$description; 257 $tt['Author']=$author; 258 259 // write 260 $zip->addFromString('metadata.json', json_encode($tt, JSON_PRETTY_PRINT)); 261 262 263$toReplace = array(); 264$replacement = array(); 265 266$nn=0; 267$doc = new DOMDocument(); 268@$doc->loadHTML($body_content); 269$tags = $doc->getElementsByTagName('img'); 270foreach ($tags as $tag) { 271 $URL=$tag->getAttribute('src'); 272 //die($URL); 273 274 $pieces = explode($NS.":", $URL); 275 $img="none"; 276 if (count($pieces)) 277 $img=$pieces[1]; 278 279 $output="data/media/".$NS."/".$img; 280 //echo $output ."\n"; 281 $imgfile_tmp=$output; 282 //die($imgfile_tmp); 283 284 $imgfile=DOKU_INC.$output; 285 //die($imgfile); 286 287 $info = pathinfo($imgfile); 288 $ext = $info["extension"]; 289 if (strlen($ext)<1) continue; 290 291 if (!file_exists($imgfile)) continue; 292 293 //die($imgfile); 294 295 //echo $imgfile ."\n"; 296 $URL = str_replace("&", "&", $URL); 297 //echo $URL . " -> " . $imgfile; 298 //die($imgfile); 299 300 //print($URL); 301 302 $zip->addFile($imgfile, $output); 303 array_push($toReplace,$URL); 304 array_push($replacement,$imgfile_tmp); 305 306 //$newbody_content = str_replace($URL, $imgfile_tmp, $body_content); 307 //$newhtml_content = str_replace($URL, $imgfile_tmp, $html_content); 308 //print($URL. "\n"); 309 310 $nn=$nn+1; 311} 312 313 314//print_r($toReplace); 315//print_r($replacement); 316//die(); 317 318//$newhtml_content = str_replace($toReplace, $replacement, $html_content); 319$newbody_content = str_replace($toReplace, $replacement, $body_content); 320 321//print($newhtml_content); 322//die(); 323 324/* 325$lines = explode("\n", $newbody_content); 326$exclude = array(); 327$n=0; 328foreach ($lines as $line) { 329 if (strpos($line, 'stylesheet') !== FALSE) { 330 $exclude[] = "<link rel=\"stylesheet\" href=\"data/css/styles.css\">"; 331 continue; 332 } 333 $exclude[] = $line; 334} 335$newhtml_content=implode("\n", $exclude); 336*/ 337 338 339$start_content = file_get_contents( DOKU_INC."/lib/plugins/zwidoku/html_start" ); 340$end_content = file_get_contents( DOKU_INC."/lib/plugins/zwidoku/html_end" ); 341 342$html_content = $start_content . $newbody_content . $end_content; 343 344 345$zip->addFromString('article.html', $html_content); 346// $zip->addFromString('article.xhtml', $newbody_content); 347$zip->addFromString("article.txt", $articleTXT); 348 349 350} 351 352// All files are added, so close the zip file. 353$zip->close(); 354 355/* 356if (file_exists($filename)) { 357 print($filename); 358}; 359*/ 360 361/* 362 if (file_exists($filename)) { 363 header('Content-Type: application/zip'); 364 header('Content-Disposition: attachment; filename="'.basename($filename).'"'); 365 header('Content-Length: ' . filesize($filename)); 366 flush(); 367 readfile($filename); 368 // delete file 369 unlink($filename); 370 } 371 */ 372 373 374//die($ext_path); 375 376$permissionErr = 0; 377$xsub=""; 378if ($permissionErr ==0) 379 $xsub="<input type=\"submit\" name=\"submit\" value=\"Submit to the Encyclosphere Network\" />"; 380else if ($permissionErr == 1) 381 $xsub="<input type=\"submit\" name=\"submit\" value=\"Submission to the Encyclosphere is disabled\" readonly=\"readonly\" onfocus=\"this.blur();\" disabled/> <br> (not registered user)"; 382else if ($permissionErr == 2) 383 $xsub="<input type=\"submit\" name=\"submit\" value=\"Submission to the Encyclosphere is disabled\" readonly=\"readonly\" onfocus=\"this.blur();\" disabled/> <br> (did not contribute to this article)"; 384 385 386$submit_script=$ext_path . "/" . "zwipush.php"; 387 388 389$str = <<<EOD 390<!DOCTYPE html> 391<html class="client-nojs" lang="en" dir="ltr"> 392<head> 393<meta charset="UTF-8"/> 394<title>ZWI submit</title> 395<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=0.25, maximum-scale=5.0"/> 396<link rel="stylesheet" type="text/css" href="$ext_path/css/zwimaker.css" /> 397</head> 398<body> 399<p> 400</p> 401<center> 402<h3>Export the article '$title' ?</h3> 403<p> 404</p> 405<form action="$submit_script" method="post"> 406$xsub 407<br>Submit this article to the Encyclosphere Network 408that can be viewed in <a href="https://encycloreader.org/">EncycloReader</a> supported by <a href='https://encyclosphere.org/about/'><img src='$ext_path/img/Encyclosphere_logo24px.png' alt="Encyclosphere" style='vertical-align:middle;margin:0;'/>KSF</a> 409<p></p> 410<input type="submit" name="download" value="Download to your computer" /> 411<br>Save this article to your computer 412<p></p> 413<input type="button" name="cancel" value="Cancel" onClick="window.location='$newlink';" /> 414<input type="hidden" id="zwititle" name="zwititle" value='$titletext' > 415<input type="hidden" id="zwifile" name="zwifile" value='$zipfilename' > 416<input type="hidden" id="posturl" name="posturl" value='$wgMzwiSubmitUrl' > 417<input type="hidden" id="postkey" name="postkey" value='$wgMzwiPassword' > 418<input type="hidden" id="backlink" name="backlink" value='$newlink' > 419<input type="hidden" id="extpath" name="extpath" value='$ext_path' > 420<input type="hidden" id="permission" name="permission" value='$permissionErr' > 421</form> 422<center> 423</body> 424</html> 425EOD; 426 427 print($str); 428 429 } 430 else 431 print "Unauthorized"; 432 433?> 434 435