Name = 'Wiki';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPLv3';
$this->Description = 'Mediawiki style page management';
$this->Dependencies = array(ModuleUser::GetName());
$this->Models = array(WikiPage::GetClassName(), WikiPageContent::GetClassName());
}
function DoStart(): void
{
$this->LoadPages();
}
function LoadPages(): void
{
$DbResult = $this->Database->select('WikiPage', '*', 'VisibleInMenu=1');
while ($DbRow = $DbResult->fetch_assoc())
{
Core::Cast($this->System)->RegisterPage([$DbRow['NormalizedName']], 'PageWiki');
Core::Cast($this->System)->RegisterMenuItem(array(
'Title' => $DbRow['Name'],
'Hint' => '',
'Link' => Core::Cast($this->System)->Link('/'.$DbRow['NormalizedName'].'/'),
'Permission' => LICENCE_ANONYMOUS,
'Icon' => '',
), 2);
}
}
}
class WikiPage extends Model
{
static function GetModelDesc(): ModelDesc
{
$Desc = new ModelDesc(self::GetClassName());
$Desc->AddString('Name');
$Desc->AddString('NormalizedName');
$Desc->AddBoolean('VisibleInMenu');
return $Desc;
}
}
class WikiPageContent extends Model
{
static function GetModelDesc(): ModelDesc
{
$Desc = new ModelDesc(self::GetClassName());
$Desc->AddReference('Page', WikiPage::GetClassName());
$Desc->AddDateTime('Time');
$Desc->AddText('Content');
$Desc->AddReference('User', User::GetClassName());
return $Desc;
}
}
class PageWiki extends Page
{
function __construct(System $System)
{
parent::__construct($System);
$this->Title = 'Wiki';
$this->Description = 'Wiki stránky';
$this->ParentClass = 'PagePortal';
}
function Show(): string
{
if (array_key_exists('Action', $_GET))
{
if ($_GET['Action'] == 'Edit') $Output = $this->EditContent();
else if ($_GET['Action'] == 'EditSave') $Output = $this->SaveContent();
else if ($_GET['Action'] == 'History') $Output = $this->ShowHistory();
else $Output = $this->ShowContent();
} else $Output = $this->ShowContent();
return $Output;
}
function ShowContent(): string
{
$PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
$DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
if (array_key_exists('ver', $_GET))
{
$DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' AND Id='.$_GET['ver']*1);
if ($DbResult2->num_rows > 0)
{
$DbRow2 = $DbResult2->fetch_assoc();
$Output = '
Archív stránky '.$DbRow['Name'].' ('.HumanDateTime($DbRow2['Time']).')
';
$Output .= $DbRow2['Content'];
if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
$Output .= '';
} else $Output = ShowmMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
} else
{
$DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' ORDER BY Time DESC LIMIT 1');
if ($DbResult2->num_rows > 0)
{
$DbRow2 = $DbResult2->fetch_assoc();
$Output = ''.$DbRow['Name'].'
';
$Output .= $DbRow2['Content'];
if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
$Output .= '';
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
}
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
return $Output;
}
function EditContent(): string
{
if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
{
$PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
$DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
$Output = 'Úprava '.$DbRow['Name'].'
';
$DbResult2 = $this->Database->select('WikiPageContent', '*', 'Page='.$DbRow['Id'].' ORDER BY Time DESC LIMIT 1');
if ($DbResult2->num_rows > 0)
{
$DbRow2 = $DbResult2->fetch_assoc();
$Output .= '';
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
} else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
return $Output;
}
function SaveContent(): string
{
if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
{
$PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
$DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
if (array_key_exists('content', $_POST) and array_key_exists('save', $_POST))
{
$this->Database->insert('WikiPageContent', array('Content' => stripslashes($_POST['content']),
'User' => ModuleUser::Cast($this->System->GetModule('User'))->User->Id, 'Time' => 'NOW()', 'Page' => $DbRow['Id']));
$Output = ShowMessage('Wiki stránka uložena', MESSAGE_INFORMATION);
} else $Output = ShowMessage('Nezadána platná data', MESSAGE_CRITICAL);
$Output .= $this->ShowContent();
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
} else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
return $Output;
}
function ShowHistory(): string
{
if (ModuleUser::Cast($this->System->GetModule('User'))->User->Licence(LICENCE_MODERATOR))
{
$PageName = $this->System->PathItems[count($this->System->PathItems) - 1];
$DbResult = $this->Database->select('WikiPage', 'Name, Id', 'NormalizedName="'.$PageName.'"');
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
$Output = 'Historie stránky '.$DbRow['Name'].'
';
$DbResult2 = $this->Database->query('SELECT COUNT(*) FROM `WikiPageContent` WHERE Page='.$DbRow['Id']);
$DbRow2 = $DbResult2->fetch_row();
$PageList = GetPageList('WikiHistory', $DbRow2[0]);
$Output .= $PageList['Output'];
$Output .= '';
$TableColumns = array(
array('Name' => 'Time', 'Title' => 'Čas'),
array('Name' => 'User', 'Title' => 'Uživatel'),
array('Name' => 'Action', 'Title' => 'Akce'),
);
$Order = GetOrderTableHeader('WikiHistory', $TableColumns, 'Time', 1);
$Output .= $Order['Output'];
$DbResult2 = $this->Database->query('SELECT *, (SELECT `Name` FROM `User` WHERE `User`.`ID`=`WikiPageContent`.`User`) AS `UserName` '.
' FROM `WikiPageContent` WHERE Page='.
$DbRow['Id'].' '.$Order['SQL'].$PageList['SQLLimit']);
while ($PageContent = $DbResult2->fetch_assoc())
{
$Output .= ''.
''.HumanDateTime($PageContent['Time']).' | '.
''.$PageContent['UserName'].' | '.
'Zobrazit | ';
$Output .= '
';
}
$Output .= '
'.
$PageList['Output'];
} else $Output = ShowMessage('Wiki stránka nenalezena', MESSAGE_CRITICAL);
} else $Output = ShowMessage('Nemáte oprávnění', MESSAGE_CRITICAL);
return $Output;
}
function ToHtml(string $text): string
{
$text = preg_replace('/<source lang="(.*?)">(.*?)<\/source>/', '$2
', $text);
$text = preg_replace('/======(.*?)======/', '$1
', $text);
$text = preg_replace('/=====(.*?)=====/', '$1
', $text);
$text = preg_replace('/====(.*?)====/', '$1
', $text);
$text = preg_replace('/===(.*?)===/', '$1
', $text);
$text = preg_replace('/==(.*?)==/', '$1
', $text);
$text = preg_replace("/'''(.*?)'''/", '$1', $text);
$text = preg_replace("/''(.*?)''/", '$1', $text);
$text = preg_replace('/<s>(.*?)<\/s>/', '$1', $text);
$text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '', $text);
$text = preg_replace('/\[(.*?) (.*?)\]/', '$2', $text);
$text = preg_replace('/>(.*?)\n/', '$1
', $text);
$text = preg_replace('/\* (.*?)\n/', '', $text);
$text = preg_replace('/<\/ul>