DeployerGuide/Others/DocsGenerator: DocsGenerator.inc

Plik DocsGenerator.inc, 4.0 KB (dodany przez TS, 12 years temu)
xx
Line 
1<?php
2
3set_time_limit(-1);
4chdir('../apps/edokumenty/');
5
6$args = array();
7if (isset($_SERVER['argv'])) {
8    $args = $_SERVER['argv'];
9    array_shift($args);
10}
11
12require_once('./../../framework/lib/util/Translator/Translator.inc');
13
14define('SCRIPT_PATH', '.');
15define('ASYNC_SERVICE', false);
16require_once('./config.inc');
17require_once('../../framework/globalVar.inc');
18require_once('./localVar.inc');
19require_once(FUNC_PATH.'GetUsrAcc.inc');
20require_once(LIB_PATH.'system.inc');
21require_once(LIB_PATH.'db/PgManager.inc');
22require_once(LIB_PATH.'util/Debug.inc');
23require_once(LIB_PATH.'db/Bean.inc');
24require_once('./classes/eDokumentyApi/EDokApi.inc');
25require_once(MOD_PATH.'ADocuments/ADocumentsService.inc');
26
27if(!defined("STDIN")) {
28    define("STDIN", "fopen('php://stdin','r')");
29}
30
31UserRights::disable();
32
33$db = new PgManager(DB_NAME);
34echo "\n";
35
36$ent_id = $db->select('podmioty', 'id_pod', 'symbol=\''.ENTITY.'\'', 'id_pod');
37
38$decreeArr = array();
39$out = $db->select('organization_units', 'orunid', 'ent_id = '.$ent_id.' AND is_del IS NOT TRUE', FALSE, PGSQL_ASSOC);
40if ((is_array($out)) AND (!empty($out))) {
41    foreach ($out as $oo) {
42        $decreeArr[] = $oo['orunid'];
43    }
44}
45
46SysContext::$ent_id = $ent_id;
47$_SESSION['curr_ent_id'] = $ent_id;
48
49SysContext::$usr_info = array(
50    'usr_id' => 1,
51    'orunid' => array(-3), // EXTSYS_SERVICE_ID = -3
52    'lasnam' => Translator::translate('Wypełniacz'),
53    'firnam' => 'Usługa',
54    'usrnam' => 'Usługa Wypełniacz',
55    'fullnm' => 'Usługa Wypełniacz'
56);
57
58// ile dokumentów ma wygenerować
59$maxDocs = 10;
60
61// po ilu wygenerowanych dokumentach na rozpoczynać transakcje (commit/begin)
62$tCommit = 500;
63
64// maksymalna ilość słów w opisie dokumentu
65$maxDocWords = 3;
66
67$edokApi = new EDokApi();
68
69$dictionary = './../../contrib/slowa-new.txt';
70
71$maxDicStrLength = prepareDictionary($dictionary);
72$dictionary = $dictionary.'_com';
73
74clearstatcache();
75
76$lineLimit = filesize($dictionary) / $maxDicStrLength;
77
78function prepareDictionary($dictionary) {
79
80    $handle = fopen($dictionary, 'r');
81
82    $dictionaryCom = $dictionary.'_com';
83    $crt = FALSE;
84    if ((!file_exists($dictionaryCom)) OR (filesize($dictionaryCom) < 20)) {
85        file_put_contents($dictionaryCom, NULL);
86        $crt = TRUE;
87    }
88
89    $maxStrLen = 0;
90    if ($handle) {
91
92        while (($buffer = fgets($handle, 4096)) !== FALSE) {
93            $l = strlen($buffer);
94            if ($maxStrLen < $l) {
95                $maxStrLen = $l;
96            }
97        }
98
99        $maxStrLen += 5;
100
101        fseek($handle, 0);
102
103        if ($crt) {
104            while (($buffer = fgets($handle, 4096)) !== FALSE) {
105                $text = str_pad(trim($buffer), $maxStrLen, '#', STR_PAD_RIGHT);
106                file_put_contents($dictionaryCom, $text, FILE_APPEND);
107            }
108        }
109
110        fclose($handle);
111
112    }
113
114    return $maxStrLen;
115
116}
117
118function getWords($dictionary, $lineLimit, $maxDicStrLength, $limit = 3) {
119
120    for ($i = 0; $i < $limit; $i++) {
121        $arr[] = rand(0, ($lineLimit-1));
122    }
123
124    $out = '';
125
126    $handle = fopen($dictionary, 'r');
127    if ($handle) {
128        foreach ($arr as $ss) {
129            fseek($handle, ($ss * $maxDicStrLength));
130            $out .= str_replace('#', '', fread($handle, $maxDicStrLength)).' ';
131
132        }
133    }
134
135    return trim($out);
136
137}
138
139$start = time();
140
141$db->begin();
142
143$srv = new ADocumentsService();
144
145for ($i = 0; $i < $maxDocs; $i++) {
146
147    $words = getWords($dictionary, $lineLimit, $maxDicStrLength, $maxDocWords);
148    $data = array(
149            'dscrpt' => $words,
150            'dctpid' => 1,
151            'target' => 2,
152            );
153
154    $to = $decreeArr[rand(0, count($decreeArr)-1)];
155
156    $orders = 'Instrukcje dla dokumentu '.$words;
157
158    $doc_id = $edokApi->createDocument($data);
159    if ($doc_id) {
160        $out = $srv->decree($doc_id, $to, array(), array(), FALSE, FALSE, $orders, TRUE, FALSE, FALSE);
161    }
162
163    if (($tCommit % ($i+1)) == 0) {
164        $db->commit();
165        $db->begin();
166    }
167
168}
169
170$db->commit();
171
172echo (time() - $start);
173
174
175?>