]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
merge
authorArthurHoaro <arthur@hoa.ro>
Sun, 18 Dec 2016 11:40:27 +0000 (12:40 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sun, 18 Dec 2016 11:40:27 +0000 (12:40 +0100)
1  2 
COPYING
application/FeedBuilder.php
application/NetscapeBookmarkUtils.php
tests/plugins/PluginMarkdownTest.php

diff --cc COPYING
index 5a825402b16133fef72e047ed37a71589a7f8b7f,22929463e48ee2a32c7caf44649903c813a96483..d8241e5092e95eb44d0b239eaaa9a9d797cd0668
+++ b/COPYING
@@@ -72,10 -72,6 +72,14 @@@ Files: plugins/wallabag/wallabag.pn
  License: MIT License (http://opensource.org/licenses/MIT)
  Copyright: (C) 2015 Nicolas LÅ“uillet - https://github.com/wallabag/wallabag
  
++Files: plugins/markdown/Parsedown.php
++License: MIT License (http://opensource.org/licenses/MIT)
++Copyright: (C) 2015 Emanuil Rusev - https://github.com/erusev/parsedown
++
 +Files: tpl/default/img/sad_star.png
 +License: MIT License (http://opensource.org/licenses/MIT)
 +Copyright: (C) 2015 kalvn - https://github.com/kalvn/Shaarli-Material
 +
  ----------------------------------------------------
  ZLIB/LIBPNG LICENSE
  
Simple merge
index dd21f05b1d9a66f4df53af4576003bc23ae33a35,e7148d005776c7c41df62e72e5cba237a6a6f753..f21ee359b2b1f8975346dbdf32c9419c42306f06
@@@ -54,142 -54,138 +54,279 @@@ class NetscapeBookmarkUtil
  
      /**
       * Generates an import status summary
+      *
+      * @param string $filename       name of the file to import
+      * @param int    $filesize       size of the file to import
+      * @param int    $importCount    how many links were imported
+      * @param int    $overwriteCount how many links were overwritten
+      * @param int    $skipCount      how many links were skipped
+      *
+      * @return string Summary of the bookmark import status
+      */
+     private static function importStatus(
+         $filename,
+         $filesize,
+         $importCount=0,
+         $overwriteCount=0,
+         $skipCount=0
+     )
+     {
+         $status = 'File '.$filename.' ('.$filesize.' bytes) ';
+         if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
+             $status .= 'has an unknown file format. Nothing was imported.';
+         } else {
+             $status .= 'was successfully processed: '.$importCount.' links imported, ';
+             $status .= $overwriteCount.' links overwritten, ';
+             $status .= $skipCount.' links skipped.';
+         }
+         return $status;
+     }
+     /**
+      * Imports Web bookmarks from an uploaded Netscape bookmark dump
+      *
+      * @param array  $post      Server $_POST parameters
+      * @param array  $files     Server $_FILES parameters
+      * @param LinkDB $linkDb    Loaded LinkDB instance
+      * @param string $pagecache Page cache
+      *
+      * @return string Summary of the bookmark import status
+      */
+     public static function import($post, $files, $linkDb, $pagecache)
+     {
+         $filename = $files['filetoupload']['name'];
+         $filesize = $files['filetoupload']['size'];
+         $data = file_get_contents($files['filetoupload']['tmp_name']);
+         if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
+             return self::importStatus($filename, $filesize);
+         }
+         // Overwrite existing links?
+         $overwrite = ! empty($post['overwrite']);
+         // Add tags to all imported links?
+         if (empty($post['default_tags'])) {
+             $defaultTags = array();
+         } else {
+             $defaultTags = preg_split(
+                 '/[\s,]+/',
+                 escape($post['default_tags'])
+             );
+         }
+         // links are imported as public by default
+         $defaultPrivacy = 0;
+         $parser = new NetscapeBookmarkParser(
+             true,                       // nested tag support
+             $defaultTags,               // additional user-specified tags
+             strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
+         );
+         $bookmarks = $parser->parseString($data);
+         $importCount = 0;
+         $overwriteCount = 0;
+         $skipCount = 0;
+         foreach ($bookmarks as $bkm) {
+             $private = $defaultPrivacy;
+             if (empty($post['privacy']) || $post['privacy'] == 'default') {
+                 // use value from the imported file
+                 $private = $bkm['pub'] == '1' ? 0 : 1;
+             } else if ($post['privacy'] == 'private') {
+                 // all imported links are private
+                 $private = 1;
+             } else if ($post['privacy'] == 'public') {
+                 // all imported links are public
+                 $private = 0;
+             }                
+             $newLink = array(
+                 'title' => $bkm['title'],
+                 'url' => $bkm['uri'],
+                 'description' => $bkm['note'],
+                 'private' => $private,
+                 'tags' => $bkm['tags']
+             );
+             $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
+             if ($existingLink !== false) {
+                 if ($overwrite === false) {
+                     // Do not overwrite an existing link
+                     $skipCount++;
+                     continue;
+                 }
+                 // Overwrite an existing link, keep its date
+                 $newLink['id'] = $existingLink['id'];
+                 $newLink['created'] = $existingLink['created'];
+                 $newLink['updated'] = new DateTime();
+                 $linkDb[$existingLink['id']] = $newLink;
+                 $importCount++;
+                 $overwriteCount++;
+                 continue;
+             }
+             // Add a new link - @ used for UNIX timestamps
+             $newLinkDate = new DateTime('@'.strval($bkm['time']));
+             $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
+             $newLink['created'] = $newLinkDate;
+             $newLink['id'] = $linkDb->getNextId();
+             $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']);
+             $linkDb[$newLink['id']] = $newLink;
+             $importCount++;
+         }
+         $linkDb->save($pagecache);
+         return self::importStatus(
+             $filename,
+             $filesize,
+             $importCount,
+             $overwriteCount,
+             $skipCount
+         );
+     }
++
++    /**
++     * Generates an import status summary
 +     *
 +     * @param string $filename       name of the file to import
 +     * @param int    $filesize       size of the file to import
 +     * @param int    $importCount    how many links were imported
 +     * @param int    $overwriteCount how many links were overwritten
 +     * @param int    $skipCount      how many links were skipped
 +     *
 +     * @return string Summary of the bookmark import status
 +     */
 +    private static function importStatus(
 +        $filename,
 +        $filesize,
 +        $importCount=0,
 +        $overwriteCount=0,
 +        $skipCount=0
 +    )
 +    {
 +        $status = 'File '.$filename.' ('.$filesize.' bytes) ';
 +        if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
 +            $status .= 'has an unknown file format. Nothing was imported.';
 +        } else {
 +            $status .= 'was successfully processed: '.$importCount.' links imported, ';
 +            $status .= $overwriteCount.' links overwritten, ';
 +            $status .= $skipCount.' links skipped.';
 +        }
 +        return $status;
 +    }
 +
 +    /**
 +     * Imports Web bookmarks from an uploaded Netscape bookmark dump
 +     *
 +     * @param array  $post      Server $_POST parameters
 +     * @param array  $files     Server $_FILES parameters
 +     * @param LinkDB $linkDb    Loaded LinkDB instance
 +     * @param string $pagecache Page cache
 +     *
 +     * @return string Summary of the bookmark import status
 +     */
 +    public static function import($post, $files, $linkDb, $pagecache)
 +    {
 +        $filename = $files['filetoupload']['name'];
 +        $filesize = $files['filetoupload']['size'];
 +        $data = file_get_contents($files['filetoupload']['tmp_name']);
 +
 +        if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
 +            return self::importStatus($filename, $filesize);
 +        }
 +
 +        // Overwrite existing links?
 +        $overwrite = ! empty($post['overwrite']);
 +
 +        // Add tags to all imported links?
 +        if (empty($post['default_tags'])) {
 +            $defaultTags = array();
 +        } else {
 +            $defaultTags = preg_split(
 +                '/[\s,]+/',
 +                escape($post['default_tags'])
 +            );
 +        }
 +
 +        // links are imported as public by default
 +        $defaultPrivacy = 0;
 +
 +        $parser = new NetscapeBookmarkParser(
 +            true,                       // nested tag support
 +            $defaultTags,               // additional user-specified tags
 +            strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
 +        );
 +        $bookmarks = $parser->parseString($data);
 +
 +        $importCount = 0;
 +        $overwriteCount = 0;
 +        $skipCount = 0;
 +
 +        foreach ($bookmarks as $bkm) {
 +            $private = $defaultPrivacy;
 +            if (empty($post['privacy']) || $post['privacy'] == 'default') {
 +                // use value from the imported file
 +                $private = $bkm['pub'] == '1' ? 0 : 1;
 +            } else if ($post['privacy'] == 'private') {
 +                // all imported links are private
 +                $private = 1;
 +            } else if ($post['privacy'] == 'public') {
 +                // all imported links are public
 +                $private = 0;
 +            }                
 +
 +            $newLink = array(
 +                'title' => $bkm['title'],
 +                'url' => $bkm['uri'],
 +                'description' => $bkm['note'],
 +                'private' => $private,
 +                'linkdate'=> '',
 +                'tags' => $bkm['tags']
 +            );
 +
 +            $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
 +
 +            if ($existingLink !== false) {
 +                if ($overwrite === false) {
 +                    // Do not overwrite an existing link
 +                    $skipCount++;
 +                    continue;
 +                }
 +
 +                // Overwrite an existing link, keep its date
 +                $newLink['linkdate'] = $existingLink['linkdate'];
 +                $linkDb[$existingLink['linkdate']] = $newLink;
 +                $importCount++;
 +                $overwriteCount++;
 +                continue;
 +            }
 +
 +            // Add a new link
 +            $newLinkDate = new DateTime('@'.strval($bkm['time']));
 +            while (!empty($linkDb[$newLinkDate->format(LinkDB::LINK_DATE_FORMAT)])) {
 +                // Ensure the date/time is not already used
 +                // - this hack is necessary as the date/time acts as a primary key
 +                // - apply 1 second increments until an unused index is found
 +                // See https://github.com/shaarli/Shaarli/issues/351
 +                $newLinkDate->add(new DateInterval('PT1S'));
 +            }
 +            $linkDbDate = $newLinkDate->format(LinkDB::LINK_DATE_FORMAT);
 +            $newLink['linkdate'] = $linkDbDate;
 +            $linkDb[$linkDbDate] = $newLink;
 +            $importCount++;
 +        }
 +
 +        $linkDb->save($pagecache);
 +        return self::importStatus(
 +            $filename,
 +            $filesize,
 +            $importCount,
 +            $overwriteCount,
 +            $skipCount
 +        );
 +    }
  }
index 12bdda24231b47a9cad0454694d030d7b4884362,17ef228031331fba63fbb5ef567a3fc1fa06c04c..4a67b2dc8566b86c6becd7e4feab9bcbf84e8fed
@@@ -152,16 -155,34 +155,47 @@@ class PluginMarkdownTest extends PHPUni
          $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
      }
  
+     /**
+      * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
+      */
+     function testNoMarkdownNotExcactlyMatching()
+     {
+         $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
+         $data = array(
+             'links' => array(array(
+                 'description' => $str,
+                 'tags' => '.' . NO_MD_TAG,
+                 'taglist' => array('.'. NO_MD_TAG),
+             ))
+         );
+         $data = hook_markdown_render_feed($data);
+         $this->assertContains('<em>', $data['links'][0]['description']);
+     }
+     /**
+      * Test hashtag links processed with markdown.
+      */
+     function testMarkdownHashtagLinks()
+     {
+         $md = file_get_contents('tests/plugins/resources/markdown.md');
+         $md = format_description($md);
+         $html = file_get_contents('tests/plugins/resources/markdown.html');
+         $data = process_markdown($md);
+         $this->assertEquals($html, $data);
+     }
++
 +    /**
 +     * Test hashtag links processed with markdown.
 +     */
 +    function testMarkdownHashtagLinks()
 +    {
 +        $md = file_get_contents('tests/plugins/resources/markdown.md');
 +        $md = format_description($md);
 +        $html = file_get_contents('tests/plugins/resources/markdown.html');
 +
 +        $data = process_markdown($md);
 +        $this->assertEquals($html, $data);
 +    }
  }