GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FullTextSearchWikiActions::updatePermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) Enalean, 2014. All Rights Reserved.
4
 *
5
 * This file is a part of Tuleap.
6
 *
7
 * Tuleap is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Tuleap is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 * Class responsible to send requests to an indexation server
23
 */
24
class FullTextSearchWikiActions {
25
26
    /**
27
     * @var FullTextSearch_IIndexDocuments
28
     */
29
    private $client;
30
31
    /** @var ElasticSearch_1_2_RequestWikiDataFactory */
32
    private $request_data_factory;
33
34
    /** @var TruncateLevelLogger */
35
    private $logger;
36
37
    public function __construct(
38
        FullTextSearch_IIndexDocuments $client,
39
        ElasticSearch_1_2_RequestWikiDataFactory $request_data_factory,
40
        TruncateLevelLogger $logger
41
    ) {
42
        $this->client               = $client;
43
        $this->request_data_factory = $request_data_factory;
44
        $this->logger               = $logger;
45
    }
46
47
    private function indexOrUpdate($project_id, $wiki_page_id, $data) {
48
        try {
49
            $this->client->getIndexedElement($project_id, $wiki_page_id);
50
            $this->client->update($project_id, $wiki_page_id, $data);
51
52
        } catch (ElasticSearch_ElementNotIndexed $exception) {
53
            $this->client->index($project_id, $wiki_page_id, $data);
54
            return;
55
        }
56
    }
57
58
    public function checkProjectMappingExists($project_id) {
59
        $this->logger->debug('[Wiki] ElasticSearch: get the mapping for project #' . $project_id);
60
61
        return count($this->client->getMapping($project_id)) > 0;
62
    }
63
64
    public function initializeProjetMapping($project_id) {
65
        $this->logger->debug('[Wiki] ElasticSearch: initialize the mapping for project #' . $project_id);
66
67
        $this->client->setMapping(
68
            $project_id,
69
            $this->request_data_factory->getPUTMappingData($project_id)
70
        );
71
    }
72
73
    /**
74
     * Index a new wiki page
75
     *
76
     * @param WikiPage $wiki_page The wiki page
77
     */
78
    public function indexNewEmptyWikiPage(WikiPage $wiki_page) {
79
        $this->logger->debug('[Wiki] ElasticSearch: index new empty wiki page ' . $wiki_page->getPagename() . ' #' . $wiki_page->getId());
80
81
        $indexed_data = $this->request_data_factory->getIndexedWikiPageData($wiki_page);
82
83
        $this->client->index($wiki_page->getGid(), $wiki_page->getId(), $indexed_data);
84
    }
85
86
    /**
87
     * Index a new wiki page
88
     *
89
     * @param WikiPage $wiki_page The wiki page
90
     */
91
    public function indexWikiPage(WikiPage $wiki_page) {
92
        $this->logger->debug('[Wiki] ElasticSearch: index wiki page ' . $wiki_page->getPagename() . ' #' . $wiki_page->getId());
93
94
        $indexed_data = $this->request_data_factory->getIndexedWikiPageData($wiki_page);
95
96
        $this->client->index($wiki_page->getGid(), $wiki_page->getId(), $indexed_data);
97
    }
98
99
    /**
100
     * Remove an indexed wiki page
101
     *
102
     * @param WikiPage $wiki_page The item to delete
103
     */
104
    public function delete(WikiPage $wiki_page) {
105
        $this->logger->debug('[Wiki] ElasticSearch: delete wiki page ' . $wiki_page->getPagename() . ' #' . $wiki_page->getId());
106
107
        try{
108
            $this->client->getIndexedElement($wiki_page->getGid(), $wiki_page->getId());
109
            $this->client->delete($wiki_page->getGid(), $wiki_page->getId());
110
        } catch (ElasticSearch_ElementNotIndexed $exception) {
111
            $this->logger->debug('[Wiki] ElasticSearch: wiki page ' . $wiki_page->getPagename() . ' #' . $wiki_page->getId() . ' not indexed, nothing to delete');
112
            return;
113
        }
114
    }
115
116
    private function deleteForProject($project_id) {
117
        $this->logger->debug('[Wiki] ElasticSearch: deleting all project wiki pages #' . $project_id);
118
119
        try{
120
            $this->client->getIndexedType($project_id);
121
            $this->client->deleteType($project_id);
0 ignored issues
show
Bug introduced by
The method deleteType() does not exist on FullTextSearch_IIndexDocuments. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
123
        } catch (ElasticSearch_TypeNotIndexed $exception) {
124
            $this->logger->debug('[Wiki] ElasticSearch: project #' . $project_id . ' not indexed, nothing to delete');
125
            return;
126
        }
127
    }
128
129
    /**
130
     *
131
     * @param WikiPage $wiki_page
132
     */
133
    public function updatePermissions(WikiPage $wiki_page) {
134
        $this->logger->debug('[Wiki] ElasticSearch: update permissions of wiki page ' . $wiki_page->getPagename() . ' #' . $wiki_page->getId());
135
136
        $update_data = array();
137
        $this->request_data_factory->setUpdatedData(
138
            $update_data,
139
            'permissions',
140
            $this->request_data_factory->getCurrentPermissions($wiki_page)
141
        );
142
143
        $this->indexOrUpdate($wiki_page->getGid(), $wiki_page->getId(), $update_data);
144
    }
145
146
    private function indexAllProjectWikiPages($project_id) {
147
        $indexable_pages = $this->getAllIndexablePagesForProject($project_id);
148
149
        foreach($indexable_pages as $indexable_page) {
150
            $this->indexWikiPage($indexable_page);
151
        }
152
    }
153
154
    protected function getAllIndexablePagesForProject($project_id) {
155
        $wiki_page = new WikiPage();
156
        return $wiki_page->getAllIndexablePages($project_id);
157
    }
158
159
    /**
160
     *
161
     * @param int $project_id
162
     */
163
    public function reIndexProjectWikiPages($project_id) {
164
        $this->deleteForProject($project_id);
165
        $this->initializeProjetMapping($project_id);
166
        $this->indexAllProjectWikiPages($project_id);
167
    }
168
}
169