Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:entwickler:plugin_development [2026/08/31 00:09] – created kainhoferen:entwickler:plugin_development [2026/12/04 17:30] (current) – install and uninstall fasse
Line 65: Line 65:
 │   │   └── Item.php                  extends Admidio\Infrastructure\Entity\Entity │   │   └── Item.php                  extends Admidio\Infrastructure\Entity\Entity
 │   ├── Service/ │   ├── Service/
-│   │   └── ItemService.php           the save and delete logic of a form+│   │   ├── ItemService.php           the save and delete logic of a form 
 +│   │   └── UpdateStepsCode.php       PHP methods called by database scripts
 │   └── Presenter/ │   └── Presenter/
 │       └── ItemPresenter.php         builds the pages and forms │       └── ItemPresenter.php         builds the pages and forms
Line 86: Line 87:
 │       └── ItemServiceTest.php │       └── ItemServiceTest.php
 │ │
-└── db_scripts/                     your own database tables +└── db_scripts/                     database scripts for the plugin 
-    ├── install.sql                   runs when the plugin is installed +    ├── install.xml                   prepares the plugin for the installation 
-    ├── uninstall.sql                 runs when it is removed +    ├── uninstall.xml                 removes the plugin's data when it is removed 
-    └── update_1_1.xml                upgrades an installed plugin to 1.1+    └── update_1_1.xml                applies the migrations for version 1.1
 </code> </code>
  
Line 263: Line 264:
 be aborted. A failing plugin **load** is only reported and skipped, because a request that never be aborted. A failing plugin **load** is only reported and skipped, because a request that never
 reaches the plugin manager cannot be repaired. reaches the plugin manager cannot be repaired.
 +
 ===== Growing a plugin ===== ===== Growing a plugin =====
  
Line 357: Line 359:
 ==== Database tables ==== ==== Database tables ====
  
-A plugin that needs its own tables puts them in ''db_scripts/'':+A plugin that needs its own tables puts its database scripts in ''db_scripts/''.
  
-  * ''install.sql'' runs when the plugin is installed, +^ File ^ When it runs ^ 
-  * ''uninstall.sql'' runs when it is removed, +| ''install.xml'' | Once for the Admidio installation, before Admidio creates the plugin's component record and seeds its settings. | 
-  * ''update_x_y.xml'' upgrades an installed plugin, in the same format as Admidio's own update scripts.+| ''uninstall.xml'' | When the installed plugin is removed, before Admidio removes its published pages, menu entries, settings and files. Disabling the plugin in an organization keeps its data. | 
 +| ''update_x_y.xml'' | When Admidio applies the migrations for version ''x.y''. During a fresh installation, Admidio also runs the update scripts, starting with the earliest one. |
  
-Use ''%PREFIX%'' for the table prefix, one statement per update step, and Admidio's generic SQL — +For installation and removal, Admidio looks for the corresponding XML file first. If 
-the same rules as for the core. Give tables the usual ''xxx_usr_id_create'', +''install.xml'' is absent, it uses ''install.sql'' if present. If ''uninstall.xml'' is absent, 
-''xxx_timestamp_create'', ''xxx_usr_id_change'' and ''xxx_timestamp_change'' columns.+it uses ''uninstall.sql'' if present. An invalid XML file aborts the operation. The SQL fallback 
 +applies only when the corresponding XML file is absent.
  
-Read and write your rows through an entity that extends ''Admidio\Infrastructure\Entity\Entity'', so +=== Writing XML steps === 
-that changes reach the change history.+ 
 +All three XML files use the same step format. Give each executable ''<step>'' a positive numeric 
 +ID and list the IDs in ascending order. Each step contains one SQL statement or one PHP method call. 
 +An optional ''<step>stop</step>'' ends execution. 
 + 
 +Use ''%PREFIX%'' for the installation's table prefix and write SQL that works on the database 
 +engines Admidio supports. If a step is specific to one engine, use ''database="mysql"'' for MySQL 
 +and MariaDB or ''database="pgsql"'' for PostgreSQL. SQL errors abort the script by default. 
 +''error="ignore"'' applies to SQL steps only. 
 + 
 +<code xml db_scripts/install.xml> 
 +<?xml version="1.0" encoding="UTF-8"?> 
 +<update> 
 +    <step id="10"> 
 +        CREATE TABLE IF NOT EXISTS %PREFIX%_my_plugin_items ( 
 +            item_id INTEGER NOT NULL PRIMARY KEY, 
 +            item_name VARCHAR(100) NOT NULL 
 +        ) 
 +    </step> 
 +    <step id="20">UpdateStepsCode::seedDefaultItem</step> 
 +    <step>stop</step> 
 +</update> 
 +</code> 
 + 
 +<code xml db_scripts/uninstall.xml> 
 +<?xml version="1.0" encoding="UTF-8"?> 
 +<update> 
 +    <step id="10">DROP TABLE IF EXISTS %PREFIX%_my_plugin_items</step> 
 +    <step>stop</step> 
 +</update> 
 +</code> 
 + 
 +=== Calling PHP from a step === 
 + 
 +Use ''UpdateStepsCode::methodName'' to call a public static method with no arguments. Admidio 
 +looks for ''UpdateStepsCode'' in the ''Service'' subnamespace of the first namespace declared in 
 +the plugin's ''autoload'' mapping. With ''AdmidioPlugin\MyPlugin\'' mapped to ''src/'', put the 
 +class in ''src/Service/UpdateStepsCode.php''. Admidio passes the active database connection to 
 +''UpdateStepsCode::setDatabase()'' before each call. 
 + 
 +<code php src/Service/UpdateStepsCode.php> 
 +<?php 
 + 
 +namespace AdmidioPlugin\MyPlugin\Service; 
 + 
 +use Admidio\Infrastructure\Database; 
 + 
 +final class UpdateStepsCode 
 +{ 
 +    private static Database $db; 
 + 
 +    public static function setDatabase(Database $database): void 
 +    { 
 +        self::$db = $database; 
 +    } 
 + 
 +    public static function seedDefaultItem(): void 
 +    { 
 +        $table = Database::prepareSqlAdmidioParameters('%PREFIX%_my_plugin_items'); 
 + 
 +        $exists = self::$db->queryPrepared( 
 +            'SELECT 1 FROM ' . $table . ' WHERE item_id = ?', 
 +            array(1) 
 +        )->fetchColumn(); 
 + 
 +        if ($exists === false) { 
 +            self::$db->queryPrepared( 
 +                'INSERT INTO ' . $table . ' (item_id, item_name) VALUES (?, ?)', 
 +                array(1, 'Default item') 
 +            ); 
 +        } 
 +    } 
 +} 
 +</code> 
 + 
 +Admidio makes the plugin's classes and language files available to the installation and removal 
 +scripts without loading ''plugin.php'' for that purpose. Install steps run before the plugin's 
 +component record and settings exist. Uninstall steps can still use those records while cleaning 
 +up plugin data. 
 + 
 +=== Retrying a failed script === 
 + 
 +A retry of ''install.xml'' or ''uninstall.xml'' starts at the first step. Write each step so it can 
 +run again after an earlier attempt partially succeeded. For example, use ''DROP TABLE IF EXISTS'' 
 +when removing a table and check whether seed data already exists before inserting it. 
 + 
 +For ''update_x_y.xml'', Admidio records the last completed step and resumes after it. Keep the IDs 
 +of released update steps stable. Because fresh installations also run the update scripts, write 
 +those scripts to handle the schema already created by ''install.xml'' as well as older schemas. 
 + 
 +When your entities use Admidio's creation and modification metadata, add the corresponding 
 +''xxx_usr_id_create'', ''xxx_timestamp_create'', ''xxx_usr_id_change'' and 
 +''xxx_timestamp_change'' columns. Use an entity that extends 
 +''Admidio\Infrastructure\Entity\Entity'' for changes that should reach the change history.
  
   * [[en:entwickler:aenderungen_in_der_datenbank_einbauen|Installing changes to the database]]   * [[en:entwickler:aenderungen_in_der_datenbank_einbauen|Installing changes to the database]]
Line 587: Line 684:
   * **The directory name is the identity.** Lowercase letters, digits and hyphens only.   * **The directory name is the identity.** Lowercase letters, digits and hyphens only.
   * **Never write into the Admidio core.** Everything you need belongs in your own directory.   * **Never write into the Admidio core.** Everything you need belongs in your own directory.
-  * **Installing must not change anything.** A freshly installed plugin should do nothing until it is switched on. +  * **Keep setup in the installation script.** Create the plugin's schema and seed any data it needs in ''db_scripts/install.xml''. Keep organization-specific behavior tied to whether the plugin is enabled for that organization. 
-  * **Removing must leave nothing behind.** Your settings and your tables go with it.+  * **Clean up plugin-owned data on removal.** Use ''db_scripts/uninstall.xml'' to drop the plugin's tables and remove any other data it owns. Admidio removes the settings declared in the manifest.
   * **Validate every request parameter** with ''admFuncVariableIsValid()'', use ''FormPresenter'' for forms and Admidio's CSRF handling, and use ''$gDb->queryPrepared()''.   * **Validate every request parameter** with ''admFuncVariableIsValid()'', use ''FormPresenter'' for forms and Admidio's CSRF handling, and use ''$gDb->queryPrepared()''.
   * **Guard files that are not pages.** A file below ''plugins/'' can be requested directly, so put this at the top of your entry file and of anything else that is not a page:   * **Guard files that are not pages.** A file below ''plugins/'' can be requested directly, so put this at the top of your entry file and of anything else that is not a page:
Line 604: Line 701:
   * [[en:plugins:index|The plugins published for Admidio]].   * [[en:plugins:index|The plugins published for Admidio]].
   * The example plugin ''plugins/hello-world/'' ships with Admidio and shows every convention on this page in one working plugin. Copy the directory, rename it, and start deleting what you do not need.   * The example plugin ''plugins/hello-world/'' ships with Admidio and shows every convention on this page in one working plugin. Copy the directory, rename it, and start deleting what you do not need.
- 
  • en/entwickler/plugin_development.txt
  • Last modified: 2026/12/04 17:30
  • by fasse