Index: trunk/phase3/includes/Categoryfinder.php |
— | — | @@ -1,5 +1,4 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | /** |
5 | 4 | * The "Categoryfinder" class takes a list of articles, creates an internal |
6 | 5 | * representation of all their parent categories (as well as parents of |
— | — | @@ -24,14 +23,14 @@ |
25 | 24 | */ |
26 | 25 | class Categoryfinder { |
27 | 26 | |
28 | | - var $articles = array () ; # The original article IDs passed to the seed function |
29 | | - var $deadend = array () ; # Array of DBKEY category names for categories that don't have a page |
30 | | - var $parents = array () ; # Array of [ID => array()] |
31 | | - var $next = array () ; # Array of article/category IDs |
32 | | - var $targets = array () ; # Array of DBKEY category names |
33 | | - var $name2id = array () ; |
34 | | - var $mode ; # "AND" or "OR" |
35 | | - var $dbr ; # Read-DB slave |
| 27 | + var $articles = array(); # The original article IDs passed to the seed function |
| 28 | + var $deadend = array(); # Array of DBKEY category names for categories that don't have a page |
| 29 | + var $parents = array(); # Array of [ID => array()] |
| 30 | + var $next = array(); # Array of article/category IDs |
| 31 | + var $targets = array(); # Array of DBKEY category names |
| 32 | + var $name2id = array(); |
| 33 | + var $mode; # "AND" or "OR" |
| 34 | + var $dbr; # Read-DB slave |
36 | 35 | |
37 | 36 | /** |
38 | 37 | * Constructor (currently empty). |
— | — | @@ -45,14 +44,14 @@ |
46 | 45 | * @param $categories FIXME |
47 | 46 | * @param $mode String: FIXME, default 'AND'. |
48 | 47 | */ |
49 | | - function seed ( $article_ids , $categories , $mode = "AND" ) { |
50 | | - $this->articles = $article_ids ; |
51 | | - $this->next = $article_ids ; |
52 | | - $this->mode = $mode ; |
| 48 | + function seed( $article_ids, $categories, $mode = "AND" ) { |
| 49 | + $this->articles = $article_ids; |
| 50 | + $this->next = $article_ids; |
| 51 | + $this->mode = $mode; |
53 | 52 | |
54 | 53 | # Set the list of target categories; convert them to DBKEY form first |
55 | | - $this->targets = array () ; |
56 | | - foreach ( $categories AS $c ) { |
| 54 | + $this->targets = array(); |
| 55 | + foreach ( $categories as $c ) { |
57 | 56 | $ct = Title::makeTitleSafe( NS_CATEGORY, $c ); |
58 | 57 | if( $ct ) { |
59 | 58 | $c = $ct->getDBkey(); |
— | — | @@ -69,19 +68,19 @@ |
70 | 69 | function run () { |
71 | 70 | $this->dbr = wfGetDB( DB_SLAVE ); |
72 | 71 | while ( count ( $this->next ) > 0 ) { |
73 | | - $this->scan_next_layer () ; |
| 72 | + $this->scan_next_layer(); |
74 | 73 | } |
75 | 74 | |
76 | 75 | # Now check if this applies to the individual articles |
77 | | - $ret = array () ; |
78 | | - foreach ( $this->articles AS $article ) { |
79 | | - $conds = $this->targets ; |
80 | | - if ( $this->check ( $article , $conds ) ) { |
| 76 | + $ret = array(); |
| 77 | + foreach ( $this->articles as $article ) { |
| 78 | + $conds = $this->targets; |
| 79 | + if ( $this->check( $article, $conds ) ) { |
81 | 80 | # Matches the conditions |
82 | | - $ret[] = $article ; |
| 81 | + $ret[] = $article; |
83 | 82 | } |
84 | 83 | } |
85 | | - return $ret ; |
| 84 | + return $ret; |
86 | 85 | } |
87 | 86 | |
88 | 87 | /** |
— | — | @@ -91,107 +90,103 @@ |
92 | 91 | * @param $path used to check for recursion loops |
93 | 92 | * @return bool Does this match the conditions? |
94 | 93 | */ |
95 | | - function check ( $id , &$conds, $path=array() ) { |
| 94 | + function check( $id , &$conds, $path = array() ) { |
96 | 95 | // Check for loops and stop! |
97 | 96 | if( in_array( $id, $path ) ) |
98 | 97 | return false; |
99 | 98 | $path[] = $id; |
100 | 99 | |
101 | 100 | # Shortcut (runtime paranoia): No contitions=all matched |
102 | | - if ( count ( $conds ) == 0 ) return true ; |
| 101 | + if ( count( $conds ) == 0 ) return true; |
103 | 102 | |
104 | | - if ( !isset ( $this->parents[$id] ) ) return false ; |
| 103 | + if ( !isset( $this->parents[$id] ) ) return false; |
105 | 104 | |
106 | 105 | # iterate through the parents |
107 | | - foreach ( $this->parents[$id] AS $p ) { |
| 106 | + foreach ( $this->parents[$id] as $p ) { |
108 | 107 | $pname = $p->cl_to ; |
109 | 108 | |
110 | 109 | # Is this a condition? |
111 | | - if ( isset ( $conds[$pname] ) ) { |
| 110 | + if ( isset( $conds[$pname] ) ) { |
112 | 111 | # This key is in the category list! |
113 | 112 | if ( $this->mode == "OR" ) { |
114 | 113 | # One found, that's enough! |
115 | | - $conds = array () ; |
116 | | - return true ; |
| 114 | + $conds = array(); |
| 115 | + return true; |
117 | 116 | } else { |
118 | 117 | # Assuming "AND" as default |
119 | | - unset ( $conds[$pname] ) ; |
120 | | - if ( count ( $conds ) == 0 ) { |
| 118 | + unset( $conds[$pname] ) ; |
| 119 | + if ( count( $conds ) == 0 ) { |
121 | 120 | # All conditions met, done |
122 | | - return true ; |
| 121 | + return true; |
123 | 122 | } |
124 | 123 | } |
125 | 124 | } |
126 | 125 | |
127 | 126 | # Not done yet, try sub-parents |
128 | | - if ( !isset ( $this->name2id[$pname] ) ) { |
| 127 | + if ( !isset( $this->name2id[$pname] ) ) { |
129 | 128 | # No sub-parent |
130 | 129 | continue ; |
131 | 130 | } |
132 | | - $done = $this->check ( $this->name2id[$pname] , $conds, $path ); |
133 | | - if ( $done OR count ( $conds ) == 0 ) { |
| 131 | + $done = $this->check( $this->name2id[$pname], $conds,$path ); |
| 132 | + if ( $done || count( $conds ) == 0 ) { |
134 | 133 | # Subparents have done it! |
135 | | - return true ; |
| 134 | + return true; |
136 | 135 | } |
137 | 136 | } |
138 | | - return false ; |
| 137 | + return false; |
139 | 138 | } |
140 | 139 | |
141 | 140 | /** |
142 | 141 | * Scans a "parent layer" of the articles/categories in $this->next |
143 | 142 | */ |
144 | | - function scan_next_layer () { |
145 | | - $fname = "Categoryfinder::scan_next_layer" ; |
146 | | - |
| 143 | + function scan_next_layer() { |
147 | 144 | # Find all parents of the article currently in $this->next |
148 | | - $layer = array () ; |
| 145 | + $layer = array(); |
149 | 146 | $res = $this->dbr->select( |
150 | 147 | /* FROM */ 'categorylinks', |
151 | 148 | /* SELECT */ '*', |
152 | 149 | /* WHERE */ array( 'cl_from' => $this->next ), |
153 | | - $fname."-1" |
| 150 | + __METHOD__ . "-1" |
154 | 151 | ); |
155 | 152 | while ( $o = $this->dbr->fetchObject( $res ) ) { |
156 | 153 | $k = $o->cl_to ; |
157 | 154 | |
158 | 155 | # Update parent tree |
159 | | - if ( !isset ( $this->parents[$o->cl_from] ) ) { |
160 | | - $this->parents[$o->cl_from] = array () ; |
| 156 | + if ( !isset( $this->parents[$o->cl_from] ) ) { |
| 157 | + $this->parents[$o->cl_from] = array(); |
161 | 158 | } |
162 | | - $this->parents[$o->cl_from][$k] = $o ; |
| 159 | + $this->parents[$o->cl_from][$k] = $o; |
163 | 160 | |
164 | 161 | # Ignore those we already have |
165 | | - if ( in_array ( $k , $this->deadend ) ) continue ; |
166 | | - if ( isset ( $this->name2id[$k] ) ) continue ; |
| 162 | + if ( in_array ( $k , $this->deadend ) ) continue; |
| 163 | + if ( isset ( $this->name2id[$k] ) ) continue; |
167 | 164 | |
168 | 165 | # Hey, new category! |
169 | | - $layer[$k] = $k ; |
| 166 | + $layer[$k] = $k; |
170 | 167 | } |
171 | | - $this->dbr->freeResult( $res ) ; |
172 | 168 | |
173 | | - $this->next = array() ; |
| 169 | + $this->next = array(); |
174 | 170 | |
175 | 171 | # Find the IDs of all category pages in $layer, if they exist |
176 | 172 | if ( count ( $layer ) > 0 ) { |
177 | 173 | $res = $this->dbr->select( |
178 | 174 | /* FROM */ 'page', |
179 | | - /* SELECT */ 'page_id,page_title', |
| 175 | + /* SELECT */ array( 'page_id', 'page_title' ), |
180 | 176 | /* WHERE */ array( 'page_namespace' => NS_CATEGORY , 'page_title' => $layer ), |
181 | | - $fname."-2" |
| 177 | + __METHOD__ . "-2" |
182 | 178 | ); |
183 | 179 | while ( $o = $this->dbr->fetchObject( $res ) ) { |
184 | | - $id = $o->page_id ; |
185 | | - $name = $o->page_title ; |
186 | | - $this->name2id[$name] = $id ; |
187 | | - $this->next[] = $id ; |
188 | | - unset ( $layer[$name] ) ; |
| 180 | + $id = $o->page_id; |
| 181 | + $name = $o->page_title; |
| 182 | + $this->name2id[$name] = $id; |
| 183 | + $this->next[] = $id; |
| 184 | + unset( $layer[$name] ); |
189 | 185 | } |
190 | | - $this->dbr->freeResult( $res ) ; |
191 | | - } |
| 186 | + } |
192 | 187 | |
193 | 188 | # Mark dead ends |
194 | | - foreach ( $layer AS $v ) { |
195 | | - $this->deadend[$v] = $v ; |
| 189 | + foreach ( $layer as $v ) { |
| 190 | + $this->deadend[$v] = $v; |
196 | 191 | } |
197 | 192 | } |
198 | 193 | |