Index: trunk/extensions/PdfBook/PdfBook.php |
— | — | @@ -14,36 +14,51 @@ |
15 | 15 | * @copyright © 2007 Aran Dunkley |
16 | 16 | * @licence GNU General Public Licence 2.0 or later |
17 | 17 | */ |
18 | | -if (!defined('MEDIAWIKI')) die('Not an entry point.'); |
| 18 | +if( !defined( 'MEDIAWIKI' ) ) die( "Not an entry point." ); |
19 | 19 | |
20 | | -define('PDFBOOK_VERSION', '1.0.4, 2010-01-05'); |
| 20 | +define( 'PDFBOOK_VERSION', "1.0.5, 2010-09-19" ); |
21 | 21 | |
22 | 22 | $wgExtensionFunctions[] = 'wfSetupPdfBook'; |
23 | 23 | $wgHooks['LanguageGetMagic'][] = 'wfPdfBookLanguageGetMagic'; |
24 | 24 | |
25 | 25 | $wgExtensionCredits['parserhook'][] = array( |
26 | 26 | 'path' => __FILE__, |
27 | | - 'name' => 'PdfBook', |
28 | | - 'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', |
29 | | - 'description' => 'Composes a book from articles in a category and exports as a PDF book', |
30 | | - 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfBook', |
| 27 | + 'name' => "PdfBook", |
| 28 | + 'author' => "[http://www.organicdesign.co.nz/nad User:Nad]", |
| 29 | + 'description' => "Composes a book from articles in a category and exports as a PDF book", |
| 30 | + 'url' => "http://www.mediawiki.org/wiki/Extension:PdfBook", |
31 | 31 | 'version' => PDFBOOK_VERSION |
32 | 32 | ); |
33 | 33 | |
| 34 | +# Set this to true in LocalSettings to add PdfBook to the action tabs |
| 35 | +$wgPdfBookTab = false; |
| 36 | + |
| 37 | +# The text displayed in the pdf tab, can be set to another value in Localsettings. |
| 38 | +$wgTabText = 'PDF'; |
| 39 | + |
| 40 | + |
34 | 41 | class PdfBook { |
35 | 42 | |
36 | 43 | function __construct() { |
37 | 44 | global $wgHooks, $wgParser, $wgPdfBookMagic; |
38 | | - global $wgLogTypes, $wgLogNames, $wgLogHeaders, $wgLogActions; |
| 45 | + global $wgLogTypes, $wgLogNames, $wgLogHeaders, $wgLogActions, $wgPdfBookTab; |
39 | 46 | $wgHooks['UnknownAction'][] = $this; |
40 | 47 | |
| 48 | + # Hooks for pre-Vector and Vector addtabs. |
| 49 | + if ( $wgPdfBookTab ) { |
| 50 | + $wgHooks['SkinTemplateTabs'][] = $this ; |
| 51 | + $wgHooks['SkinTemplateNavigation'][] = $this; |
| 52 | + } |
| 53 | + |
41 | 54 | # Add a new pdf log type |
42 | 55 | $wgLogTypes[] = 'pdf'; |
43 | 56 | $wgLogNames ['pdf'] = 'pdflogpage'; |
44 | 57 | $wgLogHeaders['pdf'] = 'pdflogpagetext'; |
45 | 58 | $wgLogActions['pdf/book'] = 'pdflogentry'; |
| 59 | + |
46 | 60 | } |
47 | 61 | |
| 62 | + |
48 | 63 | /** |
49 | 64 | * Perform the export operation |
50 | 65 | */ |
— | — | @@ -51,13 +66,13 @@ |
52 | 67 | global $wgOut, $wgUser, $wgTitle, $wgParser, $wgRequest; |
53 | 68 | global $wgServer, $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgUploadDirectory, $wgScript; |
54 | 69 | |
55 | | - if ($action == 'pdfbook') { |
| 70 | + if( $action == 'pdfbook' ) { |
56 | 71 | |
57 | 72 | $title = $article->getTitle(); |
58 | 73 | $opt = ParserOptions::newFromUser( $wgUser ); |
59 | 74 | |
60 | 75 | # Log the export |
61 | | - $msg = $wgUser->getUserPage()->getPrefixedText().' exported as a PDF book'; |
| 76 | + $msg = $wgUser->getUserPage()->getPrefixedText() . " exported as a PDF book"; |
62 | 77 | $log = new LogPage( 'pdf', false ); |
63 | 78 | $log->addEntry( 'book', $wgTitle, $msg ); |
64 | 79 | |
— | — | @@ -77,10 +92,10 @@ |
78 | 93 | $exclude = $this->setProperty( 'Exclude', array() ); |
79 | 94 | $width = $this->setProperty( 'Width', '' ); |
80 | 95 | $width = $width ? "--browserwidth $width" : ''; |
81 | | - if ( !is_array( $exclude ) ) $exclude = split( '\\s*,\\s*', $exclude ); |
| 96 | + if( !is_array( $exclude ) ) $exclude = split( '\\s*,\\s*', $exclude ); |
82 | 97 | |
83 | 98 | # Select articles from members if a category or links in content if not |
84 | | - if ( $format == 'single' ) $articles = array( $title ); |
| 99 | + if( $format == 'single' ) $articles = array( $title ); |
85 | 100 | else { |
86 | 101 | $articles = array(); |
87 | 102 | if ( $title->getNamespace() == NS_CATEGORY ) { |
— | — | @@ -99,7 +114,7 @@ |
100 | 115 | else { |
101 | 116 | $text = $article->fetchContent(); |
102 | 117 | $text = $wgParser->preprocess( $text, $title, $opt ); |
103 | | - if ( preg_match_all( '/^\\*\\s*\\[{2}\\s*([^\\|\\]]+)\\s*.*?\\]{2}/m', $text, $links ) ) |
| 118 | + if ( preg_match_all( "/^\\*\\s*\\[{2}\\s*([^\\|\\]]+)\\s*.*?\\]{2}/m", $text, $links ) ) |
104 | 119 | foreach ( $links[1] as $link ) $articles[] = Title::newFromText( $link ); |
105 | 120 | } |
106 | 121 | } |
— | — | @@ -116,25 +131,24 @@ |
117 | 132 | if ( !in_array( $ttext, $exclude ) ) { |
118 | 133 | $article = new Article( $title ); |
119 | 134 | $text = $article->fetchContent(); |
120 | | - $text = preg_replace( '/<!--([^@]+?)-->/s', '@@'.'@@$1@@'.'@@', $text ); # preserve HTML comments |
121 | | - if ( $format != 'single' ) $text .= '__NOTOC__'; |
| 135 | + $text = preg_replace( "/<!--([^@]+?)-->/s", "@@" . "@@$1@@" . "@@", $text ); # preserve HTML comments |
| 136 | + if ( $format != 'single' ) $text .= "__NOTOC__"; |
122 | 137 | $opt->setEditSection( false ); # remove section-edit links |
123 | 138 | $wgOut->setHTMLTitle( $ttext ); # use this so DISPLAYTITLE magic works |
124 | 139 | $out = $wgParser->parse( $text, $title, $opt, true, true ); |
125 | 140 | $ttext = $wgOut->getHTMLTitle(); |
126 | 141 | $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 |
130 | | - #$text = preg_replace('|<table|', '<table border borderwidth=2 cellpadding=3 cellspacing=0', $text); |
| 142 | + $text = preg_replace( "|(<img[^>]+?src=\")(/.+?>)|", "$1$wgServer$2", $text ); # make image urls absolute |
| 143 | + $text = preg_replace( "|<div\s*class=['\"]?noprint[\"']?>.+?</div>|s", "", $text ); # non-printable areas |
| 144 | + $text = preg_replace( "|@{4}([^@]+?)@{4}|s", "<!--$1-->", $text ); # HTML comments hack |
131 | 145 | $ttext = basename($ttext); |
132 | | - $h1 = $notitle ? '' : "<center><h1>$ttext</h1></center>"; |
133 | | - $html .= utf8_decode("$h1$text\n"); |
| 146 | + $h1 = $notitle ? "" : "<center><h1>$ttext</h1></center>"; |
| 147 | + $html .= utf8_decode( "$h1$text\n" ); |
134 | 148 | } |
135 | 149 | } |
136 | 150 | |
137 | 151 | # If format=html in query-string, return html content directly |
138 | | - if ( $format == 'html' ) { |
| 152 | + if( $format == 'html' ) { |
139 | 153 | $wgOut->disable(); |
140 | 154 | header( "Content-Type: text/html" ); |
141 | 155 | header( "Content-Disposition: attachment; filename=\"$book.html\"" ); |
— | — | @@ -147,8 +161,8 @@ |
148 | 162 | fwrite( $fh, $html ); |
149 | 163 | fclose( $fh ); |
150 | 164 | |
151 | | - $footer = $format == 'single' ? '...' : '.1.'; |
152 | | - $toc = $format == 'single' ? '' : " --toclevels $levels"; |
| 165 | + $footer = $format == 'single' ? "..." : ".1."; |
| 166 | + $toc = $format == 'single' ? "" : " --toclevels $levels"; |
153 | 167 | |
154 | 168 | # Send the file to the client via htmldoc converter |
155 | 169 | $wgOut->disable(); |
— | — | @@ -165,10 +179,11 @@ |
166 | 180 | } |
167 | 181 | return false; |
168 | 182 | } |
169 | | - |
| 183 | + |
170 | 184 | return true; |
171 | 185 | } |
172 | 186 | |
| 187 | + |
173 | 188 | /** |
174 | 189 | * Return a property for htmldoc using global, request or passed default |
175 | 190 | */ |
— | — | @@ -179,12 +194,36 @@ |
180 | 195 | return $default; |
181 | 196 | } |
182 | 197 | |
| 198 | + |
183 | 199 | /** |
184 | | - * Needed in some versions to prevent Special:Version from breaking |
| 200 | + * Add PDF to actions tabs in MonoBook based skins |
185 | 201 | */ |
186 | | - function __toString() { return 'PdfBook'; } |
| 202 | + function onSkinTemplateTabs( &$skin, &$actions) { |
| 203 | + global $wgTitle, $wgTabText; |
| 204 | + $actions['pdfbook'] = array( |
| 205 | + 'class' => false, |
| 206 | + 'text' => $wgTabText, |
| 207 | + 'href' => $wgTitle->getLocalURL( "action=pdfbook&format=single" ), |
| 208 | + ); |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + /** |
| 214 | + * Add PDF to actions tabs in vector based skins |
| 215 | + */ |
| 216 | + function onSkinTemplateNavigation( &$skin, &$actions ) { |
| 217 | + global $wgTitle, $wgTabText; |
| 218 | + $actions['views']['pdfbook'] = array( |
| 219 | + 'class' => false, |
| 220 | + 'text' => $wgTabText, |
| 221 | + 'href' => $wgTitle->getLocalURL( "action=pdfbook&format=single" ), |
| 222 | + ); |
| 223 | + return true; |
| 224 | + } |
187 | 225 | } |
188 | 226 | |
| 227 | + |
189 | 228 | /** |
190 | 229 | * Called from $wgExtensionFunctions array when initialising extensions |
191 | 230 | */ |
— | — | @@ -193,6 +232,7 @@ |
194 | 233 | $wgPdfBook = new PdfBook(); |
195 | 234 | } |
196 | 235 | |
| 236 | + |
197 | 237 | /** |
198 | 238 | * Needed in MediaWiki >1.8.0 for magic word hooks to work properly |
199 | 239 | */ |