Seb35 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/300756

Change subject: Some tweaks for compatibility with newer versions of MediaWiki, 
Dompdf, PHP
......................................................................

Some tweaks for compatibility with newer versions of MediaWiki, Dompdf, PHP

PHP compatibility:
* changed the class constructor to __construct instead of the class name

MediaWiki compatibility:
* Article::preSaveTransform no longer exists (since MW 1.18)

Dompdf compatibility:
* class renamed from DOMPDF to Dompdf\Dompdf
* added some installation instructions to use Composer
* added autodetection when Dompdf is installed with Composer
* removed utf8_decode, it works better without it

Warning: I only tested with the Dompdf and HTMLDoc backends, possibly the others
still don’t work; and in fact HTMLDoc currently doesn’t work, only Dompdf works.

Issue related to this commit:
https://www.mediawiki.org/wiki/Topic:T7n9rikdef7xg59c

Change-Id: I04eda179bab689e29b9f26caf10807f4136afa3f
---
M PdfExport_body.php
M converters/DomPdfConverter.php
M converters/HtmlDocPdfConverter.php
M converters/MPdfConverter.php
M converters/PdfConverterFactory.php
M converters/PrincePdfConverter.php
6 files changed, 23 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PdfExport 
refs/changes/56/300756/1

diff --git a/PdfExport_body.php b/PdfExport_body.php
index f53d28e..365c295 100755
--- a/PdfExport_body.php
+++ b/PdfExport_body.php
@@ -27,7 +27,7 @@
        /**
         * Setup the special page. Tries to detect a PDF generation tool to 
use. Prefers in order: PrincePDF, HtmlDoc, DomPdf.
         */
-       function SpecialPdf () {
+       function __construct() {
                parent::__construct('PdfPrint');
                $this->converter = PdfConverterFactory::getPdfConverter();
        }
diff --git a/converters/DomPdfConverter.php b/converters/DomPdfConverter.php
index c3b9119..34a8808 100755
--- a/converters/DomPdfConverter.php
+++ b/converters/DomPdfConverter.php
@@ -2,10 +2,16 @@
 if (!defined('MEDIAWIKI'))
        die();
 
+use Dompdf\Dompdf;
+
 /**
  * A DomPdf based conversion backend.
  * 
  * Installation:
+ * DomPdf can be easily installed with Composer: add in the file 
composer.local.json (from MediaWiki 1.25):
+ *  "dompdf/dompdf": "*" (and possibly a comma at the end of this line if it 
is not the last item)
+ * in the 'require' section, and run 'composer update --no-dev'. If you choose 
this installation way, you
+ * don’t need to set $wgPdfExportDomPdfConfigFile, it will be automatically 
detected.
  * DomPdf can be downloaded from here: http://code.google.com/p/dompdf/. Unzip 
the code into your
  * extension directory and set $wgPdfExportDomPdfConfigFile equal to the full 
path to the
  * dompdf_config.inc.php file.
@@ -22,7 +28,9 @@
        function initialize (&$options) {
                global $wgPdfExportDomPdfConfigFile;
                // Load the configuration file. This loads the whole DomPdf 
framework
-               require_once($wgPdfExportDomPdfConfigFile);
+               if( !class_exists( 'Dompdf\Dompdf' ) ) {
+                       require_once($wgPdfExportDomPdfConfigFile);
+               }
        }
 
        /**
@@ -87,7 +95,7 @@
                global $wgOut, $IP, $wgPdfExportAttach;
                $wgOut->disable();
                #$old_error_level = error_reporting( 0 );
-               $dompdf = new DOMPDF();
+               $dompdf = new Dompdf();
                $dompdf->set_base_path("$IP/");
                $dompdf->set_paper( strtolower( $options['size'] ), 
$options['orientation']);
                $dompdf->load_html($html);
@@ -112,7 +120,7 @@
                                
$dompdf->get_canvas()->get_cpdf()->setEncryption($options['user_pass'], 
$options['owner_pass'], $perms);
                        }
                }
-               $dompdf->stream(utf8_decode($options['filename']) . ".pdf", 
array('Attachment'=>($wgPdfExportAttach?1:0)));
+               $dompdf->stream($options['filename'] . ".pdf", 
array('Attachment'=>($wgPdfExportAttach?1:0)));
                #error_reporting( $old_error_level );
        }
        
@@ -136,12 +144,9 @@
                $parserOptions = ParserOptions::newFromUser( $wgUser );
                $parserOptions->setEditSection( false );
                $parserOptions->setTidy(true);
-               $parserOutput = $wgParser->parse( $article->preSaveTransform( 
"__NOTOC__\n\n".$article->getContent() ) ."\n\n", $title, $parserOptions );
+               $parserOutput = $wgParser->parse( "__NOTOC__\n\n" . 
$article->getContent() . "\n\n", $title, $parserOptions );
 
                $bhtml = $parserOutput->getText();
-               // Hack to thread the EUR sign correctly
-               $bhtml = str_replace(chr(0xE2) . chr(0x82) . chr(0xAC), 
chr(0xA4), $bhtml);
-               $bhtml = utf8_decode($bhtml);
 
                // add the '"'. so links pointing to other wikis do not get 
erroneously converted.
                $bhtml = str_replace('"'.$wgScriptPath, 
'"'.$wgServer.$wgScriptPath, $bhtml);
@@ -158,14 +163,17 @@
                // removed heights of images
                $bhtml = preg_replace('/height="\d+"/', '', $bhtml);
                // set upper limit for width
-               $bhtml = preg_replace('/width="(\d+)"/e', '"width=\"".($1> 
$wgPdfExportMaxImageWidth ? $wgPdfExportMaxImageWidth : $1)."\""', $bhtml);
+               $bhtml = preg_replace_callback('/width="(\d+)"/', function( 
$matches ) {
+                       global $wgPdfExportMaxImageWidth;
+                       return 'width="'.($matches[1] > 
$wgPdfExportMaxImageWidth ? $wgPdfExportMaxImageWidth : $matches[1]).'"';
+               }, $bhtml);
                
                if ($wgPdfExportHttpsImages) {
                        $bhtml = str_replace('img src=\"https:\/\/','img 
src=\"http:\/\/', $bhtml);
                }
 
                $css = $this->getPageCss( $page, $options );
-               return "<html><head><title>" . utf8_decode($page) . 
"</title>$css</head><body>" . $bhtml . "</body></html>";
+               return "<html><head><title>" . $page . 
"</title>$css</head><body>" . $bhtml . "</body></html>";
        }
        
        /**
@@ -180,4 +188,4 @@
                '<link rel="stylesheet" 
href="'.$wgServer.$wgScriptPath.'/skins/common/shared.css?207" type="text/css" 
media="screen" />'.
                '<link rel="stylesheet" 
href="'.$wgServer.$wgScriptPath.'/index.php?title=MediaWiki:Common.css&amp;usemsgcache=yes&amp;ctype=text%2Fcss&amp;smaxage=18000&amp;action=raw&amp;maxage=18000"
 type="text/css" media="all" />';
        }
-}
\ No newline at end of file
+}
diff --git a/converters/HtmlDocPdfConverter.php 
b/converters/HtmlDocPdfConverter.php
index c9b5963..d31fe7f 100755
--- a/converters/HtmlDocPdfConverter.php
+++ b/converters/HtmlDocPdfConverter.php
@@ -148,7 +148,7 @@
                $parserOptions = ParserOptions::newFromUser($wgUser);
                $parserOptions->setEditSection(false);
                $parserOptions->setTidy(true);
-               $parserOutput = 
$wgParser->parse($article->preSaveTransform("__NOTOC__\n\n".$article->getContent())."\n\n",
 $title, $parserOptions);
+               $parserOutput = $wgParser->parse( "__NOTOC__\n\n" . 
$article->getContent() . "\n\n", $title, $parserOptions);
 
                $bhtml = $parserOutput->getText();
                // XXX Hack to thread the EUR sign correctly
diff --git a/converters/MPdfConverter.php b/converters/MPdfConverter.php
index e8555bc..7fad5c6 100755
--- a/converters/MPdfConverter.php
+++ b/converters/MPdfConverter.php
@@ -102,7 +102,7 @@
                $parserOptions = ParserOptions::newFromUser($wgUser);
                $parserOptions->setEditSection(false);
                $parserOptions->setTidy(true);
-               $parserOutput = 
$wgParser->parse($article->preSaveTransform("__NOTOC__\n\n".$article->getContent())."\n\n",
 $title, $parserOptions);
+               $parserOutput = $wgParser->parse( "__NOTOC__\n\n" . 
$article->getContent() . "\n\n", $title, $parserOptions);
 
                $bhtml = $parserOutput->getText();
                # Note: we don't want to utf8_decode here. mPDF handles UFT-8 
characters.
diff --git a/converters/PdfConverterFactory.php 
b/converters/PdfConverterFactory.php
index 5b5b231..7212660 100755
--- a/converters/PdfConverterFactory.php
+++ b/converters/PdfConverterFactory.php
@@ -20,7 +20,7 @@
                        return new MwLibPdfConverter();
                }
 
-               if ($wgPdfExportDomPdfConfigFile && 
file_exists($wgPdfExportDomPdfConfigFile)) {
+               if ( class_exists( 'Dompdf\Dompdf' ) || 
($wgPdfExportDomPdfConfigFile && is_file($wgPdfExportDomPdfConfigFile)) ) {
                        return new DomPdfConverter();
                }
 
diff --git a/converters/PrincePdfConverter.php 
b/converters/PrincePdfConverter.php
index b859c39..515947e 100755
--- a/converters/PrincePdfConverter.php
+++ b/converters/PrincePdfConverter.php
@@ -111,7 +111,7 @@
                $parserOptions = ParserOptions::newFromUser( $wgUser );
                $parserOptions->setEditSection( false );
                $parserOptions->setTidy(true);
-               $parserOutput = $wgParser->parse( $article->preSaveTransform( 
"__NOTOC__\n\n".$article->getContent() ) ."\n\n", $title, $parserOptions );
+               $parserOutput = $wgParser->parse( "__NOTOC__\n\n" . 
$article->getContent() . "\n\n", $title, $parserOptions );
 
                $bhtml = $parserOutput->getText();
                // Hack to thread the EUR sign correctly

-- 
To view, visit https://gerrit.wikimedia.org/r/300756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04eda179bab689e29b9f26caf10807f4136afa3f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PdfExport
Gerrit-Branch: master
Gerrit-Owner: Seb35 <seb35wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to