aboutsummaryrefslogtreecommitdiff
path: root/cli_util.php
blob: e883ae75e305afca3547a371cdc3f3d8231bfb84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env php8.1
<?php

namespace cli_util;

use cli;
use pages;
use posts;
use tags;
use uploads;

require_once __DIR__.'/init.php';
require_once 'lib/stored_config.php';

$cli = new cli(__NAMESPACE__);
$cli->run();

function admin_reset(): void {
    $pwd1 = cli::silentInput("New password: ");
    $pwd2 = cli::silentInput("Again: ");

    if ($pwd1 != $pwd2)
        cli::die("Passwords do not match");

    if (trim($pwd1) == '')
        cli::die("Password can not be empty");

    if (!scSet('admin_pwd', salt_password($pwd1)))
        cli::die("Database error");
}

function admin_check(): void {
    $pwd = scGet('admin_pwd');
    echo is_null($pwd) ? "Not set" : $pwd;
    echo "\n";
}

function blog_erase(): void {
    $db = DB();
    $tables = ['posts', 'posts_tags', 'tags'];
    foreach ($tables as $t) {
        $db->query("TRUNCATE TABLE $t");
    }
}

function tags_recount(): void {
    $tags = tags::getAll(true);
    foreach ($tags as $tag)
        posts::tagsRecountPosts($tag->id);
}

function posts_html(): void {
    $kw = ['include_hidden' => true];
    $posts = posts::getList(0, posts::getCount(...$kw), ...$kw);
    foreach ($posts as $p) {
        $p->updateHtml();
        $p->updateText();
    }
}

function posts_images(): void {
    $kw = ['include_hidden' => true];
    $posts = posts::getList(0, posts::getCount(...$kw), ...$kw);
    foreach ($posts as $p) {
        $p->updateImagePreviews(true);
    }
}

function pages_html(): void {
    $pages = pages::getAll();
    foreach ($pages as $p) {
        $p->updateHtml();
    }
}

function add_files_to_uploads(): void {
    $path = cli::input('Enter path: ');
    if (!file_exists($path))
        cli::die("file $path doesn't exists");
    $name = basename($path);
    $id = uploads::add($path, $name, '');
    echo "upload id: $id\n";
}