PageRenderTime 25ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/requirements/reqImport.php

https://github.com/testlinkjp/testlink-japanese-localization
PHP | 378 lines | 252 code | 73 blank | 53 comment | 17 complexity | 68a44998002fa724308017a3de553b65 MD5 | raw file
  1. <?php
  2. /**
  3. * TestLink Open Source Project - http://testlink.sourceforge.net/
  4. * This script is distributed under the GNU General Public License 2 or later.
  5. *
  6. * @filesource reqImport.php
  7. * @package TestLink
  8. * @copyright 2008-2011, TestLink community
  9. * @author Martin Havlat
  10. * @link http://www.teamst.org/index.php
  11. *
  12. * Import ONLY requirements to a req specification.
  13. * Supported: simple CSV, Doors CSV, XML, DocBook
  14. *
  15. * @internal revisions
  16. *
  17. */
  18. require_once("../../config.inc.php");
  19. require_once("common.php");
  20. require_once('requirements.inc.php');
  21. require_once('xml.inc.php');
  22. require_once('csv.inc.php');
  23. testlinkInitPage($db);
  24. $templateCfg = templateConfiguration();
  25. $req_spec_mgr = new requirement_spec_mgr($db);
  26. $req_mgr = new requirement_mgr($db);
  27. $args = init_args($db);
  28. checkRights($db,$_SESSION['currentUser'],$args);
  29. $gui = initializeGui($db,$args,$_SESSION,$req_spec_mgr,$req_mgr);
  30. switch($args->doAction)
  31. {
  32. case 'uploadFile':
  33. $dummy = doExecuteImport($gui->fileName,$args,$req_spec_mgr,$req_mgr);
  34. $gui->items = $dummy->items;
  35. $gui->file_check = $dummy->file_check;
  36. $gui->importResult = $dummy->msg;
  37. $gui->refreshTree = $args->refreshTree && $dummy->refreshTree;
  38. break;
  39. }
  40. $smarty = new TLSmarty;
  41. $smarty->assign('gui',$gui);
  42. $smarty->display($templateCfg->template_dir . $templateCfg->default_template);
  43. /**
  44. * doExecuteImport
  45. *
  46. */
  47. function doExecuteImport($fileName,&$argsObj,&$reqSpecMgr,&$reqMgr)
  48. {
  49. $retval = new stdClass();
  50. $retval->items = array();
  51. $retval->msg = '';
  52. $retval->file_check = array('status_ok' => 1, 'msg' => 'ok');
  53. $retval->refreshTree = true;
  54. $context = new stdClass();
  55. $context->tproject_id = $argsObj->tproject_id;
  56. $context->req_spec_id = $argsObj->req_spec_id;
  57. $context->user_id = $argsObj->user_id;
  58. $context->importType = $argsObj->importType;
  59. $context->scope = $argsObj->scope;
  60. $opts = array();
  61. $opts['skipFrozenReq'] = ($argsObj->skip_frozen_req ? true : false);
  62. $opts['hitCriteria'] = $argsObj->hitCriteria;
  63. $opts['actionOnHit'] = $argsObj->actionOnHit;
  64. // manage file upload process
  65. $file_size_limit = config_get('import_file_max_size_bytes');
  66. $source = isset($_FILES['uploadedFile']['tmp_name']) ? $_FILES['uploadedFile']['tmp_name'] : null;
  67. $check = checkUploadOperation($_FILES,$file_size_limit);
  68. if($check['status_ok'])
  69. {
  70. if (move_uploaded_file($source, $fileName))
  71. {
  72. if( strcasecmp($argsObj->importType,'XML') == 0 )
  73. {
  74. $retval->file_check['status_ok']=!(($xml=simplexml_load_file($fileName)) === FALSE);
  75. }
  76. }
  77. }
  78. else
  79. {
  80. $retval->file_check=array('status_ok' => 0, 'msg' => $check['msg']);
  81. }
  82. // ----------------------------------------------------------------------------------------------
  83. if($retval->file_check['status_ok'])
  84. {
  85. $import_ok = true;
  86. if($argsObj->importType == 'XML')
  87. {
  88. try
  89. {
  90. $retval->items = doReqImportFromXML($reqSpecMgr,$reqMgr,$xml,$context,$opts);
  91. }
  92. catch (Exception $e)
  93. {
  94. $import_ok = false;
  95. $retval->items = null;
  96. $retval->msg = $e->getMessage();
  97. $retval->refreshTree=false;
  98. }
  99. }
  100. else
  101. {
  102. $retval->items = doReqImportOther($reqMgr,$fileName,$context,$opts);
  103. }
  104. unlink($fileName);
  105. if($import_ok)
  106. {
  107. $retval->msg = lang_get('req_import_finished');
  108. }
  109. }
  110. return $retval;
  111. }
  112. /*
  113. function:
  114. args :
  115. returns:
  116. */
  117. function init_args(&$dbHandler)
  118. {
  119. $args = new stdClass();
  120. $request = strings_stripSlashes($_REQUEST);
  121. $key='actionOnHit';
  122. $args->$key = isset($_REQUEST[$key]) ? $_REQUEST[$key] : 'update_last_version';
  123. $key='hitCriteria';
  124. $args->$key = isset($_REQUEST[$key]) ? $_REQUEST[$key] : 'docid';
  125. $args->req_spec_id = isset($request['req_spec_id']) ? $request['req_spec_id'] : null;
  126. $args->importType = isset($request['importType']) ? $request['importType'] : null;
  127. $args->emptyScope = isset($request['noEmpty']) ? $request['noEmpty'] : null;
  128. $args->conflictSolution = isset($request['conflicts']) ? $request['conflicts'] : null;
  129. $args->bUpload = isset($request['uploadFile']) ? 1 : 0;
  130. // useRecursion: used when you want to work on test project or req. spec
  131. $args->useRecursion = isset($request['useRecursion']) ? 1 : 0;
  132. $args->skip_frozen_req = isset($request['skip_frozen_req']) ? 1 : 0;
  133. $args->doAction='askFileName';
  134. $action_keys = array('uploadFile','executeImport');
  135. foreach($action_keys as $action)
  136. {
  137. if( isset($request[$action]) )
  138. {
  139. $args->doAction=$action;
  140. break;
  141. }
  142. }
  143. $args->achecked_req = isset($request['achecked_req']) ? $request['achecked_req'] : null;
  144. $args->tproject_name = '';
  145. $args->tproject_id = isset($_REQUEST['tproject_id']) ? intval($_REQUEST['tproject_id']) : 0;
  146. if($args->tproject_id > 0)
  147. {
  148. $treeMgr = new tree($dbHandler);
  149. $dummy = $treeMgr->get_node_hierarchy_info($args->tproject_id);
  150. $args->tproject_name = $dummy['name'];
  151. }
  152. $args->user_id = isset($_SESSION['userID']) ? $_SESSION['userID'] : 0;
  153. $args->scope = isset($_REQUEST['scope']) ? $_REQUEST['scope'] : 'items';
  154. // BUGID 3761: requirement tree refresh after requirement import
  155. $args->refreshTree = isset($_SESSION['setting_refresh_tree_on_action']) ?
  156. $_SESSION['setting_refresh_tree_on_action'] : 0;
  157. return $args;
  158. }
  159. /**
  160. * initializeGui()
  161. * create object that will be used by Smarty template
  162. *
  163. */
  164. function initializeGui(&$dbHandler,&$argsObj,$session,&$reqSpecMgr,&$reqMgr)
  165. {
  166. $gui=new stdClass();
  167. $gui->tproject_id = $argsObj->tproject_id;
  168. $gui->file_check = array('status_ok' => 1, 'msg' => 'ok');
  169. $gui->items=null;
  170. $gui->try_upload = $argsObj->bUpload;
  171. $gui->importResult = null;
  172. $gui->refreshTree = false;
  173. $gui->doAction=$argsObj->doAction;
  174. $gui->scope = $argsObj->scope;
  175. $gui->req_spec = null;
  176. $gui->req_spec_id = $argsObj->req_spec_id;
  177. $gui->hitCriteria = $argsObj->hitCriteria;
  178. $gui->actionOnHit = $argsObj->actionOnHit;
  179. switch($gui->scope)
  180. {
  181. case 'tree':
  182. $gui->main_descr = sprintf(lang_get('tproject_import_req_spec'),$argsObj->tproject_name);
  183. $gui->importTypes = $reqSpecMgr->get_import_file_types();
  184. break;
  185. case 'branch':
  186. $gui->req_spec = $reqSpecMgr->get_by_id($argsObj->req_spec_id);
  187. $gui->main_descr = sprintf(lang_get('reqspec_import_req_spec'),$gui->req_spec['title']);
  188. $gui->importTypes = $reqSpecMgr->get_import_file_types();
  189. break;
  190. case 'items':
  191. $gui->req_spec = $reqSpecMgr->get_by_id($argsObj->req_spec_id);
  192. $gui->main_descr = sprintf(lang_get('reqspec_import_requirements'),$gui->req_spec['title']);
  193. $gui->importTypes = $reqMgr->get_import_file_types();
  194. break;
  195. }
  196. $gui->importType = $argsObj->importType;
  197. $gui->fileName = TL_TEMP_PATH . "importReq-" . session_id() . ".tmp";
  198. $gui->importFileGui = new stdClass();
  199. $gui->importFileGui->importTypes = $gui->importTypes;
  200. $gui->importFileGui->importType = $argsObj->importType;
  201. $gui->importFileGui->maxFileSizeBytes=config_get('import_file_max_size_bytes');
  202. $maxFileSizeKB=round(strval($gui->importFileGui->maxFileSizeBytes)/1024);
  203. $gui->importFileGui->fileSizeLimitMsg=sprintf(lang_get('max_file_size_is'), $maxFileSizeKB . ' KB ');
  204. $gui->importFileGui->skip_frozen_req_checked = $argsObj->skip_frozen_req ? ' checked="checked" ' : '';
  205. $gui->importFileGui->return_to_url=$session['basehref'];
  206. if( is_null($argsObj->req_spec_id) )
  207. {
  208. $gui->importFileGui->return_to_url .= "lib/project/project_req_spec_mgmt.php?tproject_id=$argsObj->tproject_id" .
  209. "&id=$argsObj->tproject_id";
  210. }
  211. else
  212. {
  213. $gui->importFileGui->return_to_url .= "lib/requirements/reqSpecView.php?tproject_id=$argsObj->tproject_id" .
  214. "&req_spec_id=$argsObj->req_spec_id";
  215. }
  216. $gui->actionOptions=array('update_last_version' => lang_get('update_last_requirement_version'),
  217. 'create_new_version' => lang_get('create_new_requirement_version'));
  218. $gui->hitOptions=array('docid' => lang_get('same_docid'),'title' => lang_get('same_title'));
  219. $gui->duplicate_criteria_verbose = lang_get('duplicate_req_criteria');
  220. return $gui;
  221. }
  222. /**
  223. * checkRights
  224. *
  225. */
  226. function checkRights(&$db,&$userObj,$argsObj)
  227. {
  228. $env['tproject_id'] = isset($argsObj->tproject_id) ? $argsObj->tproject_id : 0;
  229. $env['tplan_id'] = isset($argsObj->tplan_id) ? $argsObj->tplan_id : 0;
  230. checkSecurityClearance($db,$userObj,$env,array('mgt_view_req','mgt_modify_req'),'and');
  231. }
  232. /**
  233. *
  234. *
  235. */
  236. function doReqImportFromXML(&$reqSpecMgr,&$reqMgr,&$simpleXMLObj,$importContext,$importOptions)
  237. {
  238. $items = array();
  239. $isReqSpec = property_exists($simpleXMLObj,'req_spec');
  240. // check to understand if user has provided an XML that is the requested for operation
  241. // required.
  242. $doIt = true;
  243. switch($importContext->scope)
  244. {
  245. case 'tree':
  246. case 'branch':
  247. $doIt = $isReqSpec;
  248. break;
  249. default:
  250. $doIt = !$isReqSpec;
  251. break;
  252. }
  253. if(!$doIt)
  254. {
  255. echo 'oo';
  256. throw new Exception(lang_get('bad_file_format'));
  257. echo 'ff';
  258. die();
  259. // return null; // >>>----> Brute Force exit
  260. }
  261. // OK go ahead
  262. if($isReqSpec)
  263. {
  264. foreach($simpleXMLObj->req_spec as $xkm)
  265. {
  266. $dummy = $reqSpecMgr->createFromXML($xkm,$importContext->tproject_id,$importContext->req_spec_id,
  267. $importContext->user_id,null,$importOptions);
  268. $items = array_merge($items,$dummy);
  269. }
  270. }
  271. else
  272. {
  273. $loop2do = count($simpleXMLObj->requirement);
  274. for($kdx=0; $kdx < $loop2do; $kdx++)
  275. {
  276. $dummy = $reqMgr->createFromXML($simpleXMLObj->requirement[$kdx],$importContext->tproject_id,
  277. $importContext->req_spec_id,$importContext->user_id,null,$importOptions);
  278. $items = array_merge($items,$dummy);
  279. }
  280. }
  281. return $items;
  282. }
  283. /**
  284. *
  285. *
  286. */
  287. function doReqImportOther(&$reqMgr,$fileName,$importContext,$importOptions)
  288. {
  289. $reqSet = loadImportedReq($fileName, $importContext->importType);
  290. $items = array();
  291. if( ($loop2do=count($reqSet)) )
  292. {
  293. for($kdx=0; $kdx < $loop2do; $kdx++)
  294. {
  295. $dummy = $reqMgr->createFromMap($reqSet[$kdx],$importContext->tproject_id,$importContext->req_spec_id,
  296. $importContext->user_id,null,$importOptions);
  297. $items = array_merge($items,$dummy);
  298. }
  299. }
  300. return $items;
  301. }
  302. ?>