Git Product home page Git Product logo

sitemaped's Introduction

PHP Sitemap abstraction library


  1. Add urls one-by-one

    $urlset = new Urlset($links);
    
    foreach(range('a', 'z') as $letter) {
             $url = new Url(
                 'https://vocabula.ry/'.$letter,
                 new \DateTime('2017-01-01 00:00:01'),
                 Url::CHANGEFREQ_MONTHLY,
                 0.8
             );
             
             $url->addImage(new Image('https://test.com/image/'.$i));
             $url->addVideo(new Video('https://test.com/video/'.$i, 'Title '.$i, 'Description '.$i));
             $url->addNews(new News('Awesome news '.$i, '2018-01-01', 'Awesome news name '.$i, 'ru-RU'));
             
             $urlset->addUrl($url);
         }
     
    
    $sitemap = new Sitemap($urlset);
    
    $sitemap->toXmlString();
    $sitemap->toTxtString();
    $sitemap->write(__DIR__ . '/sitemap.xml');
    $sitemap->write(__DIR__ . '/sitemap.txt', Sitemap::FORMAT_TXT);
  2. Using links generator

    $links = function() {
         foreach(range('a', 'z') as $letter) {
             $url = new Url(
                 'https://vocabula.ry/'.$letter,
                 new \DateTime('2017-01-01 00:00:01'),
                 Url::CHANGEFREQ_MONTHLY,
                 0.8
             );
             
             $url->addImage(new Image('https://test.com/image/'.$i));
             $url->addVideo(new Video('https://test.com/video/'.$i, 'Title '.$i, 'Description '.$i));
             $url->addNews(new News('Awesome news '.$i, '2018-01-01', 'Awesome news name '.$i, 'ru-RU'));
             
             yield $url;
         }
    }
     
    $urlset = new Urlset($links);
    $sitemap = new Sitemap($urlset);
    
    $sitemap->toXmlString();
    $sitemap->toTxtString();
    $sitemap->write(__DIR__ . '/sitemap.xml');
    $sitemap->write(__DIR__ . '/sitemap.txt', Sitemap::FORMAT_TXT);
  3. Sitemap index

    $index = new Sitemapindex();
    $sitemap = new Sitemap($index);
    
    foreach (range(1, 2) as $i) {
        $sitemap = new SitemapindexSitemap(
            'http://test.com/'.$i,
           '-1 year'
        );
        $index->addSitemap($sitemap);
    }
    
    $content = $sitemap->toXmlString();

sitemaped's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

sitemaped's Issues

TypeError in getCount after only toTxtString

Controller action code:

  public function actionSitemap()
  {
    $links = new UrlsIterator();
    $sitemap = new Sitemap(new Urlset($links));

    Yii::$app->response->format = Response::FORMAT_RAW;

    Yii::$app->response->headers->add('Content-Type', 'text/plain');
    $content = $sitemap->toTxtString();

    $linksCount = $sitemap->getCount();
    if ($linksCount > 50000) {
      Yii::warning(\sprintf('Sitemap links count is %d'), $linksCount);
    }

    return $content;
  }

Exception:

Exception 'TypeError' with message 'Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given' 

in /app/vendor/trntv/sitemaped/src/Sitemap.php:58

Stack trace:
#0 /app/vendor/trntv/sitemaped/src/Sitemap.php(58): DOMXPath->__construct(NULL)
#1 /app/landing/controllers/SiteController.php(178): Sitemaped\Sitemap->getCount()

The issue is caused by the fact that getCount() functions uses $this->document property which is not set by toTxtString() method

Proposed solution:

+    private $txtUrlCount = null;

    public function getCount(): int
    {
+        if ($this->document) {
            $xpath = new \DOMXPath($this->document);
            return $xpath->query('*/loc')->length;
+        }
+        if ($this->txtUrlCount === null) {
+            throw new \DomainException('txtUrlCount must be set');
+        }
+        return $this->txtUrlCount;
    }

    public function toTxtString($gzip = false): string
    {
        $str = '';
+       $this->txtUrlCount = 0;
        foreach ($this->root->getValue() as $url) {
            $str .= $url->loc . "\r\n";
+           $this->txtUrlCount++;
        }

        if ($gzip === true) {
            $this->ensureZlib();

            $str = \gzencode($str, $this->gzipLevel);
        }

        return $str;
    }

I don't create pull request because this code could be improved but it does the job

please update for php 8.*

I meet a error when I composer update yii2-starter-kit,

"trntv/sitemaped 0.1.2 requires php ^7.1 -> your php version (8.0.13) does not satisfy that requirement."

please update it for php8.*

thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.