Reedy has submitted this change and it was merged.

Change subject: Prevent warning when importing jump config (no questions)
......................................................................


Prevent warning when importing jump config (no questions)

Change-Id: I5235efc319797d75487f27ed87f5a0dbc8905072
Warning: Invalid argument supplied for foreach()
---
M cli/import.php
1 file changed, 49 insertions(+), 44 deletions(-)

Approvals:
  Reedy: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/cli/import.php b/cli/import.php
index 576b374..50609f6 100644
--- a/cli/import.php
+++ b/cli/import.php
@@ -3,29 +3,29 @@
 require( dirname( __FILE__ ) . '/cli.inc' );
 
 $usage = <<<EOT
-Import configuration files into the local SecurePoll database. Files can be 
+Import configuration files into the local SecurePoll database. Files can be
 generated with dump.php.
 
 Usage: import.php [options] <file>
 
 Options are:
-       --update-msgs       Update the internationalised text for the 
elections, do 
+       --update-msgs       Update the internationalised text for the 
elections, do
                         not update configuration.
 
-       --replace           If an election with a conflicting title exists 
already, 
-                        replace it, updating its configuration. The default is 
+       --replace           If an election with a conflicting title exists 
already,
+                        replace it, updating its configuration. The default is
                                                to exit with an error.
 
 Note that any vote records will NOT be imported.
 
-For the moment, the entity IDs are preserved, to allow easier implementation 
of 
-the message update feature. This means conflicting entity IDs in the local 
-database will generate an error. This restriction will be removed in the 
+For the moment, the entity IDs are preserved, to allow easier implementation of
+the message update feature. This means conflicting entity IDs in the local
+database will generate an error. This restriction will be removed in the
 future.
 
 EOT;
 
-# Most of the code here will eventually be refactored into the update 
interfaces 
+# Most of the code here will eventually be refactored into the update 
interfaces
 # of the entity and context classes, but that project can wait until we have a
 # setup UI.
 
@@ -75,12 +75,13 @@
        foreach ( $electionIds as $id ) {
                $elections = $store->getElectionInfo( array( $id ) );
                $electionInfo = reset( $elections );
-               $existingId = $dbw->selectField( 
-                       'securepoll_elections', 
-                       'el_entity', 
-                       array( 'el_title' => $electionInfo['title'] ), 
-                       __METHOD__, 
-                       array( 'FOR UPDATE' ) );
+               $existingId = $dbw->selectField(
+                       'securepoll_elections',
+                       'el_entity',
+                       array( 'el_title' => $electionInfo['title'] ),
+                       __METHOD__,
+                       array( 'FOR UPDATE' )
+               );
                if ( $existingId !== false ) {
                        if ( $options['replace'] ) {
                                spDeleteElection( $existingId );
@@ -153,8 +154,8 @@
  */
 function spInsertEntity( $type, $id ) {
        $dbw = wfGetDB( DB_MASTER );
-       $dbw->insert( 'securepoll_entity', 
-               array( 
+       $dbw->insert( 'securepoll_entity',
+               array(
                        'en_id' => $id,
                        'en_type' => $type,
                ),
@@ -187,31 +188,33 @@
                __METHOD__ );
        $sourceIds[] = $electionInfo['id'];
 
-       # Questions
-       $index = 1;
-       foreach ( $electionInfo['questions'] as $questionInfo ) {
-               spInsertEntity( 'question', $questionInfo['id'] );
-               $dbw->insert( 'securepoll_questions',
-                       array(
-                               'qu_entity' => $questionInfo['id'],
-                               'qu_election' => $electionInfo['id'],
-                               'qu_index' => $index++,
-                       ),
-                       __METHOD__ );
-               $sourceIds[] = $questionInfo['id'];
+       if ( isset( $electionInfo['questions'] ) ) {
+               # Questions
+               $index = 1;
+               foreach ( $electionInfo['questions'] as $questionInfo ) {
+                       spInsertEntity( 'question', $questionInfo['id'] );
+                       $dbw->insert( 'securepoll_questions',
+                               array(
+                                       'qu_entity' => $questionInfo['id'],
+                                       'qu_election' => $electionInfo['id'],
+                                       'qu_index' => $index++,
+                               ),
+                               __METHOD__ );
+                       $sourceIds[] = $questionInfo['id'];
 
-               # Options
-               $insertBatch = array();
-               foreach ( $questionInfo['options'] as $optionInfo ) {
-                       spInsertEntity( 'option', $optionInfo['id'] );
-                       $insertBatch[] = array(
-                               'op_entity' => $optionInfo['id'],
-                               'op_election' => $electionInfo['id'],
-                               'op_question' => $questionInfo['id']
-                       );
-                       $sourceIds[] = $optionInfo['id'];
+                       # Options
+                       $insertBatch = array();
+                       foreach ( $questionInfo['options'] as $optionInfo ) {
+                               spInsertEntity( 'option', $optionInfo['id'] );
+                               $insertBatch[] = array(
+                                       'op_entity' => $optionInfo['id'],
+                                       'op_election' => $electionInfo['id'],
+                                       'op_question' => $questionInfo['id']
+                               );
+                               $sourceIds[] = $optionInfo['id'];
+                       }
+                       $dbw->insert( 'securepoll_options', $insertBatch, 
__METHOD__ );
                }
-               $dbw->insert( 'securepoll_options', $insertBatch, __METHOD__ );
        }
 
        # Messages
@@ -268,13 +271,15 @@
  */
 function spUpdateMessages( $store, $electionInfo ) {
        $entityIds = array( $electionInfo['id'] );
-       foreach ( $electionInfo['questions'] as $questionInfo ) {
-               $entityIds[] = $questionInfo['id'];
-               foreach ( $questionInfo['options'] as $optionInfo ) {
-                       $entityIds[] = $optionInfo['id'];
+       if ( isset( $electionInfo['questions'] ) ) {
+               foreach ( $electionInfo['questions'] as $questionInfo ) {
+                       $entityIds[] = $questionInfo['id'];
+                       foreach ( $questionInfo['options'] as $optionInfo ) {
+                               $entityIds[] = $optionInfo['id'];
+                       }
                }
        }
-       
+
        # Delete existing messages
        $dbw = wfGetDB( DB_MASTER );
        $dbw->delete( 'securepoll_msgs', array( 'msg_entity' => $entityIds ), 
__METHOD__ );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5235efc319797d75487f27ed87f5a0dbc8905072
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/SecurePoll
Gerrit-Branch: master
Gerrit-Owner: Reedy <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to