,errors:string[]} Valid rules, and a plain-words reason for every row refused. */ public static function parse( string $json, int $min_find = self::MIN_FIND ): array { if ( '' === trim( $json ) ) { return array( 'pairs' => array(), 'errors' => array(), ); } $decoded = json_decode( $json, true ); if ( ! is_array( $decoded ) ) { return array( 'pairs' => array(), 'errors' => array( __( 'The find and replace rules could not be read.', 'migratex' ) ), ); } return self::sanitize( $decoded, $min_find ); } /** * @param array $rows * @return array{pairs:array,errors:string[]} */ public static function sanitize( array $rows, int $min_find = self::MIN_FIND ): array { $pairs = array(); $errors = array(); $seen = array(); $n = 0; foreach ( $rows as $row ) { ++$n; if ( ! is_array( $row ) ) { continue; } $find = isset( $row['find'] ) && is_string( $row['find'] ) ? $row['find'] : ''; $replace = isset( $row['replace'] ) && is_string( $row['replace'] ) ? $row['replace'] : ''; if ( '' === $find && '' === $replace ) { continue; // an empty row is just an unused row. } if ( count( $pairs ) >= self::MAX_RULES ) { /* translators: %d: maximum number of rules */ $errors[] = sprintf( __( 'No more than %d rules are allowed.', 'migratex' ), self::MAX_RULES ); break; } $problem = self::problem( $find, $replace, $min_find ); if ( null !== $problem ) { /* translators: 1: rule number, 2: what is wrong */ $errors[] = sprintf( __( 'Rule %1$d: %2$s', 'migratex' ), $n, $problem ); continue; } if ( isset( $seen[ $find ] ) ) { /* translators: %d: rule number */ $errors[] = sprintf( __( 'Rule %d searches for text an earlier rule already searches for.', 'migratex' ), $n ); continue; } $seen[ $find ] = true; $pairs[] = array( 'find' => $find, 'replace' => $replace, ); } return array( 'pairs' => $pairs, 'errors' => $errors, ); } /** * Why one rule cannot be used, or null if it can. */ public static function problem( string $find, string $replace, int $min_find = self::MIN_FIND ): ?string { if ( '' === $find ) { return __( '"Find" cannot be empty.', 'migratex' ); } if ( '' === trim( $find ) ) { return __( '"Find" cannot be only spaces.', 'migratex' ); } if ( strlen( $find ) < $min_find ) { /* translators: %d: fewest characters */ return sprintf( __( '"Find" needs at least %d characters, or it would match almost everything.', 'migratex' ), $min_find ); } if ( strlen( $find ) > self::MAX_LENGTH || strlen( $replace ) > self::MAX_LENGTH ) { /* translators: %d: most characters */ return sprintf( __( 'Each side can be at most %d characters.', 'migratex' ), self::MAX_LENGTH ); } if ( false !== strpos( $find . $replace, "\0" ) ) { return __( 'Text cannot contain a null character.', 'migratex' ); } if ( $find === $replace ) { return __( '"Find" and "Replace with" are the same, so the rule does nothing.', 'migratex' ); } return null; } /** * The automatic address rules together with the person's, as one search => replacement map. * * Order does not matter, and that is the point: the replacer looks for the longest search string at each * position and replaces it once, never re-reading what it wrote. So "www.shop.example.com" is replaced * as a whole even when "shop.example.com" and "example.com" are rules too, and a rule whose result * contains another rule's search text is not rewritten a second time. Where a person's rule searches for * exactly what an automatic one does, theirs wins. * * @param array $automatic * @param array $custom * @return array */ public static function merge( array $automatic, array $custom ): array { $map = $automatic; foreach ( $custom as $rule ) { $map[ $rule['find'] ] = $rule['replace']; } return $map; } /** * Rules that will behave in a way the person may not expect, worth a line in the log. * * @param array $custom * @return string[] */ public static function warnings( array $custom ): array { $warnings = array(); foreach ( $custom as $i => $rule ) { if ( false !== strpos( $rule['replace'], $rule['find'] ) ) { $warnings[] = sprintf( /* translators: %d: rule number */ __( 'Rule %d produces text that contains its own search. It is applied once; do not restore the same job twice.', 'migratex' ), $i + 1 ); } } return $warnings; } }