API: Account for PHP 7.2 change
authorBrad Jorsch <bjorsch@wikimedia.org>
Mon, 4 Dec 2017 18:36:48 +0000 (13:36 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Mon, 4 Dec 2017 18:46:33 +0000 (13:46 -0500)
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

16 files changed:
includes/api/ApiDelete.php
includes/api/ApiEditPage.php
includes/api/ApiImageRotate.php
includes/api/ApiOptions.php
includes/api/ApiQueryAllPages.php
includes/api/ApiQueryBacklinks.php
includes/api/ApiQueryBacklinksprop.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryCategoryMembers.php
includes/api/ApiQueryExtLinksUsage.php
includes/api/ApiQueryLinks.php
includes/api/ApiRevisionDelete.php
includes/api/ApiRollback.php
includes/api/ApiSetPageLanguage.php
includes/api/ApiTag.php
includes/api/ApiUserrights.php

index 7766acd..96c291c 100644 (file)
@@ -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 );
index 94d6e97..26d4fd1 100644 (file)
@@ -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'] );
index 71bda6d..0568403 100644 (file)
@@ -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 );
index 5b0d86a..14bd089 100644 (file)
@@ -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;
index 315def0..a084279 100644 (file)
@@ -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'] ) ) {
index 54be254..830cc48 100644 (file)
@@ -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;
index 1db15f8..ef02d09 100644 (file)
@@ -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';
                                }
                        }
index 6987dfb..8e9b1b4 100644 (file)
@@ -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;
                }
        }
index c570ec9..e3265d1 100644 (file)
@@ -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'] );
                }
index 6c29b60..43f4131 100644 (file)
@@ -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'] );
                }
index 508bdf3..119db3e 100644 (file)
@@ -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'] ) ) {
index 9d71a7d..5a51b28 100644 (file)
@@ -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 );
index 76b6cc6..4ca2955 100644 (file)
@@ -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 );
index 7e3f1ac..54394a5 100644 (file)
@@ -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 );
index 76c6762..9304c2b 100644 (file)
@@ -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 );
index 2a364d9..3813aba 100644 (file)
@@ -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
                );