summaryrefslogtreecommitdiff
path: root/cli_util.php
blob: 0c42bb5a12bf06635d8fb865ccc9227a34cf1c7b (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 posts;
use uploads;
use pages;
use config;

require_once __DIR__.'/init.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 (!config::set('admin_pwd', salt_password($pwd1)))
        cli::die("Database error");
}

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

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

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

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

function posts_images(): void {
    $kw = ['include_hidden' => true];
    $posts = posts::getPosts(0, posts::getPostsCount(...$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);
    $ext = extension($name);
    $id = uploads::add($path, $name, '');
    echo "upload id: $id\n";
}