Ejemplo n.º 1
0
/**
 * sets metadata elements of a page
 *
 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
 *
 * @param String  $id         is the ID of a wiki page
 * @param Array   $data       is an array with key ⇒ value pairs to be set in the metadata
 * @param Boolean $render     whether or not the page metadata should be generated with the renderer
 * @param Boolean $persistent indicates whether or not the particular metadata value will persist through
 *                            the next metadata rendering.
 * @return boolean true on success
 *
 * @author Esther Brunner <*****@*****.**>
 * @author Michael Hamann <*****@*****.**>
 */
function p_set_metadata($id, $data, $render = false, $persistent = true)
{
    if (!is_array($data)) {
        return false;
    }
    global $ID, $METADATA_RENDERERS;
    // if there is currently a renderer change the data in the renderer instead
    if (isset($METADATA_RENDERERS[$id])) {
        $orig =& $METADATA_RENDERERS[$id];
        $meta = $orig;
    } else {
        // cache the current page
        $cache = $ID == $id;
        $orig = p_read_metadata($id, $cache);
        // render metadata first?
        $meta = $render ? p_render_metadata($id, $orig) : $orig;
    }
    // now add the passed metadata
    $protected = array('description', 'date', 'contributor');
    foreach ($data as $key => $value) {
        // be careful with sub-arrays of $meta['relation']
        if ($key == 'relation') {
            foreach ($value as $subkey => $subvalue) {
                $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue;
                if ($persistent) {
                    $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue;
                }
            }
            // be careful with some senisitive arrays of $meta
        } elseif (in_array($key, $protected)) {
            // these keys, must have subkeys - a legitimate value must be an array
            if (is_array($value)) {
                $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key], $value) : $value;
                if ($persistent) {
                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key], $value) : $value;
                }
            }
            // no special treatment for the rest
        } else {
            $meta['current'][$key] = $value;
            if ($persistent) {
                $meta['persistent'][$key] = $value;
            }
        }
    }
    // save only if metadata changed
    if ($meta == $orig) {
        return true;
    }
    if (isset($METADATA_RENDERERS[$id])) {
        // set both keys individually as the renderer has references to the individual keys
        $METADATA_RENDERERS[$id]['current'] = $meta['current'];
        $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
        return true;
    } else {
        return p_save_metadata($id, $meta);
    }
}
Ejemplo n.º 2
0
/**
 * sets metadata elements of a page
 *
 * @author Esther Brunner <*****@*****.**>
 */
function p_set_metadata($id, $data, $render = false, $persistent = true)
{
    if (!is_array($data)) {
        return false;
    }
    global $ID;
    // cache the current page
    $cache = $ID == $id;
    $orig = p_read_metadata($id, $cache);
    // render metadata first?
    $meta = $render ? p_render_metadata($id, $orig) : $orig;
    // now add the passed metadata
    $protected = array('description', 'date', 'contributor');
    foreach ($data as $key => $value) {
        // be careful with sub-arrays of $meta['relation']
        if ($key == 'relation') {
            foreach ($value as $subkey => $subvalue) {
                $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue;
                if ($persistent) {
                    $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue;
                }
            }
            // be careful with some senisitive arrays of $meta
        } elseif (in_array($key, $protected)) {
            // these keys, must have subkeys - a legitimate value must be an array
            if (is_array($value)) {
                $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key], $value) : $value;
                if ($persistent) {
                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key], $value) : $value;
                }
            }
            // no special treatment for the rest
        } else {
            $meta['current'][$key] = $value;
            if ($persistent) {
                $meta['persistent'][$key] = $value;
            }
        }
    }
    // save only if metadata changed
    if ($meta == $orig) {
        return true;
    }
    // sync cached copies, including $INFO metadata
    global $cache_metadata, $INFO;
    if (!empty($cache_metadata[$id])) {
        $cache_metadata[$id] = $meta;
    }
    if (!empty($INFO) && $id == $INFO['id']) {
        $INFO['meta'] = $meta['current'];
    }
    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
}
Ejemplo n.º 3
0
/**
 * sets metadata elements of a page
 *
 * @author Esther Brunner <*****@*****.**>
 */
function p_set_metadata($id, $data, $render = false)
{
    if (!is_array($data)) {
        return false;
    }
    $orig = p_get_metadata($id);
    // render metadata first?
    if ($render) {
        $meta = p_render_metadata($id, $orig);
    } else {
        $meta = $orig;
    }
    // now add the passed metadata
    $protected = array('description', 'date', 'contributor');
    foreach ($data as $key => $value) {
        // be careful with sub-arrays of $meta['relation']
        if ($key == 'relation') {
            foreach ($value as $subkey => $subvalue) {
                $meta[$key][$subkey] = array_merge($meta[$key][$subkey], $subvalue);
            }
            // be careful with some senisitive arrays of $meta
        } elseif (in_array($key, $protected)) {
            if (is_array($value)) {
                #FIXME not sure if this is the intended thing:
                if (!is_array($meta[$key])) {
                    $meta[$key] = array($meta[$key]);
                }
                $meta[$key] = array_merge($meta[$key], $value);
            }
            // no special treatment for the rest
        } else {
            $meta[$key] = $value;
        }
    }
    // save only if metadata changed
    if ($meta == $orig) {
        return true;
    }
    // check if current page metadata has been altered - if so sync the changes
    global $INFO;
    if ($id == $INFO['id'] && isset($INFO['meta'])) {
        $INFO['meta'] = $meta;
    }
    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
}
Ejemplo n.º 4
0
/**
 * Will render the metadata for the page if not exists yet
 *
 * This makes sure pages which are created from outside DokuWiki will
 * gain their data when viewed for the first time.
 */
function metaUpdate()
{
    global $ID;
    print "metaUpdate(): started" . NL;
    if (!$ID) {
        return false;
    }
    $file = metaFN($ID, '.meta');
    echo "meta file: {$file}" . NL;
    // rendering needed?
    if (@file_exists($file)) {
        return false;
    }
    if (!@file_exists(wikiFN($ID))) {
        return false;
    }
    require_once DOKU_INC . 'inc/common.php';
    require_once DOKU_INC . 'inc/parserutils.php';
    global $conf;
    // gather some additional info from changelog
    $info = io_grep($conf['changelog'], '/^(\\d+)\\t(\\d+\\.\\d+\\.\\d+\\.\\d+)\\t' . preg_quote($ID, '/') . '\\t([^\\t]+)\\t([^\\t\\n]+)/', 0, true);
    $meta = array();
    if (!empty($info)) {
        $meta['date']['created'] = $info[0][1];
        foreach ($info as $item) {
            if ($item[4] != '*') {
                $meta['date']['modified'] = $item[1];
                if ($item[3]) {
                    $meta['contributor'][$item[3]] = $item[3];
                }
            }
        }
    }
    $meta = p_render_metadata($ID, $meta);
    io_saveFile($file, serialize($meta));
    echo "metaUpdate(): finished" . NL;
    return true;
}
Ejemplo n.º 5
0
            if (!$exists) {
                list($mid) = explode('#', $mid);
                //record pages without hashs
                $links[] = $mid;
            }
        }
    }
    return $links;
}
#------------------------------------------------------------------------------
$OPTS = Doku_Cli_Opts::getOptions(__FILE__, 'h', array('help'));
if ($OPTS->isError()) {
    fwrite(STDERR, $OPTS->getMessage() . "\n");
    exit(1);
}
if ($OPTS->has('h') or $OPTS->has('help')) {
    usage();
    exit(0);
}
$START_DIR = $conf['datadir'];
if ($OPTS->numArgs() == 1) {
    $START_DIR .= '/' . $OPTS->arg(0);
}
#------------------------------------------------------------------------------
foreach (dw_get_pages($START_DIR) as $WIKI_PAGE) {
    $meta = p_read_metadata($WIKI_PAGE['id'], true);
    $meta = p_render_metadata($WIKI_PAGE['id'], $meta);
    io_saveFile(metaFN($id, '.meta'), serialize($meta));
    echo $WIKI_PAGE['id'] . "\n";
}
exit(0);
Ejemplo n.º 6
0
    /**
     * Handles the INDEXER_PAGE_ADD event, prevents indexing of metadata from included pages that aren't public if enabled
     *
     * @param Doku_Event $event  the event object
     * @param array      $params optional parameters (unused)
     */
    public function handle_indexer(Doku_Event $event, $params) {
        global $USERINFO;

        // check if the feature is enabled at all
        if (!$this->getConf('safeindex')) return;

        // is there a user logged in at all? If not everything is fine already
        if (is_null($USERINFO) && !isset($_SERVER['REMOTE_USER'])) return;

        // get the include metadata in order to see which pages were included
        $inclmeta = p_get_metadata($event->data['page'], 'plugin_include', METADATA_RENDER_UNLIMITED);
        $all_public = true; // are all included pages public?
        // check if the current metadata indicates that non-public pages were included
        if ($inclmeta !== null && isset($inclmeta['pages'])) {
            foreach ($inclmeta['pages'] as $page) {
                if (auth_aclcheck($page['id'], '', array()) < AUTH_READ) { // is $page public?
                    $all_public = false;
                    break;
                }
            }
        }

        if (!$all_public) { // there were non-public pages included - action required!
            // backup the user information
            $userinfo_backup = $USERINFO;
            $remote_user = $_SERVER['REMOTE_USER'];
            // unset user information - temporary logoff!
            $USERINFO = null;
            unset($_SERVER['REMOTE_USER']);

            // metadata is only rendered once for a page in one request - thus we need to render manually.
            $meta = p_read_metadata($event->data['page']); // load the original metdata
            $meta = p_render_metadata($event->data['page'], $meta); // render the metadata
            p_save_metadata($event->data['page'], $meta); // save the metadata so other event handlers get the public metadata, too

            $meta = $meta['current']; // we are only interested in current metadata.

            // check if the tag plugin handler has already been called before the include plugin
            $tag_called = isset($event->data['metadata']['subject']);

            // Reset the metadata in the renderer. This removes data from all other event handlers, but we need to be on the safe side here.
            $event->data['metadata'] = array('title' => $meta['title']);

            // restore the relation references metadata
            if (isset($meta['relation']['references'])) {
                $event->data['metadata']['relation_references'] = array_keys($meta['relation']['references']);
            } else {
                $event->data['metadata']['relation_references'] = array();
            }

            // restore the tag metadata if the tag plugin handler has been called before the include plugin handler.
            if ($tag_called) {
                $tag_helper = $this->loadHelper('tag', false);
                if ($tag_helper) {
                    if (isset($meta['subject']))  {
                        $event->data['metadata']['subject'] = $tag_helper->_cleanTagList($meta['subject']);
                    } else {
                        $event->data['metadata']['subject'] = array();
                    }
                }
            }

            // restore user information
            $USERINFO = $userinfo_backup;
            $_SERVER['REMOTE_USER'] = $remote_user;
        }
    }