Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions maintenance/GenerateMissingIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ public function execute(): void {

$this->output( "Generating persistent ids for batch of $batchSize pages\n" );

$this->savePersistentIds( $pageIds, $this->generateBulkPersistentIds( $batchSize ) );
$idMap = array_combine( $pageIds, $this->generateBulkPersistentIds( $batchSize ) );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for array_combine.

The array_combine function returns false if the input arrays have different lengths, which would cause a fatal error when passed to subsequent functions.

Add error handling to prevent potential runtime errors:

-$idMap = array_combine( $pageIds, $this->generateBulkPersistentIds( $batchSize ) );
+$persistentIds = $this->generateBulkPersistentIds( $batchSize );
+$idMap = array_combine( $pageIds, $persistentIds );
+if ( $idMap === false ) {
+    throw new \RuntimeException( 'Failed to combine page IDs with persistent IDs - array length mismatch' );
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$idMap = array_combine( $pageIds, $this->generateBulkPersistentIds( $batchSize ) );
// Generate persistent IDs first
$persistentIds = $this->generateBulkPersistentIds( $batchSize );
// Combine page IDs with the newly generated IDs
$idMap = array_combine( $pageIds, $persistentIds );
if ( $idMap === false ) {
throw new \RuntimeException(
'Failed to combine page IDs with persistent IDs - array length mismatch'
);
}
🤖 Prompt for AI Agents
In maintenance/GenerateMissingIdentifiers.php at line 36, the call to
array_combine can return false if the input arrays have different lengths,
leading to runtime errors later. Add a check after array_combine to verify that
$idMap is not false before proceeding. If it is false, handle the error
appropriately, such as logging an error message or throwing an exception, to
prevent fatal errors in subsequent code.


$this->output( 'Generated batch of persistent IDs: ' . json_encode( $idMap ) . "\n" );

$this->savePersistentIds( $idMap );

$generatedIdsCount += $batchSize;
}

$this->output( "Generated $generatedIdsCount persistent IDs\n" );
$this->output( "Done. Generated $generatedIdsCount persistent IDs\n" );
}

/**
Expand All @@ -60,10 +64,8 @@ private function generateBulkPersistentIds( int $count ): array {
);
}

private function savePersistentIds( array $pageIds, array $persistentIds ): void {
PersistentPageIdentifiersExtension::getInstance()->getPersistentPageIdentifiersRepo()->savePersistentIds(
array_combine( $pageIds, $persistentIds )
);
private function savePersistentIds( array $idMap ): void {
PersistentPageIdentifiersExtension::getInstance()->getPersistentPageIdentifiersRepo()->savePersistentIds( $idMap );
}

}
Expand Down