Use title of remote paste for forked one; use page title as fallback
authorChristian Weiske <cweiske@cweiske.de>
Thu, 21 Nov 2013 06:22:54 +0000 (07:22 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 21 Nov 2013 06:22:54 +0000 (07:22 +0100)
src/phorkie/ForkRemote.php
src/phorkie/Forker.php
www/fork-remote.php

index 8784e8e26562fe1debed0495e6d0420261559fb9..31b483959974b6a6b239b839ffac00e732b18c10 100644 (file)
@@ -72,6 +72,8 @@ class ForkRemote
         libxml_use_internal_errors(true);
         $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
         $elems = $sx->xpath('//*[@rel="vcs-git"]');
+        $titles = $sx->xpath('/html/head/title');
+        $pageTitle = $this->cleanPageTitle((string) reset($titles));
 
         $count = $anonymous = 0;
         foreach ($elems as $elem) {
@@ -85,6 +87,8 @@ class ForkRemote
             } else if ($str != '') {
                 //<a href=".." rel="vcs-git">title</a>
                 $title = $str;
+            } else if ($pageTitle != '') {
+                $title = $pageTitle;
             } else {
                 $title = 'Unnamed repository #' . ++$anonymous;
             }
@@ -107,7 +111,7 @@ class ForkRemote
      * Iterate through all git urls and return one if there is only
      * one supported one.
      *
-     * @return mixed Boolean false or string
+     * @return mixed Boolean false or array with keys "url" and "title"
      */
     public function getUniqueGitUrl()
     {
@@ -115,7 +119,7 @@ class ForkRemote
         foreach ($this->arGitUrls as $title => $arUrls) {
             foreach ($arUrls as $url) {
                 $nFound++;
-                $uniqueUrl = $url;
+                $uniqueUrl = array('url' => $url, 'title' => $title);
             }
         }
 
@@ -152,6 +156,23 @@ class ForkRemote
         return $scheme == 'git'
             || $scheme == 'http' || $scheme == 'https';
     }
+
+    /**
+     * Remove application names from HTML page titles
+     *
+     * @param string $title HTML page title
+     *
+     * @return string Cleaned HTML page title
+     */
+    protected function cleanPageTitle($title)
+    {
+        $title = trim($title);
+        if (substr($title, -9) == '- phorkie') {
+            $title = trim(substr($title, 0, -9));
+        }
+
+        return $title;
+    }
 }
 
 ?>
index f4e1295794b790fbed1022cb55d1d86e127cf60c..419db1edd3bff9c00092c63e10ee75bb5892c764 100644 (file)
@@ -15,13 +15,13 @@ class Forker
         return $new;
     }
 
-    public function forkRemote($cloneUrl, $originalUrl)
+    public function forkRemote($cloneUrl, $originalUrl, $title = null)
     {
+        if ($title === null) {
+            $title = 'Fork of ' . $originalUrl;
+        }
         $new = $this->fork($cloneUrl);
-        file_put_contents(
-            $new->gitDir . '/description',
-            'Fork of ' . $originalUrl
-        );
+        file_put_contents($new->gitDir . '/description', $title);
         $this->index($new);
 
         $not = new Notificator();
index 4d9ca34f370a5167706bee930535ab792ffc894d..4f1b6829bfb544d6cb8fa430fc95602066657f62 100644 (file)
@@ -19,7 +19,9 @@ if (isset($_POST['remote_url'])) {
         }
         $forker = new Forker();
         try {
-            $new = $forker->forkRemote($gitUrl, $fr->getUrl());
+            $new = $forker->forkRemote(
+                $gitUrl['url'], $fr->getUrl(), $gitUrl['title']
+            );
             redirect($new->getLink('display', null, true));
         } catch (\Exception $e) {
             $error = $e->getMessage();