wilr / silverstripe-googlesitemaps

Google Sitemaps module for the SilverStripe CMS
BSD 3-Clause "New" or "Revised" License
74 stars 95 forks source link

If Use with translatable with ?locale="en_US" Page have any RequestVars #119

Open jripa opened 7 years ago

jripa commented 7 years ago

Tested on Silverstripe 3.1 /**

GoogleSitemap.php:

/**
 * The google site map is broken down into multiple smaller files to
 * prevent overbearing a server. By default separate {@link DataObject}
 * records are keep in separate files and broken down into chunks.
 *
 * @return ArrayList
 */
public function getSitemaps()
{
    $countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
    $sitemaps = new ArrayList();
    $filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
    $locale = '';

    if (class_exists('SiteTree')) {
        // move to extension hook. At the moment moduleexists config hook
        // does not work.
        if (class_exists('Translatable')) {
            //Translatable::disable_locale_filter();
            Controller::curr()->setControllerLocale();
               $locale = Translatable::get_current_locale();
        }

        $filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
        $class = 'SiteTree';
        $instances = Versioned::get_by_stage($class, 'Live', $filter);
        $this->extend("alterDataList", $instances, $class);
        $count = $instances->count();

        $neededForPage = ceil($count / $countPerFile);

        for ($i = 1; $i <= $neededForPage; $i++) {
            $lastEdited = $instances
                ->limit($countPerFile, ($i - 1) * $countPerFile)
                ->sort(array())
                ->max('LastEdited');

            $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');

            $sitemaps->push(new ArrayData(array(
                'ClassName' => 'SiteTree',
                'LastModified' => $lastModified,
                'Page' => $i. "?locale=".$locale
            )));
        }
    }

    if (count(self::$dataobjects) > 0) {
        foreach (self::$dataobjects as $class => $config) {
            $list = new DataList($class);
            $list = $list->sort('LastEdited ASC');
            $this->extend("alterDataList", $list, $class);
            $neededForClass = ceil($list->count() / $countPerFile);

            for ($i = 1; $i <= $neededForClass; $i++) {
                // determine the last modified date for this slice
                $sliced = $list
                    ->limit($countPerFile, ($i - 1) * $countPerFile)
                    ->last();

                $lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d');

                $sitemaps->push(new ArrayData(array(
                    'ClassName' => $this->sanitiseClassName($class),
                    'Page' => $i. "?locale=".$locale,
                    'LastModified' => $lastModified
                )));
            }
        }
    }

    if (count(self::$routes) > 0) {
        $needed = ceil(count(self::$routes) / $countPerFile);

        for ($i = 1; $i <= $needed; $i++) {
            $sitemaps->push(new ArrayData(array(
                'ClassName' => 'GoogleSitemapRoute',
                'Page' => $i. "?locale=".$locale
            )));
        }
    }

    return $sitemaps;
}

same on:

public function getItems($class, $page = 1)
{
    //normalise the class name
    try {
        $reflectionClass = new ReflectionClass($class);
        $class = $reflectionClass->getName();
    } catch (ReflectionException $e) {
        // this can happen when $class is GoogleSitemapRoute
        //we should try to carry on
    }

    $output = new ArrayList();
    $count = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
    $filter =  Config::inst()->get('GoogleSitemap', 'use_show_in_search');
    $redirector =  Config::inst()->get('GoogleSitemap', 'exclude_redirector_pages');
    // todo migrate to extension hook or DI point for other modules to
    // modify state filters
    if (class_exists('Translatable')) {
        //Translatable::disable_locale_filter();
         Controller::curr()->setControllerLocale();
    }