Help:Toolforge/My first PHP tool
Overview
Php webservices are used by many existing tools, including MediaWiki itself. PHP is a general-purpose scripting language geared towards web development.
This stub webservice is designed to get a sample PHP application installed onto Toolforge using the new build service, as quickly as possible. The application is written using the PHP 8.2, the Composer PHP package manager, and running on top of the Apache HTTP server, all provided by the buildpacks.
Then we'll use the given database credentials to extract the list of available databases in the replicas.
The guide will teach you how to:
- Create a new tool
- Run a PHP 8.2 webservice on Kubernetes
- Connect to the wikireplicas from the code
- Run a query and extract information
Getting started
Prerequisites
Skills
- Basic knowledge of PHP
- Basic knowledge of SSH
- Basic knowledge of the Unix command line
- Basic knowledge of Git
Accounts
Step-by-step guide
Step 1: Create a new tool account
- Follow the Toolforge quickstart guide to create a Toolforge tool and SSH into Toolforge.
- For the examples in this tutorial,
sample-php-buildpack-appis used to indicate places where your unique tool name is used in another command.
- For the examples in this tutorial,
- Make sure to create a git repository for the tool, you can get one like this:
- Log into the toolforge admin page
- Select your tool
- On the left side panel, under
Git repositoriesclickcreate repository - Copy the url in the
Clonesection- There's a private url, that we will use to clone it locally, starting with "git":
git@gitlab.wikimedia.org:toolforge-repos/sample-php-buildpack-app.git - And a public one, that we will use to build the app in toolforge, starting with "https":
https://gitlab.wikimedia.org/toolforge-repos/sample-php-buildpack-app.git
- There's a private url, that we will use to clone it locally, starting with "git":
Step 2: Create a basic PHP WSGI webservice for Apache
- What is Apache?
Apache HTTP server is a very flexible web server popular for running many different types of applications.
How to create a basic PHP WSGI webservice
- Clone your tool git repository
You will have to clone the tool repository to be able to add code to it, on your local computer (with git installed) you can run:
laptop:~$ git clone git@gitlab.wikimedia.org:toolforge-repos/sample-php-buildpack-app.git
laptop:~$ cd sample-php-buildpack-app
That will create a folder called sample-php-buildpack-app. We are going to put the code in that folder.
- Add the PHP dependencies
Note: Use a file named composer.json to keep track of the library dependencies of applications.
laptop:~sample-python-buildpack-app$ cat > composer.json << EOF
{
"require": {
"php": "^8.2"
}
}
EOF
- Create the dependencies lockfile
For this you will need to have PHP and composer installed on your environment, you can use your preferred installation method (system packages, manually, up to you).
laptop:~sample-python-buildpack-app$ composer install
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
- Create a 'hello world' PHP application, by creating an index.php file
