*/ private $reasons = array(); /** * Unknown count. * * @var int */ private $unknowns = 0; /** * Notes that do not lower the status (for example ignored extra arguments). * * @var array */ private $notes = array(); /** * Pattern count. * * @var int */ private $patterns = 0; /** * Constructor. * * @param Registry|null $registry Registry. */ public function __construct( $registry = null ) { $this->registry = $registry ? $registry : Registry::instance(); } /** * Reset the per-string counters (node() increments them while walking). */ private function reset_counts() { $this->reasons = array(); $this->unknowns = 0; $this->patterns = 0; $this->notes = array(); } /** * Translate one legacy string. * * @param string $source Original string, word for word. * @return array{tree:array, status:string, reasons:array, risk:string, risk_terms:array, detail:string, notes:array} */ public function translate( $source ) { $this->registry->ensure_loaded(); $this->reset_counts(); $parser = new Parser(); $parsed = $parser->parse( $source ); $detail = ''; if ( ! $parsed['ok'] ) { $tree = array( 'unknown' => (string) $source, 'reason' => $parsed['reason'], ); $this->reasons[ $parsed['reason'] ] = true; $detail = $parsed['detail']; $status = 'untranslated'; } else { if ( ! empty( $parsed['preamble'] ) ) { $this->notes['global_post_preamble_stripped'] = true; } $tree = $this->node( $parsed['tree'] ); $rule = Rule::from_tree( $tree ); if ( $rule->over_limit() ) { $tree = array( 'unknown' => (string) $source, 'reason' => 'too_large', ); $this->reasons = array( 'too_large' => true ); $status = 'untranslated'; } elseif ( array_key_exists( 'unknown', $tree ) ) { // A string that is one foreign operand keeps its whole original text, // "return", trailing ";" and any stripped preamble included. $tree['unknown'] = (string) $source; $status = 'untranslated'; } elseif ( $this->unknowns > 0 ) { $status = 'partial'; } elseif ( $this->patterns > 0 ) { $status = 'translated_pattern'; } else { $status = 'translated'; } } $risk = Risk_Classifier::classify( $parsed['tokens'], $tree, (string) $source, $this->registry ); return array( 'tree' => $tree, 'status' => $status, 'reasons' => array_keys( $this->reasons ), 'risk' => $risk['risk'], 'risk_terms' => $risk['terms'], 'detail' => $detail, 'notes' => array_keys( $this->notes ), ); } /** * Translate one parse node. * * @param array $p Parse node. * @return array Rule node. */ private function node( array $p ) { switch ( $p['t'] ) { case 'or': case 'and': $items = array(); foreach ( $p['items'] as $c ) { $items[] = $this->node( $c ); } return array( 'op' => 'or' === $p['t'] ? 'any' : 'all', 'items' => $items, ); case 'not': return array( 'op' => 'not', 'item' => $this->node( $p['item'] ), ); case 'bool': return array( 'op' => $p['value'] ? 'all' : 'any', 'items' => array(), ); case 'pattern': ++$this->patterns; $def = $this->registry->get( $p['cond'] ); return array( 'cond' => $p['cond'], 'args' => $p['args'], 'risk' => $def ? $def['risk'] : 'audience', 'pattern' => $p['pattern'], ); case 'call': if ( 'is_tree' === strtolower( $p['name'] ) && 1 === count( $p['args'] ) && is_array( $p['args'][0] ) ) { // The Codex is_tree() compares ancestors with ==, which never matches a // list, so 5.10.4 only ever matched the listed pages themselves. $this->notes['is_tree_list_matches_listed_pages_only'] = true; $p['name'] = 'is_page'; } $def = $this->registry->get( $p['name'] ); if ( null === $def ) { return $this->unknown( $p['raw'], 'unknown_function' ); } $ok = $this->registry->validate_args( $p['name'], $p['args'] ); if ( 'bad_arguments' === $ok ) { $trimmed = self::php_ignored_extras( $p['name'], $p['args'] ); if ( null !== $trimmed && true === $this->registry->validate_args( $p['name'], $trimmed ) ) { // PHP drops extra arguments to a userland function, so 5.10.4 ignored them too. $this->notes['extra_arguments_ignored'] = true; $p['args'] = $trimmed; $ok = true; } } if ( true !== $ok ) { return $this->unknown( $p['raw'], 'too_large' === $ok ? 'too_large' : 'bad_arguments' ); } $node = array( 'cond' => $p['name'], 'risk' => $def['risk'], ); if ( ! empty( $p['args'] ) ) { $node['args'] = $p['args']; } return $node; case 'foreign': default: $why = isset( $p['why'] ) ? $p['why'] : 'unsupported_syntax'; if ( 'bad_arguments' === $why && isset( $p['name'] ) && null === $this->registry->get( $p['name'] ) ) { $why = 'unknown_function'; } return $this->unknown( isset( $p['raw'] ) ? $p['raw'] : '', $why ); } } /** * Arguments as PHP would pass them when a userland function gets more * than it declares (the extras are silently dropped), or null. * * @param string $name Function name. * @param array $args Literal arguments. * @return array|null */ private static function php_ignored_extras( $name, array $args ) { if ( ! function_exists( $name ) ) { return null; } try { $ref = new \ReflectionFunction( $name ); } catch ( \ReflectionException $e ) { return null; } if ( $ref->isInternal() || $ref->isVariadic() ) { return null; // Internal functions throw on extras; variadics use them. } $max = $ref->getNumberOfParameters(); if ( count( $args ) <= $max ) { return null; } return array_slice( $args, 0, $max ); } /** * Make an unknown node. * * @param string $raw Original text. * @param string $reason Reason. * @return array */ private function unknown( $raw, $reason ) { ++$this->unknowns; $this->reasons[ $reason ] = true; return array( 'unknown' => $raw, 'reason' => $reason, ); } }