foldershare-8.x-1.2/src/Entity/FolderShareTraits/OperationFsckTrait.php
src/Entity/FolderShareTraits/OperationFsckTrait.php
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 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\Core\Database\Database; use Drupal\Component\Utility\Environment; use Drupal\foldershare\Utilities\CacheUtilities; use Drupal\foldershare\Entity\FolderShareScheduledTask; use Drupal\foldershare\ManageLog; /** * File system check all FolderShare entities. * * This trait includes methods to check the file system and repair it. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait OperationFsckTrait { /*--------------------------------------------------------------------- * * File system check. * *---------------------------------------------------------------------*/ /** * Runs a file system check to find and fix problems. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>It is strongly recommended that the website be in maintenance * mode when this function is called. This prevents file system activity * that can collide with this function and create unexpected and * possibly destructive results.</B> * * This method scans the file system looking for problems. These primarily * include parent and root linkage problems, such as parent or root IDs * that do not refer to existing items, or where the parent and root IDs * are not consistent with each other. This method also checks for hidden * and disabled items that do not appear to have pending tasks that justify * them. * * This method clears all memory caches before it starts. If $fix is TRUE, * it also clears the FolderShare entity caches before any checks, and * clears ALL Drupal caches upon completion. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = FALSE) When TRUE, fix file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. * * @see ::clearAllSystemHidden() * @see ::clearAllSystemDisabled() * * @todo Recurse through every folder tree and check that all children * share the same root ID. */ public static function fsck(bool $fix = FALSE) { // // Prepare. // -------- // Try to push the PHP timeout to "unlimited" so that a long operation // has a better chance to complete. This may not work if the site has // PHP timeout changes blocked and it does nothing to stop web server // timeouts. // // However, fsck() is primarily intended for execution from the // command line, which has no such timeouts. Environment::setTimeLimit(0); // Flush everything from static (memory) caches to remove the vestiges // of whatever operations occurred before this task began. CacheUtilities::flushAllMemoryCaches(); if ( $fix === TRUE) { // Flush all FolderShare entities from caches everywhere. Below we // modify the database directly. Once changed, all cached copies are // out of date. CacheUtilities::flushAllEntityCaches(self::ENTITY_TYPE_ID); } $fixed = FALSE; // // Fix parent/root linkage. // ------------------------ // The parent ID and root ID have the following possibilities: // // |#|parentid|rootid |Validity|Fix by... | // |-|--------|--------|--------|----------------------------------------| // |1|NULL |NULL |good | | // |2|NULL |valid |bad |Set parentid = rootid | // |3|NULL |invalid |bad |Set parentid = rootid = NULL | // |4|valid |NULL |bad |Set parentid = rootid = NULL | // |5|valid |valid |good | | // |6|valid |invalid |bad |Set parentid = rootid = NULL | // |7|invalid |NULL |bad |Set parentid = rootid = NULL | // |8|invalid |valid |bad |Set parentid = rootid | // |9|invalid |invalid |bad |Set parentid = rootid = NULL | // // In choosing fixes, we have to watch for circular fixes that leave // content still invalid. For instance, when the root ID is NULL but // the parent ID is valid, we could set the root ID to the parent's // root ID. Except we don't know if that root ID is valid. If not, // we're just propagating a bad root ID into another item. And if we // then update descendants with that bad root ID, we're making a mess. // // So, we choose to prioritize the root ID over the parent ID. If the // root ID is NULL, the parent ID is forced to NULL without checking // if it is or is not valid. // // Cases (1) and (5) do not require any fixes. // // Cases (4) & (7). NULL rootid and non-NULL parentid. // - Set parentid to NULL. // - Cases (4) & (7) become case (1). $status = self::fsckFixNullRootNonNullParent( $fix ); $fixed = $fixed || $status ; // Cases (3), (6) & (9). Non-NULL invalid rootid. // - Set parentid and rootid to NULL and update descendants. // - Cases (3), (6) & (9) become case (1). $status = self::fsckFixInvalidRoot( $fix ); $fixed = $fixed || $status ; // Case (2). Non-NULL valid rootid and NULL parentid. // - Set parentid to rootid. // - Case (2) becomes case (5). $status = self::fsckFixValidRootNullParent( $fix ); $fixed = $fixed || $status ; // Case (8). Non-NULL valid rootid and non-NULL invalid parentid. // - Set parentid to rootid. // - Case (8) becomes case (5). $status = self::fsckFixValidRootInvalidParent( $fix ); $fixed = $fixed || $status ; // // Fix root consistency. // --------------------- // The root ID for every descendant of a root item must be that root // item's ID. $status = self::fsckFixConsistentRoot( $fix ); $fixed = $fixed || $status ; // // Fix hidden & disabled. // ---------------------- // Items should not be marked hidden or disabled if there are no // associated tasks pending. $status = self::fsckFixHidden( $fix ); $fixed = $fixed || $status ; $status = self::fsckFixDisabled( $fix ); $fixed = $fixed || $status ; // // Finish. // ------- // Flush ALL caches everywhere. // // The activities above may have changed any number of FolderShare // entities. Because they do direct database operations, it is not // easy to track exactly which items have changed. If we did, it could // be a huge array of entity IDs that would be awkward to create and // use within PHP's memory limits. // // Without tracking every entity changed, it is not possibly to // selectively invalidate cache entries relating just to those entities. // Further, Drupal does not provide any way to flush all caches of all // entries for entities of this entity type. For instance, there is no // way to flush out of the render cache everything for all FolderShare // entities. The only way to flush out of any cache is to provide an // explicit list of entity IDs to flush. // // The only safe way to be sure we have flushed all out of date cache // entries from every cache is to flush everything. if ( $fix === TRUE && $fixed === TRUE) { drupal_flush_all_caches(); } if ( $fixed === FALSE) { ManageLog::activity( 'Folder tree: Integrity check complete. No problems found.' ); } return $fixed ; } /** * Fixes items with a NULL root ID and non-NULL parent ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If a root ID is NULL, the item has no parent and is supposed to be * a root item. In that case its parent ID should be NULL too. If this * is not the case, the item has become corrupted. * * <B>Fix</B> * Leave the root ID as NULL and set the parent ID to NULL too. There is * no need to update descendants. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixNullRootNonNullParent(bool $fix = FALSE) { $connection = Database::getConnection(); // // Count problems. // --------------- // Issue a query to find items where there is a NULL root ID, and // a non-NULL parent ID. $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->isNull( 'rootid' ); $select ->isNotNull( 'parentid' ); $ids = $select ->execute()->fetchCol(0); $count = count ( $ids ); if ( $count === 0) { return FALSE; } if ( $fix === FALSE) { ManageLog::critical( "Folder tree: Integrity check found @count invalid items with NULL root but non-NULL parent.\nThese items are incorrectly connected into the folder tree. This will cause them to be listed within a folder when they should be listed in a root list.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } // // Fix problems. // ------------- // Issue an update to find all items where the root ID is NULL, and // the parent ID is not NULL. Set the parent ID to NULL. $update = $connection ->update(self::BASE_TABLE); $update ->isNull( 'rootid' ); $update ->isNotNull( 'parentid' ); $update ->fields([ 'parentid' => NULL, ]); $count = $update ->execute(); if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count items with NULL root but non-NULL parent.\nEach invalid item's parent has been set to NULL to be consistent with its NULL root. This corrects their folder tree connections and insures that they are listed properly in a root list.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } /** * Fixes items with an invalid root ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If the root ID refers to a non-existent entity, it is invalid and * the item has become corrupted. * * <B>Fix</B> * Set the root ID and parent ID to NULL. Update descendants. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixInvalidRoot(bool $fix = FALSE) { $connection = Database::getConnection(); // // Count problems. // --------------- // Issue a query to find cases with a non-NULL rootid that does not // match any other entity. $subquery = $connection ->select(self::BASE_TABLE, 'subfs' ); $subquery ->addField( 'subfs' , 'id' , 'subfsid' ); $subquery ->where( 'fs.rootid = subfs.id' ); $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->isNotNull( 'rootid' ); $select ->notExists( $subquery ); $ids = $select ->execute()->fetchCol(0); $count = count ( $ids ); if ( $count === 0) { return FALSE; } if ( $fix === FALSE) { ManageLog::critical( "Folder tree: Integrity check found @count invalid items with non-existent roots.\nThese items are incorrectly connected into the folder tree. They are listed as children of a parent folder, but they indicate roots that do not exist. This will cause attempts to view or change these items to fail with error messages and page crashes because the root is bad and access grants are not available.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } // // Fix problems. // ------------- // Issue an update to set all of the found items to a NULL parent ID // and root ID, which makes them roots. // // Then use the first query's ID list to loop to update all // descendants to set their root ID to the item. $subquery = $connection ->select(self::BASE_TABLE, 'subfs' ); $subquery ->addField( 'subfs' , 'id' , 'subfsid' ); $subquery ->where( '{' . self::BASE_TABLE . '}.rootid = subfs.id' ); $update = $connection ->update(self::BASE_TABLE); $update ->isNotNull( 'rootid' ); $update ->notExists( $subquery ); $update ->fields([ 'rootid' => NULL, 'parentid' => NULL, ]); $update ->execute(); foreach ( $ids as $id ) { // Set the root of all descendants to be the item itself. self::setDescendantsRootItemId( $id , $id ); } ManageLog::notice( "Folder tree: Corrected @count items with non-existent root.\nEach item's parent and root have been set to NULL. This reconnects them into the folder tree so that they are listed in a root list. All of their descendants also have been updated to use the corrected item as their root.\nEntity IDs (root-level only): @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } /** * Fixes items with valid root ID and NULL parent ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If a root ID is not NULL and valid, the item is a root and it is * supposed to have a NULL parent. If the parent is not NULL, the item * has become corrupted. * * <B>Fix</B> * Set the parent ID to the root ID. There is no need to update descendants. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix and log file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixValidRootNullParent(bool $fix = FALSE) { $connection = Database::getConnection(); // // Count problems. // --------------- // Issue a query to find items with a NULL parent ID, and a // non-NULL root ID. $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->isNotNull( 'rootid' ); $select ->isNull( 'parentid' ); $ids = $select ->execute()->fetchCol(0); $count = count ( $ids ); if ( $count === 0) { return FALSE; } if ( $fix === FALSE) { ManageLog::critical( "Folder tree: Integrity check found @count invalid items with valid root but NULL parent.\nThese items are incorrectly connected into the folder tree. This will cause them to be listed in a root list, but use access grants as if they are children of a folder in another root list.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } // // Fix problems. // ------------- // Issue an update to find all items where the root ID is not NULL, // but the parent ID is NULL. Set the parent ID to the root ID. $update = $connection ->update(self::BASE_TABLE); $update ->isNotNull( 'rootid' ); $update ->isNull( 'parentid' ); $update ->expression( 'parentid' , 'rootid' ); $count = $update ->execute(); if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count items with valid root but NULL parent.\nEach item's parent has been set to it's root. This will retain their access grants and cause them to be correctly listed as children of the parent instead of incorrectly showing them in a root list.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } /** * Fixes items with valid root IDs and invalid parent IDs. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If the root ID is valid, but the parent ID refers to a non-existent * entity, it is invalid and the item has become corrupted. * * <B>Fix</B> * Set the parent ID to the root ID. There is no need to update descendants. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix and log file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixValidRootInvalidParent(bool $fix = FALSE) { $connection = Database::getConnection(); // // Count problems. // --------------- // Issue a query to find items with an invalid parent ID. $subquery = $connection ->select(self::BASE_TABLE, 'subfs' ); $subquery ->addField( 'subfs' , 'id' , 'subfsid' ); $subquery ->where( 'fs.parentid = subfs.id' ); $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->isNotNull( 'parentid' ); $select ->isNotNull( 'rootid' ); $select ->notExists( $subquery ); $ids = $select ->execute()->fetchCol(0); $count = count ( $ids ); if ( $count === 0) { return FALSE; } if ( $fix === FALSE) { ManageLog::critical( "Folder tree: Integrity check found @count invalid items with non-existent parents.\nThese items are incorrectly connected into the folder tree. They are inaccessible because they are marked as children of a non-existent parent folder.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } // // Fix problems. // ------------- // Issue an update to find all invalid parent IDs and set them to // the root ID. $subquery = $connection ->select(self::BASE_TABLE, 'subfs' ); $subquery ->addField( 'subfs' , 'id' , 'subfsid' ); $subquery ->where( '{' . self::BASE_TABLE . '}.parentid = subfs.id' ); $update = $connection ->update(self::BASE_TABLE); $update ->isNotNull( 'parentid' ); $update ->isNotNull( 'rootid' ); $update ->notExists( $subquery ); $update ->expression( 'parentid' , 'rootid' ); $count = $update ->execute(); if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count items with invalid parents.\nEach item's parent has been set to it's root. This reconnects them into the folder tree so that they are listed in owner root lists.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } /** * Fixes items with incorrect root IDs. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If an item is a descendant of a root item, but its root ID is not set * to that root item's ID, then it is incorrect and the item has become * corrupted. * * <B>Fix</B> * Set the root ID to the highest ancestor's ID. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix and log file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixConsistentRoot(bool $fix = FALSE) { $connection = Database::getConnection(); // // Get all root IDs. // ----------------- // Get a list of all root IDs owned by anybody. We'll be looping // over these. Get IDs for any user and any name, and include all // disabled AND hidden items. $rootIds = self::findAllRootItemIds( self::ANY_USER_ID, '' , TRUE, TRUE); // // Count problems. // --------------- // Iteratively recurse down through each root folder tree and count // the number of descendants that have the wrong root ID. if ( $fix === FALSE) { $count = 0; foreach ( $rootIds as $rootId ) { $rootCount = 0; $pending = [ $rootId ]; while ( empty ( $pending ) === FALSE) { // Get the next pending ID. $id = (int) array_shift ( $pending ); // Count all children that have the wrong root ID. $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->condition( 'parentid' , $id , '=' ); $select ->condition( 'rootid' , $rootId , '<>' ); $rootCount += $select ->countQuery()->execute()->fetchField(); // Get folder children and add them to the pending list. $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->condition( 'parentid' , $id , '=' ); $select ->condition( 'kind' , self::FOLDER_KIND, '=' ); $pending = array_merge ( $pending , $select ->execute()->fetchCol(0)); } $count += $rootCount ; } if ( $count === 0) { return FALSE; } ManageLog::critical( "Folder tree: Integrity check found @count invalid descendant items with incorrect root.\nThese items are incorrectly connected into the folder tree. They are children of a parent folder, but their root is not the same as the parent's. This will cause incorrect access grants.\nRepair is needed. Run 'drush foldershare-fsck --fix'." , [ '@count' => $count , ]); return TRUE; } // // Fix problems. // ------------- // Issue an update to find all invalid parent IDs and set them to // the root ID. foreach ( $rootIds as $rootId ) { $pending = [ $rootId ]; while ( empty ( $pending ) === FALSE) { // Get the next pending ID. $id = (int) array_shift ( $pending ); // Update all children that have the wrong root ID. $update = $connection ->update(self::BASE_TABLE); $update ->condition( 'parentid' , $id , '=' ); $update ->condition( 'rootid' , $rootId , '<>' ); $update ->fields([ 'rootid' => $rootId , ]); $count += $update ->execute(); // Get folder children and add them to the pending list. $select = $connection ->select(self::BASE_TABLE, 'fs' ); $select ->addField( 'fs' , 'id' , 'id' ); $select ->condition( 'parentid' , $id , '=' ); $select ->condition( 'kind' , self::FOLDER_KIND, '=' ); $pending = array_merge ( $pending , $select ->execute()->fetchCol(0)); } } if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count descendant items with incorrect root.\nEach item's root has been set to the highest ancestor's ID. This does not move items in the folder tree. It just corrects their access grants to be based on the correct root." , [ '@count' => $count , ]); return TRUE; } /** * Fixes hidden items. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If an item is marked as hidden, it is in the process of being deleted. * But if there are no delete tasks pending, there is no reason for an * item to be hidden. A prior delete may have crashed. * * <B>Fix</B> * Set the systemhidden flag to FALSE to show it again. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix and log file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixHidden(bool $fix = FALSE) { if ( $fix === FALSE) { // // Find hidden & tasks. // -------------------- // Find all hidden items and then look for pending delete tasks. // If there are hidden items, but no delete tasks, there may be // a problem. $ids = self::findAllHiddenIds(); $count = count ( $ids ); if ( $count === 0) { return FALSE; } // Get the number of phase 1 and phase 2 delete tasks. $nTasks = FolderShareScheduledTask::findNumberOfTasks( 'delete-hide' ) + FolderShareScheduledTask::findNumberOfTasks( 'delete-delete' ); if ( $nTasks === 0) { ManageLog::critical( "Folder tree: Integrity check found @count hidden items but no pending delete tasks.\nThese items are hidden from view and inaccessible. This normally means that they are about to be deleted, but there are no delete tasks pending.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } ManageLog::notice( "Folder tree: Found @count hidden items and @taskCount pending delete tasks.\nHidden items are normal while a delete task prepares to delete them. The pending tasks should be allowed to complete.\nRepair is probably not needed.\nEntity IDs: @ids" , [ '@count' => $count , '@taskCount' => $nTasks , '@ids' => implode( ', ' , $ids ), ]); return FALSE; } // // Clear hidden. // ------------- // Reset the hidden flag so that items can be seen. This will reveal // content that has not been fully deleted. $count = self::clearAllSystemHidden(); if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count hidden items by forcing them to be visible.\nThis will cause these items to be listed in root lists and folders. Since they were originally marked hidden prior to deletion, users may be confused by their sudden reappearance." , [ '@count' => $count , ]); return TRUE; } /** * Fixes disabled items. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * <B>Problem</B> * If an item is marked as disabled, it is in use by a copy or move. * But if there are no copy or move tasks pending, there is no reason for an * item to be disabled. A prior copy or move may have crashed. * * <B>Fix</B> * Set the systemdisabled flag to FALSE to show it again. * * <B>Logged messages</B> * If problems are encountered and $fix is FALSE, critical error messages * are posted to the site's log and the problems are not fixed. But if * $fix is TRUE, an attempt is made to fix the problems and notice messages * about the problems are posted to the sites log. * * @param bool $fix * (optional, default = TRUE) When TRUE, fix and log file system problems * automatically. When FALSE, just report them to the log. * * @return bool * Returns TRUE if anything in the file system was fixed or would * have been fixed, and FALSE if no changes are needed. */ private static function fsckFixDisabled(bool $fix = FALSE) { if ( $fix === FALSE) { // // Find disabled & tasks. // ---------------------- // Find all disabled items and then look for pending copy and move // tasks. If there are disabled items, but no tasks, there may be // a problem. $ids = self::findAllDisabledIds(); $count = count ( $ids ); if ( $count === 0) { return FALSE; } // Get the number of copy and delete tasks. $nTasks = FolderShareScheduledTask::findNumberOfTasks( 'copy-to-root' ) + FolderShareScheduledTask::findNumberOfTasks( 'copy-to-folder' ) + FolderShareScheduledTask::findNumberOfTasks( 'move-to-root' ) + FolderShareScheduledTask::findNumberOfTasks( 'move-to-folder' ); if ( $nTasks === 0) { ManageLog::critical( "Folder tree: Integrity check found @count disabled items but no pending copy or move tasks.\nThese items are disabled, visible in lists, but they cannot be changed or deleted. Disabled items are normal during copy and move operations, but there are no copy or move tasks pending.\nRepair is needed. Run 'drush foldershare-fsck --fix'.\nEntity IDs: @ids" , [ '@count' => $count , '@ids' => implode( ', ' , $ids ), ]); return TRUE; } ManageLog::notice( "Folder tree: Found @count disabled items and @taskCount pending copy and/or move tasks.\nDisabled items are normal during copy and move operations in order to block user activity until the operation completes. The pending tasks should be allowed to complete.\nRepair is probably not needed.\nEntity IDs: @ids" , [ '@count' => $count , '@taskCount' => $nTasks , '@ids' => implode( ', ' , $ids ), ]); return FALSE; } // // Clear disabled. // --------------- // Reset the disabled flag so that items can be shown normally. $count = self::clearAllSystemDisabled(); if ( $count === 0) { return FALSE; } ManageLog::notice( "Folder tree: Corrected @count disabled items by forcing them to be enabled.\nThis will cause these items to be listed normally and changeable. However, since these items were original disabled during a copy or move that apparently did not complete, users may be confused by items that don't seem complete." , [ '@count' => $count , ]); return TRUE; } } |