1c3437056SNickeau<?php 2c3437056SNickeau 3c3437056SNickeau 4c3437056SNickeaunamespace ComboStrap; 5c3437056SNickeau 6c3437056SNickeau 704fd306cSNickeauuse ComboStrap\Meta\Api\Metadata; 804fd306cSNickeauuse ComboStrap\Meta\Api\MetadataStore; 904fd306cSNickeauuse ComboStrap\Meta\Api\MetadataText; 1004fd306cSNickeauuse ComboStrap\Meta\Store\MetadataDokuWikiStore; 11c3437056SNickeauuse syntax_plugin_combo_frontmatter; 12c3437056SNickeau 13c3437056SNickeauclass PageDescription extends MetadataText 14c3437056SNickeau{ 1504fd306cSNickeau 16c3437056SNickeau /** 17c3437056SNickeau * The description sub key in the dokuwiki meta 18c3437056SNickeau * that has the description text 19c3437056SNickeau */ 20c3437056SNickeau const ABSTRACT_KEY = "abstract"; 21c3437056SNickeau public const PROPERTY_NAME = "description"; 22c3437056SNickeau const DESCRIPTION_ORIGIN = "origin"; 23c3437056SNickeau const PLUGIN_DESCRIPTION_META = "plugin_description"; 24c3437056SNickeau 25c3437056SNickeau 26c3437056SNickeau /** 27c3437056SNickeau * @var string - the origin of the description 28c3437056SNickeau */ 29c3437056SNickeau private $descriptionOrigin; 30c3437056SNickeau 31c3437056SNickeau 32c3437056SNickeau public const DESCRIPTION_PROPERTY = "description"; 33c3437056SNickeau /** 34c3437056SNickeau * To indicate from where the description comes 35c3437056SNickeau * This is when it's the original dokuwiki description 36c3437056SNickeau */ 37c3437056SNickeau public const DESCRIPTION_DOKUWIKI_ORIGIN = "dokuwiki"; 38c3437056SNickeau /** 39c3437056SNickeau * The origin of the description was set to frontmatter 40c3437056SNickeau * due to historic reason to say to it comes from combo 41c3437056SNickeau * (You may set it via the metadata manager and get this origin) 42c3437056SNickeau */ 43c3437056SNickeau public const DESCRIPTION_COMBO_ORIGIN = syntax_plugin_combo_frontmatter::CANONICAL; 4404fd306cSNickeau private ?string $defaultValue = null; 45c3437056SNickeau 46c3437056SNickeau public static function createForPage($page): PageDescription 47c3437056SNickeau { 48c3437056SNickeau return (new PageDescription()) 49c3437056SNickeau ->setResource($page); 50c3437056SNickeau } 51c3437056SNickeau 5204fd306cSNickeau public static function getTab(): string 53c3437056SNickeau { 54c3437056SNickeau return MetaManagerForm::TAB_PAGE_VALUE; 55c3437056SNickeau } 56c3437056SNickeau 5704fd306cSNickeau public static function getDescription(): string 58c3437056SNickeau { 59c3437056SNickeau return "The description is a paragraph that describe your page. It's advertised to external application and used in templating."; 60c3437056SNickeau } 61c3437056SNickeau 6204fd306cSNickeau public static function getLabel(): string 63c3437056SNickeau { 64c3437056SNickeau return "Description"; 65c3437056SNickeau } 66c3437056SNickeau 67c3437056SNickeau static public function getName(): string 68c3437056SNickeau { 69c3437056SNickeau return self::DESCRIPTION_PROPERTY; 70c3437056SNickeau } 71c3437056SNickeau 7204fd306cSNickeau public static function getPersistenceType(): string 73c3437056SNickeau { 7404fd306cSNickeau return MetadataDokuWikiStore::PERSISTENT_DOKUWIKI_KEY; 75c3437056SNickeau } 76c3437056SNickeau 7704fd306cSNickeau public static function getDataType(): string 78c3437056SNickeau { 79c3437056SNickeau return DataType::PARAGRAPH_TYPE_VALUE; 80c3437056SNickeau } 81c3437056SNickeau 82c3437056SNickeau 8304fd306cSNickeau public static function isMutable(): bool 84c3437056SNickeau { 85c3437056SNickeau return true; 86c3437056SNickeau } 87c3437056SNickeau 8804fd306cSNickeau 89c3437056SNickeau /** 9004fd306cSNickeau * @return string 91c3437056SNickeau */ 9204fd306cSNickeau public function getValueOrDefault(): string 9304fd306cSNickeau { 9404fd306cSNickeau 9504fd306cSNickeau try { 9604fd306cSNickeau $value = $this->getValue(); 9704fd306cSNickeau if (empty($value)) { 9804fd306cSNickeau return $this->getDefaultValue(); 9904fd306cSNickeau } 10004fd306cSNickeau return $value; 10104fd306cSNickeau } catch (ExceptionNotFound $e) { 10204fd306cSNickeau return $this->getDefaultValue(); 10304fd306cSNickeau } 10404fd306cSNickeau 10504fd306cSNickeau 10604fd306cSNickeau } 10704fd306cSNickeau 10804fd306cSNickeau 10904fd306cSNickeau /** 11004fd306cSNickeau * @return string - the dokuwiki calculated description 11104fd306cSNickeau * or the resource name if none (case when there is no text at all for instance with only a icon) 11204fd306cSNickeau */ 11304fd306cSNickeau public function getDefaultValue(): string 114c3437056SNickeau { 115c3437056SNickeau 116c3437056SNickeau $this->buildCheck(); 11704fd306cSNickeau if ($this->defaultValue === "" || $this->defaultValue === null) { 11804fd306cSNickeau return PageTitle::createForMarkup($this->getResource()) 11904fd306cSNickeau ->getValueOrDefault(); 12004fd306cSNickeau } 121c3437056SNickeau return $this->defaultValue; 122c3437056SNickeau 123c3437056SNickeau } 124c3437056SNickeau 125c3437056SNickeau 12604fd306cSNickeau public function setFromStoreValueWithoutException($value): Metadata 127c3437056SNickeau { 128c3437056SNickeau 129c3437056SNickeau $metaDataStore = $this->getReadStore(); 13004fd306cSNickeau if (!$metaDataStore->isDokuWikiStore()) { 13104fd306cSNickeau parent::setFromStoreValueWithoutException($value); 132c3437056SNickeau return $this; 133c3437056SNickeau } 134c3437056SNickeau 13504fd306cSNickeau if (is_array($value)) { 136c3437056SNickeau $description = $value[self::ABSTRACT_KEY]; 137c3437056SNickeau if ($description !== null) { 138*70bbd7f1Sgerardnico $descriptionOrigin = $value[self::DESCRIPTION_ORIGIN] ?? null; 13904fd306cSNickeau if ($descriptionOrigin !== null) { 14004fd306cSNickeau $this->descriptionOrigin = $descriptionOrigin; 14104fd306cSNickeau parent::setFromStoreValueWithoutException($description); 142c3437056SNickeau return $this; 143c3437056SNickeau } 144c3437056SNickeau } 14504fd306cSNickeau } 146c3437056SNickeau /** 147c3437056SNickeau * Plugin Plugin Description Integration 148c3437056SNickeau */ 14904fd306cSNickeau $value = $metaDataStore->getFromName(self::PLUGIN_DESCRIPTION_META); 150c3437056SNickeau if ($value !== null) { 151c3437056SNickeau $keywords = $value["keywords"]; 152c3437056SNickeau if ($keywords !== null) { 15304fd306cSNickeau parent::setFromStoreValueWithoutException($keywords); 154c3437056SNickeau $this->descriptionOrigin = self::PLUGIN_DESCRIPTION_META; 155c3437056SNickeau return $this; 156c3437056SNickeau } 157c3437056SNickeau } 158c3437056SNickeau 159c3437056SNickeau /** 160c3437056SNickeau * No description set, null 161c3437056SNickeau */ 16204fd306cSNickeau parent::setFromStoreValueWithoutException(null); 163c3437056SNickeau 164c3437056SNickeau /** 165c3437056SNickeau * Default value is derived from the meta store 166c3437056SNickeau * We need to set it at build time because the store may change 167c3437056SNickeau * after the build 168c3437056SNickeau */ 169c3437056SNickeau $this->defaultValue = $this->getGeneratedValueFromDokuWikiStore($metaDataStore); 170c3437056SNickeau 171c3437056SNickeau return $this; 172c3437056SNickeau } 173c3437056SNickeau 174c3437056SNickeau 175c3437056SNickeau public function setValue($value): Metadata 176c3437056SNickeau { 177c3437056SNickeau 178c3437056SNickeau if ($value === "" || $value === null) { 179c3437056SNickeau 180c3437056SNickeau if ($this->getReadStore() instanceof MetadataDokuWikiStore) { 181c3437056SNickeau // we need to know the origin of the actual description 182c3437056SNickeau if ($this->descriptionOrigin === null) { 183c3437056SNickeau /** 184c3437056SNickeau * we don't do {@link Metadata::buildCheck() build check} otherwise we get a loop 185c3437056SNickeau * because it will use back this method {@link Metadata::setValue()} 186c3437056SNickeau */ 187c3437056SNickeau $this->buildFromReadStore(); 188c3437056SNickeau } 189c3437056SNickeau if ($this->descriptionOrigin === PageDescription::DESCRIPTION_COMBO_ORIGIN) { 19004fd306cSNickeau throw new ExceptionCompile("The description cannot be empty", PageDescription::DESCRIPTION_PROPERTY); 191c3437056SNickeau } else { 192c3437056SNickeau // The original description is from Dokuwiki, we don't send an error 193c3437056SNickeau // otherwise all page without a first description would get an error 194c3437056SNickeau // (What fucked up is fucked up) 195c3437056SNickeau return $this; 196c3437056SNickeau } 197c3437056SNickeau } 198c3437056SNickeau 199c3437056SNickeau } 200c3437056SNickeau 201c3437056SNickeau parent::setValue($value); 202c3437056SNickeau return $this; 203c3437056SNickeau } 204c3437056SNickeau 205c3437056SNickeau /** 20604fd306cSNickeau * @return ?string|array 207c3437056SNickeau */ 208c3437056SNickeau public function toStoreValue() 209c3437056SNickeau { 210c3437056SNickeau $metaDataStore = $this->getWriteStore(); 211c3437056SNickeau if (!($metaDataStore instanceof MetadataDokuWikiStore)) { 21204fd306cSNickeau // A string 213c3437056SNickeau return parent::toStoreValue(); 214c3437056SNickeau } 215c3437056SNickeau /** 216c3437056SNickeau * For dokuwiki, this is an array 217c3437056SNickeau */ 21804fd306cSNickeau try { 219c3437056SNickeau return array( 220c3437056SNickeau self::ABSTRACT_KEY => $this->getValue(), 221c3437056SNickeau self::DESCRIPTION_ORIGIN => PageDescription::DESCRIPTION_COMBO_ORIGIN 222c3437056SNickeau ); 22304fd306cSNickeau } catch (ExceptionNotFound $e) { 22404fd306cSNickeau return null; 22504fd306cSNickeau } 226c3437056SNickeau } 227c3437056SNickeau 22804fd306cSNickeau 229c3437056SNickeau public function getDescriptionOrigin(): string 230c3437056SNickeau { 231c3437056SNickeau return $this->descriptionOrigin; 232c3437056SNickeau } 233c3437056SNickeau 23404fd306cSNickeau private function getGeneratedValueFromDokuWikiStore(MetadataStore $metaDataStore): ?string 235c3437056SNickeau { 236c3437056SNickeau 237c3437056SNickeau /** 238c3437056SNickeau * The generated is in the current metadata 239c3437056SNickeau */ 24004fd306cSNickeau $descriptionArray = $metaDataStore->getFromName(self::PROPERTY_NAME); 241c3437056SNickeau if (empty($descriptionArray)) { 242c3437056SNickeau return null; 243c3437056SNickeau } 244c3437056SNickeau if (!array_key_exists(self::ABSTRACT_KEY, $descriptionArray)) { 245c3437056SNickeau return null; 246c3437056SNickeau } 247c3437056SNickeau $value = $descriptionArray[self::ABSTRACT_KEY]; 248c3437056SNickeau 249c3437056SNickeau 250c3437056SNickeau /** 251c3437056SNickeau * Dokuwiki description 252c3437056SNickeau * With some trick 253c3437056SNickeau * TODO: Generate our own description ? 254c3437056SNickeau */ 255c3437056SNickeau // suppress the carriage return 256c3437056SNickeau $description = str_replace("\n", " ", $value); 257c3437056SNickeau // suppress the h1 258c3437056SNickeau $resourceCombo = $this->getResource(); 25904fd306cSNickeau if ($resourceCombo instanceof MarkupPath) { 260c3437056SNickeau $description = str_replace($resourceCombo->getH1OrDefault(), "", $description); 261c3437056SNickeau } 262c3437056SNickeau // Suppress the star, the tab, About 263c3437056SNickeau $description = preg_replace('/(\*|\t|About)/im', "", $description); 264c3437056SNickeau // Suppress all double space and trim 265c3437056SNickeau return trim(preg_replace('/ /m', " ", $description)); 266c3437056SNickeau } 267c3437056SNickeau 268c3437056SNickeau 26904fd306cSNickeau public static function isOnForm(): bool 27004fd306cSNickeau { 27104fd306cSNickeau return true; 27204fd306cSNickeau } 273c3437056SNickeau} 274