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