mgerring / WordPress-Wiki

A wiki plugin for WordPress
http://wp-wiki.org
19 stars 1 forks source link

namespaces appear implemented in parser but don't quite work yet - fix supplied #8

Closed arjenlentz closed 13 years ago

arjenlentz commented 13 years ago

[[NameSpace:PageName]] as per mediawiki internal link syntax appears implemented in WPW_WikiParser.php but doesn't quite work...

If I manually create/modify a wiki page to have a parent in the hierarchy, the following fix sorts out the url for references to it: if ($this->reference_wiki) { // 2010-12-31 arjen - changed ':' to '/' for href $href = $this->reference_wiki.($namespace?$namespace.'/':'').$this->wiki_link($href); } else {

So far so good!

For new pages, you then get a href url like http://domain/wiki/SpaceName/new?redlink=1&title=PageName but the namespace doesn't get set up as the hierarchy/parent when the new page is created... this may be a rewrite rule problem (preventing it from getting passed) but doubtful on that since it does work out for existing pages.

arjenlentz commented 13 years ago

Improved partial fix... Doing it in this method is cleaner, can even handle spaces in namespaces, lowercases them properly (not critical but looks cleaner and external link refs could assume that different cases refer to different pages), but most importantly incorporates the namespace into the Topic/Title as it should be.

It's tested for existing pages and wikilinks with namespaces and manually set parents. And for a new page, you now get: http://domain/wiki/SpaceName/new?redlink=1&title=SpaceName:PageName Now all we need to sort out is a way to have a new page end up with the right parent, so its url is set correctly.

class WPW_WikiParser extends WikiParser { //parent::WikiParser(); // 2011-01-02 arjen adjusted to deal with namespaces function wiki_link($topic,$namespace='') { global $wpdb; $wiki = $wpdb->get_var('SELECT p.id FROM ' . $wpdb->posts . ' p WHERE p.post_type = "wiki" AND p.post_name = "' . str_replace(' ', '-', $topic) .'"');

            if (!$wiki)
                    return ($namespace ? strtolower(str_replace(' ', '-', $namespace)).'/' : '').'new?redlink=1&title='.($namespace ? $namespace.':' : '').$topic;
            else
                    return ($namespace ? strtolower(str_replace(' ', '-', $namespace)).'/' : '').strtolower(str_replace(' ','-',$topic));
    }

...

            if ($this->reference_wiki) {
                    // 2011-01-02 arjen adjusted to deal with namespaces
                    $href = $this->reference_wiki.$this->wiki_link($href,$namespace);
            } else {
arjenlentz commented 13 years ago

Ok got it! In addition to the above patch to the wikiparser, we need this:

In wp-wiki.php:

// 2011-01-02 arjen adjusted to deal with namespaces function wpw_create_new_and_redirect() { //echo 'workin?'; if (isset($_GET['new_wiki_page']) && $_GET['new_wiki_page'] == 'true' && wp_verify_nonce($_GET['nonce'], 'wpw_new_page_nonce')) {

    global $wp_version;
    global $wpdb;

    $new_wiki = array();

    $title = strip_tags($_GET['title']);
    $pieces = explode(':',$title,2);
    if (count($pieces) == 2) {
            list($namespace,$topic) = $pieces;
            $namespace = strtolower(str_replace(' ','-',$namespace));
            $parent_id = $wpdb->get_var('SELECT id FROM `' . $wpdb->posts . '` WHERE post_name = "' . $namespace .'"');
            if ($parent_id)
                    $new_wiki['post_parent'] = $parent_id;
    }
    else {  
            $namespace = '';
            $topic = $title;
    }
    $topic = strtolower(str_replace(' ','-',$topic));
    $url = get_option('siteurl') . '/wiki/' . ($namespace ? $namespace.'/' : '') . $topic;

    $new_wiki['post_name'] = $topic;
    $new_wiki['post_title'] = $title;
    $new_wiki['guid'] = $url;
    $new_wiki['post_status'] = 'publish';

    if ($wp_version >= 3.0) {
            $new_wiki['post_type'] = 'wiki';
    }

    $new_wiki_id = wp_insert_post($new_wiki);

    if($wp_version <= 3.0) {
            update_post_meta($new_wiki_id, '_wiki_page', 1);
    }

    wp_redirect( $url );
    exit();
    }

}

arjenlentz commented 13 years ago

Calculated URLs should not end up with multiple dashes, resulting from titles with constructs like 'foo - bar'. str_replace() can't cope with this, so we use preg_replace(). This patch replaces/combines all the earlier one noted in this bug.

In WPW_WikiParser.php:

class WPW_WikiParser extends WikiParser { //parent::WikiParser(); // 2011-01-02 arjen adjusted to deal with namespaces function wiki_link($topic,$namespace='') { global $wpdb; $wiki = $wpdb->get_var('SELECT p.id FROM ' . $wpdb->posts . ' p WHERE p.post_type = "wiki" AND p.post_name = "' . preg_replace('/[ -]+/', '-', $topic) .'"');

            if (!$wiki)
                    return 'new?redlink=1&title='.($namespace ? $namespace.':' : '').$topic;
            else
                    return ($namespace ? strtolower(preg_replace('/[ -]+/', '-', $namespace)).'/' : '') . strtolower(preg_replace('/[ -]+/', '-', $topic));
    }

    // 2011-01-02 arjen adjusted to deal with namespaces
    function handle_internallink($matches) {
            global $wpdb;
            //var_dump($matches);
            $nolink = false;

            $href = $matches[4];
            $title = $matches[6] ? $matches[6] : $href.$matches[7];
            $namespace = $matches[3];

            if ($namespace=='Image') {
                    $options = explode('|',$title);
                    $title = array_pop($options);

                    return $this->handle_image($href,$title,$options);
            }

            $title = preg_replace('/\(.*?\)/','',$title);
            $title = preg_replace('/^.*?\:/','',$title);
            $wiki = $wpdb->get_var('SELECT `p`.`id` FROM `' . $wpdb->posts . '` `p` WHERE `p`.`post_type` = "wiki" AND `p`.`post_name` = "' . preg_replace('/[ -]+/', '-', $href) .'"');

            if(!$wiki)
                    $redlink = 'style="color:red"';
            else
                    $redlink = false;

            if ($this->reference_wiki) {
                    $href = $this->reference_wiki.$this->wiki_link($href,$namespace);
            } else {
                    $nolink = true;
            }

... and in wp-wiki.php:

// 2011-01-02 arjen adjusted to deal with namespaces function wpw_create_new_and_redirect() { //echo 'workin?'; if (isset($_GET['new_wiki_page']) && $_GET['new_wiki_page'] == 'true' && wp_verify_nonce($_GET['nonce'], 'wpw_new_page_nonce')) {

    global $wp_version;
    global $wpdb;

    $new_wiki = array();

    $title = strip_tags($_GET['title']);
    $pieces = explode(':',$title,2);
    if (count($pieces) == 2) {
            list($namespace,$topic) = $pieces;
            $namespace = strtolower(preg_replace('/[ -]+/', '-', $namespace));
            $parent_id = $wpdb->get_var('SELECT id FROM `' . $wpdb->posts . '` WHERE post_name = "' . $namespace .'"');
            if ($parent_id)
                    $new_wiki['post_parent'] = $parent_id;
    }
    else {  
            $namespace = '';
            $topic = $title;
    }
    $topic = strtolower(preg_replace('/[ -]+/', '-', $topic));
    $url = get_option('siteurl') . '/wiki/' . ($namespace ? $namespace.'/' : '') . $topic;

    $new_wiki['post_name'] = $topic;
    $new_wiki['post_title'] = $title;
    $new_wiki['guid'] = $url;
    $new_wiki['post_status'] = 'publish';

    if ($wp_version >= 3.0) {
            $new_wiki['post_type'] = 'wiki';
    }

    $new_wiki_id = wp_insert_post($new_wiki);

    if($wp_version <= 3.0) {
            update_post_meta($new_wiki_id, '_wiki_page', 1);
    }

    wp_redirect( $url );
    exit();
    }

}

mgerring commented 13 years ago

Wow! Fantastic work! I'll test this out and get it in there ASAP.

arjenlentz commented 13 years ago

tnx - you're welcome ;-)