]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/File_redirection.php
Merge branch 'page_title_showstream' into 'nightly'
[quix0rs-gnu-social.git] / classes / File_redirection.php
index ade04fbcbcac278057cec38ea4b20e0280fef381..2b6f86ed2728beb507d8a63bc09d7fec98de224f 100644 (file)
@@ -59,12 +59,7 @@ class File_redirection extends Managed_DataObject
 
     static public function getByUrl($url)
     {
-        $file = new File_redirection();
-        $file->urlhash = File::hashurl($url);
-        if (!$file->find(true)) {
-            throw new NoResultException($file);
-        }
-        return $file;
+        return self::getByPK(array('urlhash' => File::hashurl($url)));
     }
 
     static function _commonHttp($url, $redirs) {
@@ -99,7 +94,7 @@ class File_redirection extends Managed_DataObject
      *                size (optional): byte size from Content-Length header
      *                time (optional): timestamp from Last-Modified header
      */
-    public function lookupWhere($short_url, $redirs = 10, $protected = false) {
+    static function lookupWhere($short_url, $redirs = 10, $protected = false) {
         if ($redirs < 0) return false;
 
         if(strpos($short_url,'://') === false){
@@ -171,7 +166,7 @@ class File_redirection extends Managed_DataObject
      *                size (optional): byte size from Content-Length header
      *                time (optional): timestamp from Last-Modified header
      */
-    public function where($in_url, $discover=true) {
+    static function where($in_url, $discover=true) {
         // let's see if we know this...
         try {
             $a = File::getByUrl($in_url);
@@ -181,7 +176,7 @@ class File_redirection extends Managed_DataObject
             try {
                 $b = File_redirection::getByUrl($in_url);
                 // this is a redirect to $b->file_id
-                $a = File::getKV('id', $b->file_id);
+                $a = File::getByID($b->file_id);
                 return $a->url;
             } catch (NoResultException $e) {
                 // Oh well, let's keep going
@@ -191,10 +186,10 @@ class File_redirection extends Managed_DataObject
         if ($discover) {
             $ret = File_redirection::lookupWhere($in_url);
             return $ret;
-        } else {
-            // No manual dereferencing; leave the unknown URL as is.
-            return $in_url;
         }
+
+        // No manual dereferencing; leave the unknown URL as is.
+        return $in_url;
     }
 
     /**
@@ -211,7 +206,7 @@ class File_redirection extends Managed_DataObject
      * @param User $user whose shortening options to use; defaults to the current web session user
      * @return string
      */
-    function makeShort($long_url, $user=null)
+    static function makeShort($long_url, $user=null)
     {
         $canon = File_redirection::_canonUrl($long_url);
 
@@ -219,11 +214,7 @@ class File_redirection extends Managed_DataObject
 
         // Did we get one? Is it shorter?
 
-        if (!empty($short_url)) {
-            return $short_url;
-        } else {
-            return $long_url;
-        }
+        return !empty($short_url) ? $short_url : $long_url;
     }
 
     /**
@@ -240,28 +231,24 @@ class File_redirection extends Managed_DataObject
      * @return string
      */
 
-    function forceShort($long_url, $user)
+    static function forceShort($long_url, $user)
     {
         $canon = File_redirection::_canonUrl($long_url);
 
         $short_url = File_redirection::_userMakeShort($canon, $user, true);
 
         // Did we get one? Is it shorter?
-        if (!empty($short_url)) {
-            return $short_url;
-        } else {
-            return $long_url;
-        }
+        return !empty($short_url) ? $short_url : $long_url;
     }
 
-    function _userMakeShort($long_url, User $user=null, $force = false) {
+    static function _userMakeShort($long_url, User $user=null, $force = false) {
         $short_url = common_shorten_url($long_url, $user, $force);
         if (!empty($short_url) && $short_url != $long_url) {
             $short_url = (string)$short_url;
             // store it
             $file = File::getKV('url', $long_url);
             if ($file instanceof File) {
-                $file_id = $file->id;
+                $file_id = $file->getID();
             } else {
                 // Check if the target URL is itself a redirect...
                 $redir_data = File_redirection::where($long_url);
@@ -269,7 +256,7 @@ class File_redirection extends Managed_DataObject
                     // We haven't seen the target URL before.
                     // Save file and embedding data about it!
                     $file = File::saveNew($redir_data, $long_url);
-                    $file_id = $file->id;
+                    $file_id = $file->getID();
                 } else if (is_string($redir_data)) {
                     // The file is a known redirect target.
                     $file = File::getKV('url', $redir_data);
@@ -281,7 +268,7 @@ class File_redirection extends Managed_DataObject
                         // SSL sites with cert issues.
                         return null;
                     }
-                    $file_id = $file->id;
+                    $file_id = $file->getID();
                 }
             }
             $file_redir = File_redirection::getKV('url', $short_url);
@@ -309,7 +296,7 @@ class File_redirection extends Managed_DataObject
      * @param string $default_scheme if given a bare link; defaults to 'http://'
      * @return string
      */
-    function _canonUrl($in_url, $default_scheme = 'http://') {
+    static function _canonUrl($in_url, $default_scheme = 'http://') {
         if (empty($in_url)) return false;
         $out_url = $in_url;
         $p = parse_url($out_url);
@@ -322,6 +309,7 @@ class File_redirection extends Managed_DataObject
                 break;
 
             case 'mailto':
+            case 'magnet':
             case 'aim':
             case 'jabber':
             case 'xmpp':
@@ -346,7 +334,7 @@ class File_redirection extends Managed_DataObject
         return $out_url;
     }
 
-    function saveNew($data, $file_id, $url) {
+    static function saveNew($data, $file_id, $url) {
         $file_redir = new File_redirection;
         $file_redir->urlhash = File::hashurl($short_url);
         $file_redir->url = $url;
@@ -373,8 +361,14 @@ class File_redirection extends Managed_DataObject
         $schemadef['fields']['urlhash'] = array (
                                               'type' => 'varchar',
                                               'length' => 64,
-                                              'description' => 'sha256 of destination URL after following redirections',
+                                              'not null' => true,
+                                              'description' => 'sha256 hash of the URL',
+                                            );
+        $schemadef['fields']['url'] = array (
+                                              'type' => 'text',
+                                              'description' => 'short URL (or any other kind of redirect) for file (id)',
                                             );
+        unset($schemadef['primary key']);
         $schema->ensureTable($table, $schemadef);
         echo "DONE.\n";