Index: trunk/extensions/PdfBook/PdfBook.php |
— | — | @@ -16,7 +16,7 @@ |
17 | 17 | */ |
18 | 18 | if (!defined('MEDIAWIKI')) die('Not an entry point.'); |
19 | 19 | |
20 | | -define('PDFBOOK_VERSION', '1.0.3, 2008-12-09'); |
| 20 | +define('PDFBOOK_VERSION', '1.0.4, 2010-01-05'); |
21 | 21 | |
22 | 22 | $wgExtensionFunctions[] = 'wfSetupPdfBook'; |
23 | 23 | $wgHooks['LanguageGetMagic'][] = 'wfPdfBookLanguageGetMagic'; |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | 'description' => 'Composes a book from articles in a category and exports as a PDF book', |
30 | 30 | 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfBook', |
31 | 31 | 'version' => PDFBOOK_VERSION |
32 | | - ); |
| 32 | +); |
33 | 33 | |
34 | 34 | class PdfBook { |
35 | 35 | |
— | — | @@ -47,60 +47,60 @@ |
48 | 48 | /** |
49 | 49 | * Perform the export operation |
50 | 50 | */ |
51 | | - function onUnknownAction($action, $article) { |
| 51 | + function onUnknownAction( $action, $article ) { |
52 | 52 | global $wgOut, $wgUser, $wgTitle, $wgParser, $wgRequest; |
53 | 53 | global $wgServer, $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgUploadDirectory, $wgScript; |
54 | 54 | |
55 | 55 | if ($action == 'pdfbook') { |
56 | 56 | |
57 | 57 | $title = $article->getTitle(); |
58 | | - $opt = ParserOptions::newFromUser($wgUser); |
| 58 | + $opt = ParserOptions::newFromUser( $wgUser ); |
59 | 59 | |
60 | 60 | # Log the export |
61 | 61 | $msg = $wgUser->getUserPage()->getPrefixedText().' exported as a PDF book'; |
62 | | - $log = new LogPage('pdf', false); |
63 | | - $log->addEntry('book', $wgTitle, $msg); |
| 62 | + $log = new LogPage( 'pdf', false ); |
| 63 | + $log->addEntry( 'book', $wgTitle, $msg ); |
64 | 64 | |
65 | 65 | # Initialise PDF variables |
66 | | - $format = $wgRequest->getText('format'); |
67 | | - $notitle = $wgRequest->getText('notitle'); |
| 66 | + $format = $wgRequest->getText( 'format' ); |
| 67 | + $notitle = $wgRequest->getText( 'notitle' ); |
68 | 68 | $layout = $format == 'single' ? '--webpage' : '--firstpage toc'; |
69 | | - $charset = $this->setProperty('Charset', 'iso-8859-1'); |
70 | | - $left = $this->setProperty('LeftMargin', '1cm'); |
71 | | - $right = $this->setProperty('RightMargin', '1cm'); |
72 | | - $top = $this->setProperty('TopMargin', '1cm'); |
73 | | - $bottom = $this->setProperty('BottomMargin','1cm'); |
74 | | - $font = $this->setProperty('Font', 'Arial'); |
75 | | - $size = $this->setProperty('FontSize', '8'); |
76 | | - $linkcol = $this->setProperty('LinkColour', '217A28'); |
77 | | - $levels = $this->setProperty('TocLevels', '2'); |
78 | | - $exclude = $this->setProperty('Exclude', array()); |
79 | | - $width = $this->setProperty('Width', ''); |
| 69 | + $charset = $this->setProperty( 'Charset', 'iso-8859-1' ); |
| 70 | + $left = $this->setProperty( 'LeftMargin', '1cm' ); |
| 71 | + $right = $this->setProperty( 'RightMargin', '1cm' ); |
| 72 | + $top = $this->setProperty( 'TopMargin', '1cm' ); |
| 73 | + $bottom = $this->setProperty( 'BottomMargin','1cm' ); |
| 74 | + $font = $this->setProperty( 'Font', 'Arial' ); |
| 75 | + $size = $this->setProperty( 'FontSize', '8' ); |
| 76 | + $linkcol = $this->setProperty( 'LinkColour', '217A28' ); |
| 77 | + $levels = $this->setProperty( 'TocLevels', '2' ); |
| 78 | + $exclude = $this->setProperty( 'Exclude', array() ); |
| 79 | + $width = $this->setProperty( 'Width', '' ); |
80 | 80 | $width = $width ? "--browserwidth $width" : ''; |
81 | | - if (!is_array($exclude)) $exclude = split('\\s*,\\s*', $exclude); |
| 81 | + if ( !is_array( $exclude ) ) $exclude = split( '\\s*,\\s*', $exclude ); |
82 | 82 | |
83 | 83 | # Select articles from members if a category or links in content if not |
84 | | - if ($format == 'single') $articles = array($title); |
| 84 | + if ( $format == 'single' ) $articles = array( $title ); |
85 | 85 | else { |
86 | 86 | $articles = array(); |
87 | | - if ($title->getNamespace() == NS_CATEGORY) { |
88 | | - $db = wfGetDB(DB_SLAVE); |
89 | | - $cat = $db->addQuotes($title->getDBkey()); |
| 87 | + if ( $title->getNamespace() == NS_CATEGORY ) { |
| 88 | + $db = wfGetDB( DB_SLAVE ); |
| 89 | + $cat = $db->addQuotes( $title->getDBkey() ); |
90 | 90 | $result = $db->select( |
91 | 91 | 'categorylinks', |
92 | 92 | 'cl_from', |
93 | 93 | "cl_to = $cat", |
94 | 94 | 'PdfBook', |
95 | | - array('ORDER BY' => 'cl_sortkey') |
| 95 | + array( 'ORDER BY' => 'cl_sortkey' ) |
96 | 96 | ); |
97 | | - if ($result instanceof ResultWrapper) $result = $result->result; |
98 | | - while ($row = $db->fetchRow($result)) $articles[] = Title::newFromID($row[0]); |
| 97 | + if ( $result instanceof ResultWrapper ) $result = $result->result; |
| 98 | + while ( $row = $db->fetchRow( $result ) ) $articles[] = Title::newFromID( $row[0] ); |
99 | 99 | } |
100 | 100 | else { |
101 | 101 | $text = $article->fetchContent(); |
102 | | - $text = $wgParser->preprocess($text, $title, $opt); |
103 | | - if (preg_match_all('/^\\*\\s*\\[{2}\\s*([^\\|\\]]+)\\s*.*?\\]{2}/m', $text, $links)) |
104 | | - foreach ($links[1] as $link) $articles[] = Title::newFromText($link); |
| 102 | + $text = $wgParser->preprocess( $text, $title, $opt ); |
| 103 | + if ( preg_match_all( '/^\\*\\s*\\[{2}\\s*([^\\|\\]]+)\\s*.*?\\]{2}/m', $text, $links ) ) |
| 104 | + foreach ( $links[1] as $link ) $articles[] = Title::newFromText( $link ); |
105 | 105 | } |
106 | 106 | } |
107 | 107 | |
— | — | @@ -111,21 +111,21 @@ |
112 | 112 | $wgScriptPath = $wgServer.$wgScriptPath; |
113 | 113 | $wgUploadPath = $wgServer.$wgUploadPath; |
114 | 114 | $wgScript = $wgServer.$wgScript; |
115 | | - foreach ($articles as $title) { |
| 115 | + foreach ( $articles as $title ) { |
116 | 116 | $ttext = $title->getPrefixedText(); |
117 | | - if (!in_array($ttext, $exclude)) { |
118 | | - $article = new Article($title); |
| 117 | + if ( !in_array( $ttext, $exclude ) ) { |
| 118 | + $article = new Article( $title ); |
119 | 119 | $text = $article->fetchContent(); |
120 | | - $text = preg_replace('/<!--([^@]+?)-->/s', '@@'.'@@$1@@'.'@@', $text); # preserve HTML comments |
121 | | - if ($format != 'single') $text .= '__NOTOC__'; |
122 | | - $opt->setEditSection(false); # remove section-edit links |
123 | | - $wgOut->setHTMLTitle($ttext); # use this so DISPLAYTITLE magic works |
124 | | - $out = $wgParser->parse($text, $title, $opt, true, true); |
| 120 | + $text = preg_replace( '/<!--([^@]+?)-->/s', '@@'.'@@$1@@'.'@@', $text ); # preserve HTML comments |
| 121 | + if ( $format != 'single' ) $text .= '__NOTOC__'; |
| 122 | + $opt->setEditSection( false ); # remove section-edit links |
| 123 | + $wgOut->setHTMLTitle( $ttext ); # use this so DISPLAYTITLE magic works |
| 124 | + $out = $wgParser->parse( $text, $title, $opt, true, true ); |
125 | 125 | $ttext = $wgOut->getHTMLTitle(); |
126 | 126 | $text = $out->getText(); |
127 | | - $text = preg_replace('|(<img[^>]+?src=")(/.+?>)|', "$1$wgServer$2", $text); # make image urls absolute |
128 | | - $text = preg_replace('|<div\s*class=[\'"]?noprint["\']?>.+?</div>|s', '', $text); # non-printable areas |
129 | | - $text = preg_replace('|@{4}([^@]+?)@{4}|s', '<!--$1-->', $text); # HTML comments hack |
| 127 | + $text = preg_replace( '|(<img[^>]+?src=")(/.+?>)|', "$1$wgServer$2", $text ); # make image urls absolute |
| 128 | + $text = preg_replace( '|<div\s*class=[\'"]?noprint["\']?>.+?</div>|s', '', $text ); # non-printable areas |
| 129 | + $text = preg_replace( '|@{4}([^@]+?)@{4}|s', '<!--$1-->', $text ); # HTML comments hack |
130 | 130 | #$text = preg_replace('|<table|', '<table border borderwidth=2 cellpadding=3 cellspacing=0', $text); |
131 | 131 | $ttext = basename($ttext); |
132 | 132 | $h1 = $notitle ? '' : "<center><h1>$ttext</h1></center>"; |
— | — | @@ -134,34 +134,34 @@ |
135 | 135 | } |
136 | 136 | |
137 | 137 | # If format=html in query-string, return html content directly |
138 | | - if ($format == 'html') { |
| 138 | + if ( $format == 'html' ) { |
139 | 139 | $wgOut->disable(); |
140 | | - header("Content-Type: text/html"); |
141 | | - header("Content-Disposition: attachment; filename=\"$book.html\""); |
| 140 | + header( "Content-Type: text/html" ); |
| 141 | + header( "Content-Disposition: attachment; filename=\"$book.html\"" ); |
142 | 142 | print $html; |
143 | 143 | } |
144 | 144 | else { |
145 | 145 | # Write the HTML to a tmp file |
146 | | - $file = "$wgUploadDirectory/".uniqid('pdf-book'); |
147 | | - $fh = fopen($file, 'w+'); |
148 | | - fwrite($fh, $html); |
149 | | - fclose($fh); |
| 146 | + $file = "$wgUploadDirectory/" . uniqid( 'pdf-book' ); |
| 147 | + $fh = fopen( $file, 'w+' ); |
| 148 | + fwrite( $fh, $html ); |
| 149 | + fclose( $fh ); |
150 | 150 | |
151 | 151 | $footer = $format == 'single' ? '...' : '.1.'; |
152 | 152 | $toc = $format == 'single' ? '' : " --toclevels $levels"; |
153 | 153 | |
154 | 154 | # Send the file to the client via htmldoc converter |
155 | 155 | $wgOut->disable(); |
156 | | - header("Content-Type: application/pdf"); |
157 | | - header("Content-Disposition: attachment; filename=\"$book.pdf\""); |
| 156 | + header( "Content-Type: application/pdf" ); |
| 157 | + header( "Content-Disposition: attachment; filename=\"$book.pdf\"" ); |
158 | 158 | $cmd = "--left $left --right $right --top $top --bottom $bottom"; |
159 | 159 | $cmd .= " --header ... --footer $footer --headfootsize 8 --quiet --jpeg --color"; |
160 | 160 | $cmd .= " --bodyfont $font --fontsize $size --linkstyle plain --linkcolor $linkcol"; |
161 | | - $cmd .= "$toc --format pdf14 --numbered $layout $width"; |
| 161 | + $cmd .= "$toc --no-title --format pdf14 --numbered $layout $width"; |
162 | 162 | $cmd = "htmldoc -t pdf --charset $charset $cmd $file"; |
163 | | - putenv("HTMLDOC_NOCGI=1"); |
164 | | - passthru($cmd); |
165 | | - @unlink($file); |
| 163 | + putenv( "HTMLDOC_NOCGI=1" ); |
| 164 | + passthru( $cmd ); |
| 165 | + @unlink( $file ); |
166 | 166 | } |
167 | 167 | return false; |
168 | 168 | } |
— | — | @@ -172,10 +172,10 @@ |
173 | 173 | /** |
174 | 174 | * Return a property for htmldoc using global, request or passed default |
175 | 175 | */ |
176 | | - function setProperty($name, $default) { |
| 176 | + function setProperty( $name, $default ) { |
177 | 177 | global $wgRequest; |
178 | | - if ($wgRequest->getText("pdf$name")) return $wgRequest->getText("pdf$name"); |
179 | | - if (isset($GLOBALS["wgPdfBook$name"])) return $GLOBALS["wgPdfBook$name"]; |
| 178 | + if ( $wgRequest->getText( "pdf$name" ) ) return $wgRequest->getText( "pdf$name" ); |
| 179 | + if ( isset( $GLOBALS["wgPdfBook$name"] ) ) return $GLOBALS["wgPdfBook$name"]; |
180 | 180 | return $default; |
181 | 181 | } |
182 | 182 | |
— | — | @@ -196,8 +196,8 @@ |
197 | 197 | /** |
198 | 198 | * Needed in MediaWiki >1.8.0 for magic word hooks to work properly |
199 | 199 | */ |
200 | | -function wfPdfBookLanguageGetMagic(&$magicWords, $langCode = 0) { |
| 200 | +function wfPdfBookLanguageGetMagic( &$magicWords, $langCode = 0 ) { |
201 | 201 | global $wgPdfBookMagic; |
202 | | - $magicWords[$wgPdfBookMagic] = array($langCode, $wgPdfBookMagic); |
| 202 | + $magicWords[$wgPdfBookMagic] = array( $langCode, $wgPdfBookMagic ); |
203 | 203 | return true; |
204 | 204 | } |