[HowTo] Convert 3.0 MOD into 3.1 Extension

Discussion forum for Extension Writers regarding Extension Development.
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling
Contact:

[HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nickvergessen »

This tutorial has been moved to the extensions documentation at https://area51.phpbb.com/docs/dev/31x/e ... nsion.html
No Support via PM
User avatar
Galandas
Registered User
Posts: 732
Joined: Thu Jul 23, 2009 4:11 pm
Location: Italy
Name: Rey
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by Galandas »

Excellent basic guide.
English is not my native language My CDB Contributions My RC extensions
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5176
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

To add to the above:

Two very commonly used functions (especially in ACP pages) have been deprecated for phpBb 3.1:
set_config()
request_var()

Instead use:
$config->set()
$request->variable()

Also, for checking if a form was submitted, a new function was added to replace isset($_POST['submit']) with $request->is_set_post('submit')

Here is a basic usage example:

Code: Select all

class acp_foobar_module
{
	public $u_action;

	protected $config;
	protected $request;

	public function main($id, $mode)
	{
		global $config, $request;

		$this->config = $config;
		$this->request = $request;

		$form_key = 'acp_foobar';
		add_form_key($form_key);

		if ($this->request->is_set_post('submit'))
		{
			if (!check_form_key($form_key))
			{
				trigger_error('FORM_INVALID');
			}

			$foobar_option = $this->request->variable('foobar_option', 0);
			$this->config->set('foobar_option', $foobar_option);

			trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
		}
	}
}
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
wintstar
Registered User
Posts: 317
Joined: Sat Mar 07, 2009 12:39 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by wintstar »

but with a little htaccess rule this can be rewritten to phpBB/newspage
This is no longer necessary in my opinion, since it in ACP at build # 1045 gives the
Enable URL rewriting module. When activated, it gives the url phpBB/newspage.
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nickvergessen »

wintstar wrote:
but with a little htaccess rule this can be rewritten to phpBB/newspage
This is no longer necessary in my opinion, since it in ACP at build # 1045 gives the
Enable URL rewriting module. When activated, it gives the url phpBB/newspage.
Note, when you don't have the rule in your htaccess the acp option will only send you to 404 pages
No Support via PM
User avatar
wintstar
Registered User
Posts: 317
Joined: Sat Mar 07, 2009 12:39 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by wintstar »

nickvergessen wrote:
wintstar wrote:
but with a little htaccess rule this can be rewritten to phpBB/newspage
This is no longer necessary in my opinion, since it in ACP at build # 1045 gives the
Enable URL rewriting module. When activated, it gives the url phpBB/newspage.
Note, when you don't have the rule in your htaccess the acp option will only send you to 404 pages
Thanks for Info :)
User avatar
PayBas
Former Team Member
Posts: 930
Joined: Thu May 25, 2006 12:37 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by PayBas »

So how exactly would you populate a table you create using this method with data?

Code: Select all

public function update_schema()
{
    return array(
        'add_tables' => [
            $this->table_prefix . 'my_special_table' => [
                'COLUMNS' => [
                    'config_name'        => ['VCHAR', ''],
                    'config_value'        => ['MTEXT', ''],
                    'config_default'    => ['MTEXT', ''],
                ],
                'PRIMARY_KEY'    => 'config_name',
            ],
        ],
    );
}

public function revert_schema()
{
    return array(
        'drop_tables' => [
            $this->table_prefix . 'my_special_table',
        ],
    );
}
 

Code: Select all

public function update_data()
{
    return array(
        ???
    );
}
 
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5176
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

You would need to write a custom function that would insert your data, and then call that function from the update_data() method... something like:

Code: Select all

public function update_data()
{
    return array(
        array('custom', array(array($this, 'my_special_table_update'))),
    );
}

public function my_special_table_update()
{
    $sql_ary = array(
        'config_name' => 'foobar',
        'config_value' => 'foobar',
        'config_default' => 'foobar',
    );

    $sql = 'INSERT INTO ' . $this->table_prefix . 'my_special_table ' . $this->db->sql_build_array('INSERT', $sql_ary);
    $this->sql_query($sql);
} 
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
PayBas
Former Team Member
Posts: 930
Joined: Thu May 25, 2006 12:37 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by PayBas »

For future reference, I decided to use sql_multi_insert:

Code: Select all

    public function update_data()
    {
        return array(
            array('custom', array(array($this, 'set_initial_data'))),
        );
    }

    public function set_initial_data()
    {
        $sql_ary = array(
        array(
            'config_name' => 'foobar',
            'config_value' => 'foobar',
            'config_default' => 'foobar',
        ), array(
            'config_name' => 'bar',
            'config_value' => 'bar',
            'config_default' => 'bar',
        ));
    
        $sql = $this->db->sql_multi_insert($this->table_prefix . 'my_special_table', $sql_ary);
        $this->sql_query($sql);
    } 
User avatar
PayBas
Former Team Member
Posts: 930
Joined: Thu May 25, 2006 12:37 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by PayBas »

New day, new question.

When disabling an extension, and clicking "delete data", the migration script will run the custom "set_initial_data" function again. This of course will give SQL errors like:

Code: Select all

Duplicate entry 'foo' for key 'PRIMARY' [1062]
I tried adding the following code, but it doesn't seem to make a difference.

Code: Select all

    public function revert_data()
    {
        return array(
            array('custom', array(array($this, 'delete_data'))),
        );
    }

    public function delete_data()
    {
        $sql = 'DELETE * FROM ' . $this->table_prefix . 'my_special_table';
        $this->sql_query($sql);
    } 
Not sure if it's a bug, or I'm missing something.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5176
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

At the start of your custom function, the first thing you should do is check to see if the table already exists and if it holds data.

If it does, return, otherwise, proceed with the populating of your data table.

Code: Select all

public function set_initial_data()
{
    // Check if my table exists and holds data
    if ($this->db_tools->sql_table_exists($this->table_prefix . 'my_special_table'))
    {
        $sql = 'SELECT * FROM ' . $this->table_prefix . 'my_special_table';
        $result = $this->db->sql_query_limit($sql, 1);
        $row = $this->db->sql_fetchrow($result);
        if (!empty($row))
        {
            return;
        }
    }
    else
    {
        return;
    }

    $sql_ary = array(
    array(
        'config_name' => 'foobar',
        'config_value' => 'foobar',
        'config_default' => 'foobar',
    ), array(
        'config_name' => 'bar',
        'config_value' => 'bar',
        'config_default' => 'bar',
    ));

    $sql = $this->db->sql_multi_insert($this->table_prefix . 'my_special_table', $sql_ary);
    $this->sql_query($sql);
} 
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
PayBas
Former Team Member
Posts: 930
Joined: Thu May 25, 2006 12:37 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by PayBas »

VSE wrote:At the start of your custom function, the first thing you should do is check to see if the table already exists and if it holds data.

If it does, return, otherwise, proceed with the populating of your data table.
Thanks. I eventually used a similar method after trying some other things. I just got confused because I couldn't understand why a roll-back migration would call update_data() instead of revert_data().
User avatar
wintstar
Registered User
Posts: 317
Joined: Sat Mar 07, 2009 12:39 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by wintstar »

And how, inserts data into several independent tables? :)
tct
Registered User
Posts: 24
Joined: Wed Jan 14, 2009 6:41 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by tct »

"Catchable fatal error: Argument 4 passed to nickvergessen\newspage\newspage::__construct() must be an instance of phpbb\db\driver\driver_interface, instance of phpbb\db\driver\mysql given, called in /var/www/diendan/cache/container_dotslash.php on line 1464 and defined in /var/www/diendan/ext/nickvergessen/newspage/newspage.php on line 52"
demo download from: https://github.com/nickvergessen/phpbb-ext-newspage
using: phpbb 3.1b2

sorry my english is not good!!!
Sorry! My English is not good!!!
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling
Contact:

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nickvergessen »

Sorry the extension was updated to work with the next beta release.
In the future I will create tags, so you can download the version for your phpbb version to keep testing.
No Support via PM
Post Reply

Return to “Extension Writers Discussion”