Skip to content
Newer
Older
100755 122 lines (101 sloc) 3.43 KB
f090e14 @till * script to rollback a proposal to RFC phase
till authored Jan 7, 2011
1 #!/usr/bin/env php
2 <?php
3516a1a @till require MDB2
till authored Jan 7, 2011
3 require_once 'MDB2.php';
f090e14 @till * script to rollback a proposal to RFC phase
till authored Jan 7, 2011
4 $dsn = 'mysql://root:@localhost/pear';
5
6 try {
7 $proposal_id = (int) $argv[1];
8 if ($proposal_id < 1) {
9 throw InvalidArgumentException("Please supply a number: ./rollback.php NUM");
10 }
11
12 $mdb2 = MDB2::connect($dsn);
13 if (MDB2::isError($mdb2)) {
14 throw new RuntimeException("Could not connect to database: {$e->getDebugInfo()}");
15 }
16 $pearweb = new Pearweb_Rollback($proposal_id, $mdb2);
17 $pearweb->rollback();
18
19 } catch (Exception $e) {
20 echo $e->getMessage();
21 exit(1);
22 }
23
24 echo "All is well.\n";
25 exit;
26
27 class Pearweb_Rollback
28 {
29 protected $mdb2;
30 protected $proposal;
31
32 public function __construct($proposal, MDB2_Driver_Common $mdb2)
33 {
34 if (!is_int($proposal)) {
35 throw new InvalidArgumentException('$proposal must be an int');
36 }
37 $this->proposal = $proposal;
38 $this->mdb2 = $mdb2;
39 }
40
41 public function rollback()
42 {
43 $this->moveVotes();
44 echo "Moved votes to comments...\n";
45
46 $this->resetDate();
47 echo "Reset vote date...\n";
48
49 $this->resetStatus();
50 echo "Reset status...\n";
51 }
52
53 protected function moveVotes()
54 {
55 $sql = "SELECT * FROM package_proposal_votes WHERE pkg_prop_id = {$this->proposal}";
56 $res = $this->mdb2->query($sql);
57 if (MDB2::isError($res)) {
58 throw new RuntimeException("DB error occurred: {$res->getDebugInfo()}");
59 }
60 if ($res->numRows() == 0) {
61 return; // nothing to do
62 }
63
64 $insert = "INSERT INTO package_proposal_comments (";
65 $insert .= "user_handle, pkg_prop_id, timestamp, comment";
66 $insert .= ") VALUES(%s, {$this->proposal}, %d, %s)";
67
68 $delete = "DELETE FROM package_proposal_votes WHERE";
69 $delete .= " pkg_prop_id = {$this->proposal}";
70 $delete .= " AND user_handle = %s";
71
72 while ($row = $res->fetchRow(MDB2_FETCHMODE_OBJECT)) {
73
74 $comment = "Original vote: {$row->value}\n";
75 $comment .= "Conditional vote: " . ($row->is_conditional != 0)?'yes':'no' . "\n";
76 $comment .= "Comment on vote: " . $row->comment . "\n\n";
77 $comment .= "Reviewed: " . implode(", ", unserialize($row->reviews));
78
79 $sql = sprintf(
80 $insert,
81 $this->mdb2->quote($row->user_handle),
82 $row->timestamp,
83 $this->mdb2->quote($comment)
84 );
85 $this->queryChange($sql);
86
87 $sql = sprintf(
88 $delete,
89 $this->mdb2->quote($row->user_handle)
90 );
91 $this->queryChange($sql);
92 }
93
94 $res->free();
95 return true;
96 }
97
98 protected function queryChange($sql)
99 {
100 $affected = $this->mdb2->exec($sql);
101 if (MDB2::isError($affected)) {
102 throw new RuntimeException("DB error occurred: " . $affected->getDebugInfo());
103 }
104 if ($affected < 1) {
105 throw new UnexpectedValueException("No rows affected. Invalid proposal ID?");
106 }
107 return true;
108 }
109
110 protected function resetDate()
111 {
112 $sql = "UPDATE package_proposals SET vote_date = null WHERE id = {$this->proposal}";
113 return $this->queryChange($sql);
114 }
115
116 protected function resetStatus()
117 {
118 $sql = "UPDATE proposal SET status='proposal' WHERE id = {$this->proposal}";
119 return $this->queryChange($sql);
120 }
121 }
Something went wrong with that request. Please try again.