Author: Kamil Tekiela (kamil-tekiela)
Committer: GitHub (web-flow)
Pusher: kamil-tekiela
Date: 2022-06-29T22:24:09+01:00

Commit: 
https://github.com/php/web-php/commit/0b217c0271731b54037b3eff7ac368e0090c5688
Raw diff: 
https://github.com/php/web-php/commit/0b217c0271731b54037b3eff7ac368e0090c5688.diff

Move ManualNotesSorter to src/ (#593)

Changed paths:
  A  src/UserNotes/Sorter.php
  M  include/shared-manual.inc


Diff:

diff --git a/include/shared-manual.inc b/include/shared-manual.inc
index ddc4ef5d5..a1ad368ab 100644
--- a/include/shared-manual.inc
+++ b/include/shared-manual.inc
@@ -23,116 +23,8 @@ $PGI = array(); $SIDEBAR_DATA = '';
 // User note display functions
 // 
=============================================================================
 
-
-class ManualNotesSorter {
-    private $maxVote;
-    private $minVote;
-    private $maxAge;
-    private $minAge;
-
-    private $voteFactor;
-    private $ageFactor;
-
-    private $voteWeight = 38;
-    private $ratingWeight = 60;
-    private $ageWeight = 2;
-
-    function sort(array &$notes) {
-        // First we make a pass over the data to get the min and max values
-        // for data normalization.
-        $this->findMinMaxValues($notes);
-
-        $this->voteFactor = $this->maxVote - $this->minVote
-            ? (1 - .3)/ ($this->maxVote - $this->minVote)
-            : .5;
-        $this->ageFactor = $this->maxAge - $this->minAge
-            ? 1 / ($this->maxAge - $this->minAge)
-            : .5;
-
-        $this->ageFactor *= $this->ageWeight;
-
-        // Second we loop through to calculate sort priority using the above 
numbers
-        $this->calcSortPriority($notes);
-
-        // Third we sort the data.
-        uasort($notes, array($this, 'factorSort'));
-    }
-
-
-    private function calcVotePriority(array $note) {
-        return ($note['score'] - $this->minVote) * $this->voteFactor + .3;
-    }
-
-
-    private function calcRatingPriority(array $note) {
-        if ($note['total'] <= 2) {
-            return 0.5;
-        } else {
-            return $note['rating'];
-        }
-    }
-
-
-    private function calcSortPriority(array &$notes) {
-        foreach ($notes as &$note) {
-            $prio = array(
-                'vote' => $this->calcVotePriority($note) * $this->voteWeight,
-                'rating' => $this->calcRatingPriority($note) * 
$this->ratingWeight,
-                'age' =>  ($note['xwhen'] - $this->minAge) * $this->ageFactor
-            );
-            $note['sort'] = $prio['value'] = array_sum($prio);
-        }
-    }
-
-
-    /*
-     * Not sure why, but using `$b['sort'] - $a['sort']` does not seem to
-     * work properly.
-     */
-    private function factorSort($a, $b) {
-        if ($a['sort'] < $b['sort']) {
-            return 1;
-        } elseif ($a['sort'] == $b['sort']) {
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-
-
-    private function findMinMaxValues(array &$notes) {
-        $count = count($notes);
-        if ($count <= 0) {
-            return;
-        }
-        $note = array_shift($notes);
-        $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
-        $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
-        $note['rating'] = $totalVotes > 0
-            ? $note['votes']['up'] / $totalVotes
-            : .5;
-
-        $this->minVote = $this->maxVote = $net;
-        $this->minAge = $this->maxAge = $age = $note['xwhen'];
-
-        $first = $note;
-
-        foreach ($notes as &$note) {
-            $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
-            $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
-            $note['rating'] = $totalVotes > 0
-                ? $note['votes']['up'] / $totalVotes
-                : .5;
-            $age = $note['xwhen'];
-            $this->maxVote = max($this->maxVote, $net);
-            $this->minVote = min($this->minVote, $net);
-            $this->maxAge = max($this->maxAge, $age);
-            $this->minAge = min($this->minAge, $age);
-        }
-        array_unshift($notes, $first);
-    }
-}
-
+require_once __DIR__ . '/../src/UserNotes/Sorter.php';
+use phpweb\UserNotes\Sorter;
 
 // Print out all user notes for this manual page
 function manual_notes($notes) {
@@ -144,7 +36,7 @@ function manual_notes($notes) {
         $filename = substr($filename, 0, -4);
     }
 
-    $sorter = new ManualNotesSorter;
+    $sorter = new Sorter;
     $sorter->sort($notes);
 
     // Link target to add a note to the current manual page,
diff --git a/src/UserNotes/Sorter.php b/src/UserNotes/Sorter.php
new file mode 100644
index 000000000..3fb32188a
--- /dev/null
+++ b/src/UserNotes/Sorter.php
@@ -0,0 +1,112 @@
+<?php
+
+namespace phpweb\UserNotes;
+
+class Sorter {
+    private $maxVote;
+    private $minVote;
+    private $maxAge;
+    private $minAge;
+
+    private $voteFactor;
+    private $ageFactor;
+
+    private $voteWeight = 38;
+    private $ratingWeight = 60;
+    private $ageWeight = 2;
+
+    function sort(array &$notes) {
+        // First we make a pass over the data to get the min and max values
+        // for data normalization.
+        $this->findMinMaxValues($notes);
+
+        $this->voteFactor = $this->maxVote - $this->minVote
+            ? (1 - .3)/ ($this->maxVote - $this->minVote)
+            : .5;
+        $this->ageFactor = $this->maxAge - $this->minAge
+            ? 1 / ($this->maxAge - $this->minAge)
+            : .5;
+
+        $this->ageFactor *= $this->ageWeight;
+
+        // Second we loop through to calculate sort priority using the above 
numbers
+        $this->calcSortPriority($notes);
+
+        // Third we sort the data.
+        uasort($notes, array($this, 'factorSort'));
+    }
+
+
+    private function calcVotePriority(array $note) {
+        return ($note['score'] - $this->minVote) * $this->voteFactor + .3;
+    }
+
+
+    private function calcRatingPriority(array $note) {
+        if ($note['total'] <= 2) {
+            return 0.5;
+        } else {
+            return $note['rating'];
+        }
+    }
+
+
+    private function calcSortPriority(array &$notes) {
+        foreach ($notes as &$note) {
+            $prio = array(
+                'vote' => $this->calcVotePriority($note) * $this->voteWeight,
+                'rating' => $this->calcRatingPriority($note) * 
$this->ratingWeight,
+                'age' =>  ($note['xwhen'] - $this->minAge) * $this->ageFactor
+            );
+            $note['sort'] = $prio['value'] = array_sum($prio);
+        }
+    }
+
+
+    /*
+     * Not sure why, but using `$b['sort'] - $a['sort']` does not seem to
+     * work properly.
+     */
+    private function factorSort($a, $b) {
+        if ($a['sort'] < $b['sort']) {
+            return 1;
+        } elseif ($a['sort'] == $b['sort']) {
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+
+
+    private function findMinMaxValues(array &$notes) {
+        $count = count($notes);
+        if ($count <= 0) {
+            return;
+        }
+        $note = array_shift($notes);
+        $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
+        $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
+        $note['rating'] = $totalVotes > 0
+            ? $note['votes']['up'] / $totalVotes
+            : .5;
+
+        $this->minVote = $this->maxVote = $net;
+        $this->minAge = $this->maxAge = $age = $note['xwhen'];
+
+        $first = $note;
+
+        foreach ($notes as &$note) {
+            $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
+            $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
+            $note['rating'] = $totalVotes > 0
+                ? $note['votes']['up'] / $totalVotes
+                : .5;
+            $age = $note['xwhen'];
+            $this->maxVote = max($this->maxVote, $net);
+            $this->minVote = min($this->minVote, $net);
+            $this->maxAge = max($this->maxAge, $age);
+            $this->minAge = min($this->minAge, $age);
+        }
+        array_unshift($notes, $first);
+    }
+}

-- 
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to