Difference between revisions of "Extension:PdfBook"

From Organic Design wiki
m
(import old XmlWiki code from pdf-export.php)
Line 27: Line 27:
 
global $wgHooks,$wgParser,$wgPdfBookMagic;
 
global $wgHooks,$wgParser,$wgPdfBookMagic;
 
$wgParser->setFunctionHook($wgPdfBookMagic,array($this,'magicBook'));
 
$wgParser->setFunctionHook($wgPdfBookMagic,array($this,'magicBook'));
 +
$wgHooks['UnknownAction'][] = $this;
 +
 +
# Add a new pdf log type
 +
global $wgLogTypes,$wgLogNames,$wgLogHeaders,$wgLogActions;
 +
$wgLogTypes[]            = 'pdf';
 +
$wgLogNames  ['pdf']      = 'pdflogpage';
 +
$wgLogHeaders['pdf']      = 'pdflogpagetext';
 +
$wgLogActions['pdf/book'] = 'pdflogentry';
 
}
 
}
  
Line 37: Line 45:
 
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 
}
 
}
 
# Build text of expanded result
 
$args = '';
 
foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
 
$text = "=== magicBook(): ===\n$args";
 
  
 
# Return result with available parser flags
 
# Return result with available parser flags
Line 52: Line 55:
 
isHTML  => false
 
isHTML  => false
 
);
 
);
 +
}
 +
 +
function onUnknownAction($action,$article) {
 +
global $wgOut, $wgUser, $wgTitle;
 +
if($action == 'pdfbook') {
 +
 +
# Log the export
 +
$msg = $wgUser->getUserPage()->getPrefixedText().' exported as a PDF book';
 +
$log = new LogPage('pdf',false);
 +
$log->addEntry('book',$wgTitle,$msg);
 +
 +
# Initialise PDF variables
 +
$layout = '--firstpage toc';
 +
if (!xwGetProperty($properties,'pdf/left-margin',$left))    $left  = '1cm';
 +
if (!xwGetProperty($properties,'pdf/right-margin',$right))  $right  = '1cm';
 +
if (!xwGetProperty($properties,'pdf/top-margin',$top))      $top    = '1cm';
 +
if (!xwGetProperty($properties,'pdf/bottom-margin',$bottom)) $bottom = '5mm';
 +
if (!xwGetProperty($properties,'pdf/font-name',$font))      $font  = 'Arial';
 +
if (!xwGetProperty($properties,'pdf/font-size',$size))      $size  = '8';
 +
if (!xwGetProperty($properties,'pdf/links',$links))          $links  = '217A28';
 +
if (!xwGetProperty($properties,'pdf/toc-levels',$levels))    $levels = '2';
 +
if (xwGetProperty($properties,'pdf/exclude',$exclude))      $exclude = split('\\s*,\\s*',$exclude); else $exclude = array();
 +
 +
# Format the categories article's as a single HTML document
 +
$cat    = $article->getTitle()->getText();
 +
$db    = &wfGetDB(DB_SLAVE);
 +
$cl    = $db->tableName('categorylinks');
 +
$result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$cat' ORDER BY cl_sortkey");
 +
$content = '';
 +
while ($row = mysql_fetch_row($result)) {
 +
$t = Title::newFromID($row[0])->getText();
 +
if (!in_array($t,$exclude)) {
 +
$text = preg_replace('/((href|src)=[\'"]?)\\//','$1http://'.$_SERVER['HTTP_HOST'].'/',xwXmlWikiParse($t));
 +
for ($i = 9; $i > 0; $i--) $text = preg_replace("/(<\\/?)h$i( *>)/i",'$1h'.($i+1).'$2',$text); # start at h2
 +
$text = preg_replace('/<div class="editsection".+?<\\/div>/','',$text); # remove edit links
 +
$t = ereg_replace('^Zenovia/Business Plan/','',$t); # Zenovia hack
 +
$content .= "<h1>$t</h1>$text\n"; # add page title
 +
}
 +
$file = "$htdocs/wiki/tmp/".uniqid('pdf');
 +
$fh = fopen($file,'w+');
 +
fwrite($fh,$content);
 +
fclose($fh);
  
 +
# Send the file to the client via htmldoc converter
 +
while (@ob_end_clean());
 +
header("Content-Type: application/pdf");
 +
header("Content-Disposition: attachment; filename=\"$file\"");
 +
# if (in_array('Content-Encoding: gzip',headers_list())) $content = gzencode($content);
 +
flush();
 +
passthru("htmldoc -t pdf --left $left --right $right --top $top --bottom $bottom --header ... --footer .1. --headfootsize 8 --quiet --jpeg --color --bodyfont $font --fontsize $size --linkstyle plain --linkcolor $links --toclevels $levels --format pdf14 --numbered $layout $file");
 +
@unlink($file);
 +
die;
 +
}
 
}
 
}
  
Line 71: Line 126:
 
return true;
 
return true;
 
}
 
}
 +
 
?>
 
?>

Revision as of 02:31, 8 August 2007

<?php

  1. Extension:PdfBook
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:Extension

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:Nad
  3. - Started: 2007-08-08

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('PDFBOOK_VERSION','0.0.0, 2007-08-08');

$wgPdfBookMagic = "book"; $wgExtensionFunctions[] = 'wfSetupPdfBook'; $wgHooks['LanguageGetMagic'][] = 'wfPdfBookLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Pdf Book', 'author' => 'User:Nad', 'description' => 'Composes a book from articles in a category and exports as a PDF book', 'url' => 'http://www.mediawiki.org/wiki/Extension:Pdf_Book', 'version' => PDFBOOK_VERSION );

class PdfBook {

# Constructor function PdfBook() { global $wgHooks,$wgParser,$wgPdfBookMagic; $wgParser->setFunctionHook($wgPdfBookMagic,array($this,'magicBook')); $wgHooks['UnknownAction'][] = $this;

# Add a new pdf log type global $wgLogTypes,$wgLogNames,$wgLogHeaders,$wgLogActions; $wgLogTypes[] = 'pdf'; $wgLogNames ['pdf'] = 'pdflogpage'; $wgLogHeaders['pdf'] = 'pdflogpagetext'; $wgLogActions['pdf/book'] = 'pdflogentry'; }

# Expand the book-magic function magicBook(&$parser) {

# Populate $argv with both named and numeric parameters $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; }

# Return result with available parser flags return array( $text, found => true, nowiki => false, noparse => false, noargs => false, isHTML => false ); }

function onUnknownAction($action,$article) { global $wgOut, $wgUser, $wgTitle; if($action == 'pdfbook') {

# Log the export $msg = $wgUser->getUserPage()->getPrefixedText().' exported as a PDF book'; $log = new LogPage('pdf',false); $log->addEntry('book',$wgTitle,$msg);

# Initialise PDF variables $layout = '--firstpage toc'; if (!xwGetProperty($properties,'pdf/left-margin',$left)) $left = '1cm'; if (!xwGetProperty($properties,'pdf/right-margin',$right)) $right = '1cm'; if (!xwGetProperty($properties,'pdf/top-margin',$top)) $top = '1cm'; if (!xwGetProperty($properties,'pdf/bottom-margin',$bottom)) $bottom = '5mm'; if (!xwGetProperty($properties,'pdf/font-name',$font)) $font = 'Arial'; if (!xwGetProperty($properties,'pdf/font-size',$size)) $size = '8'; if (!xwGetProperty($properties,'pdf/links',$links)) $links = '217A28'; if (!xwGetProperty($properties,'pdf/toc-levels',$levels)) $levels = '2'; if (xwGetProperty($properties,'pdf/exclude',$exclude)) $exclude = split('\\s*,\\s*',$exclude); else $exclude = array();

# Format the categories article's as a single HTML document $cat = $article->getTitle()->getText(); $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$cat' ORDER BY cl_sortkey"); $content = ; while ($row = mysql_fetch_row($result)) { $t = Title::newFromID($row[0])->getText(); if (!in_array($t,$exclude)) { $text = preg_replace('/((href|src)=[\'"]?)\\//','$1http://'.$_SERVER['HTTP_HOST'].'/',xwXmlWikiParse($t)); for ($i = 9; $i > 0; $i--) $text = preg_replace("/(<\\/?)h$i( *>)/i",'$1h'.($i+1).'$2',$text); # start at h2 $text = preg_replace('/<div class="editsection".+?<\\/div>/',,$text); # remove edit links $t = ereg_replace('^Zenovia/Business Plan/',,$t); # Zenovia hack

$content .= "

$t

$text\n"; # add page title

} $file = "$htdocs/wiki/tmp/".uniqid('pdf'); $fh = fopen($file,'w+'); fwrite($fh,$content); fclose($fh);

# Send the file to the client via htmldoc converter while (@ob_end_clean()); header("Content-Type: application/pdf"); header("Content-Disposition: attachment; filename=\"$file\"");

  1. if (in_array('Content-Encoding: gzip',headers_list())) $content = gzencode($content);

flush(); passthru("htmldoc -t pdf --left $left --right $right --top $top --bottom $bottom --header ... --footer .1. --headfootsize 8 --quiet --jpeg --color --bodyfont $font --fontsize $size --linkstyle plain --linkcolor $links --toclevels $levels --format pdf14 --numbered $layout $file"); @unlink($file); die; } }

# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'PdfBook'; }

	}
  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupPdfBook() { global $wgPdfBook; $wgPdfBook = new PdfBook(); }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfPdfBookLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgPdfBookMagic; $magicWords[$wgPdfBookMagic] = array(0,$wgPdfBookMagic); return true; }

?>