Umherirrender has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/354860 )

Change subject: Add phpcs and make pass
......................................................................

Add phpcs and make pass

Change-Id: I2921f450aeb4e896157d81c1ac2b5b5b13c31240
---
M CreatePdfThumbnailsJob.class.php
M PdfHandler.image.php
M PdfHandler_body.php
M composer.json
A phpcs.xml
5 files changed, 91 insertions(+), 75 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PdfHandler 
refs/changes/60/354860/1

diff --git a/CreatePdfThumbnailsJob.class.php b/CreatePdfThumbnailsJob.class.php
index b8a67b2..1dfacef 100644
--- a/CreatePdfThumbnailsJob.class.php
+++ b/CreatePdfThumbnailsJob.class.php
@@ -12,23 +12,23 @@
         *
         * @param $title Title Title object
         * @param $params array Associative array of options:
-        *     page:           page number for which the thumbnail will be 
created
-        *     jobtype:        CreatePDFThumbnailsJob::BIG_THUMB or 
CreatePDFThumbnailsJob::SMALL_THUMB
-        *                     BIG_THUMB will create a thumbnail visible for 
full thumbnail view,
-        *                     SMALL_THUMB will create a thumbnail shown in 
"previous page"/"next page" boxes
-        * 
+        *     page:    page number for which the thumbnail will be created
+        *     jobtype: CreatePDFThumbnailsJob::BIG_THUMB or 
CreatePDFThumbnailsJob::SMALL_THUMB
+        *              BIG_THUMB will create a thumbnail visible for full 
thumbnail view,
+        *              SMALL_THUMB will create a thumbnail shown in "previous 
page"/"next page" boxes
         */
        public function __construct( $title, $params ) {
                parent::__construct( 'createPdfThumbnailsJob', $title, $params 
);
        }
-       
+
        /**
         * Run a thumbnail job on a given PDF file.
         * @return bool true
         */
        public function run() {
                if ( !isset( $this->params['page'] ) ) {
-                       wfDebugLog('thumbnails', 'A page for thumbnails job of 
' . $this->title->getText() . ' was not specified! That should never happen!');
+                       wfDebugLog( 'thumbnails', 'A page for thumbnails job of 
' . $this->title->getText() .
+                               ' was not specified! That should never happen!' 
);
                        return true; // no page set? that should never happen
                }
 
@@ -37,7 +37,7 @@
                        return true; // Just silently fail, perhaps the file 
was already deleted, don't bother
                }
 
-               switch ($this->params['jobtype']) {
+               switch ( $this->params['jobtype'] ) {
                        case self::BIG_THUMB:
                                global $wgImageLimits;
                                // Ignore user preferences, do default 
thumbnails
@@ -59,26 +59,27 @@
                                $height_orig = $file->getHeight( 
$this->params['page'] );
                                $height = $height_orig;
                                if ( $width > $maxWidth || $height > $maxHeight 
) {
-                                       # Calculate the thumbnail size.
-                                       # First case, the limiting factor is 
the width, not the height.
+                                       // Calculate the thumbnail size.
+                                       // First case, the limiting factor is 
the width, not the height.
                                        if ( $width / $height >= $maxWidth / 
$maxHeight ) {
-                                               //$height = round( $height * 
$maxWidth / $width );
+                                               // $height = round( $height * 
$maxWidth / $width );
                                                $width = $maxWidth;
-                                               # Note that $height <= 
$maxHeight now.
+                                               // Note that $height <= 
$maxHeight now.
                                        } else {
                                                $newwidth = floor( $width * 
$maxHeight / $height );
-                                               //$height = round( $height * 
$newwidth / $width );
+                                               // $height = round( $height * 
$newwidth / $width );
                                                $width = $newwidth;
-                                               # Note that $height <= 
$maxHeight now, but might not be identical
-                                               # because of rounding.
+                                               // Note that $height <= 
$maxHeight now, but might not be identical
+                                               // because of rounding.
                                        }
-                                       $transformParams = array( 'page' => 
$this->params['page'], 'width' => $width );
+                                       $transformParams = [ 'page' => 
$this->params['page'], 'width' => $width ];
                                        $file->transform( $transformParams );
                                }
                                break;
 
                        case self::SMALL_THUMB:
-                               Linker::makeThumbLinkObj( $this->title, $file, 
'', '', 'none', array( 'page' => $this->params['page'] ) );
+                               Linker::makeThumbLinkObj( $this->title, $file, 
'', '', 'none',
+                                       [ 'page' => $this->params['page'] ] );
                                break;
                }
 
@@ -96,14 +97,14 @@
                if ( !$wgPdfCreateThumbnailsInJobQueue ) {
                        return true;
                }
-               if (!MimeMagic::singleton()->isMatchingExtension('pdf', $mime)) 
{
+               if ( !MimeMagic::singleton()->isMatchingExtension( 'pdf', $mime 
) ) {
                        return true; // not a PDF, abort
                }
 
                $title = $upload->getTitle();
                $uploadFile = $upload->getLocalFile();
                if ( is_null( $uploadFile ) ) {
-                       wfDebugLog('thumbnails', '$uploadFile seems to be null, 
should never happen...');
+                       wfDebugLog( 'thumbnails', '$uploadFile seems to be 
null, should never happen...' );
                        return true; // should never happen, but it's better to 
be secure
                }
 
@@ -111,15 +112,15 @@
                $unserialized = unserialize( $metadata );
                $pages = intval( $unserialized['Pages'] );
 
-               $jobs = array();
+               $jobs = [];
                for ( $i = 1; $i <= $pages; $i++ ) {
                        $jobs[] = new CreatePdfThumbnailsJob(
                                $title,
-                               array( 'page' => $i, 'jobtype' => 
self::BIG_THUMB )
+                               [ 'page' => $i, 'jobtype' => self::BIG_THUMB ]
                        );
                        $jobs[] = new CreatePdfThumbnailsJob(
                                $title,
-                               array( 'page' => $i, 'jobtype' => 
self::SMALL_THUMB )
+                               [ 'page' => $i, 'jobtype' => self::SMALL_THUMB ]
                        );
                }
                JobQueueGroup::singleton()->push( $jobs );
diff --git a/PdfHandler.image.php b/PdfHandler.image.php
index 2af4d11..56d7dc0 100644
--- a/PdfHandler.image.php
+++ b/PdfHandler.image.php
@@ -50,11 +50,11 @@
                $data = $this->retrieveMetadata();
                $size = self::getPageSize( $data, 1 );
 
-               if( $size ) {
+               if ( $size ) {
                        $width = $size['width'];
                        $height = $size['height'];
-                       return array( $width, $height, 'Pdf',
-                               "width=\"$width\" height=\"$height\"" );
+                       return [ $width, $height, 'Pdf',
+                               "width=\"$width\" height=\"$height\"" ];
                }
                return false;
        }
@@ -67,18 +67,18 @@
        public static function getPageSize( $data, $page ) {
                global $wgPdfHandlerDpi;
 
-               if( isset( $data['pages'][$page]['Page size'] ) ) {
+               if ( isset( $data['pages'][$page]['Page size'] ) ) {
                        $o = $data['pages'][$page]['Page size'];
-               } elseif( isset( $data['Page size'] ) ) {
+               } elseif ( isset( $data['Page size'] ) ) {
                        $o = $data['Page size'];
                } else {
                        $o = false;
                }
 
                if ( $o ) {
-                       if( isset( $data['pages'][$page]['Page rot'] ) ) {
+                       if ( isset( $data['pages'][$page]['Page rot'] ) ) {
                                $r = $data['pages'][$page]['Page rot'];
-                       } elseif( isset( $data['Page rot'] ) ) {
+                       } elseif ( isset( $data['Page rot'] ) ) {
                                $r = $data['Page rot'];
                        } else {
                                $r = 0;
@@ -96,10 +96,10 @@
                                        $height = $t;
                                }
 
-                               return array(
+                               return [
                                        'width' => $width,
                                        'height' => $height
-                               );
+                               ];
                        }
                }
 
@@ -127,7 +127,7 @@
                        $data = null;
                }
 
-               # Read text layer
+               // Read text layer
                if ( isset( $wgPdftoText ) ) {
                        wfProfileIn( 'pdftotext' );
                        $cmd = wfEscapeShellArg( $wgPdftoText ) . ' '. 
wfEscapeShellArg( $this->mFilename ) . ' - ';
@@ -135,12 +135,12 @@
                        $retval = '';
                        $txt = wfShellExec( $cmd, $retval );
                        wfProfileOut( 'pdftotext' );
-                       if( $retval == 0 ) {
+                       if ( $retval == 0 ) {
                                $txt = str_replace( "\r\n", "\n", $txt );
                                $pages = explode( "\f", $txt );
-                               foreach( $pages as $page => $pageText ) {
-                                       # Get rid of invalid UTF-8, strip 
control characters
-                                       # Note we need to do this per page, as 
\f page feed would be stripped.
+                               foreach ( $pages as $page => $pageText ) {
+                                       // Get rid of invalid UTF-8, strip 
control characters
+                                       // Note we need to do this per page, as 
\f page feed would be stripped.
                                        $pages[$page] = Validator::cleanUp( 
$pageText );
                                }
                                $data['text'] = $pages;
@@ -159,7 +159,7 @@
                }
 
                $lines = explode( "\n", $dump );
-               $data = array();
+               $data = [];
 
                // Metadata is always the last item, and spans multiple lines.
                $inMetadata = false;
@@ -167,14 +167,14 @@
                // Basically this loop will go through each line, splitting key 
value
                // pairs on the colon, until it gets to a "Metadata:\n" at 
which point
                // it will gather all remaining lines into the xmp key.
-               foreach( $lines as $line ) {
+               foreach ( $lines as $line ) {
                        if ( $inMetadata ) {
-                               # Handle XMP differently due to diffence in 
line break
+                               // Handle XMP differently due to diffence in 
line break
                                $data['xmp'] .= "\n$line";
                                continue;
                        }
                        $bits = explode( ':', $line, 2 );
-                       if( count( $bits ) > 1 ) {
+                       if ( count( $bits ) > 1 ) {
                                $key = trim( $bits[0] );
                                if ( $key === 'Metadata' ) {
                                        $inMetadata = true;
@@ -182,10 +182,10 @@
                                        continue;
                                }
                                $value = trim( $bits[1] );
-                               $matches = array();
+                               $matches = [];
                                // "Page xx rot" will be in poppler 0.20's 
pdfinfo output
                                // See 
https://bugs.freedesktop.org/show_bug.cgi?id=41867
-                               if( preg_match( '/^Page +(\d+) (size|rot)$/', 
$key, $matches ) ) {
+                               if ( preg_match( '/^Page +(\d+) (size|rot)$/', 
$key, $matches ) ) {
                                        $data['pages'][$matches[1]][$matches[2] 
== 'size' ? 'Page size' : 'Page rot'] = $value;
                                } else {
                                        $data[$key] = $value;
@@ -206,10 +206,9 @@
         * @return Array post-processed metadata
         */
        protected function postProcessDump( array $data ) {
-
                $meta = new BitmapMetadataHandler();
-               $items = array();
-               foreach( $data as $key => $val ) {
+               $items = [];
+               foreach ( $data as $key => $val ) {
                        switch ( $key ) {
                                case 'Title':
                                        $items['ObjectName'] = $val;
@@ -276,10 +275,10 @@
                                        // This doesn't do anything with 
rotation as of yet,
                                        // mostly because I am unsure of what a 
good way to
                                        // present that information to the user 
would be.
-                                       $pageSizes = array();
-                                       foreach( $val as $page ) {
-                                               if( isset( $page['Page size'] ) 
) {
-                                                       $pageSizes[ $page['Page 
size'] ] = true;
+                                       $pageSizes = [];
+                                       foreach ( $val as $page ) {
+                                               if ( isset( $page['Page size'] 
) ) {
+                                                       $pageSizes[$page['Page 
size']] = true;
                                                }
                                        }
 
diff --git a/PdfHandler_body.php b/PdfHandler_body.php
index 77b8666..b783e14 100644
--- a/PdfHandler_body.php
+++ b/PdfHandler_body.php
@@ -22,12 +22,12 @@
  */
 
 class PdfHandler extends ImageHandler {
-       static $messages = array(
+       public static $messages = [
                'main' => 'pdf-file-page-warning',
                'header' => 'pdf-file-page-warning-header',
                'info' => 'pdf-file-page-warning-info',
                'footer' => 'pdf-file-page-warning-footer',
-       );
+       ];
 
        /**
         * @return bool
@@ -71,7 +71,7 @@
                        // e.g. [[File:Foo.pdf|thumb|Page 3 of the document 
shows foo]]
                        return false;
                }
-               if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
+               if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
                        return ( $value > 0 );
                }
                return false;
@@ -97,7 +97,7 @@
                $m = false;
 
                if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
-                       return array( 'width' => $m[2], 'page' => $m[1] );
+                       return [ 'width' => $m[2], 'page' => $m[1] ];
                }
 
                return false;
@@ -108,20 +108,20 @@
         * @return array
         */
        function getScriptParams( $params ) {
-               return array(
+               return [
                        'width' => $params['width'],
                        'page' => $params['page'],
-               );
+               ];
        }
 
        /**
         * @return array
         */
        function getParamMap() {
-               return array(
+               return [
                        'img_width' => 'width',
                        'img_page' => 'page',
-               );
+               ];
        }
 
        /**
@@ -170,11 +170,11 @@
                // Provide a way to pool count limit the number of downloaders.
                if ( $image->getSize() >= 1e7 ) { // 10MB
                        $work = new PoolCounterWorkViaCallback( 
'GetLocalFileCopy', sha1( $image->getName() ),
-                               array(
+                               [
                                        'doWork' => function() use ( $image ) {
                                                return 
$image->getLocalRefPath();
                                        }
-                               )
+                               ]
                        );
                        $srcPath = $work->execute();
                } else {
@@ -260,13 +260,17 @@
                        return false;
                }
 
-               $work = new PoolCounterWorkViaCallback( 
'PdfHandler-unserialize-metadata', $image->getName(), array(
-                       'doWork' => function() use ( $image, $metadata ) {
-                               wfSuppressWarnings();
-                               $image->pdfMetaArray = unserialize( $metadata );
-                               wfRestoreWarnings();
-                       },
-               ) );
+               $work = new PoolCounterWorkViaCallback(
+                       'PdfHandler-unserialize-metadata',
+                       $image->getName(),
+                       [
+                               'doWork' => function() use ( $image, $metadata 
) {
+                                       wfSuppressWarnings();
+                                       $image->pdfMetaArray = unserialize( 
$metadata );
+                                       wfRestoreWarnings();
+                               },
+                       ]
+               );
                $work->execute();
 
                return $image->pdfMetaArray;
@@ -295,7 +299,7 @@
                        $magic = MimeMagic::singleton();
                        $mime = $magic->guessTypesForExtension( 
$wgPdfOutputExtension );
                }
-               return array( $wgPdfOutputExtension, $mime );
+               return [ $wgPdfOutputExtension, $mime ];
        }
 
        /**
@@ -313,7 +317,7 @@
         * @return bool
         */
        function isMetadataValid( $image, $metadata ) {
-               if ( !$metadata || $metadata === serialize(array()) ) {
+               if ( !$metadata || $metadata === serialize( [] ) ) {
                        return self::METADATA_BAD;
                } elseif ( strpos( $metadata, 'mergedMetadata' ) === false ) {
                        return self::METADATA_COMPATIBLE;
@@ -380,7 +384,7 @@
                        $cache::TTL_INDEFINITE,
                        function () use ( $file ) {
                                $data = $this->getMetaArray( $file );
-                               if ( !$data || !isset( $data['Pages'] )  ) {
+                               if ( !$data || !isset( $data['Pages'] ) ) {
                                        return false;
                                }
                                unset( $data['text'] ); // lower peak RAM
@@ -417,11 +421,11 @@
         * @return array
         */
        function getWarningConfig( $file ) {
-               return array(
+               return [
                        'messages' => self::$messages,
                        'link' => 
'//www.mediawiki.org/wiki/Special:MyLanguage/Help:Security/PDF_files',
                        'module' => 'pdfhandler.messages',
-               );
+               ];
        }
 
        /**
@@ -429,9 +433,9 @@
         * @param &$resourceLoader ResourceLoader
         */
        static function registerWarningModule( &$resourceLoader ) {
-               $resourceLoader->register( 'pdfhandler.messages', array(
+               $resourceLoader->register( 'pdfhandler.messages', [
                        'messages' => array_values( self::$messages ),
-               ) );
+               ] );
        }
 
        /**
diff --git a/composer.json b/composer.json
index a3aecbe..e4e502f 100644
--- a/composer.json
+++ b/composer.json
@@ -1,11 +1,14 @@
 {
        "require-dev": {
                "jakub-onderka/php-parallel-lint": "0.9.2",
-               "jakub-onderka/php-console-highlighter": "0.3.2"
+               "jakub-onderka/php-console-highlighter": "0.3.2",
+               "mediawiki/mediawiki-codesniffer": "0.7.2"
        },
        "scripts": {
+               "fix": "phpcbf",
                "test": [
-                       "parallel-lint . --exclude node_modules --exclude 
vendor"
+                       "parallel-lint . --exclude node_modules --exclude 
vendor",
+                       "phpcs -p -s"
                ]
        }
 }
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
index 0000000..ede6c5d
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ruleset>
+       <rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/>
+       <file>.</file>
+       <arg name="extensions" value="php,php5,inc"/>
+       <arg name="encoding" value="utf8"/>
+       <exclude-pattern>vendor</exclude-pattern>
+       <exclude-pattern>node_modules</exclude-pattern>
+</ruleset>

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2921f450aeb4e896157d81c1ac2b5b5b13c31240
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PdfHandler
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to