app/DoctrineMigrations/Version20111125175224.php
author cavaliet
Wed, 02 Apr 2014 14:35:17 +0200
changeset 113 e4f8c2479e78
parent 71 b01e36991488
permissions -rw-r--r--
enhance fr.dbpedia migration for production
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
71
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     2
/*
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     3
 * This file is part of the WikiTagBundle package.
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     4
 *
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     6
 *
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     8
 * file that was distributed with this source code.
b01e36991488 add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
     9
 */
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace Application\Migrations;
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    13
use Doctrine\DBAL\Migrations\OutputWriter;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    14
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    15
use Doctrine\DBAL\Migrations\AbstractMigration;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    16
use Doctrine\DBAL\Schema\Schema;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    17
use Doctrine\Common\EventManager;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    18
use Doctrine\ORM\EntityManager;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    19
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    20
use IRI\Bundle\WikiTagBundle\Entity\Tag;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    21
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
/**
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * Auto-generated Migration: Please modify to your need!
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 */
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
class Version20111125175224 extends AbstractMigration
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
{
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    28
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    29
    private function showProgress($current, $total, $label, $width)
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    30
    {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    31
        $percent = (floatval($current)/floatval($total)) * 100.0;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    32
        $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    33
        $spaces = $width - $marks;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    34
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    35
        $status_bar="\r[";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    36
        $status_bar.=str_repeat("=", $marks);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    37
        if($marks<$width){
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    38
            $status_bar.=">";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    39
            $status_bar.=str_repeat(" ", $spaces);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    40
        } else {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    41
            $status_bar.="=";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    42
        }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    43
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    44
        $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    45
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    46
        $label = str_pad(substr($label,0,50), 50, " ");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    47
        $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    48
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    49
        $status_bar.="] $disp%  $current_str/$total : $label";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    50
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    51
        $this->write("$status_bar  ");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    52
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    53
        if($current == $total) {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    54
            $this->write("\n");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    55
        }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    56
    }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    57
    
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    public function up(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    {
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        // this up() migration is autogenerated, please modify it to your needs
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        $this->addSql("ALTER TABLE wikitag_document ADD CONSTRAINT FK_256601059F75D7B0 FOREIGN KEY (external_id) REFERENCES Document(id)");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        $this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT DEFAULT NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        $this->addSql("ALTER TABLE wikitag_tag ADD alternative_label VARCHAR(1024) DEFAULT NULL, ADD wikipedia_alternative_url VARCHAR(2048) DEFAULT NULL, ADD alternative_wikipedia_pageid BIGINT DEFAULT NULL, CHANGE normalized_pageid wikipedia_pageid BIGINT DEFAULT NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    public function postUp(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    {
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    70
        $doctrine = $GLOBALS["application"]->getKernel()->getContainer()->get('doctrine');
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    71
        $em = $doctrine->getEntityManager();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    72
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    73
        $this->skipIf($this->version->isMigrated() !== true, 'postUp can only apply if migration completes.');
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        //select all tags with redirect and alternative label null
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        $query = $em->createQuery("SELECT t FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        $query->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    78
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    79
        $querycount = $em->createQuery("SELECT count(t) FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    80
        $querycount->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    81
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    82
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    83
        $total = $querycount->getSingleScalarResult();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        $iterableResult = $query->iterate();
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        $i = 0;
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    86
        $em->beginTransaction();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    87
        foreach($iterableResult as $row) {
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            $i++;
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
            $tag = $row[0];
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    91
            $memory = ((($i%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    92
            $this->showProgress($i, $total, "tag : ".$tag->getLabel()."$memory", 50);
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            //process $tag
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            $wk_info = WikiTagUtils::getWikipediaInfo($tag->getNormalizedLabel());
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
            $tag->setWikipediaInfo($wk_info);
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    97
            
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    98
            $em->persist($tag);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    99
                            
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
            if (($i % 100) == 0) {
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   101
                $em->flush();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   102
                $em->commit();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                $em->clear(); // Detaches all objects from Doctrine!
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   104
                $em->beginTransaction();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        }
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   107
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   108
        $em->flush(); // Executes all updates.
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   109
        $em->commit();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   110
        $em->clear(); // Detaches all objects from Doctrine!
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    public function down(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    {
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        // this down() migration is autogenerated, please modify it to your needs
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        $this->addSql("ALTER TABLE wikitag_document DROP FOREIGN KEY FK_256601059F75D7B0");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        $this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT NOT NULL");
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   121
        $this->addSql("ALTER TABLE wikitag_tag DROP alternative_label, DROP wikipedia_alternative_url, DROP alternative_wikipedia_pageid, CHANGE wikipedia_pageid normalized_pageid BIGINT DEFAULT NULL");
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
}