fixed some test cases
[dokuwiki/radio.git] / bin / wantedpages.php
blob30171fc15f84ce86fb3eb40403cc5657e14c6d33
1 #!/usr/bin/php
2 <?php
3 if ('cli' != php_sapi_name()) die();
5 #------------------------------------------------------------------------------
6 ini_set('memory_limit','128M');
7 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
8 require_once DOKU_INC.'inc/init.php';
9 require_once DOKU_INC.'inc/common.php';
10 require_once DOKU_INC.'inc/search.php';
11 require_once DOKU_INC.'inc/cliopts.php';
13 #------------------------------------------------------------------------------
14 function usage() {
15 print "Usage: wantedpages.php [wiki:namespace]
17 Outputs a list of wanted pages (pages which have
18 internal links but do not yet exist).
20 If the optional [wiki:namespace] is not provided,
21 defaults to the root wiki namespace
23 OPTIONS
24 -h, --help get help
28 #------------------------------------------------------------------------------
29 define ('DW_DIR_CONTINUE',1);
30 define ('DW_DIR_NS',2);
31 define ('DW_DIR_PAGE',3);
33 #------------------------------------------------------------------------------
34 function dw_dir_filter($entry, $basepath) {
35 if ($entry == '.' || $entry == '..' ) {
36 return DW_DIR_CONTINUE;
38 if ( is_dir($basepath . '/' . $entry) ) {
39 if ( strpos($entry, '_') === 0 ) {
40 return DW_DIR_CONTINUE;
42 return DW_DIR_NS;
44 if ( preg_match('/\.txt$/',$entry) ) {
45 return DW_DIR_PAGE;
47 return DW_DIR_CONTINUE;
50 #------------------------------------------------------------------------------
51 function dw_get_pages($dir) {
52 static $trunclen = NULL;
53 if ( !$trunclen ) {
54 global $conf;
55 $trunclen = strlen($conf['datadir'].':');
58 if ( !is_dir($dir) ) {
59 fwrite( STDERR, "Unable to read directory $dir\n");
60 exit(1);
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,
76 $pages[] = $page;
79 closedir($dh);
80 return $pages;
83 #------------------------------------------------------------------------------
84 function 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;
100 return $links;
103 #------------------------------------------------------------------------------
104 $OPTS = Doku_Cli_Opts::getOptions(__FILE__,'h',array('help'));
106 if ( $OPTS->isError() ) {
107 fwrite( STDERR, $OPTS->getMessage() . "\n");
108 exit(1);
111 if ( $OPTS->has('h') or $OPTS->has('help') ) {
112 usage();
113 exit(0);
116 $START_DIR = $conf['datadir'];
118 if ( $OPTS->numArgs() == 1 ) {
119 $START_DIR .= '/' . $OPTS->arg(0);
122 #------------------------------------------------------------------------------
123 $WANTED_PAGES = array();
125 foreach ( dw_get_pages($START_DIR) as $WIKI_PAGE ) {
126 $WANTED_PAGES = array_merge($WANTED_PAGES,dw_internal_links($WIKI_PAGE));
128 $WANTED_PAGES = array_unique($WANTED_PAGES);
129 sort($WANTED_PAGES);
131 foreach ( $WANTED_PAGES as $WANTED_PAGE ) {
132 print $WANTED_PAGE."\n";
134 exit(0);