/**
  * Migrates the contao 2 newsgallery data into
  * contao 3 ce_gallery inside of news articles
  * https://contao.org/en/extension-list/view/newsgallery.10000069.en.html
  */
 private function migrateNewsGallery()
 {
     // If the tl_news addGallery field doesn't exist skip this import
     if (!$this->Database->fieldExists('addGallery', 'tl_news')) {
         return;
     }
     $selectQuery = "SELECT * FROM `tl_news` WHERE `addGallery`=?";
     $insertQuery = "INSERT INTO `tl_content` " . "%s";
     $dropQuery = "ALTER TABLE `tl_news` DROP `addGallery`";
     $objResult = $this->Database->prepare($selectQuery)->execute(1);
     // Only import if any news actually have galleries
     if ($objResult->numRows > 0) {
         $migrated = 0;
         $filesNotFound = 0;
         while ($objResult->next()) {
             // Set the insert data
             $gallery = array('pid' => $objResult->id, 'ptable' => 'tl_news', 'type' => 'gallery', 'tstamp' => time(), 'multiSRC' => '', 'perRow' => $objResult->perRow, 'sortBy' => $objResult->sortBy == 'meta' ? 'custom' : $objResult->sortBy, 'size' => $objResult->gal_size, 'imagemargin' => $objResult->gal_imagemargin, 'fullsize' => $objResult->gal_fullsize, 'headline' => $objResult->gal_headline);
             // Update the multi SRC to contao 3 files model
             $files = unserialize($objResult->multiSRC);
             $multiSRC = array();
             // Lookup each file path in the contao file model and get id
             foreach ($files as $file) {
                 $find = \FilesModel::findMultipleByPaths(array($file));
                 //var_dump($find->getResult()->count());
                 if ($find > 0) {
                     $multiSRC[] = $find->first()->id;
                 } else {
                     $this->log('Image/folder not found: ' . $file . ' skipping it.', __CLASS__ . ' migrateNewsGallery()', TL_ERROR);
                     $filesNotFound++;
                 }
             }
             $gallery['multiSRC'] = serialize($multiSRC);
             // Build an insert query
             $this->Database->prepare($insertQuery)->set($gallery)->execute();
             $migrated++;
         }
         $this->log('In total ' . $filesNotFound . ' files could not be located and were removed from galleries.', __CLASS__ . ' migrateNewsGallery()', TL_ERROR);
         $this->log('Successfully migrated ' . $migrated . '/' . $objResult->numRows . ' galleries into Contao3 News articles', __CLASS__ . ' migrateNewsGallery()', TL_CRON);
         // Drop add gallery to avoid double import
         $this->Database->prepare($dropQuery)->execute();
         // Note the other fields still have to be added manually
     }
 }
示例#2
0
文件: Dbafs.php 项目: iCodr8/core
 /**
  * Adds a file or folder with its parent folders
  *
  * @param string  $strResource      The path to the file or folder
  * @param boolean $blnUpdateFolders If true, the parent folders will be updated
  *
  * @return \FilesModel The files model
  *
  * @throws \Exception                If a parent ID entry is missing
  * @throws \InvalidArgumentException If the resource is outside the upload folder
  */
 public static function addResource($strResource, $blnUpdateFolders = true)
 {
     $strUploadPath = \Config::get('uploadPath') . '/';
     // Remove trailing slashes (see #5707)
     if (substr($strResource, -1) == '/') {
         $strResource = substr($strResource, 0, -1);
     }
     // Normalize the path (see #6034)
     $strResource = str_replace('//', '/', $strResource);
     // The resource does not exist or lies outside the upload directory
     if ($strResource == '' || strncmp($strResource, $strUploadPath, strlen($strUploadPath)) !== 0 || !file_exists(TL_ROOT . '/' . $strResource)) {
         throw new \InvalidArgumentException("Invalid resource {$strResource}");
     }
     $arrPaths = array();
     $arrChunks = explode('/', $strResource);
     $strPath = array_shift($arrChunks);
     $arrPids = array($strPath => null);
     $arrUpdate = array($strResource);
     $objDatabase = \Database::getInstance();
     // Build the paths
     while (count($arrChunks)) {
         $strPath .= '/' . array_shift($arrChunks);
         $arrPaths[] = $strPath;
     }
     unset($arrChunks);
     $objModel = null;
     $objModels = \FilesModel::findMultipleByPaths($arrPaths);
     // Unset the entries in $arrPaths if the DB entry exists
     if ($objModels !== null) {
         while ($objModels->next()) {
             if (($i = array_search($objModels->path, $arrPaths)) !== false) {
                 unset($arrPaths[$i]);
                 $arrPids[$objModels->path] = $objModels->uuid;
             }
         }
         // Store the model if it exists
         if ($objModels->path == $strResource) {
             $objModel = $objModels->current();
         }
     }
     // Return the model if it exists already
     if (empty($arrPaths)) {
         return $objModel;
     }
     $arrPaths = array_values($arrPaths);
     // If the resource is a folder, also add its contents
     if (is_dir(TL_ROOT . '/' . $strResource)) {
         // Get a filtered list of all files
         $objFiles = new \RecursiveIteratorIterator(new \Dbafs\Filter(new \RecursiveDirectoryIterator(TL_ROOT . '/' . $strResource, \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS)), \RecursiveIteratorIterator::SELF_FIRST);
         // Add the relative path
         foreach ($objFiles as $objFile) {
             $strRelpath = str_replace(TL_ROOT . '/', '', $objFile->getPathname());
             if ($objFile->isDir()) {
                 $arrUpdate[] = $strRelpath;
             }
             $arrPaths[] = $strRelpath;
         }
     }
     $objReturn = null;
     // Create the new resources
     foreach ($arrPaths as $strPath) {
         $strParent = dirname($strPath);
         // The parent ID should be in $arrPids
         // Do not use isset() here, because the PID can be null
         if (array_key_exists($strParent, $arrPids)) {
             $strPid = $arrPids[$strParent];
         } else {
             throw new \Exception("No parent entry for {$strParent}");
         }
         // Create the file or folder
         if (is_file(TL_ROOT . '/' . $strPath)) {
             $objFile = new \File($strPath, true);
             $objModel = new \FilesModel();
             $objModel->pid = $strPid;
             $objModel->tstamp = time();
             $objModel->name = $objFile->name;
             $objModel->type = 'file';
             $objModel->path = $objFile->path;
             $objModel->extension = $objFile->extension;
             $objModel->hash = $objFile->hash;
             $objModel->uuid = $objDatabase->getUuid();
             $objModel->save();
             $arrPids[$objFile->path] = $objModel->uuid;
         } else {
             $objFolder = new \Folder($strPath);
             $objModel = new \FilesModel();
             $objModel->pid = $strPid;
             $objModel->tstamp = time();
             $objModel->name = $objFolder->name;
             $objModel->type = 'folder';
             $objModel->path = $objFolder->path;
             $objModel->extension = '';
             $objModel->hash = $objFolder->hash;
             $objModel->uuid = $objDatabase->getUuid();
             $objModel->save();
             $arrPids[$objFolder->path] = $objModel->uuid;
         }
         // Store the model to be returned (see #5979)
         if ($objModel->path == $strResource) {
             $objReturn = $objModel;
         }
     }
     // Update the folder hashes
     if ($blnUpdateFolders) {
         static::updateFolderHashes($arrUpdate);
     }
     return $objReturn;
 }
示例#3
0
 /**
  * Loops all found files and parses the corresponding metafile.
  *
  * @return void
  */
 protected function parseMetaFiles()
 {
     $files = \FilesModel::findMultipleByPaths($this->foundFiles);
     if (!$files) {
         return;
     }
     while ($files->next()) {
         $path = $files->path;
         $meta = deserialize($files->meta, true);
         if (isset($meta[$this->getBaseLanguage()])) {
             $this->metaInformation[dirname($path)][basename($path)] = $meta[$this->getBaseLanguage()];
         } elseif (isset($meta[$this->getFallbackLanguage()])) {
             $this->metaInformation[dirname($path)][basename($path)] = $meta[$this->getFallbackLanguage()];
         }
     }
 }
 /**
  * modify the img tag with srcset attribute
  *
  * @param $img
  *
  * @return mixed|string
  */
 protected function modifyImageTag($img)
 {
     if (stristr($img, 'src="') && !stristr($img, 'srcset="')) {
         $path = explode('"', explode('src="', $img)[1])[0];
         $oriPath = $path;
         if (isset(static::$images[$path])) {
             $path = static::$images[$path]['path'];
         }
         $file = \FilesModel::findMultipleByPaths(array($path));
         if ($file && $file->next() && $file->activateSrcSet && $file->imageSrcSet) {
             $file->imageSrcSet = deserialize($file->imageSrcSet);
             if (is_array($file->imageSrcSet)) {
                 $srcSet = '';
                 foreach ($file->imageSrcSet as $set) {
                     if (!$set['imageForSrcSetDisable']) {
                         if ($srcSet) {
                             $srcSet .= ', ';
                         }
                         if (!$set['imageForSrcSet']) {
                             $srcSet .= $oriPath;
                         }
                         if ($set['imageForSrcSet']) {
                             $fileSet = \FilesModel::findByUuid($set['imageForSrcSet']);
                             if ($fileSet) {
                                 $srcSet .= $fileSet->path;
                             }
                         }
                         if ($srcSet) {
                             $attributes = array('w', 'h', 'x');
                             foreach ($attributes as $attribute) {
                                 if ($set['attributeSrcSet_' . $attribute] && $set['attributeSrcSet_' . $attribute] != '-') {
                                     $srcSet .= ' ' . $set['attributeSrcSet_' . $attribute] . $attribute;
                                 }
                             }
                         }
                     }
                 }
                 if ($srcSet) {
                     $attributes = array('width', 'height');
                     foreach ($attributes as $attribute) {
                         if ($value = $this->getAttribute($img, $attribute)) {
                             $img = str_replace(' ' . $value, '', $img);
                         }
                     }
                     if ($value = $this->getAttribute($img, 'alt')) {
                         $img = str_replace($value, ' srcset="' . $srcSet . '" ' . $value, $img);
                     } else {
                         $img .= ' srcset="' . $srcSet . '"';
                     }
                 }
             }
         }
     }
     return $img;
 }