J3.x:Developing an MVC Component/Adding an update server
From Joomla! Documentation
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version
.
Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).
This tutorial is part of the Developing an MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
In this part, we will cover the various aspects of setting up an update server for our helloworld component.
Under Construction!
Introduction
Updating Joomla extensions via an update server is a two step process:
- finding the updates available for the installed extensions
- selecting an update and installing it.
Typically this is done by the administrator via the Extensions / Manage / Update page.
- You click on Find Updates to find the updates available
- You mark the checkbox of each update you want to install and click on Update.
In this step we enable our Helloworld component to be updated in this way.
Finding Updates
Each Joomla extension which is updated via an update server has a URL associated with it which points to an XML file containing details of the updates available. This file could be hosted on the extension developer's own website, for example. If you go to Extensions / Manage / Update Sites you will see this URL in the Update Site column, and you can click on those URLs to see the format of the XML files. (Some of these XML files may be of type "collection" where the first XML element is <extensionset>, and these are described towards the end of this tutorial, but for now we're concentrating on the type "extension" where the first XML element is <updates>).
The XML file (described more fully below) contains details of what updates are available for that extension, including any restrictions (eg you have to be running a certain Joomla version to be able to use it), and the URL of the zip file containing the new version of the extension. You tell Joomla the URL of the XML file for your extension within the manifest file (helloworld.xml), eg
<updateservers>
<server type="extension" name="Helloworld Updates">http://myDeveloperSite/helloworld-updates/updates.xml</server>
</updateservers>
Below is a basic update XML file
<updates>
<update>
<name>Helloworld component</name>
<description>Helloworld component for Joomla MVC tutorial</description>
<element>com_helloworld</element>
<type>component</type>
<version>1.0.0</version>
<downloads>
<downloadurl type="full" format="zip">http://myDeveloperSite/helloworld-updates/helloworld-1-0-0.zip</downloadurl>
</downloads>
<targetplatform name="joomla" version="3.[23456789]" />
<php_minimum>7.1</php_minimum>
</update>
</updates>
Note that it has
- basic information about the component – eg name, version
- a link to the zip file for that update
- restrictions – for Joomla versions 3.2 to 3.9 only, and at least PHP version 7.1
Whenever an administrator clicks on the Find Updates button, Joomla sends HTTP requests to each of the URLs of these update servers to retrieve the XML files. It parses each XML file,
- ensures it's properly formed XML
- compares the version of the update with the version of the extension currently running
- checks any restrictions (Joomla versions, PHP versions, database versions) with what is currently running.
If it finds it's a valid update for this website, then it inserts a record into the "updates" table in the database. (You'll find at most 1 update for each extension, because if there are several possible then Joomla will store only the most recent valid version, ie with the highest version number).
Installing Updates
Once an update has been inserted into the "updates" table it is visible to the administrator in the Extension / Manage / Update page. After checking the checkbox and clicking the Update button Joomla will
- once again send an HTTP request to the URL of the update XML file
- parse the XML file and ensure that conditions for installing that version are still met
- send an HTTP request to the URL of the update zip file, storing the file received in the HTTP response in its /tmp directory
- proceed to install the new version from the downloaded zip file.
Approach
In this tutorial step we will update our helloworld extension to use an update server.
We'll also set up the server files so that you can verify that the update process works ok.
To do this you'll need to configure part of your development area to act as the update server. This basically means creating a directory below the root directory serviced by your web server, and including the update XML file etc in it.
For example, I have a folder /helloworld which contains my Joomla instance, so that to access the site I put the URL http://localhost/helloworld/index.php into the browser.
For the update server I created a folder /helloworld-updates at the same level as /helloworld, and put the updates.xml file into that folder. I can then access the XML file by putting the URL http://localhost/helloworld-updates/updates.xml into the browser.
You'll need to do something similar in your own environment, and verify that the URL is correct by accessing the XML file through your browser.
Updated Code
The only change we need to make to our helloworld component is to include the update server inside the manifest file. Once updated, zip up the component as usual and install it on your Joomla instance.
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.32</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>
<!-- Runs on install/uninstall/update; New in 2.5 -->
<scriptfile>script.php</scriptfile>
<install> <!-- Runs on install -->
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall> <!-- Runs on uninstall -->
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<filename>router.php</filename>
<folder>controllers</folder>
<folder>views</folder>
<folder>models</folder>
<folder>helpers</folder>
</files>
<languages folder="site/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>
</languages>
<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
<folder>js</folder>
<folder>css</folder>
</media>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>config.xml</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<filename>access.xml</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
<!-- layout files section -->
<folder>layouts</folder>
</files>
<languages folder="admin/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.sys.ini</language>
</languages>
</administration>
<updateservers>
<!-- Change the URL below to match that of your own update XML file -->
<server type="extension" name="Helloworld Updates">http://localhost/helloworld-updates/updates.xml</server>
</updateservers>
</extension>