Ticket #1128: newdatatablebug.txt

File newdatatablebug.txt, 4.7 KB (added by jetuelle, 2 years ago)
Line 
1Introduction:
2    The line "$this->rowsIndexByLabel[$label] = count($this->rows)-1;" in function
3"public function addRow( Piwik_DataTable_Row $row )" doesn't work correctly.
4For example:
5
6$rows ( 1=>"AA", 2=>"BB", 3=>"CC", 4=>"DD", 5=>"EE", 6=>"FF");
7note:1=>"AA": "1" is the index id of label "AA"
8
9delete $rows[3], we get:
10
11$rows ( 1=>"AA", 2=>"BB", 4=>"DD", 5=>"EE", 6=>"FF");
12
13addRow($newrow),the label of $newrow is "GG"
14
15The result should be:
16$rows ( 1=>"AA", 2=>"BB", 4=>"DD", 5=>"EE", 6=>"FF", 7=>"GG");
17
18while according to function addRow(), the index id of $newrow's label("GG") is
19
20$this->rowsIndexByLabel[$label] = count($this->rows)-1 =5-1=4,
21
22but the right index id of label "GG" is 7.
23
24We find the function rebuildIndex() useless after we change the function addRow().
25
26The code change contains the following three points:
27
281¡¢£š1£©original code(4 functions):
29    public function addRow( Piwik_DataTable_Row $row )
30        {
31                $this->rows[] = $row;
32               
33                if(!$this->indexNotUpToDate
34                        && $this->rebuildIndexContinuously)
35                {
36                        $label = $row->getColumn('label');
37                        if($label !== false)
38                        {
39                                $this->rowsIndexByLabel[$label] = count($this->rows)-1;                         
40                        }
41                        $this->indexNotUpToDate = false;
42                }
43        }
44       
45        static public function isEqual(Piwik_DataTable $table1, Piwik_DataTable $table2)
46        {
47                $rows1 = $table1->getRows();
48                $rows2 = $table2->getRows();
49               
50                $table1->rebuildIndex();
51                $table2->rebuildIndex();
52               
53                if($table1->getRowsCount() != $table2->getRowsCount())
54                {
55                        return false;
56                }
57               
58                foreach($rows1 as $row1)
59                {
60                        $row2 = $table2->getRowFromLabel($row1->getColumn('label'));
61                        if($row2 === false
62                                || !Piwik_DataTable_Row::isEqual($row1,$row2))
63                        {
64                                return false;
65                        }
66                }               
67                return true;
68        }
69       
70        public function getRowFromLabel( $label )
71        {
72                $this->rebuildIndexContinuously = true;
73                if($this->indexNotUpToDate)
74                {
75                        $this->rebuildIndex();
76                }
77               
78                if($label === self::LABEL_SUMMARY_ROW
79                        && !is_null($this->summaryRow))
80                {
81                        return $this->summaryRow;
82                }
83               
84                $label = (string)$label;
85                if(!isset($this->rowsIndexByLabel[$label]))
86                {
87                        return false;
88                }
89                return $this->rows[$this->rowsIndexByLabel[$label]];
90        }
91       
92                public function sort( $functionCallback, $columnSortedBy )
93        {
94                $this->indexNotUpToDate = true;
95                $this->tableSortedBy = $columnSortedBy;
96                usort( $this->rows, $functionCallback );
97               
98                if($this->enableRecursiveSort === true)
99                {
100                        foreach($this->getRows() as $row)
101                        {
102                                if(($idSubtable = $row->getIdSubDataTable()) !== null)
103                                {
104                                        $table = Piwik_DataTable_Manager::getInstance()->getTable($idSubtable);
105                                        $table->enableRecursiveSort();
106                                        $table->sort($functionCallback, $columnSortedBy);
107                                }
108                        }
109                }
110        }
111       
112
113    £š2£©changed code(4 functions):
114    public function addRow( Piwik_DataTable_Row $row )
115        {
116                $this->rows[] = $row;
117               
118                $label = $row->getColumn('label');
119                if($label !== false)
120                {
121                        $this->rowsIndexByLabel[$label] = $this->nextRowId;
122                        $this->nextRowId++;
123                }
124        }
125       
126        static public function isEqual(Piwik_DataTable $table1, Piwik_DataTable $table2)
127        {
128                $rows1 = $table1->getRows();
129                $rows2 = $table2->getRows();
130               
131               
132                if($table1->getRowsCount() != $table2->getRowsCount())
133                {
134                        return false;
135                }
136               
137                foreach($rows1 as $row1)
138                {
139                        $row2 = $table2->getRowFromLabel($row1->getColumn('label'));
140                        if($row2 === false
141                                || !Piwik_DataTable_Row::isEqual($row1,$row2))
142                        {
143                                return false;
144                        }
145                }               
146                return true;
147        }
148
149        public function getRowFromLabel( $label )
150        {
151                if($label === self::LABEL_SUMMARY_ROW
152                        && !is_null($this->summaryRow))
153                {
154                        return $this->summaryRow;
155                }
156               
157                $label = (string)$label;
158                if(!isset($this->rowsIndexByLabel[$label]))
159                {
160                        return false;
161                }
162                return $this->rows[$this->rowsIndexByLabel[$label]];
163        }
164       
165                public function sort( $functionCallback, $columnSortedBy )
166        {
167                $this->tableSortedBy = $columnSortedBy;
168                usort( $this->rows, $functionCallback );
169               
170                if($this->enableRecursiveSort === true)
171                {
172                        foreach($this->getRows() as $row)
173                        {
174                                if(($idSubtable = $row->getIdSubDataTable()) !== null)
175                                {
176                                        $table = Piwik_DataTable_Manager::getInstance()->getTable($idSubtable);
177                                        $table->enableRecursiveSort();
178                                        $table->sort($functionCallback, $columnSortedBy);
179                                }
180                        }
181                }
182        }       
183       
1842¡¢added codes:
185        protected $nextRowId = 0;       
1863¡¢deleted code:
187        protected $indexNotUpToDate = true;
188    protected $rebuildIndexContinuously = false;                 
189        private function rebuildIndex()
190        {
191                foreach($this->rows as $id => $row)
192                {
193                        $label = $row->getColumn('label');
194                        if($label !== false)
195                        {
196                                $this->rowsIndexByLabel[$label] = $id;
197                        }
198                }
199                $this->indexNotUpToDate = false;
200        }