You are here

function _potx_find_log_calls in Translation template extractor 7.2

Same name and namespace in other branches
  1. 8 potx.inc \_potx_find_log_calls()
  2. 6.3 potx.inc \_potx_find_log_calls()
  3. 7.3 potx.inc \_potx_find_log_calls()

Detect all occurances of log() calls.

These sequences are searched for: log + "(" + ..anything (might be more tokens).. + "," + T_CONSTANT_ENCAPSED_STRING + whatever.

Parameters

$file: Name of file parsed.

$save_callback: Callback function used to save strings.

1 call to _potx_find_log_calls()
_potx_process_file in ./potx.inc
Process a file and put extracted information to the given parameters.

File

./potx.inc, line 987
Extraction API used by the web and command line interface.

Code

function _potx_find_log_calls($file, $save_callback) {
  global $_potx_tokens, $_potx_lookup;
  if (isset($_potx_lookup['log'])) {
    foreach ($_potx_lookup['log'] as $ti) {
      list($prev, $ctok, $par1) = array(
        $_potx_tokens[$ti - 1],
        $_potx_tokens[$ti],
        $_potx_tokens[$ti + 1],
      );
      list($type, $string, $line) = $ctok;
      if (is_array($prev) && $prev[0] == T_FUNCTION) {
        continue;
      }
      if ($par1 == "(") {

        // Eat up everything that is used as the first parameter
        $tn = $ti + 2;
        $depth = 0;
        while (!($_potx_tokens[$tn] == "," && $depth == 0)) {
          if ($_potx_tokens[$tn] == "(") {
            $depth++;
          }
          elseif ($_potx_tokens[$tn] == ")") {
            $depth--;
          }
          $tn++;
          if ($depth < 0) {

            // There is no second argument. This log() call was a false
            // positive, continue with the next one.
            continue 2;
          }
        }

        // Get further parameters
        list($comma, $message, $par2) = array(
          $_potx_tokens[$tn],
          $_potx_tokens[$tn + 1],
          $_potx_tokens[$tn + 2],
        );
        if ($comma == ',' && in_array($par2, array(
          ')',
          ',',
        )) && (is_array($message) && $message[0] == T_CONSTANT_ENCAPSED_STRING)) {

          // Context is not supported on watchdog().
          $save_callback(_potx_format_quoted_string($message[1]), POTX_CONTEXT_NONE, $file, $line);
        }
        else {

          // log() found, but the parameters are not correct.
          _potx_marker_error($file, $line, 'log', $ti, t('In log(), the log level should be followed by a literal string. There should be no variables, concatenation, constants or even a t() call there.'), 'http://drupal.org/node/323101');
        }
      }
    }
  }
}