Passed
Push — master ( 11479c...895137 )
by Andreas
25:55
created

net_nemein_wiki_handler_create::save_callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nemein.wiki
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\schemadb;
10
use midcom\datamanager\datamanager;
11
use midcom\datamanager\controller;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Wikipage creation handler
16
 *
17
 * @package net.nemein.wiki
18
 */
19
class net_nemein_wiki_handler_create extends midcom_baseclasses_components_handler
20
{
21
    use net_nemein_wiki_handler;
0 ignored issues
show
Bug introduced by
The trait net_nemein_wiki_handler requires the property $id which is not provided by net_nemein_wiki_handler_create.
Loading history...
22
23
    /**
24
     * Wiki word we're creating page for
25
     *
26
     * @var string
27
     */
28
    private $_wikiword = '';
29
30
    /**
31
     * @var net_nemein_wiki_wikipage
32
     */
33
    private $_page;
34
35 3
    private function check_unique_wikiword(string $wikiword, string $schema)
36
    {
37 3
        $resolver = new net_nemein_wiki_resolver($this->_topic->id);
38 3
        $resolved = $resolver->path_to_wikipage($wikiword, true, true);
39
40 3
        if (!empty($resolved['latest_parent'])) {
41 1
            $to_node = $resolved['latest_parent'];
42
        } else {
43 3
            $to_node = $resolved['folder'];
44
        }
45 3
        if (strstr($resolved['remaining_path'], '/')) {
46
            // One or more namespaces left, find first, create it and recurse
47 1
            $paths = explode('/', $resolved['remaining_path']);
48 1
            $folder_title = array_shift($paths);
49 1
            $topic = new midcom_db_topic();
50 1
            $topic->up = $to_node[MIDCOM_NAV_ID];
51 1
            $topic->extra = trim($folder_title);
52 1
            $topic->title = $topic->extra;
53 1
            $topic->name = midcom_helper_misc::urlize($folder_title);
54 1
            $topic->component = 'net.nemein.wiki';
55 1
            if (!$topic->create()) {
56
                throw new midcom_error("Could not create wiki namespace '{$topic->extra}', last Midgard error was: " . midcom_connection::get_error_string());
57
            }
58
            // refresh
59 1
            $topic = new midcom_db_topic($topic->id);
60
61
            // See if we have article with same title in immediate parent
62 1
            $qb = net_nemein_wiki_wikipage::new_query_builder();
63 1
            $qb->add_constraint('title', '=', $folder_title);
64 1
            $qb->add_constraint('topic', '=', $topic->up);
65 1
            $results = $qb->execute();
66
67 1
            if (count($results) == 1) {
68
                $article = $results[0];
69
                $article->name = 'index';
70
                $article->topic = $topic->id;
71
                if (!$article->update()) {
72
                    // Could not move article, do something ?
73
                }
74
            } else {
75
                try {
76 1
                    $this->initialize_index_article($topic);
77
                } catch (midcom_error $e) {
78
                    // Could not create index
79
                    $topic->delete();
80
                    throw $e;
81
                }
82
            }
83
            // We have created a new topic, now recurse to create the rest of the path.
84 1
            return $this->check_unique_wikiword($wikiword, $schema);
85
        }
86 3
        if ($resolved['wikipage']) {
87
            // Page exists
88
            throw new midcom_error('Wiki page with that name already exists.');
89
        }
90
        // No more namespaces left, create the page to latest parent
91 3
        if ($to_node[MIDCOM_NAV_ID] != $this->_topic->id) {
92
            // Last parent is not this topic, redirect there
93 1
            $wikiword_url = rawurlencode($resolved['remaining_path']);
94 1
            return new midcom_response_relocate($to_node[MIDCOM_NAV_ABSOLUTEURL] . "create/{$schema}/?wikiword={$wikiword_url}");
95
        }
96 2
    }
97
98 3
    public function _handler_create(Request $request, array &$data, string $schema = null)
99
    {
100
        // Initialize sessioning first
101 3
        $session = new midcom_services_session();
102
103 3
        if (!$request->query->has('wikiword')) {
104
            if (!$session->exists('wikiword')) {
105
                throw new midcom_error_notfound('No wiki word given');
106
            }
107
            $this->_wikiword = $session->get('wikiword');
108
        } else {
109 3
            $this->_wikiword = $request->query->get('wikiword');
110 3
            $session->set('wikiword', $this->_wikiword);
111
        }
112
113 3
        $this->_topic->require_do('midgard:create');
114
115 3
        $schema = $schema ?: $this->_config->get('default_schema');
116
117 3
        $schemadb = schemadb::from_path($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\schemadb::from_path() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        $schemadb = schemadb::from_path(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
118 3
        if (!$schemadb->has($schema)) {
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type false; however, parameter $name of midcom\datamanager\schemadb::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        if (!$schemadb->has(/** @scrutinizer ignore-type */ $schema)) {
Loading history...
119
            throw new midcom_error_notfound('Schema ' . $schema . ' not found in schemadb');
0 ignored issues
show
Bug introduced by
Are you sure $schema of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
            throw new midcom_error_notfound('Schema ' . /** @scrutinizer ignore-type */ $schema . ' not found in schemadb');
Loading history...
120
        }
121 3
        if ($response = $this->check_unique_wikiword($this->_wikiword, $schema)) {
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type false; however, parameter $schema of net_nemein_wiki_handler_...check_unique_wikiword() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        if ($response = $this->check_unique_wikiword($this->_wikiword, /** @scrutinizer ignore-type */ $schema)) {
Loading history...
122 1
            return $response;
123
        }
124
125 2
        $this->_page = new net_nemein_wiki_wikipage();
126 2
        $this->_page->topic = $this->_topic->id;
127 2
        $this->_page->title = $this->_wikiword;
128
129 2
        $dm = new datamanager($schemadb);
130 2
        $data['controller'] = $dm
131 2
            ->set_storage($this->_page)
132 2
            ->get_controller();
133
134 2
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n->get('create wikipage %s'), $this->_wikiword));
135
136 2
        $workflow = $this->get_workflow('datamanager', [
137 2
            'controller' => $data['controller'],
138 2
            'save_callback' => [$this, 'save_callback']
139
        ]);
140 2
        return $workflow->run($request);
141
    }
142
143
    public function save_callback(controller $controller)
144
    {
145
        $indexer = midcom::get()->indexer;
146
        net_nemein_wiki_viewer::index($controller->get_datamanager(), $indexer, $this->_topic);
147
148
        midcom::get()->uimessages->add($this->_l10n->get('net.nemein.wiki'), sprintf($this->_l10n->get('page %s added'), $this->_wikiword));
149
150
        return "{$this->_page->name}/";
151
    }
152
}
153