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