Skip to content

Commit

Permalink
Merge pull request #3 from Wikia/PLATFORM-7868
Browse files Browse the repository at this point in the history
PLATFORM-7868 | Update PDFEmbed to work with MW1.39
  • Loading branch information
swietlana authored Mar 10, 2023
2 parents b246c65 + a83419c commit b0bc1b9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 107 deletions.
16 changes: 12 additions & 4 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@
"AutoloadClasses": {
"PDFEmbed": "src/PDFEmbed.hooks.php"
},
"HookHandlers": {
"main": {
"class": "PDFEmbed",
"services": ["MainConfig", "RepoGroup"]
}
},
"Hooks": {
"ParserFirstCallInit": "PDFEmbed::onParserFirstCallInit"
"ParserFirstCallInit": "main"
},
"FileExtensions": [
"pdf"
],
"config": {
"PdfEmbed": {
"width": 800,
"height": 1090
"value": {
"width": 800,
"height": 1090
}
}
},
"manifest_version": 1
"manifest_version": 2
}
200 changes: 97 additions & 103 deletions src/PDFEmbed.hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,123 +8,117 @@
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
*/

use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\MediaWikiServices;

class PDFEmbed
{
/**
* Sets up this extension's parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit(Parser $parser)
{
$parser->setHook('pdf', [self::class, 'generateTag']);
class PDFEmbed implements ParserFirstCallInitHook {
public function __construct( private Config $config, private RepoGroup $repoGroup ) {
}

return true;
}
/**
* Sets up this extension's parser functions.
*
* @param object Parser object passed as a reference.
* @return bool true
*/
public function onParserFirstCallInit( $parser ) {
$parser->setHook( 'pdf', [ $this, 'generateTag' ] );

/**
* Generates the PDF object tag.
*
* @access public
* @param string Namespace prefixed article of the PDF file to display.
* @param array Arguments on the tag.
* @param object Parser object.
* @param object PPFrame object.
* @return string HTML
*/
static public function generateTag($file, $args, Parser $parser, PPFrame $frame)
{
$request = RequestContext::getMain()->getRequest();
return true;
}

$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
$wgPdfEmbed = $config->get('PdfEmbed');
/**
* Generates the PDF object tag.
*
* @param string Namespace prefixed article of the PDF file to display.
* @param array Arguments on the tag.
* @param object Parser object.
* @param object PPFrame object.
* @return string HTML
*/
public function generateTag( $file, $args, Parser $parser, PPFrame $frame ) {
$request = RequestContext::getMain()->getRequest();
$wgPdfEmbed = $this->config->get( 'PdfEmbed' );

if (strstr($file, '{{{') !== false) {
$file = $parser->recursiveTagParse($file, $frame);
}
if ( str_contains( $file, '{{{' ) ) {
$file = $parser->recursiveTagParse( $file, $frame );
}

if ($request->getVal('action') == 'edit' || $request->getVal('action') == 'submit') {
$user = RequestContext::getMain()->getUser();
} else {
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
$user = $userFactory->newFromName($parser->getRevisionUser());
}
if ( $request->getVal( 'action' ) == 'edit' || $request->getVal( 'action' ) == 'submit' ) {
$user = RequestContext::getMain()->getUser();
} else {
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
$user = $userFactory->newFromName( $parser->getRevisionUser() );
}

if ($user === false) {
return self::error('embed_pdf_invalid_user');
}
if ( $user === false ) {
return self::error( 'embed_pdf_invalid_user' );
}

if (!$user->isAllowed('embed_pdf')) {
return self::error('embed_pdf_no_permission');
}
if ( !$user->isAllowed( 'embed_pdf' ) ) {
return self::error( 'embed_pdf_no_permission' );
}

if (empty($file) || !preg_match('#(.+?)\.pdf#is', $file)) {
return self::error('embed_pdf_blank_file');
}
if ( empty( $file ) || !preg_match( '#(.+?)\.pdf#is', $file ) ) {
return self::error( 'embed_pdf_blank_file' );
}

$file = $services->getRepoGroup()->findFile(Title::newFromText($file));
$file = $this->repoGroup->findFile( Title::newFromText( $file ) );

if (array_key_exists('width', $args)) {
$width = intval($parser->recursiveTagParse($args['width'], $frame));
} else {
$width = intval($wgPdfEmbed['width']);
}
if (array_key_exists('height', $args)) {
$height = intval($parser->recursiveTagParse($args['height'], $frame));
} else {
$height = intval($wgPdfEmbed['height']);
}
if (array_key_exists('page', $args)) {
$page = intval($parser->recursiveTagParse($args['page'], $frame));
} else {
$page = 1;
}
if ( array_key_exists( 'width', $args ) ) {
$width = intval( $parser->recursiveTagParse( $args['width'], $frame ) );
} else {
$width = intval( $wgPdfEmbed['width'] );
}
if ( array_key_exists( 'height', $args ) ) {
$height = intval( $parser->recursiveTagParse( $args['height'], $frame ) );
} else {
$height = intval( $wgPdfEmbed['height'] );
}
if ( array_key_exists( 'page', $args ) ) {
$page = intval( $parser->recursiveTagParse( $args['page'], $frame ) );
} else {
$page = 1;
}

if ($file !== false) {
return self::embed($file, $width, $height, $page);
} else {
return self::error('embed_pdf_invalid_file');
}
}
if ( $file !== false ) {
return self::embed( $file, $width, $height, $page );
} else {
return self::error( 'embed_pdf_invalid_file' );
}
}

/**
* Returns a standard error message.
*
* @access private
* @param string Error message key to display.
* @return string HTML error message.
*/
static private function error($messageKey)
{
return Xml::span(wfMessage($messageKey)->plain(), 'error');
}
/**
* Returns a standard error message.
*
* @private
* @param string Error message key to display.
* @return string HTML error message.
*/
private static function error( $messageKey ) {
return Xml::span( wfMessage( $messageKey )->plain(), 'error' );
}

/**
* Returns a HTML object as string.
*
* @access private
* @param object File object.
* @param integer Width of the object.
* @param integer Height of the object.
* @return string HTML object.
*/
static private function embed(File $file, $width, $height, $page)
{
return Html::rawElement(
'iframe',
[
'width' => $width,
'height' => $height,
'src' => $file->getFullUrl() . '#page=' . $page,
'style' => 'max-width: 100%;'
]
);
}
/**
* Returns a HTML object as string.
*
* @private
* @param object File object.
* @param integer Width of the object.
* @param integer Height of the object.
* @return string HTML object.
*/
private static function embed( File $file, $width, $height, $page ) {
return Html::rawElement(
'iframe',
[
'width' => $width,
'height' => $height,
'src' => $file->getFullUrl() . '#page=' . $page,
'style' => 'max-width: 100%;'
]
);
}
}

0 comments on commit b0bc1b9

Please sign in to comment.