]> git.mxchange.org Git - friendica.git/commitdiff
Move admin/dbsync to src/Module
authorHypolite Petovan <hypolite@mrpetovan.com>
Fri, 26 Apr 2019 03:47:10 +0000 (23:47 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 2 May 2019 13:52:51 +0000 (09:52 -0400)
mod/admin.php
src/App/Router.php
src/Module/Admin/DBSync.php [new file with mode: 0644]
src/Module/BaseAdminModule.php
view/templates/admin/dbsync/failed_updates.tpl [new file with mode: 0644]
view/templates/admin/dbsync/structure_check.tpl [new file with mode: 0644]
view/templates/failed_updates.tpl [deleted file]
view/templates/structure_check.tpl [deleted file]

index 71089cc39a97a0249805d48d7a05a57098761017..1632f7c0aa1327f7175c51af9d22198c3f0f1332 100644 (file)
@@ -166,9 +166,6 @@ function admin_content(App $a)
        // urls
        if ($a->argc > 1) {
                switch ($a->argv[1]) {
-                       case 'dbsync':
-                               $o = admin_page_dbsync($a);
-                               break;
                        case 'deleteitem':
                                $o = admin_page_deleteitem($a);
                                break;
@@ -246,104 +243,6 @@ function admin_page_deleteitem_post(App $a)
        return; // NOTREACHED
 }
 
-/**
- * @brief Generates admin panel subpage for DB syncronization
- *
- * This page checks if the database of friendica is in sync with the specs.
- * Should this not be the case, it attemps to sync the structure and notifies
- * the admin if the automatic process was failing.
- *
- * The returned string holds the HTML code of the page.
- *
- * @param App $a
- * @return string
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
-function admin_page_dbsync(App $a)
-{
-       $o = '';
-
-       if ($a->argc > 3 && intval($a->argv[3]) && $a->argv[2] === 'mark') {
-               Config::set('database', 'update_' . intval($a->argv[3]), 'success');
-               $curr = Config::get('system', 'build');
-               if (intval($curr) == intval($a->argv[3])) {
-                       Config::set('system', 'build', intval($curr) + 1);
-               }
-               info(L10n::t('Update has been marked successful') . EOL);
-               $a->internalRedirect('admin/dbsync');
-       }
-
-       if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) {
-               $retval = DBStructure::update($a->getBasePath(), false, true);
-               if ($retval === '') {
-                       $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
-                       Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);
-                       Config::set('database', 'last_successful_update_time', time());
-               } else {
-                       $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
-               }
-               if ($a->argv[2] === 'check') {
-                       return $o;
-               }
-       }
-
-       if ($a->argc > 2 && intval($a->argv[2])) {
-               require_once 'update.php';
-
-               $func = 'update_' . intval($a->argv[2]);
-
-               if (function_exists($func)) {
-                       $retval = $func();
-
-                       if ($retval === Update::FAILED) {
-                               $o .= L10n::t("Executing %s failed with error: %s", $func, $retval);
-                       } elseif ($retval === Update::SUCCESS) {
-                               $o .= L10n::t('Update %s was successfully applied.', $func);
-                               Config::set('database', $func, 'success');
-                       } else {
-                               $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);
-                       }
-               } else {
-                       $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";
-                       Config::set('database', $func, 'success');
-               }
-
-               return $o;
-       }
-
-       $failed = [];
-       $r = q("SELECT `k`, `v` FROM `config` WHERE `cat` = 'database' ");
-
-       if (DBA::isResult($r)) {
-               foreach ($r as $rr) {
-                       $upd = intval(substr($rr['k'], 7));
-                       if ($upd < 1139 || $rr['v'] === 'success') {
-                               continue;
-                       }
-                       $failed[] = $upd;
-               }
-       }
-
-       if (!count($failed)) {
-               $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure_check.tpl'), [
-                       '$base' => System::baseUrl(true),
-                       '$banner' => L10n::t('No failed updates.'),
-                       '$check' => L10n::t('Check database structure'),
-               ]);
-       } else {
-               $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('failed_updates.tpl'), [
-                       '$base' => System::baseUrl(true),
-                       '$banner' => L10n::t('Failed Updates'),
-                       '$desc' => L10n::t('This does not include updates prior to 1139, which did not return a status.'),
-                       '$mark' => L10n::t("Mark success \x28if update was manually applied\x29"),
-                       '$apply' => L10n::t('Attempt to execute this update step automatically'),
-                       '$failed' => $failed
-               ]);
-       }
-
-       return $o;
-}
-
 function admin_page_server_vital()
 {
        // Fetch the host-meta to check if this really is a vital server
index 47f71c988e0c2a845a862106ed35a8751a300259..6352a18130d43bf74fa37a72fa1e8ebb761f862c 100644 (file)
@@ -127,6 +127,10 @@ class Router
                        $collector->addRoute(['GET', 'POST'], '/blocklist/contact'      , Module\Admin\Blocklist\Contact::class);
                        $collector->addRoute(['GET', 'POST'], '/blocklist/server'       , Module\Admin\Blocklist\Server::class);
 
+                       $collector->addRoute(['GET']        , '/dbsync[/check]'         , Module\Admin\DBSync::class);
+                       $collector->addRoute(['GET']        , '/dbsync/{update:\d+}'    , Module\Admin\DBSync::class);
+                       $collector->addRoute(['GET']        , '/dbsync/mark/{update:\d+}', Module\Admin\DBSync::class);
+
                        $collector->addRoute(['GET', 'POST'], '/features'               , Module\Admin\Features::class);
                        $collector->addRoute(['GET']        , '/federation'             , Module\Admin\Federation::class);
 
diff --git a/src/Module/Admin/DBSync.php b/src/Module/Admin/DBSync.php
new file mode 100644 (file)
index 0000000..c62fefe
--- /dev/null
@@ -0,0 +1,108 @@
+<?php\r
+\r
+namespace Friendica\Module\Admin;\r
+\r
+use Friendica\Core\Config;\r
+use Friendica\Core\L10n;\r
+use Friendica\Core\Renderer;\r
+use Friendica\Core\System;\r
+use Friendica\Core\Update;\r
+use Friendica\Database\DBA;\r
+use Friendica\Database\DBStructure;\r
+use Friendica\Module\BaseAdminModule;\r
+use Friendica\Util\Strings;\r
+\r
+class DBSync extends BaseAdminModule\r
+{\r
+       public static function content()\r
+       {\r
+               parent::content();\r
+\r
+               $a = self::getApp();\r
+\r
+               $o = '';\r
+\r
+               if ($a->argc > 3 && $a->argv[2] === 'mark') {\r
+                       // @TODO: Replace with parameter from router\r
+                       $update = intval($a->argv[3]);\r
+                       if ($update) {\r
+                               Config::set('database', 'update_' . $update, 'success');\r
+                               $curr = Config::get('system', 'build');\r
+                               if (intval($curr) == $update) {\r
+                                       Config::set('system', 'build', intval($curr) + 1);\r
+                               }\r
+                               info(L10n::t('Update has been marked successful') . EOL);\r
+                       }\r
+                       $a->internalRedirect('admin/dbsync');\r
+               }\r
+\r
+               if ($a->argc > 2) {\r
+                       if ($a->argv[2] === 'check') {\r
+                               $retval = DBStructure::update($a->getBasePath(), false, true);\r
+                               if ($retval === '') {\r
+                                       $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";\r
+                                       Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);\r
+                                       Config::set('database', 'last_successful_update_time', time());\r
+                               } else {\r
+                                       $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";\r
+                               }\r
+                               if ($a->argv[2] === 'check') {\r
+                                       return $o;\r
+                               }\r
+                       } elseif (intval($a->argv[2])) {\r
+                               require_once 'update.php';\r
+\r
+                               // @TODO: Replace with parameter from router\r
+                               $update = intval($a->argv[2]);\r
+\r
+                               $func = 'update_' . $update;\r
+\r
+                               if (function_exists($func)) {\r
+                                       $retval = $func();\r
+\r
+                                       if ($retval === Update::FAILED) {\r
+                                               $o .= L10n::t("Executing %s failed with error: %s", $func, $retval);\r
+                                       } elseif ($retval === Update::SUCCESS) {\r
+                                               $o .= L10n::t('Update %s was successfully applied.', $func);\r
+                                               Config::set('database', $func, 'success');\r
+                                       } else {\r
+                                               $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);\r
+                                       }\r
+                               } else {\r
+                                       $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";\r
+                                       Config::set('database', $func, 'success');\r
+                               }\r
+\r
+                               return $o;\r
+                       }\r
+               }\r
+\r
+               $failed = [];\r
+               $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);\r
+               while ($config = DBA::fetch($configStmt)) {\r
+                       $upd = intval(substr($config['k'], 7));\r
+                       if ($upd >= 1139 && $config['v'] != 'success') {\r
+                               $failed[] = $upd;\r
+                       }\r
+               }\r
+\r
+               if (!count($failed)) {\r
+                       $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [\r
+                               '$base' => System::baseUrl(true),\r
+                               '$banner' => L10n::t('No failed updates.'),\r
+                               '$check' => L10n::t('Check database structure'),\r
+                       ]);\r
+               } else {\r
+                       $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [\r
+                               '$base' => System::baseUrl(true),\r
+                               '$banner' => L10n::t('Failed Updates'),\r
+                               '$desc' => L10n::t('This does not include updates prior to 1139, which did not return a status.'),\r
+                               '$mark' => L10n::t("Mark success \x28if update was manually applied\x29"),\r
+                               '$apply' => L10n::t('Attempt to execute this update step automatically'),\r
+                               '$failed' => $failed\r
+                       ]);\r
+               }\r
+\r
+               return $o;\r
+       }\r
+}
\ No newline at end of file
index dc944738121a98c4452d0a54e58b11226140a2f6..7ad36e8cc304a2b76872f8e3162d6dfc29c3bc4d 100644 (file)
@@ -61,6 +61,7 @@ abstract class BaseAdminModule extends BaseModule
                                'tos'          => ['admin/tos'         , L10n::t('Terms of Service')        , 'tos'],\r
                        ]],\r
                        'database' => [L10n::t('Database'), [\r
+                               'dbsync'       => ['admin/dbsync'      , L10n::t('DB updates')              , 'dbsync'],\r
                                'deferred'     => ['admin/queue/deferred', L10n::t('Inspect Deferred Workers'), 'deferred'],\r
                                'workerqueue'  => ['admin/queue'       , L10n::t('Inspect worker Queue')    , 'workerqueue'],\r
                        ]],\r
diff --git a/view/templates/admin/dbsync/failed_updates.tpl b/view/templates/admin/dbsync/failed_updates.tpl
new file mode 100644 (file)
index 0000000..8a5e0c2
--- /dev/null
@@ -0,0 +1,19 @@
+
+<div id="adminpage">
+       <h2>{{$banner}}</h2>
+
+       <div id="failed_updates_desc">{{$desc nofilter}}</div>
+
+       {{if $failed}}
+       {{foreach $failed as $f}}
+               <h4>{{$f}}</h4>
+
+               <ul>
+                       <li><a href="{{$base}}/admin/dbsync/mark/{{$f}}">{{$mark}}</a></li>
+                       <li><a href="{{$base}}/admin/dbsync/{{$f}}">{{$apply}}</a></li>
+               </ul>
+
+               <hr />
+       {{/foreach}}
+       {{/if}}
+</div>
diff --git a/view/templates/admin/dbsync/structure_check.tpl b/view/templates/admin/dbsync/structure_check.tpl
new file mode 100644 (file)
index 0000000..7193d38
--- /dev/null
@@ -0,0 +1,8 @@
+
+<div id="adminpage">
+       <h2>{{$banner}}</h2>
+
+       <p><a href="{{$base}}/admin/dbsync/check">{{$check}}</a></p>
+
+       <hr />
+</div>
diff --git a/view/templates/failed_updates.tpl b/view/templates/failed_updates.tpl
deleted file mode 100644 (file)
index 8a5e0c2..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-
-<div id="adminpage">
-       <h2>{{$banner}}</h2>
-
-       <div id="failed_updates_desc">{{$desc nofilter}}</div>
-
-       {{if $failed}}
-       {{foreach $failed as $f}}
-               <h4>{{$f}}</h4>
-
-               <ul>
-                       <li><a href="{{$base}}/admin/dbsync/mark/{{$f}}">{{$mark}}</a></li>
-                       <li><a href="{{$base}}/admin/dbsync/{{$f}}">{{$apply}}</a></li>
-               </ul>
-
-               <hr />
-       {{/foreach}}
-       {{/if}}
-</div>
diff --git a/view/templates/structure_check.tpl b/view/templates/structure_check.tpl
deleted file mode 100644 (file)
index 7193d38..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-
-<div id="adminpage">
-       <h2>{{$banner}}</h2>
-
-       <p><a href="{{$base}}/admin/dbsync/check">{{$check}}</a></p>
-
-       <hr />
-</div>