1 | <?php
|
---|
2 |
|
---|
3 | class ModuleWiki extends Module
|
---|
4 | {
|
---|
5 | function __construct(System $System)
|
---|
6 | {
|
---|
7 | parent::__construct($System);
|
---|
8 | $this->Name = 'Wiki';
|
---|
9 | $this->Version = '1.0';
|
---|
10 | $this->Creator = 'Chronos';
|
---|
11 | $this->License = 'GNU/GPLv3';
|
---|
12 | $this->Description = 'Mediawiki style page management';
|
---|
13 | $this->Dependencies = array(ModuleUser::GetName());
|
---|
14 | $this->Models = array(WikiPage::GetClassName(), WikiPageContent::GetClassName());
|
---|
15 | }
|
---|
16 |
|
---|
17 | function DoStart(): void
|
---|
18 | {
|
---|
19 | $this->LoadPages();
|
---|
20 | }
|
---|
21 |
|
---|
22 | function LoadPages(): void
|
---|
23 | {
|
---|
24 | $DbResult = $this->Database->select('WikiPage', '*', 'VisibleInMenu=1');
|
---|
25 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
26 | {
|
---|
27 | Core::Cast($this->System)->RegisterPage([$DbRow['NormalizedName']], 'PageWiki');
|
---|
28 | Core::Cast($this->System)->RegisterMenuItem(array(
|
---|
29 | 'Title' => $DbRow['Name'],
|
---|
30 | 'Hint' => '',
|
---|
31 | 'Link' => Core::Cast($this->System)->Link('/'.$DbRow['NormalizedName'].'/'),
|
---|
32 | 'Permission' => LICENCE_ANONYMOUS,
|
---|
33 | 'Icon' => '',
|
---|
34 | ), 2);
|
---|
35 | }
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | class WikiPage extends Model
|
---|
40 | {
|
---|
41 | static function GetModelDesc(): ModelDesc
|
---|
42 | {
|
---|
43 | $Desc = new ModelDesc(self::GetClassName());
|
---|
44 | $Desc->AddString('Name');
|
---|
45 | $Desc->AddString('NormalizedName');
|
---|
46 | $Desc->AddBoolean('VisibleInMenu');
|
---|
47 | return $Desc;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | class WikiPageContent extends Model
|
---|
52 | {
|
---|
53 | static function GetModelDesc(): ModelDesc
|
---|
54 | {
|
---|
55 | $Desc = new ModelDesc(self::GetClassName());
|
---|
56 | $Desc->AddReference('Page', WikiPage::GetClassName());
|
---|
57 | $Desc->AddDateTime('Time');
|
---|
58 | $Desc->AddText('Content');
|
---|
59 | $Desc->AddReference('User', User::GetClassName());
|
---|
60 | return $Desc;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | class PageWiki extends Page
|
---|
65 | {
|
---|
66 | function __construct(System $System)
|
---|
67 | {
|
---|
68 | parent::__construct($System);
|
---|
69 | $this->Title = 'Wiki';
|
---|
70 | $this->Description = 'Wiki stránky';
|
---|
71 | $this->ParentClass = 'PagePortal';
|
---|
72 | }
|
---|
73 |
|
---|
74 | function Show(): string
|
---|
75 | {
|
---|
76 | if (array_key_exists('Action', $_GET))
|
---|
77 | {
|
---|
78 | if ($_GET['Action'] == 'Edit') $Output = $this->EditContent();
|
---|
79 | else if ($_GET['Action'] == 'EditSave') $Output = $this->SaveContent();
|
---|
80 | else if ($_GET['Action'] == 'History') $Output = $this->ShowHistory();
|
---|
81 | else $Output = $this->ShowContent();
|
---|
82 | } else $Output = $this->ShowContent();
|
---|
83 | return $Output;
|
---|
84 | }
|
---|
85 |
|
---|
86 | function ShowContent(): string
|
---|
87 | {
|
---|
88 | $PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
|
---|
89 | $DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
|
---|
90 | if ($DbResult->num_rows > 0)
|
---|
91 | {
|
---|
92 | $DbRow = $DbResult->fetch_assoc();
|
---|
93 | if (array_key_exists('ver', $_GET))
|
---|
94 | {
|
---|
95 | $DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' AND Id='.$_GET['ver']*1);
|
---|
96 | if ($DbResult2->num_rows > 0)
|
---|
97 | {
|
---|
98 | $DbRow2 = $DbResult2->fetch_assoc();
|
---|
99 | $Output = '<h3>Archív stránky '.$DbRow['Name'].' ('.HumanDateTime($DbRow2['Time']).')</h3>';
|
---|
100 | $Output .= $DbRow2['Content'];
|
---|
101 | if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
|
---|
102 | $Output .= '<div><a href="?Action=Edit">Upravit nejnovější</a> <a href="?Action=History">Historie</a></div>';
|
---|
103 | } else $Output = ShowmMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
104 | } else
|
---|
105 | {
|
---|
106 | $DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' ORDER BY Time DESC LIMIT 1');
|
---|
107 | if ($DbResult2->num_rows > 0)
|
---|
108 | {
|
---|
109 | $DbRow2 = $DbResult2->fetch_assoc();
|
---|
110 | $Output = '<h3>'.$DbRow['Name'].'</h3>';
|
---|
111 | $Output .= $DbRow2['Content'];
|
---|
112 | if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
|
---|
113 | $Output .= '<div><a href="?Action=Edit">Upravit</a> <a href="?Action=History">Historie</a></div>';
|
---|
114 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
115 | }
|
---|
116 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
117 | return $Output;
|
---|
118 | }
|
---|
119 |
|
---|
120 | function EditContent(): string
|
---|
121 | {
|
---|
122 | if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
|
---|
123 | {
|
---|
124 | $PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
|
---|
125 | $DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
|
---|
126 | if ($DbResult->num_rows > 0)
|
---|
127 | {
|
---|
128 | $DbRow = $DbResult->fetch_assoc();
|
---|
129 | $Output = '<h3>Úprava '.$DbRow['Name'].'</h3>';
|
---|
130 | $DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' ORDER BY Time DESC LIMIT 1');
|
---|
131 | if ($DbResult2->num_rows > 0)
|
---|
132 | {
|
---|
133 | $DbRow2 = $DbResult2->fetch_assoc();
|
---|
134 | $Output .= '<form action="?Action=EditSave" method="post">'.
|
---|
135 | '<textarea name="content" rows="8" cols="80" onkeydown="ResizeTextArea(this)" class="textedit">'.$DbRow2['Content'].'</textarea><br/>'.
|
---|
136 | '<input type="submit" name="save" value="Uložit"/> '.
|
---|
137 | '<input type="button" name="cancel" value="Zrušit" onclick="location.href=\'?\'"/>'.
|
---|
138 | '</form>';
|
---|
139 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
140 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
141 | } else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
|
---|
142 | return $Output;
|
---|
143 | }
|
---|
144 |
|
---|
145 | function SaveContent(): string
|
---|
146 | {
|
---|
147 | if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
|
---|
148 | {
|
---|
149 | $PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
|
---|
150 | $DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
|
---|
151 | if ($DbResult->num_rows > 0)
|
---|
152 | {
|
---|
153 | $DbRow = $DbResult->fetch_assoc();
|
---|
154 | if (array_key_exists('content', $_POST) and array_key_exists('save', $_POST))
|
---|
155 | {
|
---|
156 | $this->Database->insert('WikiPageContent', array('Content' => stripslashes($_POST['content']),
|
---|
157 | 'User' => ModuleUser::Cast($this->System->GetModule('User'))->User->Id, 'Time' => 'NOW()', 'Page' => $DbRow['Id']));
|
---|
158 | $Output = ShowMessage('Wiki stránka uložena', MESSAGE_INFORMATION);
|
---|
159 | } else $Output = ShowMessage('Nezadána platná data', MESSAGE_CRITICAL);
|
---|
160 | $Output .= $this->ShowContent();
|
---|
161 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
162 | } else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
|
---|
163 | return $Output;
|
---|
164 | }
|
---|
165 |
|
---|
166 | function ShowHistory(): string
|
---|
167 | {
|
---|
168 | if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
|
---|
169 | {
|
---|
170 | $PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
|
---|
171 | $DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
|
---|
172 | if ($DbResult->num_rows > 0)
|
---|
173 | {
|
---|
174 | $DbRow = $DbResult->fetch_assoc();
|
---|
175 |
|
---|
176 | $Output = '<h3>Historie stránky '.$DbRow['Name'].'</h3>';
|
---|
177 | $DbResult2 = $this->Database->query('SELECT COUNT(*) FROM `WikiPageContent` WHERE Page='.$DbRow['Id']);
|
---|
178 | $DbRow2 = $DbResult2->fetch_row();
|
---|
179 | $PageList = GetPageList('WikiHistory', $DbRow2[0]);
|
---|
180 |
|
---|
181 | $Output .= $PageList['Output'];
|
---|
182 | $Output .= '<table class="BaseTable">';
|
---|
183 |
|
---|
184 | $TableColumns = array(
|
---|
185 | array('Name' => 'Time', 'Title' => 'Čas'),
|
---|
186 | array('Name' => 'User', 'Title' => 'Uživatel'),
|
---|
187 | array('Name' => 'Action', 'Title' => 'Akce'),
|
---|
188 | );
|
---|
189 |
|
---|
190 | $Order = GetOrderTableHeader('WikiHistory', $TableColumns, 'Time', 1);
|
---|
191 | $Output .= $Order['Output'];
|
---|
192 |
|
---|
193 | $DbResult2 = $this->Database->query('SELECT *, (SELECT `Name` FROM `User` WHERE `User`.`ID`=`WikiPageContent`.`User`) AS `UserName` '.
|
---|
194 | ' FROM `WikiPageContent` WHERE Page='.
|
---|
195 | $DbRow['Id'].' '.$Order['SQL'].$PageList['SQLLimit']);
|
---|
196 | while ($PageContent = $DbResult2->fetch_assoc())
|
---|
197 | {
|
---|
198 | $Output .= '<tr>'.
|
---|
199 | '<td>'.HumanDateTime($PageContent['Time']).'</td>'.
|
---|
200 | '<td><a href="'.$this->System->Link('/user.php?user='.$PageContent['User']).'">'.$PageContent['UserName'].'</a></td>'.
|
---|
201 | '<td><a href="?id='.$PageContent['Id'].'&ver='.$PageContent['Id'].'">Zobrazit</a></td>';
|
---|
202 | $Output .= '</tr>';
|
---|
203 | }
|
---|
204 |
|
---|
205 | $Output .= '</table>'.
|
---|
206 | $PageList['Output'];
|
---|
207 | } else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
|
---|
208 | } else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
|
---|
209 | return $Output;
|
---|
210 | }
|
---|
211 |
|
---|
212 | function ToHtml(string $text): string
|
---|
213 | {
|
---|
214 | $text = preg_replace('/<source lang="(.*?)">(.*?)<\/source>/', '<pre lang="$1">$2</pre>', $text);
|
---|
215 | $text = preg_replace('/======(.*?)======/', '<h5>$1</h5>', $text);
|
---|
216 | $text = preg_replace('/=====(.*?)=====/', '<h4>$1</h4>', $text);
|
---|
217 | $text = preg_replace('/====(.*?)====/', '<h3>$1</h3>', $text);
|
---|
218 | $text = preg_replace('/===(.*?)===/', '<h2>$1</h2>', $text);
|
---|
219 | $text = preg_replace('/==(.*?)==/', '<h1>$1</h1>', $text);
|
---|
220 | $text = preg_replace("/'''(.*?)'''/", '<strong>$1</strong>', $text);
|
---|
221 | $text = preg_replace("/''(.*?)''/", '<em>$1</em>', $text);
|
---|
222 | $text = preg_replace('/<s>(.*?)<\/s>/', '<strike>$1</strike>', $text);
|
---|
223 | $text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2" />', $text);
|
---|
224 | $text = preg_replace('/\[(.*?) (.*?)\]/', '<a href="$1" title="$2">$2</a>', $text);
|
---|
225 | $text = preg_replace('/>(.*?)\n/', '<blockquote>$1</blockquote>', $text);
|
---|
226 |
|
---|
227 | $text = preg_replace('/\* (.*?)\n/', '<ul><li>$1</li></ul>', $text);
|
---|
228 | $text = preg_replace('/<\/ul><ul>/', '', $text);
|
---|
229 |
|
---|
230 | $text = preg_replace('/# (.*?)\n/', '<ol><li>$1</li></ol>', $text);
|
---|
231 | $text = preg_replace('/<\/ol><ol>/', '', $text);
|
---|
232 |
|
---|
233 | $text = str_replace("\r\n\r\n", '</p><p>', $text);
|
---|
234 | $text = str_replace("\r\n", '<br/>', $text);
|
---|
235 | $text = '<p>'.$text.'</p>';
|
---|
236 | return $text;
|
---|
237 | }
|
---|
238 | }
|
---|