小璋丸 has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/334405 )
Change subject: Follow coding conventions. ...................................................................... Follow coding conventions. * Import official PHP lint ruleset. * Fix code automatically. * Fix code manually. * Ignore directories of dependencies. Bug: T133633 Change-Id: I709085df913c93041ca7d10c72cbbd3f4549ce2c --- A .gitignore M QuickGV.body.php M QuickGV.template.php A composer.json A phpcs.xml 5 files changed, 429 insertions(+), 336 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/QuickGV refs/changes/05/334405/1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e98c83 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Node packages +node_modules + +# PHP packages +vendor +composer.lock diff --git a/QuickGV.body.php b/QuickGV.body.php index 1058711..f0a63b3 100644 --- a/QuickGV.body.php +++ b/QuickGV.body.php @@ -1,6 +1,6 @@ <?php /** - * Graphviz 快速製圖器 + * QuickGV core / QuickGV 核心 * * @author Raymond Wu https://github.com/virus-warnning */ @@ -14,10 +14,10 @@ /* 自定義 php 路徑 */ const PHP_PATH = ''; - //const PHP_PATH = 'C:\wamp\bin\php\php5.4.3\php.exe'; + // const PHP_PATH = 'C:\wamp\bin\php\php5.4.3\php.exe'; /* 錯誤訊息暫存區 */ - private static $errmsgs = array(); + private static $errmsgs = []; /* 版本字串 */ private static $version; @@ -28,18 +28,18 @@ * @since 0.1.0 * @param $parser MediaWiki 的語法處理器 */ - public static function init(&$parser) { + public static function init( &$parser ) { // 取得版本字串 global $wgExtensionCredits; - foreach ($wgExtensionCredits['parserhook'] as $ext) { - if ($ext['name']==='QuickGV') { + foreach ( $wgExtensionCredits['parserhook'] as $ext ) { + if ( $ext['name']==='QuickGV' ) { self::$version = $ext['version']; break; } } // 設定函數鉤 - $parser->setHook('quickgv', array('QuickGV', 'render')); + $parser->setHook( 'quickgv', [ 'QuickGV', 'render' ] ); return true; } @@ -53,60 +53,69 @@ * @param $parser MediaWiki 的語法處理器 * @param $frame 不知道是啥小 */ - public static function render($in, $param=array(), $parser=null, $frame=false) { + public static function render( $in, $param=[], $parser=null, $frame=false ) { + // $IP occurs an error but it's still in use. $wgIP cannot work. + // @codingStandardsIgnoreStart global $IP, $wgScriptPath; + // @codingStandardsIgnoreEnd // 計時開始,效能計算用 - $beg_time = microtime(true); + $beg_time = microtime( true ); // 參數檢查 - self::validateParam($param); - if (count(self::$errmsgs)>0) { + self::validateParam( $param ); + if ( count( self::$errmsgs )>0 ) { return self::showError(); } // dot 環境檢查 - $dotcmd = self::findExecutable('dot', self::DOT_PATH); - if ($dotcmd=='') return self::showError(); + $dotcmd = self::findExecutable( 'dot', self::DOT_PATH ); + if ( $dotcmd=='' ) { + return self::showError(); + } // PHP 環境檢查 - $phpcmd = self::findExecutable('php', self::PHP_PATH); - if ($phpcmd=='') return self::showError(); + $phpcmd = self::findExecutable( 'php', self::PHP_PATH ); + if ( $phpcmd=='' ) { + return self::showError(); + } // $in 上限管制 - if (strlen($in)>self::MAX_INPUTSIZE) { - $msg = sprintf('Input data exceed %s.', self::getFriendlySize(self::MAX_INPUTSIZE)); - return self::showError($msg); + if ( strlen( $in )>self::MAX_INPUTSIZE ) { + $msg = sprintf( 'Input data exceed %s.', self::getFriendlySize( self::MAX_INPUTSIZE ) ); + return self::showError( $msg ); } // 計算新的摘要,快取處理用 - $sum_curr = md5(json_encode($param).$in); + $sum_curr = md5( json_encode( $param ).$in ); // 讀取參數,或是預設值 - $gname = self::getParam($param, 'name' , 'G'); - $theme = self::getParam($param, 'theme' , ''); - $usage = self::getParam($param, 'usage' , ''); - $showmeta = self::getParam($param, 'showmeta', 'false'); - $showdot = self::getParam($param, 'showdot' , 'false'); - //return '<pre>' . print_r($param, true) . '</pre>'; + $gname = self::getParam( $param, 'name', 'G' ); + $theme = self::getParam( $param, 'theme', '' ); + $usage = self::getParam( $param, 'usage', '' ); + $showmeta = self::getParam( $param, 'showmeta', 'false' ); + $showdot = self::getParam( $param, 'showdot', 'false' ); + // return '<pre>' . print_r($param, true) . '</pre>'; $prefix = $parser->mTitle; - $prefix = str_replace(array('\\','/',' '), '_', $prefix); // TODO: 搬去 self::getSafeName() + $prefix = str_replace( [ '\\','/',' ' ], '_', $prefix ); // TODO: 搬去 self::getSafeName() - $imgdir = sprintf('%s/images/quickgv', $IP); - if (!is_dir($imgdir)) mkdir($imgdir); + $imgdir = sprintf( '%s/images/quickgv', $IP ); + if ( !is_dir( $imgdir ) ) { + mkdir( $imgdir ); + } - $fn = self::getSafeName(sprintf('%s-%s', $prefix, $gname)); - $metafile = sprintf('%s/images/quickgv/%s-meta.json', $IP, $fn); - $svgfile = sprintf('%s/images/quickgv/%s.svg', $IP, $fn); - $svgurl = sprintf('%s/images/quickgv/%s.svg', $wgScriptPath, $fn); + $fn = self::getSafeName( sprintf( '%s-%s', $prefix, $gname ) ); + $metafile = sprintf( '%s/images/quickgv/%s-meta.json', $IP, $fn ); + $svgfile = sprintf( '%s/images/quickgv/%s.svg', $IP, $fn ); + $svgurl = sprintf( '%s/images/quickgv/%s.svg', $wgScriptPath, $fn ); // 更新狀況檢查 $modified = true; - if (is_file($metafile) && is_file($svgfile)) { - $meta = json_decode(file_get_contents($metafile),true); + if ( is_file( $metafile ) && is_file( $svgfile ) ) { + $meta = json_decode( file_get_contents( $metafile ), true ); $sum_prev = $meta['md5sum']; - if ($sum_curr==$sum_prev) { + if ( $sum_curr==$sum_prev ) { $modified = false; $elapsed = $meta['elapsed']; $dotcode = $meta['dotcode']; @@ -114,114 +123,147 @@ } // 有更新才轉檔 - if ($modified) { + if ( $modified ) { // 執行 php, 產生 dot 語法 $dottpl = __DIR__ . '/QuickGV.template.php'; $cmd = sprintf( '%s %s %s %s %s', - escapeshellarg($phpcmd), // php - escapeshellarg($dottpl), // $argv[0] - escapeshellarg($gname), // $argv[1] - escapeshellarg($theme), // $argv[2] - escapeshellarg($usage) // $argv[3] + escapeshellarg( $phpcmd ), // php + escapeshellarg( $dottpl ), // $argv[0] + escapeshellarg( $gname ), // $argv[1] + escapeshellarg( $theme ), // $argv[2] + escapeshellarg( $usage ) // $argv[3] ); - $retval = self::pipeExec($cmd, $in, $dotcode, $err, 'utf-8'); + $retval = self::pipeExec( $cmd, $in, $dotcode, $err, 'utf-8' ); // 執行 dot, 產生 svg 圖檔 - $cmd = sprintf('%s -Tsvg > %s', - escapeshellarg($dotcmd), // dot fullpath - escapeshellarg($svgfile) // stdout + $cmd = sprintf( '%s -Tsvg > %s', + escapeshellarg( $dotcmd ), // dot fullpath + escapeshellarg( $svgfile ) // stdout ); - $retval = self::pipeExec($cmd, $dotcode, $out, $err, 'utf-8'); + $retval = self::pipeExec( $cmd, $dotcode, $out, $err, 'utf-8' ); // dot 指令的錯誤處理 - if ($retval!=0) { - $html = self::showError($err); - $html .= sprintf('<pre>%s</pre>', $dotcode); + if ( $retval!=0 ) { + $html = self::showError( $err ); + $html .= sprintf( '<pre>%s</pre>', $dotcode ); return $html; } // SVG 動手腳 (Graphviz 無法處理的部分) - $svgxml = simplexml_load_file($svgfile); - $svgxml->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg'); + $svgxml = simplexml_load_file( $svgfile ); + $svgxml->registerXPathNamespace( 'svg', 'http://www.w3.org/2000/svg' ); // 連結加上 target="_blank" - $links = $svgxml->xpath('//svg:a'); - foreach ($links as $link) { - $link->addAttribute('target', '_blank'); + $links = $svgxml->xpath( '//svg:a' ); + foreach ( $links as $link ) { + $link->addAttribute( 'target', '_blank' ); } // 連結文字變色 - $txtns = $svgxml->xpath('//svg:a/svg:text'); - foreach ($txtns as $txt) { + $txtns = $svgxml->xpath( '//svg:a/svg:text' ); + foreach ( $txtns as $txt ) { $txt->attributes()->fill = '#0000ff'; } // 頁尾加上 powered by QuickGV $w = (int)$svgxml->attributes()->width; $h = (int)$svgxml->attributes()->height; - if ($w>200 && $h>50) { - $footer = $svgxml->addChild('svg:text'); - $footer->addAttribute('text-anchor','end'); - $footer->addAttribute('x','100%'); - $footer->addAttribute('y','100%'); - $footer->addAttribute('transform','translate(-5,-5)'); - $footer->addAttribute('font-family','Times,serif'); - $footer->addAttribute('font-size','10'); + if ( $w>200 && $h>50 ) { + $footer = $svgxml->addChild( 'svg:text' ); + $footer->addAttribute( 'text-anchor', 'end' ); + $footer->addAttribute( 'x', '100%' ); + $footer->addAttribute( 'y', '100%' ); + $footer->addAttribute( 'transform', 'translate(-5,-5)' ); + $footer->addAttribute( 'font-family', 'Times,serif' ); + $footer->addAttribute( 'font-size', '10' ); - $ftlink = $footer->addChild('svg:a', 'powered by QuickGV'); - $ftlink->addAttribute('xlink:href','https://www.mediawiki.org/wiki/Extension:QuickGV','http://www.w3.org/1999/xlink'); - $ftlink->addAttribute('target','_blank'); - $ftlink->addAttribute('fill','#aaaaaa'); + $ftlink = $footer->addChild( 'svg:a', 'powered by QuickGV' ); + $ftlink->addAttribute( + 'xlink:href', + 'https://www.mediawiki.org/wiki/Extension:QuickGV', + 'http://www.w3.org/1999/xlink' + ); + $ftlink->addAttribute( 'target', '_blank' ); + $ftlink->addAttribute( 'fill', '#aaaaaa' ); } - file_put_contents($svgfile, $svgxml->asXML()); + file_put_contents( $svgfile, $svgxml->asXML() ); // 如果輸出成功,記錄 "轉圖時間"、"MD5" // 如果有開啟顯示原始碼,也記錄 "dot 原始碼" - $elapsed = microtime(true) - $beg_time; - $meta = array( + $elapsed = microtime( true ) - $beg_time; + $meta = [ 'md5sum' => $sum_curr, 'elapsed' => $elapsed, 'dotcode' => '' - ); - if ($showdot==='true') $meta['dotcode'] = $dotcode; - file_put_contents($metafile, json_encode($meta)); + ]; + if ( $showdot==='true' ) { + $meta['dotcode'] = $dotcode; + } + file_put_contents( $metafile, json_encode( $meta ) ); } // 輸出 (利用 mtime 讓圖片正確使用快取) - $mtime = filemtime($svgfile); - $html = sprintf('<p><embed type="image/svg+xml" src="%s?t=%d" style="border:1px solid #777;" /></p>', $svgurl, $mtime); + $mtime = filemtime( $svgfile ); + $html = sprintf( + '<p><embed type="image/svg+xml" src="%s?t=%d" style="border:1px solid #777;" /></p>', + $svgurl, $mtime + ); - if ($showmeta==='true') { + if ( $showmeta==='true' ) { // 取 Graphviz 版本資訊 (需要獨立 function) - $cmd = sprintf('%s -V', escapeshellarg($dotcmd)); - self::pipeExec($cmd, '', $out, $err); - $verstr = trim($err); - $verpos = strpos($verstr,'version')+8; - $verstr = substr($verstr,$verpos); + $cmd = sprintf( '%s -V', escapeshellarg( $dotcmd ) ); + self::pipeExec( $cmd, '', $out, $err ); + $verstr = trim( $err ); + $verpos = strpos( $verstr, 'version' )+8; + $verstr = substr( $verstr, $verpos ); // 取人性化的檔案大小 - $size = self::getFriendlySize(filesize($svgfile)); + $size = self::getFriendlySize( filesize( $svgfile ) ); - $table_html = array(); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('filepath'), $svgurl); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('filesize')->plain(), $size); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('filemtime')->plain(), date('Y-m-d H:i:s',$mtime)); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('exectime')->plain(), wfMessage('seconds', round($elapsed, 3))->plain()); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('md5sum')->plain(), $sum_curr); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('graphviz-path')->plain(), $dotcmd); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', wfMessage('graphviz-ver')->plain(), $verstr); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;"><a href="%s" target="_blank">%2$s</a></td></tr>', wfMessage('graphviz-ref')->plain(), 'http://www.graphviz.org/doc/info/attrs.html'); - $table_html[] = sprintf('<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s - <a href="https://www.mediawiki.org/wiki/Extension:QuickGV" target="_blank">%s</a></td></tr>', wfMessage('quickgv-ver')->plain(), self::$version, wfMessage('quickgv-about')->plain()); - $table_html = implode("\n", $table_html); - $table_html = sprintf('<table class="mw_metadata" style="width:600px; margin:5px 0 0 0;"><tbody>%s</tbody></table>',$table_html); + // TODO: Make them more readable. + $table_html = []; + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'filepath' ), $svgurl ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'filesize' )->plain(), $size ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'filemtime' )->plain(), date( 'Y-m-d H:i:s', $mtime ) ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'exectime' )->plain(), wfMessage( 'seconds', round( $elapsed, 3 ) )->plain() ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'md5sum' )->plain(), $sum_curr ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'graphviz-path' )->plain(), $dotcmd ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">%s</td></tr>', + wfMessage( 'graphviz-ver' )->plain(), $verstr ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">' . + '<a href="%s" target="_blank">%2$s</a></td></tr>', + wfMessage( 'graphviz-ref' )->plain(), 'http://www.graphviz.org/doc/info/attrs.html' ); + $table_html[] = sprintf( + '<tr><th style="white-space:nowrap;">%s</th><td style="text-align:left;">' . + '%s - <a href="https://www.mediawiki.org/wiki/Extension:QuickGV" target="_blank">%s</a>' . + '</td></tr>', + wfMessage( 'quickgv-ver' )->plain(), self::$version, wfMessage( 'quickgv-about' )->plain() ); + $table_html = implode( "\n", $table_html ); + $table_html = sprintf( + '<table class="mw_metadata" style="width:600px; margin:5px 0 0 0;"><tbody>%s</tbody></table>', + $table_html ); $html .= $table_html; - unset($table_html); + unset( $table_html ); } - if ($showdot==='true') { - $html .= sprintf('<pre>%s</pre>', $dotcode); + if ( $showdot==='true' ) { + $html .= sprintf( '<pre>%s</pre>', $dotcode ); } return $html; @@ -233,7 +275,7 @@ * @since 0.1.1 * @param $msg 錯誤訊息 */ - private static function addError($msg) { + private static function addError( $msg ) { self::$errmsgs[] = $msg; } @@ -243,22 +285,22 @@ * @since 0.1.1 * @param $msg 錯誤訊息,如果沒有提供,會使用 addError 增加的錯誤訊息 */ - private static function showError($msg='') { + private static function showError( $msg='' ) { // 內建 CSS: // .errorbox - MW 顯示錯誤訊息的 CSS class // .warningbox - MW 顯示警示訊息的 CSS class // 這兩個都有 float: left; 用完後需要 clear 一下 // 這些 CSS 真是夠醜的,以後要修一下 - if ($msg==='') { - if (count(self::$errmsgs)>0) { + if ( $msg==='' ) { + if ( count( self::$errmsgs )>0 ) { $html = ''; - foreach (self::$errmsgs as $cached_msg) { + foreach ( self::$errmsgs as $cached_msg ) { $html .= "<p>$cached_msg</p>"; } // Clear messages, or graphs after this one will broken. - self::$errmsgs = array(); + self::$errmsgs = []; } else { $html = "<p>Test</p>"; } @@ -266,7 +308,7 @@ $html = "<p>$msg</p>"; } - $html = sprintf('<div class="errorbox" style="margin:0;">%s</div>',$html); + $html = sprintf( '<div class="errorbox" style="margin:0;">%s</div>', $html ); $html .= '<div style="clear:both;"></div>'; return $html; } @@ -276,34 +318,34 @@ * * @param $params 設定值組 */ - private static function validateParam(&$params) { + private static function validateParam( &$params ) { // 正向表列格式清單 - $patterns = array( + $patterns = [ 'bool' => '/^(true|false)$/', 'name' => '/^[\w_]+$/u', // 防止符號字元,而且支援中文 - ); + ]; // 驗證失敗時的錯誤訊息 - $descs = array( + $descs = [ 'bool' => 'true or false', 'name' => 'word characters or underscore', - ); + ]; // 驗證欄位與格式對應 - $formats = array( + $formats = [ 'name' => 'name', 'showdot' => 'bool', 'showmeta' => 'bool', - ); + ]; - foreach ($formats as $prmk => $patk) { - if (isset($params[$prmk])) { + foreach ( $formats as $prmk => $patk ) { + if ( isset( $params[$prmk] ) ) { $param = $params[$prmk]; $pattern = $patterns[$patk]; - if (!preg_match($pattern,$param)) { + if ( !preg_match( $pattern, $param ) ) { // TODO: 之後需要翻譯一下 - $msg = sprintf('Attribute %s="%s" needs %s.', $prmk, $param, $descs[$patk]); - self::addError($msg); + $msg = sprintf( 'Attribute %s="%s" needs %s.', $prmk, $param, $descs[$patk] ); + self::addError( $msg ); } } } @@ -319,18 +361,18 @@ * @param $exec_custom 自定義程式路徑 * @return 程式完整路徑 */ - private static function findExecutable($exec_name, $exec_custom) { - if ($exec_custom==='') { - if (PHP_OS!=='WINNT') { - $exec_path = exec("which $exec_name"); - if ($exec_path==='') { - $search_dirs = array( + private static function findExecutable( $exec_name, $exec_custom ) { + if ( $exec_custom==='' ) { + if ( PHP_OS!=='WINNT' ) { + $exec_path = exec( "which $exec_name" ); + if ( $exec_path==='' ) { + $search_dirs = [ '/usr/bin', '/usr/local/bin' - ); - foreach ($search_dirs as $dir) { - $p = sprintf('%s/%s',$dir,$exec_name); - if (file_exists($p)) { + ]; + foreach ( $search_dirs as $dir ) { + $p = sprintf( '%s/%s', $dir, $exec_name ); + if ( file_exists( $p ) ) { $exec_path = $p; break; } @@ -341,67 +383,74 @@ // * %ProgramFiles(x86)% - C:\Program Files (x86) // * %ProgramFiles% - C:\Program Files // [Gg]raphviz\s?2\.\d+\bin\dot - //$exec_path = exec("where $exec_name"); + // $exec_path = exec("where $exec_name"); - $prog_files = getenv('ProgramFiles(x86)'); // for 64-bits Windows - if ($prog_files===false) { - $prog_files = getenv('ProgramFiles'); // for 32-bits Windows + $prog_files = getenv( 'ProgramFiles(x86)' ); // for 64-bits Windows + if ( $prog_files===false ) { + $prog_files = getenv( 'ProgramFiles' ); // for 32-bits Windows } - $matched_dirs = array(); - $dh = opendir($prog_files); - while (($prog_dir = readdir($dh))!==false) { - if (preg_match('/[Gg]raphviz\s?(2\.\d+)/', $prog_dir, $matches)) { + $matched_dirs = []; + $dh = opendir( $prog_files ); + $prog_dir = readdir( $dh ); + while ( $prog_dir !== false ) { + if ( preg_match( '/[Gg]raphviz\s?(2\.\d+)/', $prog_dir, $matches ) ) { $gv_ver = (float)$matches[1]; - if ($gv_ver>=2.0) $matched_dirs[] = $prog_dir; + if ( $gv_ver>=2.0 ) { + $matched_dirs[] = $prog_dir; + } } + $prog_dir = readdir( $dh ); } - closedir($dh); - - if (count($matched_dirs)) { - rsort($matched_dirs); + closedir( $dh ); + + if ( count( $matched_dirs ) ) { + rsort( $matched_dirs ); $prog_dir = $matched_dirs[0]; - $exec_path = sprintf('%s\\%s\\bin\\dot.exe', $prog_files, $prog_dir); + $exec_path = sprintf( '%s\\%s\\bin\\dot.exe', $prog_files, $prog_dir ); } } } else { $exec_path = $exec_custom; } - if ($exec_path==='' || !file_exists($exec_path)) { - if ($exec_name==='dot') $exec_name = 'Graphviz'; - self::addError("$exec_name is not installed."); + if ( $exec_path==='' || !file_exists( $exec_path ) ) { + if ( $exec_name==='dot' ) { + $exec_name = 'Graphviz'; + } + self::addError( "$exec_name is not installed." ); // How to install graphviz $os = PHP_OS; - switch ($os) { + switch ( $os ) { case 'Darwin': $url = 'http://brew.sh'; - self::addError('Run the command to install:'); - self::addError('<blockquote>brew install graphviz</blockquote>'); - self::addError(sprintf('If you didn\'t install Homebrew yet, see <a href="%1$s">%1$s</a>.', $url)); + self::addError( 'Run the command to install:' ); + self::addError( '<blockquote>brew install graphviz</blockquote>' ); + $msg = sprintf( 'If you didn\'t install Homebrew yet, see <a href="%1$s">%1$s</a>.', $url ); + self::addError( $msg ); break; case 'WINNT': $url = 'http://www.graphviz.org/Download_windows.php'; - self::addError(sprintf('Click here to download installer: <a href="%1$s">%1$s</a>', $url)); + self::addError( sprintf( 'Click here to download installer: <a href="%1$s">%1$s</a>', $url ) ); break; case 'Linux': - self::addError('For CentOS users, run the command to install:'); - self::addError('<blockquote>yum install graphviz</blockquote>'); - self::addError('For Ubuntu or Debian users, run the command to install:'); - self::addError('<blockquote>sudo apt-get install graphviz</blockquote>'); + self::addError( 'For CentOS users, run the command to install:' ); + self::addError( '<blockquote>yum install graphviz</blockquote>' ); + self::addError( 'For Ubuntu or Debian users, run the command to install:' ); + self::addError( '<blockquote>sudo apt-get install graphviz</blockquote>' ); break; case 'FreeBSD': - self::addError('Run the command to install:'); - self::addError('<blockquote>pkg_add -r graphviz</blockquote>'); + self::addError( 'Run the command to install:' ); + self::addError( '<blockquote>pkg_add -r graphviz</blockquote>' ); break; } return ''; } - if (!is_executable($exec_path)) { - self::addError("$exec_path is not executable."); + if ( !is_executable( $exec_path ) ) { + self::addError( "$exec_path is not executable." ); return ''; } @@ -416,9 +465,11 @@ * @param $default 預設值 * @return 預期結果 */ - private static function getParam(&$params, $key, $default='') { - if (isset($params[$key])) { - if (trim($params[$key])!=='') return $params[$key]; + private static function getParam( &$params, $key, $default='' ) { + if ( isset( $params[$key] ) ) { + if ( trim( $params[$key] )!=='' ) { + return $params[$key]; + } } return $default; } @@ -428,35 +479,35 @@ * * @param $size 位元組數 */ - private static function getFriendlySize($size) { - static $unit_ch = array('B','KB','MB'); + private static function getFriendlySize( $size ) { + static $unit_ch = [ 'B','KB','MB' ]; $unit_lv = 0; - while ($size>=1024 && $unit_lv<=2) { + while ( $size>=1024 && $unit_lv<=2 ) { $size /= 1024; $unit_lv++; } - if ($unit_lv==0) { - return sprintf('%d %s', $size, $unit_ch[$unit_lv]); + if ( $unit_lv==0 ) { + return sprintf( '%d %s', $size, $unit_ch[$unit_lv] ); } else { - return sprintf('%.2f %s', $size, $unit_ch[$unit_lv]); + return sprintf( '%.2f %s', $size, $unit_ch[$unit_lv] ); } } /** * 檔名迴避 Windows 不接受的字元 */ - private static function getSafeName($unsafename) { + private static function getSafeName( $unsafename ) { $safename = ''; - $slen = strlen($unsafename); + $slen = strlen( $unsafename ); // escape non-ascii chars - for($i=0;$i<$slen;$i++) { + for ( $i=0;$i<$slen;$i++ ) { $ch = $unsafename[$i]; - $cc = ord($ch); - if ($cc<32 || $cc>127) { - $safename .= sprintf('x%02x',$cc); + $cc = ord( $ch ); + if ( $cc<32 || $cc>127 ) { + $safename .= sprintf( 'x%02x', $cc ); } else { $safename .= $ch; } @@ -470,25 +521,29 @@ * * @since 0.2.0 */ - private static function pipeExec($cmd, $stdin='', &$stdout='', &$stderr='', $encoding='sys') { + private static function pipeExec( $cmd, $stdin='', &$stdout='', &$stderr='', $encoding='sys' ) { static $sys_encoding = ''; - if ($encoding==='sys') { + if ( $encoding==='sys' ) { // detect system encoding once - if ($sys_encoding==='') { - if (PHP_OS==='WINNT') { + if ( $sys_encoding==='' ) { + if ( PHP_OS==='WINNT' ) { // for Windows - $lastln = exec('chcp', $stdout, $retval); - if ($retval===0) { - $ok = preg_match('/: (\d+)$/', $lastln, $matches); - if ($ok===1) $sys_encoding = sprintf('cp%d', (int)$matches[1]); + $lastln = exec( 'chcp', $stdout, $retval ); + if ( $retval===0 ) { + $ok = preg_match( '/: (\d+)$/', $lastln, $matches ); + if ( $ok===1 ) { + $sys_encoding = sprintf( 'cp%d', (int)$matches[1] ); + } } } else { // for Linux / OSX / BSD // TODO: ... } - if ($sys_encoding==='') $sys_encoding = 'utf-8'; + if ( $sys_encoding==='' ) { + $sys_encoding = 'utf-8'; + } } // apply system encoding @@ -496,40 +551,43 @@ } // pipe all streams - $desc = array( - array('pipe', 'r'), // stdin - array('pipe', 'w'), // stdout - array('pipe', 'w') // stderr - ); + $desc = [ + [ 'pipe', 'r' ], // stdin + [ 'pipe', 'w' ], // stdout + [ 'pipe', 'w' ] // stderr + ]; // run the command - if (PHP_OS==='WINNT') $cmd = sprintf('"%s"', $cmd); // hack for windows - $proc = proc_open($cmd, $desc, $pipes); - if (is_resource($proc)) { - $encoding = strtolower($encoding); + if ( PHP_OS==='WINNT' ) { + $cmd = sprintf( '"%s"', $cmd ); // hack for windows + } + + $proc = proc_open( $cmd, $desc, $pipes ); + if ( is_resource( $proc ) ) { + $encoding = strtolower( $encoding ); // feed stdin - if ($encoding!=='utf-8') { - $stdin = iconv('utf-8', $encoding, $stdin); + if ( $encoding!=='utf-8' ) { + $stdin = iconv( 'utf-8', $encoding, $stdin ); } - fwrite($pipes[0], $stdin); - fclose($pipes[0]); + fwrite( $pipes[0], $stdin ); + fclose( $pipes[0] ); // read stdout - $stdout = stream_get_contents($pipes[1]); - if ($encoding!=='utf-8') { - $stdout = iconv($encoding, 'utf-8', $stdout); + $stdout = stream_get_contents( $pipes[1] ); + if ( $encoding!=='utf-8' ) { + $stdout = iconv( $encoding, 'utf-8', $stdout ); } - fclose($pipes[1]); + fclose( $pipes[1] ); // read stderr - $stderr = stream_get_contents($pipes[2]); - if ($encoding!=='utf-8') { - $stderr = iconv($encoding, 'utf-8', $stderr); + $stderr = stream_get_contents( $pipes[2] ); + if ( $encoding!=='utf-8' ) { + $stderr = iconv( $encoding, 'utf-8', $stderr ); } - fclose($pipes[2]); + fclose( $pipes[2] ); - $retval = proc_close($proc); + $retval = proc_close( $proc ); } else { $retval = -1; } diff --git a/QuickGV.template.php b/QuickGV.template.php index 7204ba7..60ca025 100644 --- a/QuickGV.template.php +++ b/QuickGV.template.php @@ -4,48 +4,48 @@ *************************************************/ // Theme definitions -$THEME_ATTRS = array( - 'cold' => array( - 'graph_bg' => '#555566', - 'graph_label' => '#ffffff', - 'graph_border' => '#ffffff', - 'edge_path' => '#ffffff', - 'edge_font' => '#ffffff', - 'node_border' => '#ffffff', - 'node_font' => '#000000', - 'node_fill' => '#ccffff:#00c0ff' - ), - 'warm' => array( - 'graph_bg' => '#fffff7', - 'graph_label' => '#000000', - 'graph_border' => '#804000', - 'edge_path' => '#704000', - 'edge_font' => '#704000', - 'node_border' => '#c07000', - 'node_font' => '#000000', - 'node_fill' => '#ffffff:#ffffc0' - ), - 'sakura' => array( - 'graph_bg' => '#996677', - 'graph_label' => '#ffffff', - 'graph_border' => '#ffffff', - 'edge_path' => '#ffffff', - 'edge_font' => '#ffffff', - 'node_border' => '#cc4444', - 'node_font' => '#000000', - 'node_fill' => '#ffffff:#ffc0d0' - ), - 'default' => array( - 'graph_bg' => '#f0f0f0:#ffffff', - 'graph_label' => '#000000', - 'graph_border' => '#555555', - 'edge_path' => '#555555', - 'edge_font' => '#000000', - 'node_border' => '#aaaaaa', - 'node_font' => '#000000', - 'node_fill' => '#ffffff:#e7e7e7' - ), -); +$THEME_ATTRS = [ + 'cold' => [ + 'graph_bg' => '#555566', + 'graph_label' => '#ffffff', + 'graph_border' => '#ffffff', + 'edge_path' => '#ffffff', + 'edge_font' => '#ffffff', + 'node_border' => '#ffffff', + 'node_font' => '#000000', + 'node_fill' => '#ccffff:#00c0ff' + ], + 'warm' => [ + 'graph_bg' => '#fffff7', + 'graph_label' => '#000000', + 'graph_border' => '#804000', + 'edge_path' => '#704000', + 'edge_font' => '#704000', + 'node_border' => '#c07000', + 'node_font' => '#000000', + 'node_fill' => '#ffffff:#ffffc0' + ], + 'sakura' => [ + 'graph_bg' => '#996677', + 'graph_label' => '#ffffff', + 'graph_border' => '#ffffff', + 'edge_path' => '#ffffff', + 'edge_font' => '#ffffff', + 'node_border' => '#cc4444', + 'node_font' => '#000000', + 'node_fill' => '#ffffff:#ffc0d0' + ], + 'default' => [ + 'graph_bg' => '#f0f0f0:#ffffff', + 'graph_label' => '#000000', + 'graph_border' => '#555555', + 'edge_path' => '#555555', + 'edge_font' => '#000000', + 'node_border' => '#aaaaaa', + 'node_font' => '#000000', + 'node_fill' => '#ffffff:#e7e7e7' + ], +]; /** * Get shell argument. If not exists, use default instead. @@ -55,127 +55,137 @@ * @param $default_value default for this argument * @return If the argument exists return it, otherwise return default. */ -function shell_arg($rank, $default_value='') -{ - global $argv; +function wfQuickGVShellArg( $rank, $default_value='' ) { - if (isset($argv[$rank])) { - $v = trim($argv[$rank]); - if ( $v !== '' ) return $v; - } + global $argv; - return $default_value; + if ( isset( $argv[$rank] ) ) { + $v = trim( $argv[$rank] ); + if ( $v !== '' ) { + return $v; + } + } + + return $default_value; } // Get graph name & theme name -$gname = shell_arg(1, 'G'); -$theme = shell_arg(2, 'default'); -$usage = shell_arg(3); +$gname = wfQuickGVShellArg( 1, 'G' ); +$theme = wfQuickGVShellArg( 2, 'default' ); +$usage = wfQuickGVShellArg( 3 ); // Replace the alias of usage -if ( $usage === 'er' || $usage === 'ram' ) $usage = 'record'; -if ( $usage === 'mindmap' ) $usage = 'neato'; +if ( $usage === 'er' || $usage === 'ram' ) { + $usage = 'record'; +} +if ( $usage === 'mindmap' ) { + $usage = 'neato'; +} // Get graph description -$gdata = trim(file_get_contents('php://stdin')); +$gdata = trim( file_get_contents( 'php://stdin' ) ); -if ( !isset($THEME_ATTRS[$theme]) ) $theme = 'default'; -if ( $gdata === '' ) $theme = 'warm'; +if ( !isset( $THEME_ATTRS[$theme] ) ) { + $theme = 'default'; +} +if ( $gdata === '' ) { + $theme = 'warm'; +} $attrs =& $THEME_ATTRS[$theme]; ?> digraph <?php echo $gname; ?> { - // Options - // theme = <?php echo "$theme\n"; ?> - // usage = <?php echo "$usage\n"; ?> + // Options + // theme = <?php echo "$theme\n"; ?> + // usage = <?php echo "$usage\n"; ?> - // default settings of graphs - graph [ - rankdir = LR, - color = "<?php echo $attrs['graph_border']; ?>", - bgcolor = "<?php echo $attrs['graph_bg']; ?>", - fontcolor = "<?php echo $attrs['graph_label']; ?>", - fontsize = 12, - style = dashed, - gradientangle = 65, + // default settings of graphs + graph [ + rankdir = LR, + color = "<?php echo $attrs['graph_border']; ?>", + bgcolor = "<?php echo $attrs['graph_bg']; ?>", + fontcolor = "<?php echo $attrs['graph_label']; ?>", + fontsize = 12, + style = dashed, + gradientangle = 65, - <?php if ( $usage == '' ) : ?> - splines = ortho, - <?php endif; ?> + <?php if ( $usage == '' ) { ?> + splines = ortho, + <?php } ?> - <?php if ( $usage == 'neato' ) : ?> - splines = curved, - layout = neato, - start = "A", - <?php endif; ?> + <?php if ( $usage == 'neato' ) { ?> + splines = curved, + layout = neato, + start = "A", + <?php } ?> - <?php if ( $usage === 'record' ) : ?> - // * ortho, curved are bad - // * polyline acts as line - // * spline (default) is ok - splines = spline, - <?php endif; ?> - ]; + <?php if ( $usage === 'record' ) { ?> + // * ortho, curved are bad + // * polyline acts as line + // * spline (default) is ok + splines = spline, + <?php } ?> + ]; - // default settings of nodes - node [ - <?php if ( $usage === 'record' ) : ?> - shape = record, - style = filled, - labelloc = l, - <?php else: ?> - shape = box, - style = "filled,rounded", - <?php endif; ?> + // default settings of nodes + node [ + <?php if ( $usage === 'record' ) { ?> + shape = record, + style = filled, + labelloc = l, + <?php } else { ?> + shape = box, + style = "filled,rounded", + <?php } ?> - height = 0.3, - fontsize = 10, + height = 0.3, + fontsize = 10, - // theme - color = "<?php echo $attrs['node_border']; ?>", - fontcolor = "<?php echo $attrs['node_font']; ?>", - fillcolor = "<?php echo $attrs['node_fill']; ?>", - gradientangle = 295 // left, top -> right, bottom - ]; + // theme + color = "<?php echo $attrs['node_border']; ?>", + fontcolor = "<?php echo $attrs['node_font']; ?>", + fillcolor = "<?php echo $attrs['node_fill']; ?>", + gradientangle = 295 // left, top -> right, bottom + ]; - // default settings of edges - edge [ - color = "<?php echo $attrs['edge_path']; ?>", - fontcolor = "<?php echo $attrs['edge_font']; ?>", - fontsize = 10, - arrowsize = 0.6 - ]; + // default settings of edges + edge [ + color = "<?php echo $attrs['edge_path']; ?>", + fontcolor = "<?php echo $attrs['edge_font']; ?>", + fontsize = 10, + arrowsize = 0.6 + ]; - <?php if ( $gdata === '' ) : ?> + <?php if ( $gdata === '' ) { ?> - //-------------------------- - // default graph ---- begin - //-------------------------- + //-------------------------- + // default graph ---- begin + //-------------------------- - graph [rankdir=TB]; + graph [rankdir=TB]; - A [label="Can it work?"]; - B [label="Did you touch it?"]; - C [label="Does anybody know that?"]; - Z1 [label="It's OK! Don't touch it."]; - Z2 [label="Oh! You are such a fool."]; + A [label="Can it work?"]; + B [label="Did you touch it?"]; + C [label="Does anybody know that?"]; + Z1 [label="It's OK! Don't touch it."]; + Z2 [label="Oh! You are such a fool."]; - A -> Z1 [label="Yes"]; - A -> B [label="No"]; - B -> Z1 [label="No"]; - B -> C [label="Yes"]; - C -> Z1 [label="No"]; - C -> Z2 [label="Yes"]; + A -> Z1 [label="Yes"]; + A -> B [label="No"]; + B -> Z1 [label="No"]; + B -> C [label="Yes"]; + C -> Z1 [label="No"]; + C -> Z2 [label="Yes"]; - //-------------------------- - // default graph ---- end - //-------------------------- + //-------------------------- + // default graph ---- end + //-------------------------- - <?php else: ?> + <?php } else { ?> - // nodes, edges, and clusters - <?php echo $gdata; ?> + // nodes, edges, and clusters + <?php echo $gdata; ?> - <?php endif; ?> + <?php } ?> } diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4c482e5 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "require-dev": { + "mediawiki/mediawiki-codesniffer": "0.7.2" + }, + "scripts": { + "test": [ + "phpcs -p -s" + ], + "fix": "phpcbf" + } +} \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..aec8c8b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,8 @@ +<?xml version="1.0"?> +<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> +</ruleset> \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/334405 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I709085df913c93041ca7d10c72cbbd3f4549ce2c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/QuickGV Gerrit-Branch: master Gerrit-Owner: 小璋丸 <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
