Przeglądaj źródła

Add settings for friendly url

https://forkbb.ru/post/280#p280
Visman 1 rok temu
rodzic
commit
8e3c74367e

+ 23 - 0
app/Core/Func.php

@@ -302,4 +302,27 @@ class Func
             return $timestamp;
         }
     }
+
+    /**
+     * Преобразует строку в соотвествии с правилами FRIENDLY_URL
+     */
+    public function friendly(string $str): string
+    {
+        $conf = $this->c->FRIENDLY_URL;
+        $rule = $conf['translit'];
+
+        if (true === $conf['lowercase']) {
+            $rule .= 'Lower();';
+        }
+
+        if ('' !== $rule) {
+            $str = \transliterator_transliterate($rule, $str);
+        }
+
+        if (true === $conf['WtoHyphen']) {
+            $str = \trim(\preg_replace(['%[^\w-]+%u', '%_+%'], ['-', '_'], $str), '-_');
+        }
+
+        return isset($str[0]) ? $str : '-';
+    }
 }

+ 3 - 3
app/Models/Forum/Forum.php

@@ -129,7 +129,7 @@ class Forum extends DataModel
                 'Forum',
                 [
                     'id'   => $this->id,
-                    'name' => $this->forum_name,
+                    'name' => $this->c->Func->friendly($this->forum_name),
                 ]
             );
         }
@@ -261,7 +261,7 @@ class Forum extends DataModel
                         'User',
                         [
                             'id'   => $id,
-                            'name' => $cur,
+                            'name' => $this->c->Func->friendly($cur),
                         ]
                     )
                     : null,
@@ -391,7 +391,7 @@ class Forum extends DataModel
             'Forum',
             [
                 'id'   => $this->id,
-                'name' => $this->forum_name,
+                'name' => $this->c->Func->friendly($this->forum_name),
             ]
         );
     }

+ 1 - 1
app/Models/Online/Info.php

@@ -37,7 +37,7 @@ class Info extends Method
                         'User',
                         [
                             'id'   => $id,
-                            'name' => $name,
+                            'name' => $this->c->Func->friendly($name),
                         ]
                     )
                     : null,

+ 22 - 0
app/Models/Pages/Admin/Update.php

@@ -979,4 +979,26 @@ class Update extends Admin
 
         return null;
     }
+
+    /**
+     * rev.70 to rev.71
+     */
+    protected function stageNumber70(array $args): ?int
+    {
+        $coreConfig = new CoreConfig($this->configFile);
+
+        $coreConfig->add(
+            'FRIENDLY_URL',
+            [
+                'lowercase' => 'false',
+                'translit'  => '\'\'',
+                'WtoHyphen' => 'false',
+            ],
+            'TIME_FORMATS'
+        );
+
+        $coreConfig->save();
+
+        return null;
+    }
 }

+ 1 - 8
app/Models/Pages/Forum.php

@@ -44,14 +44,7 @@ class Forum extends Page
 
         $this->nameTpl    = 'forum';
         $this->onlinePos  = 'forum-' . $args['id'];
-        $this->canonical  = $this->c->Router->link(
-            'Forum',
-            [
-                'id'   => $args['id'],
-                'name' => $forum->forum_name,
-                'page' => $forum->page,
-            ]
-        );
+        $this->canonical  = $forum->link;
         $this->model      = $forum;
         $this->topics     = $forum->pageData();
         $this->crumbs     = $this->crumbs($forum);

+ 1 - 1
app/Models/Pages/Index.php

@@ -31,7 +31,7 @@ class Index extends Page
                     'User',
                     [
                         'id'   => $this->c->stats->userLast['id'],
-                        'name' => $this->c->stats->userLast['username'],
+                        'name' => $this->c->Func->friendly($this->c->stats->userLast['username']),
                     ]
                 )
                 : null,

+ 2 - 12
app/Models/Pages/Post.php

@@ -85,12 +85,7 @@ class Post extends Page
         }
 
         $this->nameTpl   = 'post';
-        $this->canonical = $this->c->Router->link(
-            'NewTopic',
-            [
-                'id' => $forum->id,
-            ]
-        );
+        $this->canonical = $forum->linkCreateTopic;
         $this->robots    = 'noindex';
         $this->formTitle = 'Post new topic';
         $this->crumbs    = $this->crumbs($this->formTitle, $forum);
@@ -157,12 +152,7 @@ class Post extends Page
         }
 
         $this->nameTpl    = 'post';
-        $this->canonical  = $this->c->Router->link(
-            'NewReply',
-            [
-                'id' => $topic->id,
-            ]
-        );
+        $this->canonical  = $topic->linkReply;
         $this->robots     = 'noindex';
         $this->formTitle  = 'Post a reply';
         $this->crumbs     = $this->crumbs($this->formTitle, $topic);

+ 1 - 8
app/Models/Pages/Topic.php

@@ -136,14 +136,7 @@ class Topic extends Page
         $this->nameTpl      = 'topic';
         $this->onlinePos    = 'topic-' . $topic->id;
         $this->onlineDetail = true;
-        $this->canonical    = $this->c->Router->link(
-            'Topic',
-            [
-                'id'   => $topic->id,
-                'name' => $topic->name,
-                'page' => $topic->page
-            ]
-        );
+        $this->canonical    = $topic->link;
         $this->model        = $topic;
         $this->crumbs       = $this->crumbs($topic);
         $this->online       = $this->c->Online->calc($this)->info();

+ 2 - 2
app/Models/Topic/Topic.php

@@ -101,7 +101,7 @@ class Topic extends DataModel
             'Topic',
             [
                 'id'   => $this->moved_to ?: $this->id,
-                'name' => $this->name,
+                'name' => $this->c->Func->friendly($this->name),
             ]
         );
     }
@@ -329,7 +329,7 @@ class Topic extends DataModel
                 'Topic',
                 [
                     'id'   => $this->id,
-                    'name' => $this->name,
+                    'name' => $this->c->Func->friendly($this->name),
                 ]
             );
         }

+ 1 - 1
app/Models/User/User.php

@@ -168,7 +168,7 @@ class User extends DataModel
                 'User',
                 [
                     'id'   => $this->id,
-                    'name' => $this->username,
+                    'name' => $this->c->Func->friendly($this->username),
                 ]
             );
         }

+ 5 - 0
app/config/main.dist.php

@@ -74,6 +74,11 @@ return [
     ],
     'DATE_FORMATS' => ['Y-m-d', 'd M Y', 'Y-m-d', 'Y-d-m', 'd-m-Y', 'm-d-Y', 'M j Y', 'jS M Y'],
     'TIME_FORMATS' => ['H:i:s', 'H:i', 'H:i:s', 'H:i', 'g:i:s a', 'g:i a'],
+    'FRIENDLY_URL' => [
+        'lowercase' => true,
+        'translit'  => 'Russian-Latin/BGN;Any-Latin;Latin-ASCII;',
+        'WtoHyphen' => true,
+    ],
 
     'shared' => [
         '%DIR_ROOT%'   => \realpath(__DIR__ . '/../..'),