jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/364392 )
Change subject: UEModulePDF: Refactor findFiles logic
......................................................................
UEModulePDF: Refactor findFiles logic
Removed logic that resolves the file and encapsulated it in a new class.
Improvements:
- Remove query string from file name
- Add width attribute to the image in case it is not specified already
Change-Id: If2674a75bf4da1c3285dc8f367542097142facc1
ERM: #6806
---
M UEModulePDF/extension.json
A UEModulePDF/includes/PDFFileResolver.php
M UEModulePDF/includes/PDFPageProvider.class.php
M UEModulePDF/includes/PDFServlet.class.php
4 files changed, 186 insertions(+), 80 deletions(-)
Approvals:
Robert Vogel: Looks good to me, approved
jenkins-bot: Verified
diff --git a/UEModulePDF/extension.json b/UEModulePDF/extension.json
index 12653f1..0763731 100644
--- a/UEModulePDF/extension.json
+++ b/UEModulePDF/extension.json
@@ -23,7 +23,8 @@
"BsPDFTemplateProvider":
"includes/PDFTemplateProvider.class.php",
"BsPDFWebService": "includes/PDFWebService.class.php",
"BsPDFServlet": "includes/PDFServlet.class.php",
- "BsExportModulePDF": "includes/ExportModulePDF.class.php"
+ "BsExportModulePDF": "includes/ExportModulePDF.class.php",
+ "PDFFileResolver": "includes/PDFFileResolver.php"
},
"Hooks": {
"LoadExtensionSchemaUpdates": "UEModulePDF::getSchemaUpdates"
diff --git a/UEModulePDF/includes/PDFFileResolver.php
b/UEModulePDF/includes/PDFFileResolver.php
new file mode 100644
index 0000000..99906b7
--- /dev/null
+++ b/UEModulePDF/includes/PDFFileResolver.php
@@ -0,0 +1,181 @@
+<?php
+
+class PDFFileResolver {
+
+ /**
+ * @var DOMElement
+ */
+ protected $oImgNode = null;
+
+ /**
+ * @var string
+ */
+ protected $sWebrootFileSystemPath = '';
+
+ /**
+ * @var string
+ */
+ protected $sFileName = '';
+
+ /**
+ * @var string
+ */
+ protected $sSourceFileName = '';
+
+ /**
+ * @var string
+ */
+ protected $sSourceFilePath = '';
+
+ /**
+ * @var Title
+ */
+ protected $oFileTitle = null;
+
+ /**
+ * @var File
+ */
+ protected $oFileObject = null;
+
+ /**
+ * @var string
+ */
+ protected $sAbsoluteFilesystemName = '';
+
+ /**
+ *
+ * @param DOMElement $imgEl
+ */
+ public function __construct ( $oImgEl, $sWebrootFileSystemPath ) {
+ $this->oImgNode= $oImgEl;
+ $this->sWebrootFileSystemPath = $sWebrootFileSystemPath;
+
+ $this->init();
+ }
+
+ protected function init () {
+ $this->extractSourceFilename();
+ $this->setFileTitle();
+ $this->setFileObject();
+ $this->setWidthAttribute();
+ $this->setAbsoluteFilesystemPath();
+ $this->setFileName();
+ $this->setSourceAttributes();
+ }
+
+ protected function extractSourceFilename() {
+ global $wgServer, $wgThumbnailScriptPath, $wgUploadPath,
$wgScriptPath;
+ $aForPreg = array(
+ $wgServer,
+ $wgThumbnailScriptPath . "?f=",
+ $wgUploadPath,
+ $wgScriptPath
+ );
+
+ $sOrigUrl = $this->oImgNode->getAttribute( 'src' );
+ if( strpos( $sOrigUrl, '?' ) ) {
+ $sOrigUrl = substr( $sOrigUrl, 0, strpos( $sOrigUrl,
'?' ) );
+ }
+ $sSrcUrl = urldecode( $sOrigUrl );
+
+ //Extracting the filename
+ foreach( $aForPreg as $sForPreg ) {
+ $sSrcUrl = preg_replace( "#" . preg_quote( $sForPreg
,"#" ) . "#", '', $sSrcUrl );
+ $sSrcUrl = preg_replace( '/(&.*)/','', $sSrcUrl );
+ };
+
+ $this->sSourceFilePath = $sSrcUrl;
+
+ $sSrcFilename = wfBaseName( $sSrcUrl );
+ $oAnchor = BsDOMHelper::getParentDOMElement( $this->oImgNode,
array( 'a' ) );
+ if( $oAnchor instanceof DOMElement && $oAnchor->getAttribute(
'data-bs-title' ) !== '' ) {
+ $sSrcFilename = $oAnchor->getAttribute( 'data-bs-title'
);
+ }
+
+ $bIsThumb = UploadBase::isThumbName($sSrcFilename);
+ $sTmpFilename = $sSrcFilename;
+ if( $bIsThumb ) {
+ //HINT: Thumbname-to-filename-conversion taken from
includes/Upload/UploadBase.php
+ //Check for filenames like 50px- or 180px-, these are
mostly thumbnails
+ $sTmpFilename = substr( $sTmpFilename , strpos(
$sTmpFilename , '-' ) +1 );
+ }
+
+ $this->sSourceFileName = $sTmpFilename;
+ }
+
+ protected function setFileTitle() {
+ $this->oFileTitle = Title::newFromText( $this->sSourceFileName,
NS_FILE );
+ }
+
+ protected function setFileObject() {
+ $this->oFileObject = RepoGroup::singleton()->findFile(
$this->oFileTitle );
+ }
+
+ protected function setWidthAttribute() {
+ $iWidth = $this->oImgNode->getAttribute( 'width' );
+ if( empty( $iWidth ) && $this->oFileObject instanceof File &&
$this->oFileObject->exists() ) {
+ $iWidth = $this->oFileObject->getWidth();
+ $this->oImgNode->setAttribute( 'width', $iWidth );
+ }
+ if( $iWidth > 700 ) {
+ $this->oImgNode->setAttribute( 'width', 700 );
+ $this->oImgNode->removeAttribute( 'height' );
+
+ $sClasses = $this->oImgNode->getAttribute( 'class' );
+ $this->oImgNode->setAttribute( 'class', $sClasses.'
maxwidth' );
+ }
+ }
+
+ protected function setAbsoluteFilesystemPath() {
+ global $wgUploadPath;
+
+ if( $this->oFileObject instanceof File &&
$this->oFileObject->exists() ) {
+ $oFileRepoLocalRef =
$this->oFileObject->getRepo()->getLocalReference( $this->oFileObject->getPath()
);
+ if ( !is_null( $oFileRepoLocalRef ) ) {
+ $this->sAbsoluteFilesystemPath =
$oFileRepoLocalRef->getPath();
+ }
+ } else {
+ $this->sAbsoluteFilesystemPath =
$this->getFileSystemPath( $wgUploadPath . $this->sSourceFilePath );
+ }
+ }
+
+ protected function setFileName() {
+ if( !empty( $this->sAbsoluteFilesystemPath ) ) {
+ $this->sSourceFileName = $this->oFileObject->getName();
+ }
+ }
+
+ protected function setSourceAttributes() {
+ $this->oImgNode->setAttribute( 'data-orig-src',
$this->oImgNode->getAttribute( 'src' ) );
+ $this->oImgNode->setAttribute( 'src', 'images/' . urlencode(
$this->sSourceFileName ) );
+ }
+
+ public function getAbsoluteFilesystemPath() {
+ return $this->sAbsoluteFilesystemName;
+ }
+
+ public function getFileName() {
+ return $this->sFileName;
+ }
+
+ /**
+ * This helper method resolves the local file system path of a found
file
+ * @param string $sUrl
+ * @return string The local file system path
+ */
+ protected function getFileSystemPath( $sUrl ) {
+ if( $sUrl{0} !== '/' || strpos( $sUrl,
$this->sWebrootFileSystemPath ) === 0 ) {
+ return $sUrl; //not relative to webroot or absolute
filesystempath
+ }
+
+ $sScriptUrlDir = dirname( $_SERVER['SCRIPT_NAME'] );
+ $sScriptFSDir = str_replace( '\\', '/', dirname(
$_SERVER['SCRIPT_FILENAME'] ) );
+ if( strpos( $sScriptFSDir, $sScriptUrlDir) == 0 ){ //detect
virtual path (webserver setting)
+ $sUrl = '/'.substr( $sUrl, strlen( $sScriptUrlDir ) );
+ }
+
+ $sNewUrl = $this->sWebrootFileSystemPath . $sUrl; // TODO RBV
(08.02.11 15:56): What about $wgUploadDirectory?
+ return $sNewUrl;
+ }
+
+}
diff --git a/UEModulePDF/includes/PDFPageProvider.class.php
b/UEModulePDF/includes/PDFPageProvider.class.php
index 05df087..395b6b0 100644
--- a/UEModulePDF/includes/PDFPageProvider.class.php
+++ b/UEModulePDF/includes/PDFPageProvider.class.php
@@ -313,19 +313,6 @@
}
}
- //TODO: Should this be in PdfServlet::findFiles()? Or we should
add the images as attachments
- //Prevent large images from clipping
- foreach( $oPageDOM->getElementsByTagName( 'img' ) as
$oImgElement ) {
- $iWidth = $oImgElement->getAttribute( 'width' );
- if( $iWidth > 700 ) {
- $oImgElement->setAttribute( 'width', 700 );
- $oImgElement->removeAttribute( 'height' );
-
- $sClasses = $oImgElement->getAttribute( 'class'
);
- $oImgElement->setAttribute( 'class',
$sClasses.' maxwidth' );
- }
- }
-
//Prevent "first page empty" bug
$oBodyContent = $oDOMXPath->query( "//*[contains(@class,
'bodyContent')]" )->item(0);
$oAntiBugP = $oPageDOM->createElement( 'p' );
diff --git a/UEModulePDF/includes/PDFServlet.class.php
b/UEModulePDF/includes/PDFServlet.class.php
index c50d0bf..52bc4b5 100644
--- a/UEModulePDF/includes/PDFServlet.class.php
+++ b/UEModulePDF/includes/PDFServlet.class.php
@@ -195,56 +195,14 @@
* @return boolean Well, always true.
*/
protected function findFiles( &$oHtml ) {
- global $wgServer, $wgThumbnailScriptPath, $wgUploadPath,
$wgScriptPath;
-
- $aForPreg = array(
- $wgServer,
- $wgThumbnailScriptPath . "?f=",
- $wgUploadPath,
- $wgScriptPath
- );
//Find all images
$oImageElements = $oHtml->getElementsByTagName( 'img' );
foreach( $oImageElements as $oImageElement ) {
- $sSrcUrl = urldecode(
$oImageElement->getAttribute( 'src' ) );
+ $oFileResolver = new PDFFileResolver( $oImageElement,
$this->aParams['webroot-filesystempath'] );
- //Extracting the filename
- foreach( $aForPreg as $sForPreg ) {
- $sSrcUrl = preg_replace( "#" . preg_quote(
$sForPreg ,"#" ) . "#", '', $sSrcUrl );
- $sSrcUrl = preg_replace( '/(&.*)/','', $sSrcUrl
);
- };
-
- $sSrcFilename = wfBaseName( $sSrcUrl );
- $oAnchor = BsDOMHelper::getParentDOMElement(
$oImageElement, array( 'a' ) );
- if( $oAnchor instanceof DOMElement &&
$oAnchor->getAttribute( 'data-bs-title' ) !== '' ) {
- $sSrcFilename = $oAnchor->getAttribute(
'data-bs-title' );
- }
-
- $bIsThumb = UploadBase::isThumbName($sSrcFilename);
- $sTmpFilename = $sSrcFilename;
- if( $bIsThumb ) {
- //HINT: Thumbname-to-filename-conversion taken
from includes/Upload/UploadBase.php
- //Check for filenames like 50px- or 180px-,
these are mostly thumbnails
- $sTmpFilename = substr( $sTmpFilename , strpos(
$sTmpFilename , '-' ) +1 );
- }
- $oFileTitle = Title::newFromText( $sTmpFilename,
NS_FILE );
- $oImage = RepoGroup::singleton()->findFile( $oFileTitle
);
-
- if( $oImage instanceof File && $oImage->exists() ) {
- $oFileRepoLocalRef =
$oImage->getRepo()->getLocalReference( $oImage->getPath() );
- if ( !is_null( $oFileRepoLocalRef ) ) {
- $sAbsoluteFileSystemPath =
$oFileRepoLocalRef->getPath();
- }
- $sSrcFilename = $oImage->getName();
- }
- else {
- $sAbsoluteFileSystemPath =
$this->getFileSystemPath( $wgUploadPath . $sSrcUrl );
- }
- // TODO RBV (05.04.12 11:48): Check if urlencode has
side effects
- $oImageElement->setAttribute( 'data-orig-src',
$oImageElement->getAttribute( 'src' ) );
- $oImageElement->setAttribute( 'src',
'images/'.urlencode($sSrcFilename) );
- $sFileName = $sSrcFilename;
+ $sFileName = $oFileResolver->getFileName();
+ $sAbsoluteFileSystemPath =
$oFileResolver->getAbsoluteFilesystemPath();
wfRunHooks( 'BSUEModulePDFFindFiles', array( $this,
$oImageElement, &$sAbsoluteFileSystemPath, &$sFileName, 'images' ) );
wfRunHooks( 'BSUEModulePDFWebserviceFindFiles', array(
$this, $oImageElement, &$sAbsoluteFileSystemPath, &$sFileName, 'images' ) );
$this->aFiles['images'][$sFileName] =
$sAbsoluteFileSystemPath;
@@ -267,27 +225,6 @@
wfRunHooks( 'BSUEModulePDFAfterFindFiles', array( $this,
$oHtml, &$this->aFiles, $this->aParams, $oDOMXPath ) );
return true;
- }
-
- //<editor-fold desc="Helper Methods" defaultstate="collapsed">
- /**
- * This helper method resolves the local file system path of a found
file
- * @param string $sUrl
- * @return string The local file system path
- */
- public function getFileSystemPath( $sUrl ) {
- if( $sUrl{0} !== '/' || strpos( $sUrl,
$this->aParams['webroot-filesystempath'] ) === 0 ) {
- return $sUrl; //not relative to webroot or absolute
filesystempath
- }
-
- $sScriptUrlDir = dirname( $_SERVER['SCRIPT_NAME'] );
- $sScriptFSDir = str_replace( '\\', '/', dirname(
$_SERVER['SCRIPT_FILENAME'] ) );
- if( strpos( $sScriptFSDir, $sScriptUrlDir) == 0 ){ //detect
virtual path (webserver setting)
- $sUrl = '/'.substr( $sUrl, strlen( $sScriptUrlDir ) );
- }
-
- $sNewUrl = $this->aParams['webroot-filesystempath'].$sUrl; //
TODO RBV (08.02.11 15:56): What about $wgUploadDirectory?
- return $sNewUrl;
}
protected function doFilesUpload( $aPostData, $aErrors = array() ) {
--
To view, visit https://gerrit.wikimedia.org/r/364392
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If2674a75bf4da1c3285dc8f367542097142facc1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceExtensions
Gerrit-Branch: REL1_27
Gerrit-Owner: Robert Vogel <[email protected]>
Gerrit-Reviewer: ItSpiderman <[email protected]>
Gerrit-Reviewer: Robert Vogel <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits