From: Brad Jorsch Date: Mon, 4 Dec 2017 18:36:48 +0000 (-0500) Subject: API: Account for PHP 7.2 change X-Git-Tag: 1.31.0-rc.0~1296^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=9f8626100049a3cd37e23744ed235000c3fef3c8;p=lhc%2Fweb%2Fwiklou.git API: Account for PHP 7.2 change PHP 7.2 broke existing functionality in making count( null ) raise a warning. So add tests for null all over the place, or change tests where we know the value is null or an array (but not false, empty-string, or 0) to just cast to boolean. Bug: T182004 Change-Id: Idfe23a07daa9f60eee72f2daf04304be87057a29 --- diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index 7766acd363..96c291c660 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -59,7 +59,7 @@ class ApiDelete extends ApiBase { // If change tagging was requested, check that the user is allowed to tag, // and the tags are valid - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( !$tagStatus->isOK() ) { $this->dieStatus( $tagStatus ); diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 94d6e97b24..26d4fd1e43 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -334,7 +334,7 @@ class ApiEditPage extends ApiBase { } // Apply change tags - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( $tagStatus->isOK() ) { $requestArray['wpChangeTags'] = implode( ',', $params['tags'] ); diff --git a/includes/api/ApiImageRotate.php b/includes/api/ApiImageRotate.php index 71bda6d7e4..05684036e9 100644 --- a/includes/api/ApiImageRotate.php +++ b/includes/api/ApiImageRotate.php @@ -43,7 +43,7 @@ class ApiImageRotate extends ApiBase { ] ); // Check if user can add tags - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() ); if ( !$ableToTag->isOK() ) { $this->dieStatus( $ableToTag ); diff --git a/includes/api/ApiOptions.php b/includes/api/ApiOptions.php index 5b0d86a7f6..14bd089929 100644 --- a/includes/api/ApiOptions.php +++ b/includes/api/ApiOptions.php @@ -64,7 +64,7 @@ class ApiOptions extends ApiBase { } $changes = []; - if ( count( $params['change'] ) ) { + if ( $params['change'] ) { foreach ( $params['change'] as $entry ) { $array = explode( '=', $entry, 2 ); $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null; diff --git a/includes/api/ApiQueryAllPages.php b/includes/api/ApiQueryAllPages.php index 315def049b..a084279a2b 100644 --- a/includes/api/ApiQueryAllPages.php +++ b/includes/api/ApiQueryAllPages.php @@ -136,12 +136,12 @@ class ApiQueryAllPages extends ApiQueryGeneratorBase { } // Page protection filtering - if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) { + if ( $params['prtype'] || $params['prexpiry'] != 'all' ) { $this->addTables( 'page_restrictions' ); $this->addWhere( 'page_id=pr_page' ); $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" ); - if ( count( $params['prtype'] ) ) { + if ( $params['prtype'] ) { $this->addWhereFld( 'pr_type', $params['prtype'] ); if ( isset( $params['prlevel'] ) ) { diff --git a/includes/api/ApiQueryBacklinks.php b/includes/api/ApiQueryBacklinks.php index 54be254d59..830cc48477 100644 --- a/includes/api/ApiQueryBacklinks.php +++ b/includes/api/ApiQueryBacklinks.php @@ -138,7 +138,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { if ( count( $this->cont ) >= 2 ) { $op = $this->params['dir'] == 'descending' ? '<' : '>'; - if ( count( $this->params['namespace'] ) > 1 ) { + if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) { $this->addWhere( "{$this->bl_from_ns} $op {$this->cont[0]} OR " . "({$this->bl_from_ns} = {$this->cont[0]} AND " . @@ -160,7 +160,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' ); $orderBy = []; - if ( count( $this->params['namespace'] ) > 1 ) { + if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) { $orderBy[] = $this->bl_from_ns . $sort; } $orderBy[] = $this->bl_from . $sort; @@ -246,7 +246,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { $where = "{$this->bl_from} $op= {$this->cont[5]}"; // Don't bother with namespace, title, or from_namespace if it's // otherwise constant in the where clause. - if ( count( $this->params['namespace'] ) > 1 ) { + if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) { $where = "{$this->bl_from_ns} $op {$this->cont[4]} OR " . "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))"; } @@ -278,7 +278,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { if ( count( $allRedirDBkey ) > 1 ) { $orderBy[] = $this->bl_title . $sort; } - if ( count( $this->params['namespace'] ) > 1 ) { + if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) { $orderBy[] = $this->bl_from_ns . $sort; } $orderBy[] = $this->bl_from . $sort; diff --git a/includes/api/ApiQueryBacklinksprop.php b/includes/api/ApiQueryBacklinksprop.php index 1db15f87e8..ef02d095c8 100644 --- a/includes/api/ApiQueryBacklinksprop.php +++ b/includes/api/ApiQueryBacklinksprop.php @@ -161,7 +161,9 @@ class ApiQueryBacklinksprop extends ApiQueryGeneratorBase { } } else { $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] ); - if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) { + if ( !empty( $settings['from_namespace'] ) + && $params['namespace'] !== null && count( $params['namespace'] ) > 1 + ) { $sortby["{$p}_from_namespace"] = 'int'; } } diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index 6987dfb13f..8e9b1b4973 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -262,9 +262,7 @@ abstract class ApiQueryBase extends ApiBase { * @param string|string[] $value Value; ignored if null or empty array; */ protected function addWhereFld( $field, $value ) { - // Use count() to its full documented capabilities to simultaneously - // test for null, empty array or empty countable object - if ( count( $value ) ) { + if ( $value !== null && count( $value ) ) { $this->where[$field] = $value; } } diff --git a/includes/api/ApiQueryCategoryMembers.php b/includes/api/ApiQueryCategoryMembers.php index c570ec997e..e3265d1cdc 100644 --- a/includes/api/ApiQueryCategoryMembers.php +++ b/includes/api/ApiQueryCategoryMembers.php @@ -97,7 +97,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase { // how to have efficient subcategory access :-) ~~~~ (oh well, domas) $miser_ns = []; if ( $this->getConfig()->get( 'MiserMode' ) ) { - $miser_ns = $params['namespace']; + $miser_ns = $params['namespace'] ?: []; } else { $this->addWhereFld( 'page_namespace', $params['namespace'] ); } diff --git a/includes/api/ApiQueryExtLinksUsage.php b/includes/api/ApiQueryExtLinksUsage.php index 6c29b6030f..43f41312fd 100644 --- a/includes/api/ApiQueryExtLinksUsage.php +++ b/includes/api/ApiQueryExtLinksUsage.php @@ -61,7 +61,7 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { $miser_ns = []; if ( $this->getConfig()->get( 'MiserMode' ) ) { - $miser_ns = $params['namespace']; + $miser_ns = $params['namespace'] ?: []; } else { $this->addWhereFld( 'page_namespace', $params['namespace'] ); } diff --git a/includes/api/ApiQueryLinks.php b/includes/api/ApiQueryLinks.php index 508bdf3f9d..119db3e6a9 100644 --- a/includes/api/ApiQueryLinks.php +++ b/includes/api/ApiQueryLinks.php @@ -114,7 +114,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase { } } elseif ( $params['namespace'] ) { $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] ); - $multiNS = count( $params['namespace'] ) !== 1; + $multiNS = $params['namespace'] === null || count( $params['namespace'] ) !== 1; } if ( !is_null( $params['continue'] ) ) { diff --git a/includes/api/ApiRevisionDelete.php b/includes/api/ApiRevisionDelete.php index 9d71a7db7e..5a51b2843a 100644 --- a/includes/api/ApiRevisionDelete.php +++ b/includes/api/ApiRevisionDelete.php @@ -47,7 +47,7 @@ class ApiRevisionDelete extends ApiBase { } // Check if user can add tags - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( !$ableToTag->isOK() ) { $this->dieStatus( $ableToTag ); diff --git a/includes/api/ApiRollback.php b/includes/api/ApiRollback.php index 76b6cc6722..4ca2955079 100644 --- a/includes/api/ApiRollback.php +++ b/includes/api/ApiRollback.php @@ -52,7 +52,7 @@ class ApiRollback extends ApiBase { // If change tagging was requested, check that the user is allowed to tag, // and the tags are valid - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( !$tagStatus->isOK() ) { $this->dieStatus( $tagStatus ); diff --git a/includes/api/ApiSetPageLanguage.php b/includes/api/ApiSetPageLanguage.php index 7e3f1acf96..54394a57d9 100644 --- a/includes/api/ApiSetPageLanguage.php +++ b/includes/api/ApiSetPageLanguage.php @@ -73,7 +73,7 @@ class ApiSetPageLanguage extends ApiBase { // If change tagging was requested, check that the user is allowed to tag, // and the tags are valid - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( !$tagStatus->isOK() ) { $this->dieStatus( $tagStatus ); diff --git a/includes/api/ApiTag.php b/includes/api/ApiTag.php index 76c676293f..9304c2b414 100644 --- a/includes/api/ApiTag.php +++ b/includes/api/ApiTag.php @@ -37,7 +37,7 @@ class ApiTag extends ApiBase { } // Check if user can add tags - if ( count( $params['tags'] ) ) { + if ( $params['tags'] ) { $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); if ( !$ableToTag->isOk() ) { $this->dieStatus( $ableToTag ); diff --git a/includes/api/ApiUserrights.php b/includes/api/ApiUserrights.php index 2a364d9756..3813aba7a1 100644 --- a/includes/api/ApiUserrights.php +++ b/includes/api/ApiUserrights.php @@ -64,14 +64,15 @@ class ApiUserrights extends ApiBase { } else { $expiry = [ 'infinity' ]; } - if ( count( $expiry ) !== count( $params['add'] ) ) { + $add = (array)$params['add']; + if ( count( $expiry ) !== count( $add ) ) { if ( count( $expiry ) === 1 ) { - $expiry = array_fill( 0, count( $params['add'] ), $expiry[0] ); + $expiry = array_fill( 0, count( $add ), $expiry[0] ); } else { $this->dieWithError( [ 'apierror-toofewexpiries', count( $expiry ), - count( $params['add'] ) + count( $add ) ] ); } } @@ -79,7 +80,7 @@ class ApiUserrights extends ApiBase { // Validate the expiries $groupExpiries = []; foreach ( $expiry as $index => $expiryValue ) { - $group = $params['add'][$index]; + $group = $add[$index]; $groupExpiries[$group] = UserrightsPage::expiryToTimestamp( $expiryValue ); if ( $groupExpiries[$group] === false ) { @@ -109,7 +110,7 @@ class ApiUserrights extends ApiBase { $r['user'] = $user->getName(); $r['userid'] = $user->getId(); list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups( - $user, (array)$params['add'], (array)$params['remove'], + $user, (array)$add, (array)$params['remove'], $params['reason'], $tags, $groupExpiries );