src/Job/TidySiteTask.php
public function recursiveTidy($children)
    {

        foreach ($children as $page) {
            $write = false;

            // This is so something (events for example) can be purged.

            if ($this->purge && is_a($page, $this->purge, true)) {
                $page->delete();
                $this->purged++;
                continue;
            }

            // The most common issue is that the site ID doesn't match.

            if ($page->SiteID != $this->siteID) {

                // Update it to match.

                $page->SiteID = $this->siteID;
                $write        = true;
            }

            // The next issue is that duplicated URL segments have numbers appended (`URL-segment-2` for example).

            if (preg_match('%-[0-9]$%', $page->URLSegment)) {

                // When a page title ends with a number, the URL should match this.

                $last = substr($page->Title, -1);
                if (is_numeric($last)) {

                    // Determine whether this actually needs fixing.

                    if ($last !== substr($page->URLSegment, -1)) {

                        // Update it to match.

                        $page->URLSegment = $page->Title;
                        $write            = true;
                    }
                }

                // The number appended shouldn't exist.
                else {

                    // Determine whether this is considered unique (otherwise it can't be updated).

                    $to = substr($page->URLSegment, 0, -2);
                    if (!SiteTree::get()->filter(array(
                            'ParentID' => $page->ParentID,
                            'URLSegment' => $to
                        ))->exists()) {

                        // This isn't unique, so update it.

                        $page->URLSegment = $to;
                        $write            = true;
                    }
                }
            }

            // Determine whether this page needs a write.

            if ($write) {
                $page->write();
                $this->count++;
            }

            // Where necessary, continue further down.

            $this->recursiveTidy($page->AllChildren());
        }
    }
public function recursiveTidy($children)
    {

        foreach ($children as $page) {
            $write = false;

            // This is so something (events for example) can be purged.

            if ($this->purge && is_a($page, $this->purge, true)) {
                $page->delete();
                $this->purged++;
                continue;
            }

            // The most common issue is that the site ID doesn't match.

            if ($page->SiteID != $this->siteID) {

                // Update it to match.

                $page->SiteID = $this->siteID;
                $write        = true;
            }

            // The next issue is that duplicated URL segments have numbers appended (`URL-segment-2` for example).

            if (preg_match('%-[0-9]$%', $page->URLSegment)) {

                // When a page title ends with a number, the URL should match this.

                $last = substr($page->Title, -1);
                if (is_numeric($last)) {

                    // Determine whether this actually needs fixing.

                    if ($last !== substr($page->URLSegment, -1)) {

                        // Update it to match.

                        $page->URLSegment = $page->Title;
                        $write            = true;
                    }
                }

                // The number appended shouldn't exist.
                else {

                    // Determine whether this is considered unique (otherwise it can't be updated).

                    $to = substr($page->URLSegment, 0, -2);
                    if (!SiteTree::get()->filter(array(
                            'ParentID' => $page->ParentID,
                            'URLSegment' => $to
                        ))->exists()) {

                        // This isn't unique, so update it.

                        $page->URLSegment = $to;
                        $write            = true;
                    }
                }
            }

            // Determine whether this page needs a write.

            if ($write) {
                $page->write();
                $this->count++;
            }

            // Where necessary, continue further down.

            $this->recursiveTidy($page->AllChildren());
        }
    }
public function recursiveTidy ( $ children ) { foreach ( $ children as $ page ) { $ write = false ; // This is so something (events for example) can be purged. if ( $ this -> purge && is_a ( $ page , $ this -> purge , true ) ) { $ page -> delete ( ) ; $ this -> purged ++ ; continue ; } // The most common issue is that the site ID doesn't match. if ( $ page -> SiteID != $ this -> siteID ) { // Update it to match. $ page -> SiteID = $ this -> siteID ; $ write = true ; } // The next issue is that duplicated URL segments have numbers appended (`URL-segment-2` for example). if ( preg_match ( '%-[0-9]$%' , $ page -> URLSegment ) ) { // When a page title ends with a number, the URL should match this. $ last = substr ( $ page -> Title , - 1 ) ; if ( is_numeric ( $ last ) ) { // Determine whether this actually needs fixing. if ( $ last !== substr ( $ page -> URLSegment , - 1 ) ) { // Update it to match. $ page -> URLSegment = $ page -> Title ; $ write = true ; } } // The number appended shouldn't exist. else { // Determine whether this is considered unique (otherwise it can't be updated). $ to = substr ( $ page -> URLSegment , 0 , - 2 ) ; if ( ! SiteTree :: get ( ) -> filter ( array ( 'ParentID' => $ page -> ParentID , 'URLSegment' => $ to ) ) -> exists ( ) ) { // This isn't unique, so update it. $ page -> URLSegment = $ to ; $ write = true ; } } } // Determine whether this page needs a write. if ( $ write ) { $ page -> write ( ) ; $ this -> count ++ ; } // Where necessary, continue further down. $ this -> recursiveTidy ( $ page -> AllChildren ( ) ) ; } }
This recursively goes through children to ensure everything is tidy.
This recursively goes through children to ensure everything is tidy .