1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
|
<?php
/**
* This file reads and interpretes the CLI arguments
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Jeroen Roos
* @package Zoph
*/
namespace cli;
use log;
use album;
use category;
use group;
use person;
use place;
use user;
use conf\conf;
use userUnknownAccessRightException;
use groupNotFoundException;
use set\notFoundException as setNotFoundException;
use set\model as set;
/**
* This class reads and interpretes CLI arguments
*/
class arguments {
/** Contains the non-interpreted arguments */
private $arguments = array();
/** Contains the interpreted arguments, before lookup */
private $processed = array();
/** Contains the interpreted arguments, after lookup */
private $vars = array();
/** Default command */
public static $command="import";
/**
* Create a new instance of the class.
* This construct also takes care of interpreting and looking up of the
* values
* @param array CLI arguments
*/
public function __construct(array $argv) {
// We don't care about the name of the script
array_shift($argv);
$this->arguments=$argv;
if (count($this->arguments)===0) {
$this->arguments[]="--help";
}
$this->process();
$this->lookup();
}
/**
* Process the arguments
*/
private function process() {
$argv=$this->arguments;
$args=&$this->processed;
$args["albums"]=array();
$args["categories"]=array();
$args["files"]=array();
$args["people"]=array();
$args["photographer"]=array();
$args["location"]=array();
$args["sets"]=array();
$args["instance"]="";
$args["fields"]=array();
$args["path"]="";
$args["dirpattern"]="";
/* For new albums, categories, places, people */
$parent=0;
$args["palbum"]=array();
$args["pcat"]=array();
$args["pplace"]=array();
/*
Used short arguments: A C D F H I N P U V X a c d f g h i l n p r s t u v w
*/
$size=sizeof($argv);
for ($i=0; $i<$size; $i++) {
switch ($argv[$i]) {
case "":
break;
case "--instance":
case "-i":
$args["instance"]=$argv[++$i];
break;
case "--add-access":
case "--access":
$this->argsAddAccess(explode(",", $argv[++$i]));
break;
case "--revoke-access":
case "--remove-access":
case "--no-access":
$this->argsRemoveAccess(explode(",", $argv[++$i]));
break;
case "--admin":
$args["_admin"]=true;
$args["_no_admin"]=false;
break;
case "--no-admin":
$args["_admin"]=false;
$args["_no_admin"]=true;
break;
case "--albums":
case "--album":
case "-a":
$this->argsAlbums(explode(",", $argv[++$i]), $parent);
$parent=0;
break;
case "--category":
case "--categories":
case "-c":
$this->argsCategories(explode(",", $argv[++$i]), $parent);
$parent=0;
break;
case "--config":
case "-C":
static::$command="config";
$args["_configitem"]=$argv[++$i];
if (isset($argv[$i+1])) {
$args["_configvalue"]=$argv[++$i];
} else {
$args["_configdefault"]=true;
}
break;
case "--dumpconfig":
case "--dump-config":
static::$command="dumpconfig";
break;
case "-f":
$args["_force"]=true;
break;
case "--fields":
case "--field":
case "-F":
$args["fields"][]=$argv[++$i];
break;
case "--getconfig":
case "--get-config":
case "-g":
static::$command="getconfig";
$args["_getconfigitem"]=$argv[++$i];
break;
case "--groups":
case "--group":
case "-G":
$groups=explode(",", $argv[++$i]);
foreach ($groups as $group) {
$args["_groups"][]=strtolower(trim($group));
}
break;
case "--no-group":
case "--remove-group":
case "--remove-groups":
$groups=explode(",", $argv[++$i]);
foreach ($groups as $group) {
$args["_remove_groups"][]=strtolower(trim($group));
}
break;
case "--place":
case "--location":
case "-l":
// Multiple locations are possible when using --new
$locs=explode(",", $argv[++$i]);
foreach ($locs as $loc) {
$args["location"][]=trim($loc);
if (isset($parent)) {
$args["pplace"][]=trim($parent);
}
}
$parent=0;
break;
case "--people":
case "--persons":
case "--person":
case "-p":
$people=explode(",", $argv[++$i]);
foreach ($people as $person) {
$args["people"][]=trim($person);
}
break;
case "--photographer":
case "-P":
$args["photographer"]=$argv[++$i];
break;
case "--parent":
$parent=$argv[++$i];
break;
case "--password":
$args["_password"]=$argv[++$i];
break;
case "--sets":
case "--set":
case "-s":
$this->argsSets(explode(",", $argv[++$i]));
break;
case "--thumbs":
case "-t":
conf::set("import.cli.thumbs", true);
break;
case "--nothumbs":
case "--no-thumbs":
case "-n":
conf::set("import.cli.thumbs", false);
break;
case "--exif":
case "--EXIF":
conf::set("import.cli.exif", true);
break;
case "--no-exif":
case "--noEXIF":
case "--noexif":
case "--no-EXIF":
conf::set("import.cli.exif", false);
break;
case "--size":
conf::set("import.cli.size", true);
break;
case "--nosize":
case "--no-size":
conf::set("import.cli.size", false);
break;
case "--hash":
conf::set("import.cli.hash", true);
break;
case "--no-hash":
conf::set("import.cli.hash", false);
break;
case "--username":
case "--user-name":
$args["_username"]=$argv[++$i];
break;
case "--userid":
case "--user-id":
$args["_userid"]=(int) $argv[++$i];
break;
case "--update":
case "-u":
static::$command="update";
break;
case "--import":
case "-I":
static::$command="import";
break;
case "--new":
case "-N":
static::$command="new";
break;
case "--delete":
static::$command="delete";
break;
case "--user":
case "-U":
switch ($argv[++$i]) {
case "add":
static::$command="adduser";
break;
case "delete":
static::$command="deleteuser";
break;
case "show":
static::$command="showuser";
break;
case "update":
static::$command="updateuser";
break;
default:
throw new \cliNoUserCommandException(translate("The option --user must be followed by 'add', 'delete', 'show' or 'update' to indicate what you would like to do with a user"));
break;
}
case "--useIds":
case "--useids":
case "--use-ids":
case "--useid":
case "--use-id":
conf::set("import.cli.useids", true);
break;
case "--copy":
conf::set("import.cli.copy", true);
break;
case "--move":
conf::set("import.cli.copy", false);
break;
case "-A":
case "--autoadd":
case "--auto-add":
conf::set("import.cli.add.auto", true);
break;
case "-w":
case "--add-always":
case "--addalways":
conf::set("import.cli.add.always", true);
break;
case "-r":
case "--recursive":
conf::set("import.cli.recursive", true);
break;
case "--dateddirs":
case "--datedDirs":
case "--dated":
case "-d":
conf::set("import.dated", true);
break;
case "--hierarchical":
case "--hier":
case "-H":
conf::set("import.dated", true);
conf::set("import.dated.hier", true);
break;
case "--no-dateddirs":
case "--no-datedDirs":
case "--no-dated":
case "--nodateddirs":
case "--nodatedDirs":
case "--nodated":
conf::set("import.dated", false);
break;
case "--no-hierarchical":
case "--no-hier":
case "--nohierarchical":
case "--nohier":
conf::set("import.dated.hier", false);
break;
case "-D":
case "--path":
$args["path"]=$argv[++$i];
break;
case "--dirpattern":
$args["dirpattern"]=$argv[++$i];
break;
case "-V":
case "--version":
static::$command="version";
break;
case "-h":
case "--help":
static::$command="help";
break;
case "-X":
case "--xmp":
case "--XMP":
$args["_xmp"] = true;
break;
case "-v":
case "--verbose":
$verbose=conf::get("import.cli.verbose");
conf::set("import.cli.verbose", ++$verbose);
break;
default:
if (substr($argv[$i], 0, 1)=="-") {
echo "unknown argument: " . $argv[$i] . "\n";
exit(1);
} else {
$args["files"][]=$argv[$i];
}
break;
}
}
if (isset($args["fields"])) {
$newfields=array();
foreach ($args["fields"] as $f) {
$field=explode("=", $f);
$newfields[$field[0]]=$field[1];
}
$args["fields"]=$newfields;
}
if (conf::get("import.cli.useids") && static::$command=="import") {
static::$command="update";
}
}
private function argsAddAccess(array $access) {
foreach ($access as $acc) {
$this->processed["_add_access"][]=trim($acc);
}
}
private function argsRemoveAccess(array $access) {
foreach ($access as $acc) {
$this->processed["_remove_access"][]=trim($acc);
}
}
private function argsAlbums(array $albums, $parent=null) {
foreach ($albums as $album) {
$this->processed["albums"][]=trim($album);
if (isset($parent)) {
$this->processed["palbum"][]=trim($parent);
}
}
}
private function argsCategories(array $categories, $parent=null) {
foreach ($categories as $cat) {
$this->processed["categories"][]=trim($cat);
if (isset($parent)) {
$this->processed["pcat"][]=trim($parent);
}
}
}
private function argsSets(array $sets) {
foreach ($sets as $set) {
$this->processed["sets"][]=trim($set);
}
}
/**
* Looks up the given parameters in the database and gives back ids
*/
private function lookup() {
$args=$this->processed;
$vars=&$this->vars;
foreach ($args as $type => $arg) {
if (empty($arg) || empty($type)) {
continue;
}
log::msg($type . "\t->\t" . implode(",", (array) $arg), log::DEBUG, log::IMPORT);
switch ($type) {
case "albums":
$this->lookupAlbums($arg, $args["palbum"]);
break;
case "categories":
$this->lookupCategories($arg, $args["pcat"]);
break;
case "people":
$this->lookupPeople($arg);
break;
case "photographer":
$this->lookupPhotographer($arg);
break;
case "location":
$this->lookupLocations($arg, $args["pplace"]);
break;
case "sets":
$this->lookupSets($arg);
break;
case "path":
$vars["_path"]=$arg;
break;
case "dirpattern":
if (!preg_match("/^[aclpDP]+$/", $arg)) {
throw new \cliIllegalDirpatternException("Illegal characters in " .
"--dirpattern, allowed are: aclpDP");
} else {
$vars["_dirpattern"]=$arg;
}
break;
case "fields":
foreach ($arg as $field => $value) {
$vars[$field]=$value;
}
break;
case "_xmp":
$vars["_xmp"] = true;
break;
case "_admin":
$vars["_admin"] = (bool) $arg;
break;
case "_no_admin":
$vars["_no_admin"] = (bool) $arg;
break;
case "_force":
$vars["_force"] = (bool) $arg;
break;
case "_add_access":
case "_remove_access":
foreach ($arg as $right) {
if (!array_key_exists($right, user::getAccessRightsDescription())) {
throw new userUnknownAccessRightException(e($right) . " is not a known access right");
}
}
$vars[$type]=$arg;
break;
case "_groups":
case "_remove_groups":
foreach ($arg as $group) {
if (!group::exists($group)) {
throw new groupNotFoundException(e($group) . " is not a known group");
}
}
$vars[$type]=$arg;
break;
case "_password":
$vars["_password"]=$arg;
break;
case "_username":
case "_userid":
if (static::$command=="adduser" && $type == "_username") {
$vars["_username"]=$arg;
} else if ($type == "_username") {
$user = user::getByName($arg);
$vars["_userid"]=(int) $user->getId();
} else {
$vars["_userid"]=(int) $arg;
}
break;
case "_configitem":
case "_configvalue":
case "_configdefault":
case "_getconfigitem":
$vars[$type]=$arg;
break;
default:
log::msg($type . "\t->\t" . implode(",", (array) $arg), log::DEBUG, log::IMPORT);
}
}
}
/**
* Returns the list of files
*/
public function getFiles() {
return $this->processed["files"];
}
/**
* Returns an array of variables, with keys.
*/
public function getVars() {
return $this->vars;
}
private function lookupAlbums(array $albums, $parents) {
foreach ($albums as $name) {
if (static::$command=="new" ||
(conf::get("import.cli.add.auto") && !album::getByName($name))) {
$parent=array_shift($parents);
// this is a string comparison because the trim() in process() changes
// everything into a string...
if ($parent==="0") {
if (conf::get("import.cli.add.always")) {
$parentId=album::getRoot()->getId();
} else {
throw new \cliNoParentException("No parent for album " . $name);
}
} else {
$palbum=album::getByName($parent);
if ($palbum) {
$parentId=$palbum[0]->getId();
} else {
throw new \albumNotFoundException("Album not found: $parent");
}
}
$this->vars["_new_album"][]=array("parent" => $parentId, "name" => $name);
} else {
$album=album::getByName($name);
if ($album) {
$this->vars["_album_id"][]=$album[0]->getId();
} else {
throw new \albumNotFoundException("Album not found: $name");
}
}
}
}
private function lookupCategories(array $categories, $parents) {
foreach ($categories as $name) {
if (static::$command=="new" ||
(conf::get("import.cli.add.auto") && !category::getByName($name))) {
$parent=array_shift($parents);
// this is a string comparison because the trim() in process() changes
// everything into a string...
if ($parent==="0") {
if (conf::get("import.cli.add.always")) {
$parentId=category::getRoot()->getId();
} else {
throw new \cliNoParentException("No parent for category " . $name);
}
} else {
$pcat=category::getByName($parent);
if ($pcat) {
$parentId=$pcat[0]->getId();
} else {
throw new \categoryNotFoundException("Category not found: $parent");
}
}
$this->vars["_new_cat"][]=array("parent" => $parentId, "name" => $name);
} else {
$cat=category::getByName($name);
if ($cat) {
$this->vars["_category_id"][]=$cat[0]->getId();
} else {
throw new \categoryNotFoundException("Category not found: $name");
}
}
}
}
private function lookupPeople(array $people) {
foreach ($people as $name) {
if (static::$command=="new" || (conf::get("import.cli.add.auto") &&
!person::getByName($name))) {
$this->vars["_new_person"][]=$name;
} else {
$person=person::getByName($name);
if ($person) {
$personId=$person[0]->getId();
$this->vars["_person_id"][]=$personId;
} else {
throw new \personNotFoundException("Person not found: $name");
}
}
}
}
private function lookupPhotographer(string $name) {
if (static::$command=="new" ||
(conf::get("import.cli.add.auto") && !person::getByName($name))) {
$this->vars["_new_photographer"][]=$name;
} else {
$person=person::getByName($name);
if ($person) {
$personId=$person[0]->getId();
$this->vars["photographer_id"]=$personId;
} else {
throw new \personNotFoundException("Person not found: $name");
}
}
}
private function lookupLocations(array $locations, $parents) {
foreach ($locations as $name) {
if (static::$command=="new" || (conf::get("import.cli.add.auto") &&
!place::getByName($name))) {
$parent=array_shift($parents);
// this is a string comparison because the trim() in process() changes
// everything into a string...
if ($parent==="0") {
if (conf::get("import.cli.add.always")) {
$parentId=place::getRoot()->getId();
} else {
throw new \cliNoParentException("No parent for location " . $name);
}
} else {
$pplace=place::getByName($parent);
if ($pplace) {
$parentId=$pplace[0]->getId();
} else {
throw new \placeNotFoundException("Location not found: $parent");
}
}
$this->vars["_new_place"][]=array("parent" => $parentId, "name" => $name);
} else {
$name=$locations[0];
$place=place::getByName($name);
if ($place) {
$placeId=$place[0]->getId();
$this->vars["location_id"]=$placeId;
} else {
throw new \placeNotFoundException("Location not found: $name");
}
}
}
}
private function lookupSets(array $sets) {
foreach ($sets as $name) {
$set=set::getByName($name);
if ($set) {
$this->vars["_set_id"][]=$set[0]->getId();
} else {
throw new setNotFoundException("Set not found: $name");
}
}
}
}
?>
|