[CORE] Plugin API now extends a new Module API

This commit is contained in:
Diogo Cordeiro
2019-08-12 15:03:30 +01:00
committed by Diogo Peralta Cordeiro
parent 3821d2317e
commit 20a6f1d4f4
397 changed files with 3093 additions and 1450 deletions

View File

@@ -1,59 +1,62 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* GNU social - a federating social network
*
* A plugin to use memcached for the interface with memcache
*
* This used to be encoded as config-variable options in the core code;
* it's now broken out to a separate plugin. The same interface can be
* implemented by other plugins.
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Cache
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @author Craig Andrews <candrews@integralblue.com>
* @author Miguel Dantas <biodantas@gmail.com>
* @copyright 2009 StatusNet, Inc.
* @copyright 2009, 2019 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* A plugin to use memcached for the cache interface
*
* This used to be encoded as config-variable options in the core code;
* it's now broken out to a separate plugin. The same interface can be
* implemented by other plugins.
*
* @copyright 2009, 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class MemcachedPlugin extends Plugin
{
const PLUGIN_VERSION = '2.1.0';
static $cacheInitialized = false;
public static $cacheInitialized = false;
public $servers = ['127.0.0.1'];
public $defaultExpiry = 86400; // 24h
private $_conn = null;
private $_conn = null;
/**
* Initialize the plugin
*
* Note that onStartCacheGet() may have been called before this!
*
* @return boolean flag value
* @return bool flag value
*/
function onInitializePlugin()
public function initialize(): bool
{
if (self::$cacheInitialized) {
$this->persistent = true;
@@ -83,12 +86,12 @@ class MemcachedPlugin extends Plugin
*
* The value should have been set previously.
*
* @param string &$key in; Lookup key
* @param string &$key in; Lookup key
* @param mixed &$value out; value associated with key
*
* @return boolean hook success
* @return bool hook success
*/
function onStartCacheGet(&$key, &$value)
public function onStartCacheGet(&$key, &$value): bool
{
try {
$this->_ensureConn();
@@ -108,15 +111,15 @@ class MemcachedPlugin extends Plugin
/**
* Associate a value with a key
*
* @param string &$key in; Key to use for lookups
* @param mixed &$value in; Value to associate
* @param integer &$flag in; Flag empty or Memcached::OPT_COMPRESSION (translated by the `flag` method)
* @param integer &$expiry in; Expiry (passed through to Memcache)
* @param boolean &$success out; Whether the set was successful
* @param string &$key in; Key to use for lookups
* @param mixed &$value in; Value to associate
* @param integer &$flag in; Flag empty or Memcached::OPT_COMPRESSION (translated by the `flag` method)
* @param integer &$expiry in; Expiry (passed through to Memcache)
* @param bool &$success out; Whether the set was successful
*
* @return boolean hook success
* @return bool hook success
*/
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
public function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success): bool
{
if ($expiry === null) {
$expiry = $this->defaultExpiry;
@@ -138,13 +141,13 @@ class MemcachedPlugin extends Plugin
* Atomically increment an existing numeric key value.
* Existing expiration time will not be changed.
*
* @param string &$key in; Key to use for lookups
* @param int &$step in; Amount to increment (default 1)
* @param mixed &$value out; Incremented value, or false if key not set.
* @param string &$key in; Key to use for lookups
* @param int &$step in; Amount to increment (default 1)
* @param mixed &$value out; Incremented value, or false if key not set.
*
* @return boolean hook success
* @return bool hook success
*/
function onStartCacheIncrement(&$key, &$step, &$value)
public function onStartCacheIncrement(&$key, &$step, &$value): bool
{
try {
$this->_ensureConn();
@@ -164,12 +167,12 @@ class MemcachedPlugin extends Plugin
/**
* Delete a value associated with a key
*
* @param string &$key in; Key to lookup
* @param boolean &$success out; whether it worked
* @param string &$key in; Key to lookup
* @param bool &$success out; whether it worked
*
* @return boolean hook success
* @return bool hook success
*/
function onStartCacheDelete(&$key, &$success)
public function onStartCacheDelete(&$key, &$success): bool
{
try {
$this->_ensureConn();
@@ -181,7 +184,11 @@ class MemcachedPlugin extends Plugin
return !$success;
}
function onStartCacheReconnect(&$success)
/**
* @param $success
* @return bool
*/
public function onStartCacheReconnect(&$success): bool
{
if (empty($this->_conn)) {
// nothing to do
@@ -206,7 +213,7 @@ class MemcachedPlugin extends Plugin
*
* @return void
*/
private function _ensureConn()
private function _ensureConn(): void
{
if (empty($this->_conn)) {
$this->_conn = new Memcached(common_config('site', 'nickname'));
@@ -243,7 +250,7 @@ class MemcachedPlugin extends Plugin
* @param int $flag
* @return int
*/
protected function flag($flag)
protected function flag(int $flag): int
{
$out = 0;
if ($flag & Cache::COMPRESSED == Cache::COMPRESSED) {
@@ -252,15 +259,15 @@ class MemcachedPlugin extends Plugin
return $out;
}
function onPluginVersion(array &$versions)
public function onPluginVersion(array &$versions): bool
{
$versions[] = array('name' => 'Memcached',
'version' => self::PLUGIN_VERSION,
'author' => 'Evan Prodromou, Craig Andrews',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Memcached',
'description' =>
// TRANS: Plugin description.
_m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
'version' => self::PLUGIN_VERSION,
'author' => 'Evan Prodromou, Craig Andrews',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Memcached',
'rawdescription' =>
// TRANS: Plugin description.
_m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
return true;
}
}