xref: /dokuwiki/bin/wantedpages.php (revision 018a837f3cecfae17b9547f38110335bd4ff2fe2)
1#!/usr/bin/php -d short_open_tag=on
2<?php
3#------------------------------------------------------------------------------
4ini_set('memory_limit','128M');
5if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
6require_once DOKU_INC.'inc/init.php';
7require_once DOKU_INC.'inc/common.php';
8require_once DOKU_INC.'inc/search.php';
9require_once DOKU_INC.'inc/cliopts.php';
10
11#------------------------------------------------------------------------------
12function usage() {
13    print "Usage: wantedpages.php [wiki:namespace]
14
15    Outputs a list of wanted pages (pages which have
16    internal links but do not yet exist).
17
18    If the optional [wiki:namespace] is not provided,
19    defaults to the root wiki namespace
20
21    OPTIONS
22        -h, --help get help
23";
24}
25
26#------------------------------------------------------------------------------
27define ('DW_DIR_CONTINUE',1);
28define ('DW_DIR_NS',2);
29define ('DW_DIR_PAGE',3);
30
31#------------------------------------------------------------------------------
32function dw_dir_filter($entry, $basepath) {
33    if ($entry == '.' || $entry == '..' ) {
34        return DW_DIR_CONTINUE;
35    }
36    if ( is_dir($basepath . '/' . $entry) ) {
37        if ( strpos($entry, '_') === 0 ) {
38            return DW_DIR_CONTINUE;
39        }
40        return DW_DIR_NS;
41    }
42    if ( preg_match('/\.txt$/',$entry) ) {
43        return DW_DIR_PAGE;
44    }
45    return DW_DIR_CONTINUE;
46}
47
48#------------------------------------------------------------------------------
49function dw_get_pages($dir) {
50    static $trunclen = NULL;
51    if ( !$trunclen ) {
52        global $conf;
53        $trunclen = strlen($conf['datadir'].':');
54    }
55
56    if ( !is_dir($dir) ) {
57        fwrite( STDERR, "Unable to read directory $dir\n");
58        exit(1);
59    }
60
61    $pages = array();
62    $dh = opendir($dir);
63    while ( FALSE !== ( $entry = readdir($dh) ) ) {
64        $status = dw_dir_filter($entry, $dir);
65        if ( $status == DW_DIR_CONTINUE ) {
66            continue;
67        } else if ( $status == DW_DIR_NS ) {
68            $pages = array_merge($pages, dw_get_pages($dir . '/' . $entry));
69        } else {
70            $page = array(
71                'id'  => pathID(substr($dir.'/'.$entry,$trunclen)),
72                'file'=> $dir.'/'.$entry,
73                );
74            $pages[] = $page;
75        }
76    }
77    closedir($dh);
78    return $pages;
79}
80
81#------------------------------------------------------------------------------
82function dw_internal_links($page) {
83    global $conf;
84    $instructions = p_get_instructions(file_get_contents($page['file']));
85    $links = array();
86    $cns = getNS($page['id']);
87    $exists = FALSE;
88    foreach($instructions as $ins){
89        if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
90            $mid = $ins[1][0];
91            resolve_pageid($cns,$mid,$exists);
92            if ( !$exists ) {
93								list($mid) = explode('#',$mid); //record pages without hashs
94                $links[] = $mid;
95            }
96        }
97    }
98    return $links;
99}
100
101#------------------------------------------------------------------------------
102$OPTS = Doku_Cli_Opts::getOptions(__FILE__,'h',array('help'));
103
104if ( $OPTS->isError() ) {
105    fwrite( STDERR, $OPTS->getMessage() . "\n");
106    exit(1);
107}
108
109if ( $OPTS->has('h') or $OPTS->has('help') ) {
110    usage();
111    exit(0);
112}
113
114$START_DIR = $conf['datadir'];
115
116if ( $OPTS->numArgs() == 1 ) {
117    $START_DIR .= '/' . $OPTS->arg(0);
118}
119
120#------------------------------------------------------------------------------
121$WANTED_PAGES = array();
122
123foreach ( dw_get_pages($START_DIR) as $WIKI_PAGE ) {
124    $WANTED_PAGES = array_merge($WANTED_PAGES,dw_internal_links($WIKI_PAGE));
125}
126$WANTED_PAGES = array_unique($WANTED_PAGES);
127sort($WANTED_PAGES);
128
129foreach ( $WANTED_PAGES as $WANTED_PAGE ) {
130    print $WANTED_PAGE."\n";
131}
132exit(0);
133