| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Description action plugin. |
||
| 5 | * |
||
| 6 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
||
| 7 | * @author Ikuo Obataya <[email protected]> |
||
| 8 | * @author Matthias Schulte <[email protected]>. |
||
| 9 | * @author Mark C. Prins <[email protected]> |
||
| 10 | * |
||
| 11 | * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
||
| 12 | * @noinspection AutoloadingIssuesInspection |
||
| 13 | */ |
||
| 14 | |||
| 15 | use dokuwiki\Extension\ActionPlugin; |
||
| 16 | use dokuwiki\Extension\Event; |
||
|
0 ignored issues
–
show
|
|||
| 17 | use dokuwiki\Extension\EventHandler; |
||
| 18 | |||
| 19 | const KEYWORD_SOURCE_ABSTRACT = 'abstract'; |
||
| 20 | const KEYWORD_SOURCE_GLOBAL = 'global'; |
||
| 21 | const KEYWORD_SOURCE_SYNTAX = 'syntax'; |
||
| 22 | |||
| 23 | class action_plugin_description extends ActionPlugin |
||
| 24 | { |
||
| 25 | final public function register(EventHandler $controller): void |
||
| 26 | { |
||
| 27 | $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description', []); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Add an abstract, global value or a specified string to meta header |
||
| 32 | */ |
||
| 33 | final public function description(Event $event): void |
||
| 34 | { |
||
| 35 | if (empty($event->data) || empty($event->data['meta'])) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | global $ID; |
||
| 40 | $source = $this->getConf('keyword_source'); |
||
| 41 | if (empty($source)) { |
||
| 42 | $source = 'abstract'; |
||
| 43 | } |
||
| 44 | |||
| 45 | $metaContent = ''; |
||
| 46 | switch ($source) { |
||
| 47 | case KEYWORD_SOURCE_ABSTRACT: |
||
| 48 | if (auth_quickaclcheck($ID) < AUTH_READ) { |
||
|
0 ignored issues
–
show
|
|||
| 49 | // don't add meta header when user has no read permissions |
||
| 50 | return; |
||
| 51 | } |
||
| 52 | $d = p_get_metadata($ID, 'description'); |
||
| 53 | if (empty($d)) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | $metaContent = str_replace("\n", " ", $d['abstract']); |
||
| 57 | if (empty($metaContent)) { |
||
| 58 | return; |
||
| 59 | } |
||
| 60 | break; |
||
| 61 | case KEYWORD_SOURCE_GLOBAL: |
||
| 62 | $metaContent = $this->getConf('global_description'); |
||
| 63 | if (empty($metaContent)) { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | break; |
||
| 67 | case KEYWORD_SOURCE_SYNTAX: |
||
| 68 | if (auth_quickaclcheck($ID) < AUTH_READ) { |
||
| 69 | // don't add meta header when user has no read permissions |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | $metadata = p_get_metadata($ID); |
||
| 73 | |||
| 74 | // Normalize to an array |
||
| 75 | if (!is_array($metadata)) { |
||
| 76 | $metadata = []; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Safely read keywords if the structure is present and an array |
||
| 80 | if (isset($metadata['plugin_description']) && is_array($metadata['plugin_description'])) { |
||
| 81 | $metaContent = $metadata['plugin_description']['keywords'] ?? ''; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (empty($metaContent)) { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | |||
| 90 | $event->data['meta'][] = ["name" => "description", "content" => $metaContent]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: