Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
initViewPrivileges.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * initViewPrivileges
8  *
9  * @author Anakeen
10  * @version $Id: freedom_import.php,v 1.9 2008/11/13 16:49:16 eric Exp $
11  * @package FDL
12  * @subpackage WSH
13  */
14 
15 global $action;
16 
17 include_once ("FDL/Class.Doc.php");
18 include_once ("FDL/Class.SearchDoc.php");
19 
20 $usage = new ApiUsage();
21 $usage->setDefinitionText("Init view privilege ");
22 // --reset-account
23 $accountOnly = $usage->addOptionalParameter("reset-account", "reset account members of", array(
24  "yes",
25  "no"
26 ) , "no");
27 $accountOnly = ($accountOnly == "yes");
28 // --famid=<id>
29 $famid = $usage->addHiddenParameter("famid", "Sub-process a single family");
30 if (!is_string($famid)) {
31  $famid = '';
32 }
33 // --progress=<str>
34 $progress = $usage->addHiddenParameter("progress", "Parent process progress");
35 if (!is_string($progress)) {
36  $progress = '';
37 }
38 // --experimental-parallel=<number_of_concurrent_jobs>
39 $parallel = $usage->addHiddenParameter("experimental-parallel", "[EXPERIMENTAL] Run sub-processes in parallel. This parameter sets the number of concurrent jobs.");
40 if (!is_string($parallel)) {
41  $parallel = '';
42 }
43 $usage->verify();
44 
45 if ($accountOnly) {
46  recomputeAccounts($action);
47 } else {
48  if ($famid !== '') {
49  initViewSingleFamily($action, $famid, $progress);
50  } else {
51  initViewAllFamily($action, $parallel);
52  }
53 }
54 
55 function recomputeAccounts(Action & $action)
56 {
57  if (($err = simpleQuery($action->dbaccess, "SELECT * FROM users ORDER BY id", $tusers)) !== '') {
58  $action->exitError(sprintf("Query error: %s", $err));
59  return false;
60  }
61  $card = count($tusers);
62  printf("[+] %d user privilege to update.\n", $card);
63  $u = new Account($action->dbaccess);
64  $count = count($tusers);
65  $i = 1;
66  $pom = (new \Dcp\ConsoleProgressOMeter())->setInteractive(false)->setMax($count)->setTimeInterval(10)->start();
67  foreach ($tusers as $tu) {
68  printf("%d) %s \n", $card, $tu["login"]);
69  $u->affect($tu);
70  $u->updateMemberOf();
71  $card--;
72  $pom->progress($i++);
73  }
74  $pom->finish();
75  printf("[+] Done.\n");
76  return true;
77 }
78 /**
79  * Process a single family (used when running in a sub-process)
80  *
81  * @param Action $action
82  * @param $famid
83  * @param $progress
84  * @return bool
85  * @throws \Dcp\Core\Exception
86  */
87 function initViewSingleFamily(Action & $action, $famid, $progress)
88 {
89  if ($famid != - 1) {
90  $fam = new_Doc($action->dbaccess, $famid);
91  if (!is_object($fam) || !$fam->isAlive()) {
92  $action->exitError(sprintf("Could not find family with id or logical name '%s'.", $famid));
93  }
94  $famid = $fam->name;
95  }
96  /* Count number of documents in family */
97  $s = new \SearchDoc($action->dbaccess, $famid);
98  $s->only = true;
99  $s->addFilter("views IS NULL");
100  $s->addFilter("profid > 0");
101  $s->setObjectReturn();
102  $s->setOrder('id');
103  $s->latest = false;
104  $s->search();
105  if (($err = $s->searchError()) !== '') {
106  $action->exitError(sprintf("Error: search error on family '%s': %s", $famid, $err));
107  return false;
108  }
109  $count = $s->count();
110  /*
111  * Process each documents in groups of 1000 documents. The documents are processed ordered by increasing id, so the
112  * last document's id of the group is memorized in $lastId in order to select the next 1000 documents to process.
113  */
114  printf("[+] (%s) Processing %d document%s from family '%s'...\n", $progress, $count, (($count == 1) ? '' : 's') , $famid);
115  $processTitle = sprintf('initViewPrivileges (family %s %s)', $progress, $famid);
116  $pom = (new \Dcp\ConsoleProgressOMeter())->setPrefix($processTitle)->setInteractive(false)->setMax($count)->setTimeInterval(10)->setUpdateProcessTitle($processTitle)->start();
117  $i = 1;
118  $lastId = 0;
119  $subcount = $count;
120  while ($subcount > 0) {
121  $s = new \SearchDoc($action->dbaccess, $famid);
122  $s->setStart(0);
123  $s->setSlice(1000);
124  $s->only = true;
125  $s->addFilter("views IS NULL");
126  $s->addFilter("profid > 0");
127  $s->addFilter(sprintf("id > %s", pg_escape_literal($lastId)));
128  $s->setObjectReturn();
129  $s->setOrder('id');
130  $s->latest = false;
131  $s->search();
132  if (($err = $s->searchError()) !== '') {
133  $action->exitError(sprintf("Error: search error on family '%s': %s", $famid, $err));
134  return false;
135  }
136  $subcount = $s->count();
137  while ($doc = $s->getNextDoc()) {
138  $doc->setViewProfil();
139  $lastId = $doc->id;
140  $pom->progress($i++);
141  }
142  }
143  $pom->finish();
144  printf("[+] Done.\n");
145  return true;
146 }
147 /**
148  * Process the family in a sub-process
149  *
150  * @param $famid
151  * @param $progress
152  * @return int
153  */
154 function runSubProcessFam($famid, $progress)
155 {
156  $wsh = getWshPath();
157  if ($famid == '') {
158  /* Prevent end-less recursive execution */
159  return 0;
160  }
161  $cmd = sprintf("php %s --api=initViewPrivileges --famid=%s --progress=%s", escapeshellarg($wsh) , escapeshellarg($famid) , escapeshellarg($progress));
162  passthru($cmd, $ret);
163  return $ret;
164 }
165 /**
166  * Process families sequentially
167  *
168  * @param Action $action
169  * @param $famIdList
170  * @throws \Dcp\Core\Exception
171  */
172 function sequentialSubProcessFam(Action & $action, $famIdList)
173 {
174  $count = count($famIdList);
175  $i = 1;
176  foreach ($famIdList as $famId) {
177  $ret = runSubProcessFam($famId, sprintf("%s/%s", $i, $count));
178  if ($ret !== 0) {
179  $action->exitError(sprintf("Error processing family '%s': sub-process ended with exit code %d", $famId, $ret));
180  }
181  $i++;
182  }
183 }
184 /**
185  * Process families in parallel
186  *
187  * @param Action $action
188  * @param $famIdList
189  * @param $parallel
190  * @throws \Dcp\Core\Exception
191  */
192 function parallelSubProcessFam(Action & $action, $famIdList, $parallel)
193 {
194  $wsh = getWshPath();
195  $count = count($famIdList);
196  if (($jobsFile = tempnam(getTmpDir('') , 'initViewPrivileges')) === false) {
197  $action->exitError(sprintf("Error: could not create temporary jobs file!\n"));
198  }
199  $jobs = array();
200  $i = 1;
201  foreach ($famIdList as $famId) {
202  $jobs[] = sprintf("%s --api=initViewPrivileges --famid=%s --progress=%s", escapeshellarg($wsh) , escapeshellarg($famId) , escapeshellarg(sprintf("%s/%s", $i, $count)));
203  $i++;
204  }
205  if (file_put_contents($jobsFile, join("\n", $jobs)) === false) {
206  $action->exitError(sprintf("Error: error writing content to '%s'!\n", $jobsFile));
207  }
208  $cmd = sprintf("%s/programs/parallel -j %s -f %s", escapeshellarg(DEFAULT_PUBDIR) , escapeshellarg($parallel) , escapeshellarg($jobsFile));
209  passthru($cmd, $ret);
210  unlink($jobsFile);
211 }
212 /**
213  * Process all families
214  *
215  * @param Action $action
216  * @param $parallel
217  * @return bool
218  * @throws \Dcp\Core\Exception
219  * @throws \Dcp\Db\Exception
220  * @throws \Dcp\Exception
221  */
222 function initViewAllFamily(Action & $action, $parallel)
223 {
224  if (($err = simpleQuery($action->dbaccess, "SELECT * FROM users WHERE memberof IS NULL", $tusers)) !== '') {
225  $action->exitError(sprintf("Query error: %s", $err));
226  return false;
227  }
228  $card = count($tusers);
229  printf("[+] %d user privilege to update.\n", $card);
230  $i = 1;
231  $pom = (new \Dcp\ConsoleProgressOMeter())->setInteractive(false)->setMax($card)->setTimeInterval(10)->start();
232  $u = new Account($action->dbaccess);
233  foreach ($tusers as $tu) {
234  printf("%d) %s \n", $card, $tu["login"]);
235  $u->affect($tu);
236  $u->updateMemberOf();
237  $card--;
238  $pom->progress($i++);
239  }
240  $pom->finish();
241  printf("[+] Done.\n");
242  /*
243  * Get list of PDOC (3) family and it's childs
244  */
245  if (($err = simpleQuery($action->dbaccess, "WITH RECURSIVE child_fams(id, fromid) AS ( SELECT id, fromid FROM docfam WHERE id = 3 UNION SELECT docfam.id, docfam.fromid FROM docfam, child_fams WHERE child_fams.id = docfam.fromid ) SELECT id FROM child_fams ORDER BY id", $rows)) !== '') {
246  $action->exitError(sprintf("Query error: %s", $err));
247  }
248  $pdocs = array();
249  foreach ($rows as $row) {
250  $pdocs[] = $row['id'];
251  }
252  /*
253  * Sequentially process PDOC and it's childs first
254  */
255  $i = 1;
256  foreach ($pdocs as $famid) {
257  $ret = runSubProcessFam($famid, sprintf("%d/%d", $i, count($pdocs)));
258  if ($ret !== 0) {
259  $action->exitError(sprintf("Error processing family '%s': sub-process ended with exit code %d", $famid, $ret));
260  }
261  $i++;
262  }
263  /*
264  * Get remaining families excluding PDOC and it's childs
265  */
266  if (($err = simpleQuery($action->dbaccess, sprintf("SELECT id, name FROM docfam WHERE id NOT IN (%s) ORDER BY id", join(', ', $pdocs)) , $rows)) !== '') {
267  $action->exitError(sprintf("Query error: %s", $err));
268  }
269  $famIdList = array();
270  foreach ($rows as $row) {
271  $famIdList[] = $row['id'];
272  }
273  /*
274  * Process docfam itself
275  */
276  $famIdList[] = - 1;
277  if ($parallel === '') {
278  sequentialSubProcessFam($action, $famIdList);
279  } else {
280  parallelSubProcessFam($action, $famIdList, $parallel);
281  }
282  printf("[+] Update empty profil...\n");
283  if (($err = simpleQuery($action->dbaccess, "UPDATE doc SET views = '{0}' WHERE (profid=0 OR profid IS NULL)")) !== '') {
284  $action->exitError(sprintf("Query error: %s", $err));
285  }
286  printf("[+] Finished profiling.\n");
287  return true;
288 }
289 /**
290  * Return the absolute path to the `wsh.php` script.
291  */
292 function getWshPath()
293 {
294  return sprintf("%s/wsh.php", DEFAULT_PUBDIR);
295 }
if(!is_string($progress)) $parallel
global $action
$ret
if($famId) $s
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
if(!is_string($famid)) $progress
exitError($texterr, $exit=true, $code="")
foreach($argv as $arg) $cmd
$rows
Definition: Api/ods2csv.php:23
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
new_Doc($dbaccess, $id= '', $latest=false)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
Verify arguments for wsh programs.
← centre documentaire © anakeen