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