To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Ticket #426: rebuildslugsTask.patch

File rebuildslugsTask.patch, 3.6 KB (added by anonymous, 2 years ago)
  • plugins/apostrophePlugin/trunk/lib/task/rebuildslugsTask.class.php

     
     1<?php 
     2/* 
     3 
     4Copyright (c) 2010 Euranova 
     5 
     6Permission is hereby granted, free of charge, to any person obtaining a copy 
     7of this software and associated documentation files (the "Software"), to deal 
     8in the Software without restriction, including without limitation the rights 
     9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
     10copies of the Software, and to permit persons to whom the Software is 
     11furnished to do so, subject to the following conditions: 
     12 
     13The above copyright notice and this permission notice shall be included in 
     14all copies or substantial portions of the Software. 
     15 
     16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
     17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
     18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
     19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
     20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
     21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
     22THE SOFTWARE. 
     23*/ 
     24 
     25/** 
     26 * rebuildslugsTask 
     27 *  
     28 * Generate slugs from apostrophe tree.  
     29 *  
     30 * @author Jehan Bruggeman <jehanbruggeman at gmail dot com> 
     31 * 
     32 */ 
     33 
     34class rebuildslugsTask extends sfBaseTask 
     35{ 
     36  protected function configure() 
     37  { 
     38 
     39    $this->addOptions(array( 
     40      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'), 
     41      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), 
     42      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'), 
     43    )); 
     44 
     45    $this->namespace        = 'apostrophe'; 
     46    $this->name             = 'rebuild-slugs'; 
     47    $this->briefDescription = 'Rebuild the slugs to reflect the current tree'; 
     48    $this->detailedDescription = <<<EOF 
     49The [apostrohe:rebuild-slugs|INFO] task updates the slugs of your pages  to reflect the Doctrine 
     50nested set tree of your site. 
     51 
     52Call it with: 
     53 
     54  [php symfony a:rebuild-slugs|INFO] 
     55EOF; 
     56  } 
     57 
     58  protected function execute($arguments = array(), $options = array()) 
     59  { 
     60        // retreive pages 
     61    $databaseManager = new sfDatabaseManager($this->configuration); 
     62    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); 
     63    $root = aPageTable::retrieveBySlugWithTitles('/'); 
     64    $tree = $root->getTreeInfo(false); 
     65     
     66    // launch the rebuild 
     67    $this->cleanChildren($tree, '/'); 
     68  } 
     69   
     70        private function cleanChildren($tree,$currentPath){ 
     71                foreach($tree as $p) 
     72            { 
     73                // Retreive page object 
     74                $page = Doctrine_Query::Create()-> 
     75                              select("p.*")-> 
     76                              from("aPage p")->where('p.id = ?', array($p['id']))-> 
     77                              fetchOne(); 
     78                         
     79                        // New slug based on the page title 
     80                        // Source : BaseaActions.class.php line 205 and 247 (in BaseaActions::executeCreate) 
     81                        $baseSlug = strtolower(preg_replace("/[\W]+/", "-", htmlspecialchars_decode($page->title))); 
     82                               
     83                        // children 
     84                        if($page->hasChildren()){ 
     85                                $this->cleanChildren($p['children'],$currentPath.$baseSlug.'/'); 
     86                        } 
     87                         
     88                        // save current page's slug 
     89                $page->slug = $currentPath.$baseSlug; 
     90                $page->save(); 
     91                } 
     92        }  
     93} 
     94 
     95 
     96