store = $store; $this->runner = $runner; } /** * Loads the job named in the request, enforcing that it exists, belongs to the * current user and (optionally) is of the expected type. * * @throws HttpException */ protected function ownedJob( Request $request, ?string $type = null ): JobState { $job = $this->store->load( $request->jobId() ); if ( null === $job ) { throw new HttpException( 'om_job_not_found', esc_html__( 'Job not found. It may have been cancelled or expired.', 'migratex' ), 404 ); } // After the database swap, user ids refer to the restored site, so ownership no longer means anything. if ( $job->userId() !== get_current_user_id() && ! $job->get( 'swapped' ) ) { throw new HttpException( 'om_job_forbidden', esc_html__( 'This job belongs to another user.', 'migratex' ), 403 ); } if ( null !== $type && $job->type() !== $type ) { throw new HttpException( 'om_job_type_mismatch', esc_html__( 'Job type does not match this endpoint.', 'migratex' ), 409 ); } return $job; } /** * The find-and-replace rules sent with a restore, checked. A rule that cannot be used refuses the whole * request with the reason, so a typo is corrected before a restore starts, not silently dropped. * * @return array * @throws HttpException */ protected function customRules( Request $request ): array { $parsed = ReplacementRules::parse( $request->raw( 'custom_replacements' ) ); if ( $parsed['errors'] ) { throw new HttpException( 'om_bad_replacement', esc_html( $parsed['errors'][0] ), 400 ); } return $parsed['pairs']; } /** * A reset deletes files and swaps the database; an export or restore started in the middle of * one would read or write half of each. * * @throws HttpException */ protected function assertNoResetRunning(): void { foreach ( $this->store->ids() as $id ) { $job = $this->store->load( $id ); if ( null !== $job && 'reset' === $job->type() && ! $job->isTerminal() ) { throw new HttpException( 'om_reset_active', esc_html__( 'A reset is running. Wait for it to finish first.', 'migratex' ), 409 ); } } } }