PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/crossref/import_project_symbols.php

http://github.com/facebook/phabricator
PHP | 153 lines | 116 code | 19 blank | 18 comment | 12 complexity | 10070fb61bb90b555663758a589e8e1e MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright 2011 Facebook, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. $root = dirname(dirname(dirname(__FILE__)));
  19. require_once $root.'/scripts/__init_script__.php';
  20. if ($argc !== 2) {
  21. echo phutil_console_format(
  22. "usage: import_project_symbols.php __project_name__ < __symbol_file__\n");
  23. exit(1);
  24. }
  25. $project_name = $argv[1];
  26. $project = id(new PhabricatorRepositoryArcanistProject())->loadOneWhere(
  27. 'name = %s',
  28. $project_name);
  29. if (!$project) {
  30. // TODO: Provide a less silly way to do this explicitly, or just do it right
  31. // here.
  32. echo "Project '{$project_name}' is unknown. Upload a diff to implicitly ".
  33. "create it.\n";
  34. exit(1);
  35. }
  36. echo "Parsing input from stdin...\n";
  37. $input = file_get_contents('php://stdin');
  38. $input = trim($input);
  39. $input = explode("\n", $input);
  40. $map = array();
  41. $symbols = array();
  42. foreach ($input as $key => $line) {
  43. $line_no = $key + 1;
  44. $matches = null;
  45. $ok = preg_match('/^([^ ]+) ([^ ]+) ([^ ]+) (\d+) (.*)$/', $line, $matches);
  46. if (!$ok) {
  47. throw new Exception(
  48. "Line #{$line_no} of input is invalid. Expected five space-delimited ".
  49. "fields: symbol name, symbol type, symbol language, line number, path. ".
  50. "For example:\n\n".
  51. "idx function php 13 /path/to/some/file.php\n\n".
  52. "Actual line was:\n\n".
  53. "{$line}");
  54. }
  55. list($all, $name, $type, $lang, $line_number, $path) = $matches;
  56. if (isset($map[$name][$type][$lang])) {
  57. $previous = $map[$name][$type][$lang] + 1;
  58. throw new Exception(
  59. "Line #{$line_no} of input is invalid. It specifies a duplicate symbol ".
  60. "(same name, language, and type) which has already been defined ".
  61. "elsewhere. You must preprocess the symbol list to remove duplicates ".
  62. "and choose exactly one master definition for each symbol. This symbol ".
  63. "was previously defined on line #{$previous}.\n\n".
  64. "Line #{$line_no}:\n".
  65. $line."\n\n".
  66. "Line #{$previous}:\n".
  67. $input[$previous - 1]);
  68. } else {
  69. $map[$name][$type][$lang] = $key;
  70. }
  71. if (strlen($name) > 128) {
  72. throw new Exception(
  73. "Symbol name '{$name}' defined on line #{$line_no} is too long, maximum ".
  74. "symbol name length is 128 characters.");
  75. }
  76. if (strlen($type) > 12) {
  77. throw new Exception(
  78. "Symbol type '{$type}' defined on line #{$line_no} is too long, maximum ".
  79. "symbol type length is 12 characters.");
  80. }
  81. if (strlen($lang) > 32) {
  82. throw new Exception(
  83. "Symbol language '{$lang}' defined on line #{$line_no} is too long, ".
  84. "maximum symbol language length is 32 characters.");
  85. }
  86. if (!strlen($path) || $path[0] != 0) {
  87. throw new Exception(
  88. "Path '{$path}' defined on line #{$line_no} is invalid. Paths should be ".
  89. "begin with '/' and specify a path from the root of the project, like ".
  90. "'/src/utils/utils.php'.");
  91. }
  92. $symbols[] = array(
  93. 'name' => $name,
  94. 'type' => $type,
  95. 'lang' => $lang,
  96. 'line' => $line_number,
  97. 'path' => $path,
  98. );
  99. }
  100. echo "Looking up path IDs...\n";
  101. $path_map = PhabricatorRepositoryCommitChangeParserWorker::lookupOrCreatePaths(
  102. ipull($symbols, 'path'));
  103. $symbol = new PhabricatorRepositorySymbol();
  104. $conn_w = $symbol->establishConnection('w');
  105. echo "Preparing queries...\n";
  106. $sql = array();
  107. foreach ($symbols as $dict) {
  108. $sql[] = qsprintf(
  109. $conn_w,
  110. '(%d, %s, %s, %s, %d, %d)',
  111. $project->getID(),
  112. $dict['name'],
  113. $dict['type'],
  114. $dict['lang'],
  115. $dict['line'],
  116. $path_map[$dict['path']]);
  117. }
  118. echo "Purging old symbols...\n";
  119. queryfx(
  120. $conn_w,
  121. 'DELETE FROM %T WHERE arcanistProjectID = %d',
  122. $symbol->getTableName(),
  123. $project->getID());
  124. echo "Loading ".number_format(count($sql))." symbols...\n";
  125. foreach (array_chunk($sql, 128) as $chunk) {
  126. queryfx(
  127. $conn_w,
  128. 'INSERT INTO %T
  129. (arcanistProjectID, symbolName, symbolType, symbolLanguage, lineNumber,
  130. pathID) VALUES %Q',
  131. $symbol->getTableName(),
  132. implode(', ', $chunk));
  133. }
  134. echo "Done.\n";