r60653 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60652‎ | r60653 | r60654 >
Date:10:01, 5 January 2010
Author:nad
Status:deferred
Tags:
Comment:
add --no-title switch to htmldoc command
Modified paths:
  • /trunk/extensions/PdfBook/PdfBook.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PdfBook/PdfBook.php
@@ -16,7 +16,7 @@
1717 */
1818 if (!defined('MEDIAWIKI')) die('Not an entry point.');
1919
20 -define('PDFBOOK_VERSION', '1.0.3, 2008-12-09');
 20+define('PDFBOOK_VERSION', '1.0.4, 2010-01-05');
2121
2222 $wgExtensionFunctions[] = 'wfSetupPdfBook';
2323 $wgHooks['LanguageGetMagic'][] = 'wfPdfBookLanguageGetMagic';
@@ -28,7 +28,7 @@
2929 'description' => 'Composes a book from articles in a category and exports as a PDF book',
3030 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfBook',
3131 'version' => PDFBOOK_VERSION
32 - );
 32+);
3333
3434 class PdfBook {
3535
@@ -47,60 +47,60 @@
4848 /**
4949 * Perform the export operation
5050 */
51 - function onUnknownAction($action, $article) {
 51+ function onUnknownAction( $action, $article ) {
5252 global $wgOut, $wgUser, $wgTitle, $wgParser, $wgRequest;
5353 global $wgServer, $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgUploadDirectory, $wgScript;
5454
5555 if ($action == 'pdfbook') {
5656
5757 $title = $article->getTitle();
58 - $opt = ParserOptions::newFromUser($wgUser);
 58+ $opt = ParserOptions::newFromUser( $wgUser );
5959
6060 # Log the export
6161 $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 );
6464
6565 # Initialise PDF variables
66 - $format = $wgRequest->getText('format');
67 - $notitle = $wgRequest->getText('notitle');
 66+ $format = $wgRequest->getText( 'format' );
 67+ $notitle = $wgRequest->getText( 'notitle' );
6868 $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', '' );
8080 $width = $width ? "--browserwidth $width" : '';
81 - if (!is_array($exclude)) $exclude = split('\\s*,\\s*', $exclude);
 81+ if ( !is_array( $exclude ) ) $exclude = split( '\\s*,\\s*', $exclude );
8282
8383 # 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 );
8585 else {
8686 $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() );
9090 $result = $db->select(
9191 'categorylinks',
9292 'cl_from',
9393 "cl_to = $cat",
9494 'PdfBook',
95 - array('ORDER BY' => 'cl_sortkey')
 95+ array( 'ORDER BY' => 'cl_sortkey' )
9696 );
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] );
9999 }
100100 else {
101101 $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 );
105105 }
106106 }
107107
@@ -111,21 +111,21 @@
112112 $wgScriptPath = $wgServer.$wgScriptPath;
113113 $wgUploadPath = $wgServer.$wgUploadPath;
114114 $wgScript = $wgServer.$wgScript;
115 - foreach ($articles as $title) {
 115+ foreach ( $articles as $title ) {
116116 $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 );
119119 $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 );
125125 $ttext = $wgOut->getHTMLTitle();
126126 $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
130130 #$text = preg_replace('|<table|', '<table border borderwidth=2 cellpadding=3 cellspacing=0', $text);
131131 $ttext = basename($ttext);
132132 $h1 = $notitle ? '' : "<center><h1>$ttext</h1></center>";
@@ -134,34 +134,34 @@
135135 }
136136
137137 # If format=html in query-string, return html content directly
138 - if ($format == 'html') {
 138+ if ( $format == 'html' ) {
139139 $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\"" );
142142 print $html;
143143 }
144144 else {
145145 # 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 );
150150
151151 $footer = $format == 'single' ? '...' : '.1.';
152152 $toc = $format == 'single' ? '' : " --toclevels $levels";
153153
154154 # Send the file to the client via htmldoc converter
155155 $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\"" );
158158 $cmd = "--left $left --right $right --top $top --bottom $bottom";
159159 $cmd .= " --header ... --footer $footer --headfootsize 8 --quiet --jpeg --color";
160160 $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";
162162 $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 );
166166 }
167167 return false;
168168 }
@@ -172,10 +172,10 @@
173173 /**
174174 * Return a property for htmldoc using global, request or passed default
175175 */
176 - function setProperty($name, $default) {
 176+ function setProperty( $name, $default ) {
177177 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"];
180180 return $default;
181181 }
182182
@@ -196,8 +196,8 @@
197197 /**
198198 * Needed in MediaWiki >1.8.0 for magic word hooks to work properly
199199 */
200 -function wfPdfBookLanguageGetMagic(&$magicWords, $langCode = 0) {
 200+function wfPdfBookLanguageGetMagic( &$magicWords, $langCode = 0 ) {
201201 global $wgPdfBookMagic;
202 - $magicWords[$wgPdfBookMagic] = array($langCode, $wgPdfBookMagic);
 202+ $magicWords[$wgPdfBookMagic] = array( $langCode, $wgPdfBookMagic );
203203 return true;
204204 }

Status & tagging log