Git Product home page Git Product logo

sitemaped's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

sitemaped's Issues

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.

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

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.