| author | ymh <ymh.work@gmail.com> |
| Wed, 14 Dec 2011 23:55:33 +0100 | |
| changeset 60 | c8790bc6417a |
| parent 40 | d67ff36f17e2 |
| child 71 | b01e36991488 |
| permissions | -rw-r--r-- |
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
namespace Application\Migrations; |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
|
| 40 | 5 |
use Doctrine\DBAL\Migrations\OutputWriter; |
6 |
||
7 |
use Doctrine\DBAL\Migrations\AbstractMigration; |
|
8 |
use Doctrine\DBAL\Schema\Schema; |
|
9 |
use Doctrine\Common\EventManager; |
|
10 |
use Doctrine\ORM\EntityManager; |
|
11 |
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
12 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
|
13 |
||
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
/** |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
* 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
|
17 |
*/ |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
class Version20111125175224 extends AbstractMigration |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
{ |
| 40 | 20 |
|
21 |
private function showProgress($current, $total, $label, $width) |
|
22 |
{ |
|
23 |
$percent = (floatval($current)/floatval($total)) * 100.0; |
|
24 |
$marks = intval(floor(floatval($width) * ($percent / 100.0) )); |
|
25 |
$spaces = $width - $marks; |
|
26 |
||
27 |
$status_bar="\r["; |
|
28 |
$status_bar.=str_repeat("=", $marks); |
|
29 |
if($marks<$width){ |
|
30 |
$status_bar.=">"; |
|
31 |
$status_bar.=str_repeat(" ", $spaces); |
|
32 |
} else { |
|
33 |
$status_bar.="="; |
|
34 |
} |
|
35 |
||
36 |
$disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT); |
|
37 |
||
38 |
$label = str_pad(substr($label,0,50), 50, " "); |
|
39 |
$current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT); |
|
40 |
||
41 |
$status_bar.="] $disp% $current_str/$total : $label"; |
|
42 |
||
43 |
$this->write("$status_bar "); |
|
44 |
||
45 |
if($current == $total) { |
|
46 |
$this->write("\n"); |
|
47 |
} |
|
48 |
} |
|
49 |
||
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
public function up(Schema $schema) |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
{ |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
// 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
|
53 |
$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
|
54 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
$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
|
56 |
$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
|
57 |
$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
|
58 |
} |
|
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 |
public function postUp(Schema $schema) |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
{ |
| 40 | 62 |
$doctrine = $GLOBALS["application"]->getKernel()->getContainer()->get('doctrine'); |
63 |
$em = $doctrine->getEntityManager(); |
|
64 |
||
65 |
$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
|
66 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
//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
|
68 |
$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
|
69 |
$query->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']); |
| 40 | 70 |
|
71 |
$querycount = $em->createQuery("SELECT count(t) FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL"); |
|
72 |
$querycount->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']); |
|
73 |
||
74 |
||
75 |
$total = $querycount->getSingleScalarResult(); |
|
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
$iterableResult = $query->iterate(); |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
$i = 0; |
| 40 | 78 |
$em->beginTransaction(); |
79 |
foreach($iterableResult as $row) { |
|
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
$i++; |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
$tag = $row[0]; |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
|
| 40 | 83 |
$memory = ((($i%10)==0)?" - mem: ".strval(memory_get_usage(true)):""); |
84 |
$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
|
85 |
//process $tag |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
$wk_info = WikiTagUtils::getWikipediaInfo($tag->getNormalizedLabel()); |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
$tag->setWikipediaInfo($wk_info); |
| 40 | 89 |
|
90 |
$em->persist($tag); |
|
91 |
||
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
if (($i % 100) == 0) { |
| 40 | 93 |
$em->flush(); |
94 |
$em->commit(); |
|
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
$em->clear(); // Detaches all objects from Doctrine! |
| 40 | 96 |
$em->beginTransaction(); |
|
39
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
} |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
} |
| 40 | 99 |
|
100 |
$em->flush(); // Executes all updates. |
|
101 |
$em->commit(); |
|
102 |
$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
|
103 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
} |
|
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 |
public function down(Schema $schema) |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
{ |
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
// 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
|
109 |
$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
|
110 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
$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
|
112 |
$this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT NOT NULL"); |
| 40 | 113 |
$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
|
114 |
} |
|
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 |
|
|
03b14b0fe101
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
} |