17 include_once (
"FDL/Class.Doc.php");
18 include_once (
"FDL/Class.SearchDoc.php");
21 $usage->setDefinitionText(
"Init view privilege ");
29 $famid =
$usage->addHiddenParameter(
"famid",
"Sub-process a single family");
39 $parallel =
$usage->addHiddenParameter(
"experimental-parallel",
"[EXPERIMENTAL] Run sub-processes in parallel. This parameter sets the number of concurrent jobs.");
57 if ((
$err =
simpleQuery($action->dbaccess,
"SELECT * FROM users ORDER BY id", $tusers)) !==
'') {
61 $card = count($tusers);
62 printf(
"[+] %d user privilege to update.\n",
$card);
63 $u =
new Account($action->dbaccess);
64 $count = count($tusers);
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"]);
75 printf(
"[+] Done.\n");
91 if (!is_object($fam) || !$fam->isAlive()) {
92 $action->
exitError(sprintf(
"Could not find family with id or logical name '%s'.",
$famid));
97 $s = new \SearchDoc($action->dbaccess,
$famid);
99 $s->addFilter(
"views IS NULL");
100 $s->addFilter(
"profid > 0");
101 $s->setObjectReturn();
105 if ((
$err =
$s->searchError()) !==
'') {
109 $count =
$s->count();
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();
120 while ($subcount > 0) {
121 $s = new \SearchDoc($action->dbaccess,
$famid);
125 $s->addFilter(
"views IS NULL");
126 $s->addFilter(
"profid > 0");
127 $s->addFilter(sprintf(
"id > %s", pg_escape_literal($lastId)));
128 $s->setObjectReturn();
132 if ((
$err =
$s->searchError()) !==
'') {
136 $subcount =
$s->count();
137 while (
$doc =
$s->getNextDoc()) {
138 $doc->setViewProfil();
140 $pom->progress($i++);
144 printf(
"[+] Done.\n");
161 $cmd = sprintf(
"php %s --api=initViewPrivileges --famid=%s --progress=%s", escapeshellarg(
$wsh) , escapeshellarg(
$famid) , escapeshellarg(
$progress));
172 function sequentialSubProcessFam(
Action & $action, $famIdList)
174 $count = count($famIdList);
176 foreach ($famIdList as
$famId) {
177 $ret = runSubProcessFam($famId, sprintf(
"%s/%s", $i, $count));
179 $action->
exitError(sprintf(
"Error processing family '%s': sub-process ended with exit code %d", $famId,
$ret));
192 function parallelSubProcessFam(
Action & $action, $famIdList,
$parallel)
195 $count = count($famIdList);
196 if (($jobsFile = tempnam(
getTmpDir(
'') ,
'initViewPrivileges')) ===
false) {
197 $action->
exitError(sprintf(
"Error: could not create temporary jobs file!\n"));
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)));
205 if (file_put_contents($jobsFile, join(
"\n", $jobs)) ===
false) {
206 $action->
exitError(sprintf(
"Error: error writing content to '%s'!\n", $jobsFile));
208 $cmd = sprintf(
"%s/programs/parallel -j %s -f %s", escapeshellarg(
DEFAULT_PUBDIR) , escapeshellarg(
$parallel) , escapeshellarg($jobsFile));
224 if ((
$err =
simpleQuery($action->dbaccess,
"SELECT * FROM users WHERE memberof IS NULL", $tusers)) !==
'') {
228 $card = count($tusers);
229 printf(
"[+] %d user privilege to update.\n",
$card);
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"]);
236 $u->updateMemberOf();
238 $pom->progress($i++);
241 printf(
"[+] Done.\n");
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)) !==
'') {
249 foreach (
$rows as $row) {
250 $pdocs[] = $row[
'id'];
256 foreach ($pdocs as
$famid) {
257 $ret = runSubProcessFam($famid, sprintf(
"%d/%d", $i, count($pdocs)));
259 $action->
exitError(sprintf(
"Error processing family '%s': sub-process ended with exit code %d", $famid,
$ret));
266 if ((
$err =
simpleQuery($action->dbaccess, sprintf(
"SELECT id, name FROM docfam WHERE id NOT IN (%s) ORDER BY id", join(
', ', $pdocs)) ,
$rows)) !==
'') {
269 $famIdList = array();
270 foreach (
$rows as $row) {
271 $famIdList[] = $row[
'id'];
278 sequentialSubProcessFam($action, $famIdList);
280 parallelSubProcessFam($action, $famIdList,
$parallel);
282 printf(
"[+] Update empty profil...\n");
283 if ((
$err =
simpleQuery($action->dbaccess,
"UPDATE doc SET views = '{0}' WHERE (profid=0 OR profid IS NULL)")) !==
'') {
286 printf(
"[+] Finished profiling.\n");
292 function getWshPath()
if(!is_string($progress)) $parallel
if(!is_string($famid)) $progress
exitError($texterr, $exit=true, $code="")
foreach($argv as $arg) $cmd
new_Doc($dbaccess, $id= '', $latest=false)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
if($file) if($subject==""&&$file) if($subject=="") $err
Verify arguments for wsh programs.