From e1630b6a53c40ed1cd377338944765e0d68a2981 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Mon, 11 Jun 2018 11:16:48 +0200 Subject: [PATCH] PHP: Use short ternary operator (?:) where possible Change-Id: Idcc7e4fcdd4d8302ceda44bf6d294fa8c2219381 --- includes/AjaxResponse.php | 2 +- includes/EditPage.php | 2 +- includes/MediaWiki.php | 4 ++-- includes/OutputPage.php | 4 ++-- includes/Setup.php | 2 +- includes/cache/GenderCache.php | 2 +- includes/changetags/ChangeTags.php | 2 +- includes/filerepo/RepoGroup.php | 2 +- includes/libs/jsminplus.php | 6 +++--- includes/libs/lockmanager/DBLockManager.php | 2 +- includes/libs/mime/IEContentAnalyzer.php | 4 ++-- includes/parser/Parser.php | 2 +- includes/specials/SpecialEmailuser.php | 2 +- includes/specials/SpecialMIMEsearch.php | 2 +- includes/specials/SpecialPageLanguage.php | 4 ++-- includes/specials/SpecialVersion.php | 2 +- includes/specials/pagers/AllMessagesTablePager.php | 2 +- includes/upload/UploadBase.php | 2 +- maintenance/storage/checkStorage.php | 2 +- profileinfo.php | 4 ++-- tests/parser/DjVuSupport.php | 8 ++++---- tests/phpunit/includes/Storage/RevisionStoreTest.php | 6 +++--- 22 files changed, 34 insertions(+), 34 deletions(-) diff --git a/includes/AjaxResponse.php b/includes/AjaxResponse.php index 3e42c08650..0c115053ba 100644 --- a/includes/AjaxResponse.php +++ b/includes/AjaxResponse.php @@ -242,7 +242,7 @@ class AjaxResponse { # this breaks strtotime(). $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] ); $modsinceTime = strtotime( $modsince ); - $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 ); + $ismodsince = wfTimestamp( TS_MW, $modsinceTime ?: 1 ); wfDebug( "$fname: -- client send If-Modified-Since: $modsince", 'private' ); wfDebug( "$fname: -- we might send Last-Modified : $lastmod", 'private' ); diff --git a/includes/EditPage.php b/includes/EditPage.php index 22c29d6413..581a28f096 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -2812,7 +2812,7 @@ ERROR; $this->autoSumm = md5( '' ); } - $autosumm = $this->autoSumm ? $this->autoSumm : md5( $this->summary ); + $autosumm = $this->autoSumm ?: md5( $this->summary ); $out->addHTML( Html::hidden( 'wpAutoSummary', $autosumm ) ); $out->addHTML( Html::hidden( 'oldid', $this->oldid ) ); diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php index 79787272eb..72b1090ef2 100644 --- a/includes/MediaWiki.php +++ b/includes/MediaWiki.php @@ -104,7 +104,7 @@ class MediaWiki { if ( $ret === null || !$ret->isSpecialPage() ) { // We can have urls with just ?diff=,?oldid= or even just ?diff= $oldid = $request->getInt( 'oldid' ); - $oldid = $oldid ? $oldid : $request->getInt( 'diff' ); + $oldid = $oldid ?: $request->getInt( 'diff' ); // Allow oldid to override a changed or missing title if ( $oldid ) { $rev = Revision::newFromId( $oldid ); @@ -426,7 +426,7 @@ class MediaWiki { // If $target is set, then a hook wanted to redirect. if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) { // Is the target already set by an extension? - $target = $target ? $target : $page->followRedirect(); + $target = $target ?: $page->followRedirect(); if ( is_string( $target ) ) { if ( !$this->config->get( 'DisableHardRedirects' ) ) { // we'll need to redirect diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 6700cdbb49..50cc991b70 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -3111,13 +3111,13 @@ class OutputPage extends ContextSource { // Pre-process information $separatorTransTable = $lang->separatorTransformTable(); - $separatorTransTable = $separatorTransTable ? $separatorTransTable : []; + $separatorTransTable = $separatorTransTable ?: []; $compactSeparatorTransTable = [ implode( "\t", array_keys( $separatorTransTable ) ), implode( "\t", $separatorTransTable ), ]; $digitTransTable = $lang->digitTransformTable(); - $digitTransTable = $digitTransTable ? $digitTransTable : []; + $digitTransTable = $digitTransTable ?: []; $compactDigitTransTable = [ implode( "\t", array_keys( $digitTransTable ) ), implode( "\t", $digitTransTable ), diff --git a/includes/Setup.php b/includes/Setup.php index e2fab4578e..41d59455cf 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -825,7 +825,7 @@ $wgInitialSessionId = null; if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) { // If session.auto_start is there, we can't touch session name if ( $wgPHPSessionHandling !== 'disable' && !wfIniGetBool( 'session.auto_start' ) ) { - session_name( $wgSessionName ? $wgSessionName : $wgCookiePrefix . '_session' ); + session_name( $wgSessionName ?: $wgCookiePrefix . '_session' ); } // Create the SessionManager singleton and set up our session handler, diff --git a/includes/cache/GenderCache.php b/includes/cache/GenderCache.php index 1ec39a54d6..7228814d47 100644 --- a/includes/cache/GenderCache.php +++ b/includes/cache/GenderCache.php @@ -171,7 +171,7 @@ class GenderCache { $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins ); foreach ( $res as $row ) { - $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; + $this->cache[$row->user_name] = $row->up_value ?: $default; } } diff --git a/includes/changetags/ChangeTags.php b/includes/changetags/ChangeTags.php index 0c81144531..7f860c1693 100644 --- a/includes/changetags/ChangeTags.php +++ b/includes/changetags/ChangeTags.php @@ -466,7 +466,7 @@ class ChangeTags { // $prevTags can be out of date on replica DBs, especially when addTags is called consecutively, // causing loss of tags added recently in tag_summary table. $prevTags = $dbw->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ ); - $prevTags = $prevTags ? $prevTags : ''; + $prevTags = $prevTags ?: ''; $prevTags = array_filter( explode( ',', $prevTags ) ); // add tags diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 7dd8b25cef..89287af0bd 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -163,7 +163,7 @@ class RepoGroup { } } - $image = $image ? $image : false; // type sanity + $image = $image ?: false; // type sanity # Cache file existence or non-existence if ( $useCache && ( !$image || $image->isCacheable() ) ) { $this->cache->set( $dbkey, $time, $image ); diff --git a/includes/libs/jsminplus.php b/includes/libs/jsminplus.php index e3c2d75863..08e9d690db 100644 --- a/includes/libs/jsminplus.php +++ b/includes/libs/jsminplus.php @@ -909,7 +909,7 @@ class JSParser } else { - $n->setup = $n2 ? $n2 : null; + $n->setup = $n2 ?: null; $this->t->mustMatch(OP_SEMICOLON); $n->condition = $this->t->peek() == OP_SEMICOLON ? null : $this->Expression($x); $this->t->mustMatch(OP_SEMICOLON); @@ -1656,7 +1656,7 @@ class JSNode { if ($token = $t->currentToken()) { - $this->type = $type ? $type : $token->type; + $this->type = $type ?: $token->type; $this->value = $token->value; $this->lineno = $token->lineno; $this->start = $token->start; @@ -1752,7 +1752,7 @@ class JSTokenizer public function init($source, $filename = '', $lineno = 1) { $this->source = $source; - $this->filename = $filename ? $filename : '[inline]'; + $this->filename = $filename ?: '[inline]'; $this->lineno = $lineno; $this->cursor = 0; diff --git a/includes/libs/lockmanager/DBLockManager.php b/includes/libs/lockmanager/DBLockManager.php index 564616dd97..aec9f25b9f 100644 --- a/includes/libs/lockmanager/DBLockManager.php +++ b/includes/libs/lockmanager/DBLockManager.php @@ -82,7 +82,7 @@ abstract class DBLockManager extends QuorumLockManager { $this->lockExpiry = $config['lockExpiry']; } else { $met = ini_get( 'max_execution_time' ); - $this->lockExpiry = $met ? $met : 60; // use some sane amount if 0 + $this->lockExpiry = $met ?: 60; // use some sane amount if 0 } $this->safeDelay = ( $this->lockExpiry <= 0 ) ? 60 // pick a safe-ish number to match DB timeout default diff --git a/includes/libs/mime/IEContentAnalyzer.php b/includes/libs/mime/IEContentAnalyzer.php index e9fb11f7a8..802ed2decb 100644 --- a/includes/libs/mime/IEContentAnalyzer.php +++ b/includes/libs/mime/IEContentAnalyzer.php @@ -500,13 +500,13 @@ class IEContentAnalyzer { < ( $counters['ctrl'] + $counters['high'] ) * 16 ) { $kindOfBinary = true; - $type = $binaryType ? $binaryType : $textType; + $type = $binaryType ?: $textType; if ( $type === false ) { $type = 'application/octet-stream'; } } else { $kindOfBinary = false; - $type = $textType ? $textType : $binaryType; + $type = $textType ?: $binaryType; if ( $type === false ) { $type = 'text/plain'; } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 15ed93c5ff..28e7450612 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -2641,7 +2641,7 @@ class Parser { $this->mOutput->setFlag( 'vary-revision' ); wfDebug( __METHOD__ . ": {{PAGEID}} used in a new page, setting vary-revision...\n" ); } - $value = $pageid ? $pageid : null; + $value = $pageid ?: null; break; case 'revisionid': # Let the edit saving system know we should parse the page diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index f322ac40ef..0e931949cc 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -204,7 +204,7 @@ class SpecialEmailUser extends UnlistedSpecialPage { $nu = User::newFromName( $target ); $error = self::validateTarget( $nu, $sender ); - return $error ? $error : $nu; + return $error ?: $nu; } /** diff --git a/includes/specials/SpecialMIMEsearch.php b/includes/specials/SpecialMIMEsearch.php index a54d72de58..f43ed9bd6a 100644 --- a/includes/specials/SpecialMIMEsearch.php +++ b/includes/specials/SpecialMIMEsearch.php @@ -160,7 +160,7 @@ class MIMEsearchPage extends QueryPage { } public function execute( $par ) { - $this->mime = $par ? $par : $this->getRequest()->getText( 'mime' ); + $this->mime = $par ?: $this->getRequest()->getText( 'mime' ); $this->mime = trim( $this->mime ); list( $this->major, $this->minor ) = File::splitMime( $this->mime ); diff --git a/includes/specials/SpecialPageLanguage.php b/includes/specials/SpecialPageLanguage.php index e3485ff724..37412722bb 100644 --- a/includes/specials/SpecialPageLanguage.php +++ b/includes/specials/SpecialPageLanguage.php @@ -224,8 +224,8 @@ class SpecialPageLanguage extends FormSpecialPage { } // Hardcoded [def] if the language is set to null - $logOld = $oldLanguage ? $oldLanguage : $defLang . '[def]'; - $logNew = $newLanguage ? $newLanguage : $defLang . '[def]'; + $logOld = $oldLanguage ?: $defLang . '[def]'; + $logNew = $newLanguage ?: $defLang . '[def]'; // Writing new page language to database $dbw->update( diff --git a/includes/specials/SpecialVersion.php b/includes/specials/SpecialVersion.php index 911c9a63e3..35c5689e4f 100644 --- a/includes/specials/SpecialVersion.php +++ b/includes/specials/SpecialVersion.php @@ -1134,7 +1134,7 @@ class SpecialVersion extends SpecialPage { */ public function getEntryPointInfo() { global $wgArticlePath, $wgScriptPath; - $scriptPath = $wgScriptPath ? $wgScriptPath : "/"; + $scriptPath = $wgScriptPath ?: "/"; $entryPoints = [ 'version-entrypoints-articlepath' => $wgArticlePath, 'version-entrypoints-scriptpath' => $scriptPath, diff --git a/includes/specials/pagers/AllMessagesTablePager.php b/includes/specials/pagers/AllMessagesTablePager.php index e6a0f0be51..35c9931caa 100644 --- a/includes/specials/pagers/AllMessagesTablePager.php +++ b/includes/specials/pagers/AllMessagesTablePager.php @@ -58,7 +58,7 @@ class AllMessagesTablePager extends TablePager { $this->talk = $this->msg( 'talkpagelinktext' )->escaped(); - $this->lang = ( $langObj ? $langObj : $wgContLang ); + $this->lang = $langObj ?: $wgContLang; $this->langcode = $this->lang->getCode(); $this->foreign = !$this->lang->equals( $wgContLang ); diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index 5352d95b8d..87b96acc79 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -160,7 +160,7 @@ abstract class UploadBase { * @return null|UploadBase */ public static function createFromRequest( &$request, $type = null ) { - $type = $type ? $type : $request->getVal( 'wpSourceType', 'File' ); + $type = $type ?: $request->getVal( 'wpSourceType', 'File' ); if ( !$type ) { return null; diff --git a/maintenance/storage/checkStorage.php b/maintenance/storage/checkStorage.php index bd0556aa7c..0ec24aff14 100644 --- a/maintenance/storage/checkStorage.php +++ b/maintenance/storage/checkStorage.php @@ -502,7 +502,7 @@ class CheckStorage { function importRevision( &$revision, &$importer ) { $id = $revision->getID(); $content = $revision->getContent( Revision::RAW ); - $id = $id ? $id : ''; + $id = $id ?: ''; if ( $content === null ) { echo "Revision $id is broken, we have no content available\n"; diff --git a/profileinfo.php b/profileinfo.php index 9ebd57b7a2..8bd37dd4cb 100644 --- a/profileinfo.php +++ b/profileinfo.php @@ -396,8 +396,8 @@ if ( isset( $_REQUEST['filter'] ) ) { return htmlspecialchars( '?' . wfArrayToCgi( [ - 'filter' => $_filter ? $_filter : $filter, - 'sort' => $_sort ? $_sort : $sort, + 'filter' => $_filter ?: $filter, + 'sort' => $_sort ?: $sort, 'expand' => implode( ',', array_keys( $_expand ) ) ] ) ); diff --git a/tests/parser/DjVuSupport.php b/tests/parser/DjVuSupport.php index 73d4a47f82..eede54e0f2 100644 --- a/tests/parser/DjVuSupport.php +++ b/tests/parser/DjVuSupport.php @@ -31,10 +31,10 @@ class DjVuSupport { public function __construct() { global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgFileExtensions, $wgDjvuTxt; - $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu'; - $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump'; - $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml'; - $wgDjvuTxt = $wgDjvuTxt ? $wgDjvuTxt : '/usr/bin/djvutxt'; + $wgDjvuRenderer = $wgDjvuRenderer ?: '/usr/bin/ddjvu'; + $wgDjvuDump = $wgDjvuDump ?: '/usr/bin/djvudump'; + $wgDjvuToXML = $wgDjvuToXML ?: '/usr/bin/djvutoxml'; + $wgDjvuTxt = $wgDjvuTxt ?: '/usr/bin/djvutxt'; if ( !in_array( 'djvu', $wgFileExtensions ) ) { $wgFileExtensions[] = 'djvu'; diff --git a/tests/phpunit/includes/Storage/RevisionStoreTest.php b/tests/phpunit/includes/Storage/RevisionStoreTest.php index 3749f294bf..61d0512542 100644 --- a/tests/phpunit/includes/Storage/RevisionStoreTest.php +++ b/tests/phpunit/includes/Storage/RevisionStoreTest.php @@ -29,9 +29,9 @@ class RevisionStoreTest extends MediaWikiTestCase { $WANObjectCache = null ) { return new RevisionStore( - $loadBalancer ? $loadBalancer : $this->getMockLoadBalancer(), - $blobStore ? $blobStore : $this->getMockSqlBlobStore(), - $WANObjectCache ? $WANObjectCache : $this->getHashWANObjectCache(), + $loadBalancer ?: $this->getMockLoadBalancer(), + $blobStore ?: $this->getMockSqlBlobStore(), + $WANObjectCache ?: $this->getHashWANObjectCache(), MediaWikiServices::getInstance()->getCommentStore(), MediaWikiServices::getInstance()->getActorMigration() ); -- 2.20.1