Not logged in. · Lost password · Register
Forum: General Help and Support Development RSS
How to know if a page exist on the wiki ?
Avatar
Alinea #1
Member since Jan 2013 · 28 posts · Location: France
Group memberships: Members
Show profile · Link to this post
Subject: How to know if a page exist on the wiki ?
Hello,
I want to create a template with tab (like "talk", but more than it), and use the css class "wikilink1" if the page exist and "wikilink2" if it doesn't exist.

For example, I have a tab for "general", "tab1", "tab2". Pages "general" and "tab1" exist but not "tab2".

But for the moment I can't find how know if "page on tab2" exist or not. I think I don't find the good variable for
file_exists($complet_link) ? 'wikilink1' : 'wikilink2';

Here, my complete code about tabs :
<?php
if (!defined("DOKU_INC")){
    die();
}


// Declare tab type (for links) :
$tab_category = array('general', 'animation', 'gameplay', 'dev', 'talk');
$number_cat = count($tab_category);
// Creation of the dynamic variable $name (TODO : rename it, stupid var).
for ($i = 0; $i < $number_cat; $i++) {
    $name = $tab_category[$i] ;
   
    // Retrieve the current link from the page and explode it so you can process each piece separately
$tab_actualid = explode(":", getID() ) ;

// Recover wiki url 
$kh_wikibase = DOKU_BASE ;

    // Links
    $name_link = $name ;
    // translations
    $khtab_loc = 'tab_' . $name . '_loc' ;
    $tab_general_loc = tpl_getLang("$khtab_loc");



// Detect if translation plugin is active or not. If yes, add the lang namespace ("fr", "en", etc.) at the beginning.
if ($translation) {
    $tab_listlang = explode(" ", $conf["plugin"]["translation"]["translations"]) ;
    // Make sure the first ns is a language thing.
    if (in_array($tab_actualid[0], $tab_listlang)) {
        // get the language namespace
        $tab_nsactuallang = "$tab_actualid[0]";
        // retrieves the last element of the array
        $lastid = array_pop($tab_actualid);
        // removes the first 2 elements of the table
        $firstid = array_slice($tab_actualid, 2);
        // glue up what's left
        $tab_actualns = implode(":", $firstid);
                // Génération des liens
        // if $tab_actualns is empty, we don't put an NS link
        if (empty($tab_actualns)) {
        // The fist tab (general article) is NOT in :general: NS, otherwise startpage don't work good.
            if ( $i == 0) {
            $tab_link = $tab_nsactuallang.":".$lastid;
            }
            else
            $tab_link = $tab_nsactuallang.":".$name_link.":".$lastid;

            }
        else {
            if ( $i == 0) {
            $tab_link = $tab_nsactuallang.":".$tab_actualns.":".$lastid;
            }
            else
            $tab_link = $tab_nsactuallang.":".$name_link.":".$tab_actualns.":".$lastid;
            }
        }
    else { // If it doesn't start with one of the languages, but there is something at the beginning and the translation plugin treated as without translation plugin, copied from below...
        $lastid = array_pop($tab_actualid);
        // removes the first element from the table (category)
        $firstid = array_slice($tab_actualid, 1);
        // glue up what's left
        $tab_actualns = implode(":", $firstid);
        if (empty($tab_actualns)) {
            // if NS is empty, don't create NS
            if ( $i == 0) {
                $tab_link = $lastid;
                }
            else
            $tab_link = $name_link.":".$lastid;
            }
            else {
            $tab_link = $tab_actualns.":".$lastid;
            $tab_link = $name_link.":".$tab_actualns.":".$lastid;
            }
        }
    }
// If there are not plugin Translation, we don't use "en" (or "fr", "de", etc.) and start with tab category
else
    {
    $lastid = array_pop($tab_actualid);
    // removes the first element from the table (category)
    $firstid = array_slice($tab_actualid, 1);
    // glue up what's left
    $tab_actualns = implode(":", $firstid);
    if (empty($tab_actualns)) {
        // if NS is empty, don't create NS
        if ( $i == 0) {
            $tab_link = $lastid;
            }
            else
            $tab_link = $name_link.":".$lastid;
        }
    else {
        if ( $i == 0) {
            $tab_link = $tab_actualns.":".$lastid;
            }
            else
            $tab_link = $name_link.":".$tab_actualns.":".$lastid;
        }
    }


$complet_link = "$kh_wikibase$tab_link" ;
// creation of as many tabs as links
        $class_khtab = file_exists($complet_link) ? 'wikilink1' : 'wikilink2';
        echo '<div class="khtab"><span><a href="' . "$complet_link" . '" class="' . "$class_khtab" . '" title="' . "$tab_link" . '" >' . "$tab_general_loc" . '</a></span></div>' ;


}


?>
Avatar
LarsDW223 #2
Member since Sep 2014 · 370 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
Hi,
I found this code snippet in inc/common.php, function userlink():

$shortcut = 'user';
$exists = null;
$data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists);
$data['link']['class'] .= ' interwiki iw_user';
if($exists !== null) {
    if($exists) {
        $data['link']['class'] .= ' wikilink1';
    } else {
        $data['link']['class'] .= ' wikilink2';
        $data['link']['rel'] = 'nofollow';
    }
}

Maybe you can use that code or at least it's a good point for starting a search yourself.
Avatar
turnermm (Moderator) #3
Member since Oct 2009 · 4446 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
In reply to post #1
There is already a function for this in pageutils.php:
   resolve_pageid($namesace,$page,$exists);
$exists is a reference, so should be set to null in advance of calling the function.  If $namespace:$page exists, $exists will be true after the function completes, otherwise false.
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
Avatar
andi (Administrator) #4
User title: splitbrain
Member since May 2006 · 3370 posts · Location: Berlin Germany
Group memberships: Administrators, Members
Show profile · Link to this post
There's also page_exists()
Read this if you don't get any useful answers.
Lies dies wenn du keine hilfreichen Antworten bekommst.
Avatar
Alinea #5
Member since Jan 2013 · 28 posts · Location: France
Group memberships: Members
Show profile · Link to this post
Thanks, it works :)

Thanks to your help, my links are displayed properly.

But I have a lot of design errors in this tabpage management. I think I should take it all back, and try to make it a plugin. If I make it a plugin, in which category? Action or Renderer ? I am not sure to understand specificity of both.
Avatar
gilcot #6
Member since Aug 2018 · 12 posts · Location: France
Group memberships: Members
Show profile · Link to this post
I have a similar issue...

I'm writing a plugin to grab data from our database.
There's a field with links to our DokuWiki.
So, when I retrieve those links back in the wiki, I want them to integrate correctly. :-)

But I don't understant how to use resolved_pageid or page_exists I must be missing some point.

Thanks.
Avatar
turnermm (Moderator) #7
Member since Oct 2009 · 4446 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
It depends what your database returns.  If it is a wiki namespace:id, then pass that into page_exists.  If it returns the full path to the page or if you can easily construct it, then use the php function file_exists.  If it returns a url, then you will have to extract the namespace:id from that.

This site gives a good account of the possible extraction methods:
    https://www.electrictoolbox.com/php-directory-name-filename/
See also:
   http://php.net/manual/en/function.parse-url.php
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
Avatar
gilcot #8
Member since Aug 2018 · 12 posts · Location: France
Group memberships: Members
Show profile · Link to this post
Thanks @turnermm

I undestood page_exists uses "namespace:pagename" and that's why I was in trouble... The values in the database are full URL...

Some of them look like "http://wiki.our.domain.name/doku.php?id=blah:foo:baz#duh" So I extracted "blah:foo:baz#duh" and pass it to page_exists without success. I've just found why: I have to strip the part starting with "#" (anchor in page) and it's good...

Some others look like "http://wiki.our.domain.name/blah/foo/baz#duh" That newer form because we switch later to nice URL rewriting. So, according to your reply, I have to extract "blah/foo/baz" then convert it to "blah:foo:baz" before. I'll try and let you know. Not sure file_exists will help here as it's not a true path to the file (saying that, where DokuWiki store the pages?)

Warm regards.
This post was edited on 2018-08-16, 02:32 by gilcot.
Avatar
turnermm (Moderator) #9
Member since Oct 2009 · 4446 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
Dokuwiki stores pages in data/pages.  The path to your DokuWiki is in the constant DOKU_INC. (See: devel:enironment#doku).  So if you get a pageid like: abc:def, then your path is DOKU_INC/abc/def.
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
Avatar
gilcot #10
Member since Aug 2018 · 12 posts · Location: France
Group memberships: Members
Show profile · Link to this post
Thanks for all those advices. I end up with this:
    function setAAttr($url_in) {
        $url_out = "<a href='$url_in' class='" ;
        $_href = parse_url($url_in, PHP_URL_SCHEME) ;
        if ($_href['scheme']) { // absolute URL
            if ($_href['scheme'] == 'mailto') {
                $url_out .= "mail' title='" . substr($url_in, 6) ;
            } elseif ($_href['scheme'] == 'smb') {
                $url_out .= "windows' title='" . substr($url_in, 3) ;
            } elseif ($_href['scheme'] == 'file') {
                $url_out .= "windows' title='" . substr($url_in, 4) ;
            } else {
                $_wiki = '' ;  // current InterWiki
                foreach (getInterWiki as $_key => $_val) {
                    if (strpos($url_in, strtok($_val, '{'), 0) !== false) {
                        $_wiki = $_key ;
                        break ;
                    }
                }
                if ($_wiki == 'this') {
                    if ($_href['path'] == '/doku.php') {
                        parse_str($_href['query'], $_arg) ;
                        $url_out .= 'wikilink' . (page_exists($_arg['id']) ? '1' : '2') ;
                        $url_out .= "' title='" . $_arg['id'] ;
                        unset($_arg) ;
                    } else {
                        if (file_exists(DOKU_INC . 'data/pages' . $_href['path'] . '.txt')) {
                            $url_out .= "wikilink1' title='" . str_replace('/', ':', ltrim($_href['path'], '/')) ;
                        } elseif (file_exists(rtrim(DOKU_INC, '/') . $_href['path'])) {
                            $url_out .= "interwiki iw_this' title='" . $_href['path'] ;
                        } else {
                            $url_out .= "wikilink2' title='" . str_replace('/', ':', ltrim($_href['path'], '/')) ;
                        }
                    }
                } else {
                    $url_out .=  ($_wiki ? "interwiki iw_$_wiki" : 'urlextern') ;
                }
            }
        } else { // relative URL
            if ($_href['path'] == '/doku.php') {
                parse_str($_href['query'], $_arg) ;
                $_page = $_arg['id'] ;
            } else {
                $_page = str_replace('/', ':', $_href['path'], $_nbr) ;
            }
            $url_out .= 'wikilink' . (page_exists($_page) ? '1' : '2') ;
            $url_out .= "' title='$_page" ;
        }
        $url_out .= "'>" ;
        return "$url_out" ;
    }

I'm posting it here as example to help those who will read the thread later, but I didn't test it yet...
Let me know if you find some logical error or some misuse of DokuWiki internals.
Avatar
andi (Administrator) #11
User title: splitbrain
Member since May 2006 · 3370 posts · Location: Berlin Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Woah. Nobody will be able to point out any logic errors in that. This needs to be refactored to reduce complexity.
Read this if you don't get any useful answers.
Lies dies wenn du keine hilfreichen Antworten bekommst.
Avatar
turnermm (Moderator) #12
Member since Oct 2009 · 4446 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
Amen!
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-01-17, 20:42:29 (UTC +01:00)