Don't require an existence check before calling loadPageData(). Added an accessor...
[lhc/web/wiklou.git] / includes / Categoryfinder.php
1 <?php
2 /*
3 The "Categoryfinder" class takes a list of articles, creates an internal representation of all their parent
4 categories (as well as parents of parents etc.). From this representation, it determines which of these articles
5 are in one or all of a given subset of categories.
6
7 Example use :
8
9 # Determines wether the article with the page_id 12345 is in both
10 # "Category 1" and "Category 2" or their subcategories, respectively
11
12 $cf = new Categoryfinder ;
13 $cf->seed (
14 array ( 12345 ) ,
15 array ( "Category 1","Category 2" ) ,
16 "AND"
17 ) ;
18 $a = $cf->run() ;
19 print implode ( "," , $a ) ;
20
21 */
22
23
24 class Categoryfinder {
25
26 var $articles = array () ; # The original article IDs passed to the seed function
27 var $deadend = array () ; # Array of DBKEY category names for categories that don't have a page
28 var $parents = array () ; # Array of [ID => array()]
29 var $next = array () ; # Array of article/category IDs
30 var $targets = array () ; # Array of DBKEY category names
31 var $name2id = array () ;
32 var $mode ; # "AND" or "OR"
33 var $dbr ; # Read-DB slave
34
35 /**
36 * Constructor (currently empty).
37 */
38 function Categoryfinder () {
39 }
40
41 /**
42 * Initializes the instance. Do this prior to calling run().
43 @param $article_ids Array of article IDs
44 */
45 function seed ( $article_ids , $categories , $mode = "AND" ) {
46 $this->articles = $article_ids ;
47 $this->next = $article_ids ;
48 $this->mode = $mode ;
49
50 # Set the list of target categories; convert them to DBKEY form first
51 $this->targets = array () ;
52 foreach ( $categories AS $c ) {
53 $ct = Title::newFromText ( $c , NS_CATEGORY ) ;
54 $c = $ct->getDBkey () ;
55 $this->targets[$c] = $c ;
56 }
57 }
58
59 /**
60 * Iterates through the parent tree starting with the seed values,
61 * then checks the articles if they match the conditions
62 @return array of page_ids (those given to seed() that match the conditions)
63 */
64 function run () {
65 $this->dbr =& wfGetDB( DB_SLAVE );
66 while ( count ( $this->next ) > 0 ) {
67 $this->scan_next_layer () ;
68 }
69
70 # Now check if this applies to the individual articles
71 $ret = array () ;
72 foreach ( $this->articles AS $article ) {
73 $conds = $this->targets ;
74 if ( $this->check ( $article , $conds ) ) {
75 # Matches the conditions
76 $ret[] = $article ;
77 }
78 }
79 return $ret ;
80 }
81
82 /**
83 * This functions recurses through the parent representation, trying to match the conditions
84 @param $id The article/category to check
85 @param $conds The array of categories to match
86 @return bool Does this match the conditions?
87 */
88 function check ( $id , &$conds ) {
89 # Shortcut (runtime paranoia): No contitions=all matched
90 if ( count ( $conds ) == 0 ) return true ;
91
92 if ( !isset ( $this->parents[$id] ) ) return false ;
93
94 # iterate through the parents
95 foreach ( $this->parents[$id] AS $p ) {
96 $pname = $p->cl_to ;
97
98 # Is this a condition?
99 if ( isset ( $conds[$pname] ) ) {
100 # This key is in the category list!
101 if ( $this->mode == "OR" ) {
102 # One found, that's enough!
103 $conds = array () ;
104 return true ;
105 } else {
106 # Assuming "AND" as default
107 unset ( $conds[$pname] ) ;
108 if ( count ( $conds ) == 0 ) {
109 # All conditions met, done
110 return true ;
111 }
112 }
113 }
114
115 # Not done yet, try sub-parents
116 if ( !isset ( $this->name2id[$pname] ) ) {
117 # No sub-parent
118 continue ;
119 }
120 $done = $this->check ( $this->name2id[$pname] , $conds ) ;
121 if ( $done OR count ( $conds ) == 0 ) {
122 # Subparents have done it!
123 return true ;
124 }
125 }
126 return false ;
127 }
128
129 /**
130 * Scans a "parent layer" of the articles/categories in $this->next
131 */
132 function scan_next_layer () {
133 $fname = "Categoryfinder::scan_next_layer" ;
134
135 # Find all parents of the article currently in $this->next
136 $layer = array () ;
137 $res = $this->dbr->select(
138 /* FROM */ 'categorylinks',
139 /* SELECT */ '*',
140 /* WHERE */ array( 'cl_from' => $this->next ),
141 $fname."-1"
142 );
143 while ( $o = $this->dbr->fetchObject( $res ) ) {
144 $k = $o->cl_to ;
145
146 # Update parent tree
147 if ( !isset ( $this->parents[$o->cl_from] ) ) {
148 $this->parents[$o->cl_from] = array () ;
149 }
150 $this->parents[$o->cl_from][$k] = $o ;
151
152 # Ignore those we already have
153 if ( in_array ( $k , $this->deadend ) ) continue ;
154 if ( isset ( $this->name2id[$k] ) ) continue ;
155
156 # Hey, new category!
157 $layer[$k] = $k ;
158 }
159 $this->dbr->freeResult( $res ) ;
160
161 $this->next = array() ;
162
163 # Find the IDs of all category pages in $layer, if they exist
164 if ( count ( $layer ) > 0 ) {
165 $res = $this->dbr->select(
166 /* FROM */ 'page',
167 /* SELECT */ 'page_id,page_title',
168 /* WHERE */ array( 'page_namespace' => NS_CATEGORY , 'page_title' => $layer ),
169 $fname."-2"
170 );
171 while ( $o = $this->dbr->fetchObject( $res ) ) {
172 $id = $o->page_id ;
173 $name = $o->page_title ;
174 $this->name2id[$name] = $id ;
175 $this->next[] = $id ;
176 unset ( $layer[$name] ) ;
177 }
178 $this->dbr->freeResult( $res ) ;
179 }
180
181 # Mark dead ends
182 foreach ( $layer AS $v ) {
183 $this->deadend[$v] = $v ;
184 }
185 }
186
187 } # END OF CLASS "Categoryfinder"
188
189 ?>