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
#------------------------------------------------------------------------------
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
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;
44
if ( preg_match('/\.txt$/',$entry) ) {
47
return DW_DIR_CONTINUE;
50
#------------------------------------------------------------------------------
51
function dw_get_pages($dir) {
52
static $trunclen = null;
55
$trunclen = strlen($conf['datadir'].':');
58
if ( !is_dir($dir) ) {
59
fwrite( STDERR, "Unable to read directory $dir\n");
65
while ( false !== ( $entry = readdir($dh) ) ) {
66
$status = dw_dir_filter($entry, $dir);
67
if ( $status == DW_DIR_CONTINUE ) {
69
} else if ( $status == DW_DIR_NS ) {
70
$pages = array_merge($pages, dw_get_pages($dir . '/' . $entry));
3
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');
4
define('NOSESSION', 1);
5
require_once(DOKU_INC.'inc/init.php');
10
class WantedPagesCLI extends DokuCLI {
12
const DIR_CONTINUE = 1;
17
* Register options and arguments on the given $options object
19
* @param DokuCLI_Options $options
22
protected function setup(DokuCLI_Options $options) {
24
'Outputs a list of wanted pages (pages which have internal links but do not yet exist).'
26
$options->registerArgument(
28
'The namespace to lookup. Defaults to root namespace',
36
* Arguments and options have been parsed when this is run
38
* @param DokuCLI_Options $options
41
protected function main(DokuCLI_Options $options) {
44
$startdir = dirname(wikiFN($options->args[0].':xxx'));
73
'id' => pathID(substr($dir.'/'.$entry,$trunclen)),
74
'file'=> $dir.'/'.$entry,
46
$startdir = dirname(wikiFN('xxx'));
49
$this->info("searching $startdir");
51
$wanted_pages = array();
53
foreach($this->get_pages($startdir) as $page) {
54
$wanted_pages = array_merge($wanted_pages, $this->internal_links($page));
56
$wanted_pages = array_unique($wanted_pages);
59
foreach($wanted_pages as $page) {
65
* Determine directions of the search loop
67
* @param string $entry
68
* @param string $basepath
71
protected function dir_filter($entry, $basepath) {
72
if($entry == '.' || $entry == '..') {
73
return WantedPagesCLI::DIR_CONTINUE;
75
if(is_dir($basepath.'/'.$entry)) {
76
if(strpos($entry, '_') === 0) {
77
return WantedPagesCLI::DIR_CONTINUE;
79
return WantedPagesCLI::DIR_NS;
81
if(preg_match('/\.txt$/', $entry)) {
82
return WantedPagesCLI::DIR_PAGE;
84
return WantedPagesCLI::DIR_CONTINUE;
88
* Collects recursively the pages in a namespace
92
* @throws DokuCLI_Exception
94
protected function get_pages($dir) {
95
static $trunclen = null;
98
$trunclen = strlen($conf['datadir'].':');
102
throw new DokuCLI_Exception("Unable to read directory $dir");
107
while(false !== ($entry = readdir($dh))) {
108
$status = $this->dir_filter($entry, $dir);
109
if($status == WantedPagesCLI::DIR_CONTINUE) {
111
} else if($status == WantedPagesCLI::DIR_NS) {
112
$pages = array_merge($pages, $this->get_pages($dir.'/'.$entry));
115
'id' => pathID(substr($dir.'/'.$entry, $trunclen)),
116
'file' => $dir.'/'.$entry,
83
#------------------------------------------------------------------------------
84
function dw_internal_links($page) {
86
$instructions = p_get_instructions(file_get_contents($page['file']));
88
$cns = getNS($page['id']);
90
foreach($instructions as $ins){
91
if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
93
resolve_pageid($cns,$mid,$exists);
95
list($mid) = explode('#',$mid); //record pages without hashs
103
#------------------------------------------------------------------------------
104
$OPTS = Doku_Cli_Opts::getOptions(__FILE__,'h',array('help'));
106
if ( $OPTS->isError() ) {
107
fwrite( STDERR, $OPTS->getMessage() . "\n");
111
if ( $OPTS->has('h') or $OPTS->has('help') ) {
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);
131
foreach ( $WANTED_PAGES as $WANTED_PAGE ) {
132
print $WANTED_PAGE."\n";
126
* Parse instructions and returns the non-existing links
128
* @param array $page array with page id and file path
131
function internal_links($page) {
133
$instructions = p_get_instructions(file_get_contents($page['file']));
135
$cns = getNS($page['id']);
137
foreach($instructions as $ins) {
138
if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink')) {
140
resolve_pageid($cns, $mid, $exists);
142
list($mid) = explode('#', $mid); //record pages without hashs
152
$cli = new WantedPagesCLI();
b'\\ No newline at end of file'