/** * Called when an unknown action occurs on url. We are only interested in zipmanual action. */ function onUnknownAction($action, $article) { global $wgOut, $wgUser, $wgTitle, $wgParser, $wgRequest; global $wgServer, $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgUploadDirectory, $wgScript, $wgStylePath; // We don't do any processing unless it's zipmanual if ($action != 'zipmanual') { return true; } $zipAllowed = false; PonyDocsExtension::onUserCan($wgTitle, $wgUser, 'zipmanual', &$zipAllowed); if (!$zipAllowed) { error_log("WARNING [" . __METHOD__ . "] User attempted to perform a ZIP Export without permission."); $defaultRedirect = PonyDocsExtension::getDefaultUrl(); header("Location: " . $defaultRedirect); exit; } // Get the title and make sure we're in Documentation namespace $title = $article->getTitle(); if ($title->getNamespace() != NS_PONYDOCS) { return true; } // Grab parser options for the logged in user. $opt = ParserOptions::newFromUser($wgUser); // Any potential titles to exclude $exclude = array(); // Determine articles to gather $articles = array(); $pieces = explode(":", $wgTitle->__toString()); // Try and get rid of the TOC portion of the title if (strpos($pieces[2], "TOC") && count($pieces) == 3) { $pieces[2] = substr($pieces[2], 0, strpos($pieces[2], "TOC")); } else { if (count($pieces) != 5) { // something is wrong, let's get out of here $defaultRedirect = PonyDocsExtension::getDefaultUrl(); if (PONYDOCS_DEBUG) { error_log("DEBUG [" . __METHOD__ . ":" . __LINE__ . "] redirecting to {$defaultRedirect}"); } header("Location: " . $defaultRedirect); exit; } } $productName = $pieces[1]; $ponydocs = PonyDocsWiki::getInstance($productName); $pProduct = PonyDocsProduct::GetProductByShortName($productName); if ($pProduct === NULL) { // product wasn't valid wfProfileOut(__METHOD__); $wgOut->setStatusCode(404); return FALSE; } $productLongName = $pProduct->getLongName(); if (PonyDocsProductManual::isManual($productName, $pieces[2])) { $pManual = PonyDocsProductManual::GetManualByShortName($productName, $pieces[2]); } $versionText = PonyDocsProductVersion::GetSelectedVersion($productName); if (!empty($pManual)) { // We should always have a pManual, if we're printing // from a TOC $v = PonyDocsProductVersion::GetVersionByName($productName, $versionText); $toc = new PonyDocsTOC($pManual, $v, $pProduct); list($manualtoc, $tocprev, $tocnext, $tocstart) = $toc->loadContent(); // We successfully got our table of contents. It's // stored in $manualtoc foreach ($manualtoc as $tocEntry) { if ($tocEntry['level'] > 0 && strlen($tocEntry['title']) > 0) { $title = Title::newFromText($tocEntry['title']); $articles[$tocEntry['section']][] = array('title' => $title, 'text' => $tocEntry['text']); } } } else { error_log("WARNING [" . __METHOD__ . "] " . php_uname('n') . ": User attempted to export ZIP from a non TOC page with path:" . $wgTitle->__toString()); } $html = self::getManualHTML($pProduct, $pManual, $v); $coverPageHTML = self::getCoverPageHTML($pProduct, $pManual, $v, false); // Make a temporary directory to store our archive contents. $tempDirPath = sys_get_temp_dir() . '/ponydocs-zip-export-' . time(); $success = @mkdir($tempDirPath); if (!$success) { error_log("FATAL [" . __METHOD__ . "] Failed to create temporary directory " . $tempDirPath . " for Zip Export."); throw new Exception('Failed to create temporary directory for Zip Export.'); } // Now, let's fetch all the img elements for both and grab them all in // parallel. $imgData = array(); // Initialize our RollingCurl instance $rollingCurl = new \RollingCurl\RollingCurl(); $mh = curl_multi_init(); $manualDoc = new DOMDocument(); @$manualDoc->loadHTML($html); $coverPageDoc = new DOMDocument(); @$coverPageDoc->loadHTML($coverPageHTML); self::prepareImageRequests($manualDoc, $rollingCurl, $tempDirPath, &$imgData); self::prepareImageRequests($coverPageDoc, $rollingCurl, $tempDirPath, &$imgData); // Execute the RollingCurl requests $rollingCurl->execute(); // Now update all our image elements in our appropriate DOMDocs. foreach ($imgData as $img) { // Put the data into it. file_put_contents($img['local_path'], $img['request']->getResponseText()); // Modify element $img['element']->setAttribute('src', $img['new_path']); // Do curl cleanup } $html = $manualDoc->saveHTML(); $coverPageHTML = $coverPageDoc->saveHTML(); // Write the HTML to a tmp file $file = tempnam($tempDirPath, "zipexport-"); $fh = fopen($file, 'w+'); fwrite($fh, $html); fclose($fh); // Okay, write the title page $titlepagefile = tempnam($tempDirPath, "zipexport-"); $fh = fopen($titlepagefile, 'w+'); fwrite($fh, $coverPageHTML); fclose($fh); // Disable output of our standard mediawiki output. We will be outputting a zip file instead. $wgOut->disable(); // Create ZIP Archive which contains a cover and manual html $zip = new ZipArchive(); $tempZipFilePath = tempnam($tempDirPath, "zipexport-"); $zipFileName = $productName . '-' . $versionText . '-' . $pManual->getShortName() . '.zip'; $zip->open($tempZipFilePath, ZipArchive::OVERWRITE); $zip->addFile($titlepagefile, 'cover.html'); $zip->addFile($file, 'manual.html'); // Iterate through all the images foreach ($imgData as $img) { $zip->addFile($img['local_path'], $img['new_path']); } $zip->close(); header("Content-Type: application/zip"); header("Content-Length: " . filesize($tempZipFilePath)); header("Content-Disposition: attachment; filename=\"" . $zipFileName . "\""); readfile($tempZipFilePath); // Now remove all temp files self::rrmdir($tempDirPath); // Okay, let's add an entry to the error log to dictate someone requested a pdf error_log("INFO [" . __METHOD__ . "] " . php_uname('n') . ": zip export serve username=\"" . $wgUser->getName() . "\" version=\"{$versionText}\" " . " manual=\"" . $pManual->getShortName() . "\""); // No more processing return false; }
/** * Called when an unknown action occurs on url. We are only interested in pdfbook action. */ function onUnknownAction($action, $article) { global $wgOut, $wgUser, $wgTitle, $wgParser, $wgRequest; global $wgServer, $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgUploadDirectory, $wgScript, $wgStylePath; // We don't do any processing unless it's pdfbook if ($action != 'pdfbook') { return true; } // Get the title and make sure we're in Documentation namespace $title = $article->getTitle(); if ($title->getNamespace() != NS_PONYDOCS) { return true; } // Grab parser options for the logged in user. $opt = ParserOptions::newFromUser($wgUser); // Log the export $msg = $wgUser->getUserPage()->getPrefixedText() . ' exported as a PonyDocs PDF Book'; $log = new LogPage('ponydocspdfbook', false); $log->addEntry('book', $wgTitle, $msg); // Initialise PDF variables $layout = '--firstpage p1'; $x_margin = '1.25in'; $y_margin = '1in'; $font = 'Arial'; $size = '12'; $linkcol = '4d9bb3'; $levels = '2'; $exclude = array(); $width = '1024'; $width = "--browserwidth 1024"; // Determine articles to gather $articles = array(); $pieces = explode(":", $wgTitle->__toString()); // Try and get rid of the TOC portion of the title if (strpos($pieces[2], "TOC") && count($pieces) == 3) { $pieces[2] = substr($pieces[2], 0, strpos($pieces[2], "TOC")); } else { if (count($pieces) != 5) { // something is wrong, let's get out of here $defaultRedirect = PonyDocsExtension::getDefaultUrl(); if (PONYDOCS_DEBUG) { error_log("DEBUG [" . __METHOD__ . ":" . __LINE__ . "] redirecting to {$defaultRedirect}"); } header("Location: " . $defaultRedirect); exit; } } $productName = $pieces[1]; $ponydocs = PonyDocsWiki::getInstance($productName); $pProduct = PonyDocsProduct::GetProductByShortName($productName); if ($pProduct === NULL) { // product wasn't valid wfProfileOut(__METHOD__); $wgOut->setStatusCode(404); return FALSE; } $productLongName = $pProduct->getLongName(); if (PonyDocsProductManual::isManual($productName, $pieces[2])) { $pManual = PonyDocsProductManual::GetManualByShortName($productName, $pieces[2]); } $versionText = PonyDocsProductVersion::GetSelectedVersion($productName); if (!empty($pManual)) { // We should always have a pManual, if we're printing from a TOC $v = PonyDocsProductVersion::GetVersionByName($productName, $versionText); // We have our version and our manual Check to see if a file already exists for this combination $pdfFileName = "{$wgUploadDirectory}/ponydocspdf-" . $productName . "-" . $versionText . "-" . $pManual->getShortName() . "-book.pdf"; // Check first to see if this PDF has already been created and is up to date. If so, serve it to the user and stop // execution. if (file_exists($pdfFileName)) { error_log("INFO [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": cache serve username=\"" . $wgUser->getName() . "\" product=\"" . $productName . "\" version=\"" . $versionText . "\" " . " manual=\"" . $pManual->getShortName() . "\""); PonyDocsPdfBook::servePdf($pdfFileName, $productName, $versionText, $pManual->getShortName()); // No more processing return false; } } else { error_log("ERROR [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": User attempted to print a pdfbook from a non TOC page with path:" . $wgTitle->__toString()); } $html = self::getManualHTML($pProduct, $pManual, $v); // HTMLDOC does not care for utf8. $html = utf8_decode("{$html}\n"); // Write the HTML to a tmp file $file = "{$wgUploadDirectory}/" . uniqid('ponydocs-pdf-book'); $fh = fopen($file, 'w+'); fwrite($fh, $html); fclose($fh); // Okay, create the title page $titlepagefile = "{$wgUploadDirectory}/" . uniqid('ponydocs-pdf-book-title'); $fh = fopen($titlepagefile, 'w+'); $coverPageHTML = self::getCoverPageHTML($pProduct, $pManual, $v); fwrite($fh, $coverPageHTML); fclose($fh); $format = 'manual'; /* @todo Modify so single topics can be printed in pdf */ $footer = $format == 'single' ? '...' : '.1.'; $toc = $format == 'single' ? '' : " --toclevels {$levels}"; // Send the file to the client via htmldoc converter $wgOut->disable(); $cmd = " --left {$x_margin} --right {$x_margin} --top {$y_margin} --bottom {$y_margin}"; $cmd .= " --header ... --footer {$footer} --tocfooter .i. --quiet --jpeg --color"; $cmd .= " --bodyfont {$font} --fontsize {$size} --linkstyle plain --linkcolor {$linkcol}"; $cmd .= "{$toc} --format pdf14 {$layout} {$width} --titlefile {$titlepagefile} --size letter"; $cmd = "htmldoc -t pdf --book --charset iso-8859-1 --no-numbered {$cmd} {$file} > {$pdfFileName}"; putenv("HTMLDOC_NOCGI=1"); $output = array(); $returnVar = 1; exec($cmd, $output, $returnVar); if ($returnVar != 0) { // 0 is success error_log("INFO [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": Failed to run htmldoc (" . $returnVar . ") Output is as follows: " . implode("-", $output)); print "Failed to create PDF. Our team is looking into it."; } // Delete the htmlfile and title file from the filesystem. @unlink($file); if (file_exists($file)) { error_log("ERROR [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": Failed to delete temp file {$file}"); } @unlink($titlepagefile); if (file_exists($titlepagefile)) { error_log("ERROR [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": Failed to delete temp file {$titlepagefile}"); } // Okay, let's add an entry to the error log to dictate someone requested a pdf error_log("INFO [PonyDocsPdfBook::onUnknownAction] " . php_uname('n') . ": fresh serve username=\"" . $wgUser->getName() . "\" version=\"{$versionText}\" " . " manual=\"" . $pManual->getLongName() . "\""); PonyDocsPdfBook::servePdf($pdfFileName, $productName, $versionText, $pManual->getLongName()); // No more processing return false; }