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
44 * @param $categories FIXME
45 * @param $mode String: FIXME, default 'AND'.
47 function seed ( $article_ids , $categories , $mode = "AND" ) {
48 $this->articles
= $article_ids ;
49 $this->next
= $article_ids ;
52 # Set the list of target categories; convert them to DBKEY form first
53 $this->targets
= array () ;
54 foreach ( $categories AS $c ) {
55 $ct = Title
::newFromText ( $c , NS_CATEGORY
) ;
56 $c = $ct->getDBkey () ;
57 $this->targets
[$c] = $c ;
62 * Iterates through the parent tree starting with the seed values,
63 * then checks the articles if they match the conditions
64 @return array of page_ids (those given to seed() that match the conditions)
67 $this->dbr
=& wfGetDB( DB_SLAVE
);
68 while ( count ( $this->next
) > 0 ) {
69 $this->scan_next_layer () ;
72 # Now check if this applies to the individual articles
74 foreach ( $this->articles
AS $article ) {
75 $conds = $this->targets
;
76 if ( $this->check ( $article , $conds ) ) {
77 # Matches the conditions
85 * This functions recurses through the parent representation, trying to match the conditions
86 @param $id The article/category to check
87 @param $conds The array of categories to match
88 @return bool Does this match the conditions?
90 function check ( $id , &$conds ) {
91 # Shortcut (runtime paranoia): No contitions=all matched
92 if ( count ( $conds ) == 0 ) return true ;
94 if ( !isset ( $this->parents
[$id] ) ) return false ;
96 # iterate through the parents
97 foreach ( $this->parents
[$id] AS $p ) {
100 # Is this a condition?
101 if ( isset ( $conds[$pname] ) ) {
102 # This key is in the category list!
103 if ( $this->mode
== "OR" ) {
104 # One found, that's enough!
108 # Assuming "AND" as default
109 unset ( $conds[$pname] ) ;
110 if ( count ( $conds ) == 0 ) {
111 # All conditions met, done
117 # Not done yet, try sub-parents
118 if ( !isset ( $this->name2id
[$pname] ) ) {
122 $done = $this->check ( $this->name2id
[$pname] , $conds ) ;
123 if ( $done OR count ( $conds ) == 0 ) {
124 # Subparents have done it!
132 * Scans a "parent layer" of the articles/categories in $this->next
134 function scan_next_layer () {
135 $fname = "Categoryfinder::scan_next_layer" ;
137 # Find all parents of the article currently in $this->next
139 $res = $this->dbr
->select(
140 /* FROM */ 'categorylinks',
142 /* WHERE */ array( 'cl_from' => $this->next
),
145 while ( $o = $this->dbr
->fetchObject( $res ) ) {
149 if ( !isset ( $this->parents
[$o->cl_from
] ) ) {
150 $this->parents
[$o->cl_from
] = array () ;
152 $this->parents
[$o->cl_from
][$k] = $o ;
154 # Ignore those we already have
155 if ( in_array ( $k , $this->deadend
) ) continue ;
156 if ( isset ( $this->name2id
[$k] ) ) continue ;
161 $this->dbr
->freeResult( $res ) ;
163 $this->next
= array() ;
165 # Find the IDs of all category pages in $layer, if they exist
166 if ( count ( $layer ) > 0 ) {
167 $res = $this->dbr
->select(
169 /* SELECT */ 'page_id,page_title',
170 /* WHERE */ array( 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ),
173 while ( $o = $this->dbr
->fetchObject( $res ) ) {
175 $name = $o->page_title
;
176 $this->name2id
[$name] = $id ;
177 $this->next
[] = $id ;
178 unset ( $layer[$name] ) ;
180 $this->dbr
->freeResult( $res ) ;
184 foreach ( $layer AS $v ) {
185 $this->deadend
[$v] = $v ;
189 } # END OF CLASS "Categoryfinder"