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.
9 # Determines wether the article with the page_id 12345 is in both
10 # "Category 1" and "Category 2" or their subcategories, respectively
12 $cf = new Categoryfinder ;
15 array ( "Category 1","Category 2" ) ,
19 print implode ( "," , $a ) ;
24 class Categoryfinder
{
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
36 * Constructor (currently empty).
38 function Categoryfinder () {
42 * Initializes the instance. Do this prior to calling run().
43 @param $article_ids Array of article IDs
45 function seed ( $article_ids , $categories , $mode = "AND" ) {
46 $this->articles
= $article_ids ;
47 $this->next
= $article_ids ;
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 ;
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)
65 $this->dbr
=& wfGetDB( DB_SLAVE
);
66 while ( count ( $this->next
) > 0 ) {
67 $this->scan_next_layer () ;
70 # Now check if this applies to the individual articles
72 foreach ( $this->articles
AS $article ) {
73 $conds = $this->targets
;
74 if ( $this->check ( $article , $conds ) ) {
75 # Matches the conditions
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?
88 function check ( $id , &$conds ) {
89 # Shortcut (runtime paranoia): No contitions=all matched
90 if ( count ( $conds ) == 0 ) return true ;
92 if ( !isset ( $this->parents
[$id] ) ) return false ;
94 # iterate through the parents
95 foreach ( $this->parents
[$id] AS $p ) {
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!
106 # Assuming "AND" as default
107 unset ( $conds[$pname] ) ;
108 if ( count ( $conds ) == 0 ) {
109 # All conditions met, done
115 # Not done yet, try sub-parents
116 if ( !isset ( $this->name2id
[$pname] ) ) {
120 $done = $this->check ( $this->name2id
[$pname] , $conds ) ;
121 if ( $done OR count ( $conds ) == 0 ) {
122 # Subparents have done it!
130 * Scans a "parent layer" of the articles/categories in $this->next
132 function scan_next_layer () {
133 $fname = "Categoryfinder::scan_next_layer" ;
135 # Find all parents of the article currently in $this->next
137 $res = $this->dbr
->select(
138 /* FROM */ 'categorylinks',
140 /* WHERE */ array( 'cl_from' => $this->next
),
143 while ( $o = $this->dbr
->fetchObject( $res ) ) {
147 if ( !isset ( $this->parents
[$o->cl_from
] ) ) {
148 $this->parents
[$o->cl_from
] = array () ;
150 $this->parents
[$o->cl_from
][$k] = $o ;
152 # Ignore those we already have
153 if ( in_array ( $k , $this->deadend
) ) continue ;
154 if ( isset ( $this->name2id
[$k] ) ) continue ;
159 $this->dbr
->freeResult( $res ) ;
161 $this->next
= array() ;
163 # Find the IDs of all category pages in $layer, if they exist
164 if ( count ( $layer ) > 0 ) {
165 $res = $this->dbr
->select(
167 /* SELECT */ 'page_id,page_title',
168 /* WHERE */ array( 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ),
171 while ( $o = $this->dbr
->fetchObject( $res ) ) {
173 $name = $o->page_title
;
174 $this->name2id
[$name] = $id ;
175 $this->next
[] = $id ;
176 unset ( $layer[$name] ) ;
178 $this->dbr
->freeResult( $res ) ;
182 foreach ( $layer AS $v ) {
183 $this->deadend
[$v] = $v ;
187 } # END OF CLASS "Categoryfinder"