Passed
Push — development ( 5c6121...81d1fa )
by Thomas
02:06
created

SiteMapXml::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 4
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 * For license information see doc/license.txt
4
 *
5
 *  Generate sitemap.xml as specified by http://www.sitemaps.org
6
 *  And send ping to search engines
7
 ***************************************************************************/
8
9
class SiteMapXml
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    public $sDefaultChangeFreq = 'monthly';
12
    public $nMaxFileSize = 9961472; // max file size, 10MB by specification
13
    public $nMaxUrlCount = 50000; // max number of URLs per file, 50000 by specification
14
15
    public $sPath = '';
16
    public $sDomain = '';
17
    public $oIndexFile;
18
    public $nSiteMapIndex = 0;
19
    public $oSiteMapFile;
20
    public $nWrittenSize = 0;
21
    public $nWrittenCount = 0;
22
23
    public function open($sPath, $sDomain)
24
    {
25
        if (substr($sPath, - 1, 1) != '/') {
26
            $sPath .= '/';
27
        }
28
        if (substr($sDomain, - 1, 1) != '/') {
29
            $sDomain .= '/';
30
        }
31
32
        $this->sPath = $sPath;
33
        $this->sDomain = $sDomain;
34
35
        $this->oIndexFile = fopen($sPath . 'sitemap.xml', 'wb');
36
        if ($this->oIndexFile === false) {
37
            return false;
38
        }
39
40
        fwrite($this->oIndexFile, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
41
        fwrite($this->oIndexFile, '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
42
    }
43
44
    /* sChaneFreq = {always, hourly, daily, weekly, monthly, yearly, never}
45
     * nPriority  = {0.0 to 1.0}
46
     */
47
48
    /**
49
     * @param string $sFile
50
     * @param integer $dLastMod
51
     * @param string $sChangeFreq
52
     * @param float $nPriority
53
     */
54
    public function write($sFile, $dLastMod, $sChangeFreq, $nPriority = 0.5)
55
    {
56
        if (!$sChangeFreq) {
57
            $sChangeFreq = $this->sDefaultChangeFreq;
58
        }
59
60
        $sXML = '<url>';
61
        $sXML .= '<loc>' . xmlentities($this->sDomain . $sFile) . '</loc>';
62
        $sXML .= '<lastmod>' . xmlentities(date('c', $dLastMod)) . '</lastmod>';
63
        $sXML .= '<changefreq>' . xmlentities($sChangeFreq) . '</changefreq>';
64
        $sXML .= '<priority>' . xmlentities($nPriority) . '</priority>';
65
        $sXML .= '</url>'."\n";
66
67
        $this->writeInternal($sXML);
68
    }
69
70
    /**
71
     * @param string $str
72
     */
73
    public function writeInternal($str)
74
    {
75
        // close the last file?
76
        if (($this->oSiteMapFile) && (($this->nWrittenSize + strlen($str) > $this->nMaxFileSize) || ($this->nWrittenCount >= $this->nMaxUrlCount))) {
77
            gzwrite($this->oSiteMapFile, '</urlset>');
78
            gzclose($this->oSiteMapFile);
79
            $this->oSiteMapFile = null;
80
        }
81
82
        // open new XML file?
83
        if (!$this->oSiteMapFile) {
84
            $this->nSiteMapIndex++;
85
            $sFilename = 'sitemap-' . $this->nSiteMapIndex . '.xml.gz';
86
            $this->oSiteMapFile = gzopen($this->sPath . $sFilename, 'wb');
87
88
            fwrite(
89
                $this->oIndexFile,
90
                '<sitemap><loc>' . xmlentities($this->sDomain . $sFilename) . '</loc>' .
91
                '<lastmod>' . xmlentities(date('c')) . '</lastmod></sitemap>'
92
            );
93
94
            gzwrite($this->oSiteMapFile, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
95
            gzwrite($this->oSiteMapFile, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
96
            // includes end of xml-tag
97
            $this->nWrittenSize = 108;
98
            $this->nWrittenCount = 0;
99
        }
100
101
        // write string to XML
102
        gzwrite($this->oSiteMapFile, $str);
103
        $this->nWrittenSize += strlen($str);
104
        $this->nWrittenCount++;
105
    }
106
107
    public function close()
108
    {
109
        if ($this->oSiteMapFile) {
110
            gzwrite($this->oSiteMapFile, '</urlset>');
111
            gzclose($this->oSiteMapFile);
112
            $this->oSiteMapFile = null;
113
        }
114
115
        if ($this->oIndexFile !== false) {
116
            fwrite($this->oIndexFile, '</sitemapindex>');
117
            fclose($this->oIndexFile);
118
            $this->oIndexFile = false;
119
        }
120
    }
121
}
122