~yoboy-leguesh/ubuntu-fr-doc/maj20150810a

« back to all changes in this revision

Viewing changes to bin/wantedpages.php

  • Committer: YoBoY
  • Date: 2015-11-11 10:05:14 UTC
  • Revision ID: yoboy.leguesh@gmail.com-20151111100514-bw7p06lrhban4g2t
Mise à jour vers Dokuwiki 2015-08-10a avec nos patchs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/php
2
2
<?php
3
 
if ('cli' != php_sapi_name()) die();
4
 
 
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';
12
 
 
13
 
#------------------------------------------------------------------------------
14
 
function 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
 
#------------------------------------------------------------------------------
29
 
define ('DW_DIR_CONTINUE',1);
30
 
define ('DW_DIR_NS',2);
31
 
define ('DW_DIR_PAGE',3);
32
 
 
33
 
#------------------------------------------------------------------------------
34
 
function 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
 
#------------------------------------------------------------------------------
51
 
function 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));
 
3
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');
 
4
define('NOSESSION', 1);
 
5
require_once(DOKU_INC.'inc/init.php');
 
6
 
 
7
/**
 
8
 * Find wanted pages
 
9
 */
 
10
class WantedPagesCLI extends DokuCLI {
 
11
 
 
12
    const DIR_CONTINUE = 1;
 
13
    const DIR_NS       = 2;
 
14
    const DIR_PAGE     = 3;
 
15
 
 
16
    /**
 
17
     * Register options and arguments on the given $options object
 
18
     *
 
19
     * @param DokuCLI_Options $options
 
20
     * @return void
 
21
     */
 
22
    protected function setup(DokuCLI_Options $options) {
 
23
        $options->setHelp(
 
24
            'Outputs a list of wanted pages (pages which have internal links but do not yet exist).'
 
25
        );
 
26
        $options->registerArgument(
 
27
            'namespace',
 
28
            'The namespace to lookup. Defaults to root namespace',
 
29
            false
 
30
        );
 
31
    }
 
32
 
 
33
    /**
 
34
     * Your main program
 
35
     *
 
36
     * Arguments and options have been parsed when this is run
 
37
     *
 
38
     * @param DokuCLI_Options $options
 
39
     * @return void
 
40
     */
 
41
    protected function main(DokuCLI_Options $options) {
 
42
 
 
43
        if($options->args) {
 
44
            $startdir = dirname(wikiFN($options->args[0].':xxx'));
71
45
        } else {
72
 
            $page = array(
73
 
                'id'  => pathID(substr($dir.'/'.$entry,$trunclen)),
74
 
                'file'=> $dir.'/'.$entry,
 
46
            $startdir = dirname(wikiFN('xxx'));
 
47
        }
 
48
 
 
49
        $this->info("searching $startdir");
 
50
 
 
51
        $wanted_pages = array();
 
52
 
 
53
        foreach($this->get_pages($startdir) as $page) {
 
54
            $wanted_pages = array_merge($wanted_pages, $this->internal_links($page));
 
55
        }
 
56
        $wanted_pages = array_unique($wanted_pages);
 
57
        sort($wanted_pages);
 
58
 
 
59
        foreach($wanted_pages as $page) {
 
60
            print $page."\n";
 
61
        }
 
62
    }
 
63
 
 
64
    /**
 
65
     * Determine directions of the search loop
 
66
     *
 
67
     * @param string $entry
 
68
     * @param string $basepath
 
69
     * @return int
 
70
     */
 
71
    protected function dir_filter($entry, $basepath) {
 
72
        if($entry == '.' || $entry == '..') {
 
73
            return WantedPagesCLI::DIR_CONTINUE;
 
74
        }
 
75
        if(is_dir($basepath.'/'.$entry)) {
 
76
            if(strpos($entry, '_') === 0) {
 
77
                return WantedPagesCLI::DIR_CONTINUE;
 
78
            }
 
79
            return WantedPagesCLI::DIR_NS;
 
80
        }
 
81
        if(preg_match('/\.txt$/', $entry)) {
 
82
            return WantedPagesCLI::DIR_PAGE;
 
83
        }
 
84
        return WantedPagesCLI::DIR_CONTINUE;
 
85
    }
 
86
 
 
87
    /**
 
88
     * Collects recursively the pages in a namespace
 
89
     *
 
90
     * @param string $dir
 
91
     * @return array
 
92
     * @throws DokuCLI_Exception
 
93
     */
 
94
    protected function get_pages($dir) {
 
95
        static $trunclen = null;
 
96
        if(!$trunclen) {
 
97
            global $conf;
 
98
            $trunclen = strlen($conf['datadir'].':');
 
99
        }
 
100
 
 
101
        if(!is_dir($dir)) {
 
102
            throw new DokuCLI_Exception("Unable to read directory $dir");
 
103
        }
 
104
 
 
105
        $pages = array();
 
106
        $dh    = opendir($dir);
 
107
        while(false !== ($entry = readdir($dh))) {
 
108
            $status = $this->dir_filter($entry, $dir);
 
109
            if($status == WantedPagesCLI::DIR_CONTINUE) {
 
110
                continue;
 
111
            } else if($status == WantedPagesCLI::DIR_NS) {
 
112
                $pages = array_merge($pages, $this->get_pages($dir.'/'.$entry));
 
113
            } else {
 
114
                $page    = array(
 
115
                    'id'   => pathID(substr($dir.'/'.$entry, $trunclen)),
 
116
                    'file' => $dir.'/'.$entry,
75
117
                );
76
 
            $pages[] = $page;
77
 
        }
78
 
    }
79
 
    closedir($dh);
80
 
    return $pages;
81
 
}
82
 
 
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;
97
 
            }
98
 
        }
99
 
    }
100
 
    return $links;
101
 
}
102
 
 
103
 
#------------------------------------------------------------------------------
104
 
$OPTS = Doku_Cli_Opts::getOptions(__FILE__,'h',array('help'));
105
 
 
106
 
if ( $OPTS->isError() ) {
107
 
    fwrite( STDERR, $OPTS->getMessage() . "\n");
108
 
    exit(1);
109
 
}
110
 
 
111
 
if ( $OPTS->has('h') or $OPTS->has('help') ) {
112
 
    usage();
113
 
    exit(0);
114
 
}
115
 
 
116
 
$START_DIR = $conf['datadir'];
117
 
 
118
 
if ( $OPTS->numArgs() == 1 ) {
119
 
    $START_DIR .= '/' . $OPTS->arg(0);
120
 
}
121
 
 
122
 
#------------------------------------------------------------------------------
123
 
$WANTED_PAGES = array();
124
 
 
125
 
foreach ( 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);
129
 
sort($WANTED_PAGES);
130
 
 
131
 
foreach ( $WANTED_PAGES as $WANTED_PAGE ) {
132
 
    print $WANTED_PAGE."\n";
133
 
}
134
 
exit(0);
 
118
                $pages[] = $page;
 
119
            }
 
120
        }
 
121
        closedir($dh);
 
122
        return $pages;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Parse instructions and returns the non-existing links
 
127
     *
 
128
     * @param array $page array with page id and file path
 
129
     * @return array
 
130
     */
 
131
    function internal_links($page) {
 
132
        global $conf;
 
133
        $instructions = p_get_instructions(file_get_contents($page['file']));
 
134
        $links        = array();
 
135
        $cns          = getNS($page['id']);
 
136
        $exists       = false;
 
137
        foreach($instructions as $ins) {
 
138
            if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink')) {
 
139
                $mid = $ins[1][0];
 
140
                resolve_pageid($cns, $mid, $exists);
 
141
                if(!$exists) {
 
142
                    list($mid) = explode('#', $mid); //record pages without hashs
 
143
                    $links[] = $mid;
 
144
                }
 
145
            }
 
146
        }
 
147
        return $links;
 
148
    }
 
149
}
 
150
 
 
151
// Main
 
152
$cli = new WantedPagesCLI();
 
153
$cli->run();
 
 
b'\\ No newline at end of file'