Example #1
0
 public function actionDeleteInvalid()
 {
     $folders = array('email', 'billing/notifications');
     foreach ($folders as $name) {
         echo "Deleting " . $name . "\n";
         \GO\Files\Model\Folder::$deleteInDatabaseOnly = true;
         \GO\Files\Model\File::$deleteInDatabaseOnly = true;
         try {
             $folder = \GO\Files\Model\Folder::model()->findByPath($name);
             if ($folder) {
                 $folder->delete();
             }
         } catch (\Exception $e) {
             if (PHP_SAPI != 'cli') {
                 echo "<span style='color:red;'>" . $e->getMessage() . "</span>\n";
             } else {
                 echo $e->getMessage() . "\n";
             }
         }
     }
     $findParams = \GO\Base\Db\FindParams::newInstance();
     $findParams->getCriteria()->addCondition('parent_id', null, 'IS');
     $stmt = \GO\Files\Model\Folder::model()->find($findParams);
     foreach ($stmt as $folder) {
         if (!$folder->fsFolder->exists()) {
             echo "Deleting " . $folder->path . "\n";
             $folder->delete();
         }
     }
 }
Example #2
0
 protected function actionRemoveDuplicates($params)
 {
     if (!\GO::modules()->tools) {
         throw new \GO\Base\Exception\AccessDenied();
     }
     \GO::session()->runAsRoot();
     \GO\Base\Fs\File::setAllowDeletes(false);
     //VERY IMPORTANT:
     \GO\Files\Model\Folder::$deleteInDatabaseOnly = true;
     \GO\Files\Model\File::$deleteInDatabaseOnly = true;
     $this->lockAction();
     \GO::session()->closeWriting();
     //close writing otherwise concurrent requests are blocked.
     $checkModels = array("GO\\Calendar\\Model\\Event" => array('name', 'start_time', 'end_time', 'calendar_id', 'rrule'), "GO\\Tasks\\Model\\Task" => array('name', 'start_time', 'due_time', 'tasklist_id', 'rrule', 'user_id'), "GO\\Addressbook\\Model\\Contact" => array('first_name', 'middle_name', 'last_name', 'addressbook_id', 'company_id', 'email'), "GO\\Files\\Model\\Folder" => array('name', 'parent_id'));
     echo '<p style="color:red;"><font style="font-size:18px;" >Warning: This script only checks for duplicate items on the displayed columns!</font></p>';
     foreach ($checkModels as $modelName => $checkFields) {
         if (empty($params['model']) || $modelName == $params['model']) {
             echo '<h1>' . $modelName . '</h1>';
             $checkFieldsStr = 't.' . implode(', t.', $checkFields);
             $findParams = \GO\Base\Db\FindParams::newInstance()->ignoreAcl()->select('t.id, count(*) AS n, ' . $checkFieldsStr)->group($checkFields)->having('n>1');
             $stmt1 = \GO::getModel($modelName)->find($findParams);
             echo '<table border="1">';
             echo '<tr><td>ID</th><th>' . implode('</th><th>', $checkFields) . '</th></tr>';
             $count = 0;
             while ($dupModel = $stmt1->fetch()) {
                 $select = 't.id';
                 if (\GO::getModel($modelName)->hasFiles()) {
                     $select .= ', t.files_folder_id';
                 }
                 $findParams = \GO\Base\Db\FindParams::newInstance()->ignoreAcl()->select($select . ', ' . $checkFieldsStr)->order('id', 'ASC');
                 $criteria = $findParams->getCriteria();
                 foreach ($checkFields as $field) {
                     $criteria->addCondition($field, $dupModel->getAttribute($field));
                 }
                 $stmt = \GO::getModel($modelName)->find($findParams);
                 $first = true;
                 while ($model = $stmt->fetch()) {
                     echo '<tr><td>';
                     if (!$first) {
                         echo '<span style="color:red">';
                     }
                     echo $model->id;
                     if (!$first) {
                         echo '</span>';
                     }
                     echo '</th>';
                     foreach ($checkFields as $field) {
                         echo '<td>' . $model->getAttribute($field, 'html') . '</td>';
                     }
                     echo '</tr>';
                     if (!$first) {
                         if (!empty($params['delete'])) {
                             if ($model->hasLinks() && $model->countLinks()) {
                                 echo '<tr><td colspan="99">Skipped delete because model has links</td></tr>';
                             } elseif (($filesFolder = $model->getFilesFolder(false)) && ($filesFolder->hasFileChildren() || $filesFolder->hasFolderChildren())) {
                                 echo '<tr><td colspan="99">Skipped delete because model has folder or files</td></tr>';
                             } else {
                                 $model->delete(true);
                             }
                         }
                         $count++;
                     }
                     $first = false;
                 }
             }
             echo '</table>';
             echo '<p>Found ' . $count . ' duplicates</p>';
             echo '<br /><br /><a href="' . \GO::url('maintenance/removeDuplicates', array('delete' => true, 'model' => $modelName)) . '">Click here to delete the newest duplicates marked in red for model ' . $modelName . '.</a>';
         }
     }
     if (empty($params['model'])) {
         echo '<br /><br /><a href="' . \GO::url('maintenance/removeDuplicates', array('delete' => true)) . '">Click here to delete the newest duplicates marked in red.</a>';
     } else {
         echo '<br /><br /><a href="' . \GO::url('maintenance/removeDuplicates') . '">Show all models.</a>';
     }
 }
Example #3
0
 /**
  * Adds missing files and folders from the filesystem to the database and
  * removes files and folders from the database that are not on the filesystem.
  *
  * @param boolean $recurseAll
  * @param boolean $recurseOneLevel
  */
 public function syncFilesystem($recurseAll = false, $recurseOneLevel = true)
 {
     if (\GO::config()->debug) {
         \GO::debug("syncFilesystem " . $this->path);
     }
     $oldIgnoreAcl = \GO::setIgnoreAclPermissions(true);
     $oldCache = \GO::$disableModelCache;
     GO::$disableModelCache = $recurseAll;
     //		if(class_exists("GO\Filesearch\FilesearchModule"))
     //			\GO\Filesearch\FilesearchModule::$disableIndexing=true;
     if ($this->fsFolder->exists()) {
         $items = $this->fsFolder->ls();
         foreach ($items as $item) {
             try {
                 //\GO::debug("FS SYNC: Adding fs ".$item->name()." to database");
                 if ($item->isFile()) {
                     $file = $this->hasFile($item->name());
                     if (!$file) {
                         $this->addFile($item->name());
                     } else {
                         //this will update timestamp and size of file
                         if ($file->mtime != $file->fsFile->mtime()) {
                             $file->save();
                         }
                     }
                 } else {
                     $willSync = $recurseOneLevel || $recurseAll;
                     $folder = $this->hasFolder($item->name());
                     if (!$folder) {
                         $folder = $this->addFolder($item->name(), false, !$willSync);
                     }
                     if ($willSync) {
                         $folder->syncFilesystem($recurseAll, false);
                     }
                 }
             } catch (\Exception $e) {
                 echo "<span style='color:red;'>" . $e->getMessage() . "</span>\n";
             }
         }
     } else {
         $this->fsFolder->create();
     }
     //make sure no filesystem items are deleted. Sometimes folders are stored as files somehow.
     $oldFileDeleteInDatabaseOnly = File::$deleteInDatabaseOnly;
     $oldFolderDeleteInDatabaseOnly = Folder::$deleteInDatabaseOnly;
     File::$deleteInDatabaseOnly = true;
     Folder::$deleteInDatabaseOnly = true;
     $stmt = $this->folders();
     while ($folder = $stmt->fetch()) {
         try {
             if (!$folder->fsFolder->exists() || $folder->fsFolder->isFile()) {
                 $folder->delete(true);
             }
         } catch (\Exception $e) {
             echo "<span style='color:red;'>" . $e->getMessage() . "</span>\n";
         }
     }
     $stmt = $this->files();
     while ($file = $stmt->fetch()) {
         try {
             if (!$file->fsFile->exists() || $file->fsFile->isFolder()) {
                 $file->delete(true);
             }
         } catch (\Exception $e) {
             echo "<span style='color:red;'>" . $e->getMessage() . "</span>\n";
         }
     }
     $this->mtime = $this->fsFolder->mtime();
     $this->save(true);
     \GO::$disableModelCache = $oldCache;
     \GO::setIgnoreAclPermissions($oldIgnoreAcl);
     File::$deleteInDatabaseOnly = $oldFileDeleteInDatabaseOnly;
     Folder::$deleteInDatabaseOnly = $oldFolderDeleteInDatabaseOnly;
 }