示例#1
0
 /**
  * @return null|LockFile
  */
 public function getLockFile()
 {
     if (null === $this->lockFile) {
         $filePath = $this->getPath('composer.lock');
         if (file_exists($filePath)) {
             $this->lockFile = LockFile::fromFilePath($this->root, $filePath);
         }
     }
     return $this->lockFile;
 }
/***
* This script is intended to be placed in a cronjob.
* It must be run every Thursday, at 01hOO for example.
* On Unix, you can use crontab -e and place this :
* 00 01 * * 4 /path/php/binary /path/to/your/vcs/dir/doc-editor/scripts/cron/check_entities.php
****/
require_once dirname(__FILE__) . '/../../php/Conf.php';
require_once dirname(__FILE__) . '/../../php/LockFile.php';
require_once dirname(__FILE__) . '/../../php/ProjectManager.php';
require_once dirname(__FILE__) . '/../../php/RepositoryManager.php';
require_once dirname(__FILE__) . '/../../php/ToolsCheckEntities.php';
$rm = RepositoryManager::getInstance();
$pm = ProjectManager::getInstance();
$availableProject = $pm->getAvailableProject();
while (list($key, $project) = each($availableProject)) {
    if ($project['code'] != "php") {
        continue;
    }
    // Define it as a project
    $pm->setProject($project['code']);
    $lock = new LockFile('project_' . $project['code'] . '_lock_check_entities');
    if ($lock->lock()) {
        ToolsCheckEntities::getInstance()->startCheck();
        // Set lastUpdate date/time
        $info = array();
        $info['user'] = '******';
        $rm->setStaticValue('info', 'checkEntities', json_encode($info), true);
    }
    // Remove the lock File
    $lock->release();
}
 /**
  * Update a single folder of the repository to sync our local copy.
  */
 public function updateFolder($path)
 {
     $rf = RepositoryFetcher::getInstance();
     $am = AccountManager::getInstance();
     $project = $am->project;
     // We reset the session var
     unset($_SESSION['updateFolder']['newFolders']);
     unset($_SESSION['updateFolder']['newFiles']);
     // We search for the first folder ; can be en/ LANG/ or doc-base/ for example
     $t = explode("/", $path);
     $firstFolder = $t[1];
     array_shift($t);
     array_shift($t);
     $pathWithoutLang = '/' . implode("/", $t) . '/';
     // If we are in the root folder, we have //. We must consider this case.
     if ($pathWithoutLang == '//') {
         $pathWithoutLang = '/';
     }
     if ($firstFolder == "") {
         $firstFolder = 'root';
     }
     $lock = new LockFile('project_' . $project . '_' . $firstFolder . '_lock_update_folder');
     if ($lock->lock()) {
         if ($this->isValidLanguage($firstFolder)) {
             // This is only for EN and LANG. For others, we don't make any version's comparaison.
             // We start be get files & folders in this folder to compare it after the update
             $actual = $rf->getFilesByDirectory($path);
             $actualFiles = $actualFolders = array();
             for ($i = 0; $i < count($actual); $i++) {
                 // We get files and folders
                 if ($actual[$i]['type'] === 'folder') {
                     $actualFolders[$actual[$i]['text']] = array("name" => $actual[$i]['text']);
                 } else {
                     $actualFiles[$actual[$i]['text']] = array("name" => $actual[$i]['text'], "version" => "");
                 }
             }
             // We get versions for this files
             while (list($k, $v) = each($actualFiles)) {
                 $file = new File($firstFolder, $pathWithoutLang . $k);
                 $info = $file->getInfo();
                 $actualFiles[$k]['version'] = $info['rev'];
             }
         }
         // We update the repository recursively
         VCSFactory::getInstance()->updateSingleFolder($path);
         if ($this->isValidLanguage($firstFolder)) {
             // We throw the revCheck on this folder only if the langue is valide
             $this->applyRevCheck($pathWithoutLang, 'update', $firstFolder);
             // We get files under this folder to make comparaison after the update
             $now = $rf->getFilesByDirectory($path);
             $nowFiles = $nowFolders = array();
             for ($i = 0; $i < count($now); $i++) {
                 // We get all folders & files
                 if ($now[$i]['type'] === 'folder') {
                     $nowFolders[$now[$i]['text']] = array("name" => $now[$i]['text']);
                 } else {
                     $nowFiles[$now[$i]['text']] = array("name" => $now[$i]['text'], "version" => "");
                 }
             }
             // We get versions of this files
             while (list($k, $v) = each($nowFiles)) {
                 $file = new File($firstFolder, $pathWithoutLang . $k);
                 $info = $file->getInfo();
                 $nowFiles[$k]['version'] = $info['rev'];
             }
             //~ debug(json_encode($nowFiles));
             //~ debug(json_encode($actualFiles));
             // We search for differences
             reset($nowFiles);
             reset($nowFolders);
             while (list($k, $v) = each($nowFiles)) {
                 // If the file exist before, and at the same version, we delete it from $nowFiles
                 if (isset($actualFiles[$k]) && $actualFiles[$k]['version'] == $v['version']) {
                     unset($nowFiles[$k]);
                 }
             }
             while (list($k, $v) = each($nowFolders)) {
                 // If the folder exist before, we delete it from $nowFolders
                 if (isset($actualFolders[$k])) {
                     unset($nowFolders[$k]);
                 }
             }
             // $nowFolders contains only new folders who don't exist before the update
             // and $nowFiles, new files who don't exist before the update or who the version have changed
             // We store this result in session to allow get it if this processus take more than 30 seconds (max execution time)
             $_SESSION['updateFolder']['newFolders'] = $nowFolders;
             $_SESSION['updateFolder']['newFiles'] = $nowFiles;
         }
     }
     $lock->release();
     return json_encode($_SESSION['updateFolder']);
 }
 /**
  * Commit modified files.
  */
 public function vcsCommit()
 {
     $am = AccountManager::getInstance();
     $rm = RepositoryManager::getInstance();
     if (!$am->isLogged()) {
         return JsonResponseBuilder::failure();
     }
     if (!$am->haveKarma) {
         return JsonResponseBuilder::failure();
     }
     $patchID = $this->getRequestVariable('patchID');
     $nodes = $this->getRequestVariable('nodes');
     $logMessage = stripslashes($this->getRequestVariable('logMessage'));
     $anode = json_decode(stripslashes($nodes));
     $commitResponse = $tmp = '';
     // We create a lock for this commit process
     $lock = new LockFile('project_' . $am->project . '_lock_' . $am->vcsLogin . '_commit');
     if ($lock->lock()) {
         $tmp = $rm->commitChanges($anode, $logMessage);
         $commitResponse = $tmp['commitResponse'];
         $anode = $tmp['anode'];
         $err = $tmp['err'];
         // Store the response into session to display later
         $_SESSION['commitResponse'] = $commitResponse;
         if (0 == $err) {
             // Start all process after the VCS commit (related to db changes)
             $nodes = RepositoryFetcher::getInstance()->getModifiesById($anode);
             // We need to provide a different treatment regarding the file's type...
             $existFiles = array();
             // Can be an updated file or a new file
             $deleteFiles = array();
             $j = 0;
             for ($i = 0; $i < count($nodes); $i++) {
                 if ($nodes[$i]['type'] == 'update' || $nodes[$i]['type'] == 'new') {
                     $existFiles[] = new File($nodes[$i]['lang'], $nodes[$i]['path'] . $nodes[$i]['name']);
                 }
                 if ($nodes[$i]['type'] == 'delete') {
                     $deleteFiles[$j]->lang = $nodes[$i]['lang'];
                     $deleteFiles[$j]->path = $nodes[$i]['path'];
                     $deleteFiles[$j]->name = $nodes[$i]['name'];
                     $j++;
                 }
             }
             // ... for existing Files (new or update)
             if (!empty($existFiles)) {
                 // Update revision & reviewed for all this files (LANG & EN)
                 $rm->updateFileInfo($existFiles);
                 // Remove all this files in work
                 $rm->delWork($existFiles);
             }
             // End of $existFiles stuff
             // ... for deleted Files
             if (!empty($deleteFiles)) {
                 // Remove this files from db
                 $rm->delFiles($deleteFiles);
                 // Remove all this files in work tables
                 $rm->delWork($deleteFiles);
             }
             // End of $deleteFiles stuff
             // We re-compute summary statistics for the global documentation & by translators
             $lang = AccountManager::getInstance()->vcsLang;
             $rm->updateTranslatorInfo();
             TranslationStatistic::getInstance()->computeSummary($lang);
             TranslatorStatistic::getInstance()->computeSummary($lang);
         }
     }
     // Manage log message (add new or ignore it if this message already exist for this user)
     LogManager::getInstance()->addCommitLog($logMessage);
     // We send an email only if this commit is from a patch
     if ($patchID) {
         $rm->postPatchCommit($patchID);
     }
     // Remove the lock File
     $lock->release();
     return JsonResponseBuilder::success(array('mess' => $commitResponse));
 }
 function Execute($php_cli = NULL, $dir = NULL)
 {
     global $config_vars;
     $lock_file = new LockFile($config_vars['cache']['dir'] . DIRECTORY_SEPARATOR . $this->getName() . '.lock');
     //Check job last updated date, if its more then 12hrs and its still in the "running" status,
     //chances are its an orphan. Change status.
     //if ( $this->getStatus() != 10 AND $this->getLastRunDate() < time()-(12*3600) ) {
     if ($this->getStatus() != 10 and $this->getUpdatedDate() > 0 and $this->getUpdatedDate() < time() - 6 * 3600) {
         Debug::text('ERROR: Job has been running for more then 6 hours! Asssuming its an orphan, marking as ready for next run.', __FILE__, __LINE__, __METHOD__, 10);
         $this->setStatus(10);
         $this->Save(FALSE);
         $lock_file->delete();
     }
     if (!is_executable($php_cli)) {
         Debug::text('ERROR: PHP CLI is not executable: ' . $php_cli, __FILE__, __LINE__, __METHOD__, 10);
         return FALSE;
     }
     if ($this->isSystemLoadValid() == FALSE) {
         Debug::text('System load is too high, skipping...', __FILE__, __LINE__, __METHOD__, 10);
         return FALSE;
     }
     //Cron script to execute
     $script = $dir . DIRECTORY_SEPARATOR . $this->getCommand();
     if ($this->getStatus() == 10 and $lock_file->exists() == FALSE) {
         $lock_file->create();
         $this->setExecuteFlag(TRUE);
         Debug::text('Job is NOT currently running, running now...', __FILE__, __LINE__, __METHOD__, 10);
         //Mark job as running
         $this->setStatus(20);
         //Running
         $this->Save(FALSE);
         //Even if the file does not exist, we still need to "pretend" the cron job ran (set last ran date) so we don't
         //display the big red error message saying that NO jobs have run in the last 24hrs.
         if (file_exists($script)) {
             $command = '"' . $php_cli . '" "' . $script . '"';
             //if ( OPERATING_SYSTEM == 'WIN' ) {
             //Windows requires quotes around the entire command, and each individual section with that might have spaces.
             //23-May-13: This seems to cause the command to fail now. Perhaps its related to newer versions of PHP?
             //$command = '"'. $command .'"';
             //}
             Debug::text('Command: ' . $command, __FILE__, __LINE__, __METHOD__, 10);
             $start_time = microtime(TRUE);
             exec($command, $output, $retcode);
             Debug::Arr($output, 'Time: ' . (microtime(TRUE) - $start_time) . 's - Command RetCode: ' . $retcode . ' Output: ', __FILE__, __LINE__, __METHOD__, 10);
             TTLog::addEntry($this->getId(), 500, TTi18n::getText('Executing Cron Job') . ': ' . $this->getID() . ' ' . TTi18n::getText('Command') . ': ' . $command . ' ' . TTi18n::getText('Return Code') . ': ' . $retcode, NULL, $this->getTable());
         } else {
             Debug::text('WARNING: File does not exist, skipping: ' . $script, __FILE__, __LINE__, __METHOD__, 10);
         }
         $this->setStatus(10);
         //Ready
         $this->setLastRunDate(TTDate::roundTime(time(), 60, 30));
         $this->Save(FALSE);
         $this->setExecuteFlag(FALSE);
         $lock_file->delete();
         return TRUE;
     } else {
         Debug::text('Job is currently running, skipping...', __FILE__, __LINE__, __METHOD__, 10);
     }
     return FALSE;
 }
    echo "\nOnly in CLI mode !\n\n";
    exit;
}
$rm = RepositoryManager::getInstance();
$pm = ProjectManager::getInstance();
$availableProject = $pm->getAvailableProject();
while (list($key, $project) = each($availableProject)) {
    // We must delete this var to be re-generated
    unset($rm->existingLanguage);
    // Define it as a project
    $pm->setProject($project['code']);
    /*
     * Lock
     * 
     */
    $lock = new LockFile('project_' . strtoupper($project['code']) . '_lock_update_data');
    if ($lock->lock()) {
        // Write into the lock the update position
        $lock->writeIntoLock('vcs_update');
        // VCS update
        $rm->updateRepository();
        // Write into the lock the update position
        $lock->writeIntoLock('cleanUp_DB');
        // Clean Up DB
        $rm->cleanUp();
        // Write into the lock the update position
        $lock->writeIntoLock('revcheck');
        // Start RevCheck
        $rm->applyRevCheck();
        // Write into the lock the update position
        $lock->writeIntoLock('checkErrors');