Exemplo n.º 1
0
function draft_save(&$entry, $id = null, $update_index = false, $update_date = false)
{
    if (!$id) {
        $id = bdb_idfromtime('entry', $entry['date']);
    }
    $ed = entry_dir($id);
    $dd = draft_dir($id);
    if (file_exists($ed . EXT)) {
        // move collateral files
        @rename($ed, $dd);
        if ($update_index) {
            // delete normal entry
            fs_delete($ed . EXT);
            // remove from normal flow
            $o =& entry_init();
            $o->delete($id, null);
        }
    }
    $new_entry = entry_prepare($entry);
    if ($new_entry['categories']) {
        $new_entry['categories'] = implode(',', $entry['categories']);
    } else {
        unset($new_entry['categories']);
    }
    $string = utils_kimplode($new_entry);
    if (!io_write_file($dd . EXT, $string)) {
        return false;
    } else {
        return $id;
    }
    return false;
}
Exemplo n.º 2
0
/**
 * function bdb_save_comment
 *
 * <p>Saves the content of the $comment array, associating it to the entry-ID $id.</p>
 * <p>$comment must be formatted as the one returned by {@link bdb_parse_entry()}.</p>
 * <p>Returns true on success, or false on failure</p>
 *
 * @param string $id string formatted like "prefixYYMMDD-HHMMSS"
 * @param array $comment array formatted as the one returned by {@link bdb_parse_entry()}
 * @return bool
 * 
 * @see bdb_parse_entry()
 */
function comment_save($id, $comment)
{
    comment_clean($comment);
    $comment = array_change_key_case($comment, CASE_UPPER);
    $comment_dir = bdb_idtofile($id, BDB_COMMENT);
    if (!isset($comment['DATE'])) {
        $comment['DATE'] = date_time();
    }
    $id = bdb_idfromtime(BDB_COMMENT, $comment['DATE']);
    $f = $comment_dir . $id . EXT;
    $str = utils_kimplode($comment);
    if (io_write_file($f, $str)) {
        return $id;
    }
    return false;
}
Exemplo n.º 3
0
/**
 *
 * @param array entry 	contents
 * @param string|null 	entry id, null if can be deducted from the date field of $entry; 
 * 						defaults to null
 *
 * @param bool 			updates entry index; defaults to true	
 *
 *
 * @return integer 		-1 failure while storing preliminar draft, abort. Index not touched.
 * 						-2 index updated succesfully, but draft doesn't exist anymore 
 * 						   (should never happen!) OR
 * 						   failure while trying to move draft to entry path, draft does not exist anymore
 * 						   index not touched
 * 						-3 error while moving draft still exists, index written succesfully but rolled back
 * 						-4 failure while saving to index, aborted (draft still exists)
 *
 *
 */
function entry_save($entry, $id = null, $update_index = true)
{
    // PHASE 1 : prepare entry
    if (!$id) {
        if (!@$entry['date']) {
            $entry['date'] = date_time();
        }
        $id = bdb_idfromtime(BDB_ENTRY, $entry['date']);
    }
    // PHASE 2 : Store
    // secure data as DRAFT
    // (entry is also implicitly entry_prepare()'d here)
    $ret = draft_save($entry, $id);
    do_action('publish_post', $id, $entry);
    if ($ret === false) {
        return -1;
        // FAILURE: ABORT
    }
    // PHASE 3 : Update index
    $delete_cats = array();
    $all_cats = @$entry['categories'];
    $update_title = true;
    if ($old_entry = entry_parse($id)) {
        if ($all_cats) {
            $delete_cats = array_diff($old_entry['categories'], $all_cats);
        }
        $all_cats = $all_cats ? array_merge($all_cats, $old_entry['categories']) : $old_entry['categories'];
        $update_title = $entry['subject'] != $old_entry['subject'];
    }
    /*
    echo 'old';
    print_r($old_entry['categories']);
    echo 'new';
    print_r($entry['categories']);
    echo 'del';
    print_r($delete_cats);
    echo 'all';
    print_r($all_cats);
    */
    $INDEX =& entry_init();
    $ok = $update_index ? $INDEX->add($id, $entry, $delete_cats, $update_title) : true;
    // PHASE 4 : index updated; let's move back the entry
    if ($ok) {
        $entryd = entry_dir($id, true);
        $entryf = $entryd . $id . EXT;
        $draftf = draft_exists($id);
        if ($draftf === false) {
            // this should never happen!
            if ($update_index) {
                $INDEX->delete($id, $all_cats);
            }
            return -2;
        }
        fs_delete($entryf);
        fs_mkdir($entryd);
        $ret = rename($draftf, $entryf);
        if (!$ret) {
            if (draft_exists($id)) {
                // rollback changes in the index
                // (keep the draft file)
                if ($update_index) {
                    $INDEX->delete($id, $all_cats);
                }
                return -3;
            } else {
                return -2;
            }
        } else {
            // SUCCESS : delete draft, move comments along
            draft_to_entry($id);
            return $id;
        }
    }
    return -4;
}