Merge "[FileBackend] Added 'recursive' flag to directory clean() function."
[lhc/web/wiklou.git] / includes / cache / LinkBatch.php
1 <?php
2
3 /**
4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
6 *
7 * @ingroup Cache
8 */
9 class LinkBatch {
10 /**
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
12 */
13 var $data = array();
14
15 /**
16 * For debugging which method is using this class.
17 */
18 protected $caller;
19
20 function __construct( $arr = array() ) {
21 foreach( $arr as $item ) {
22 $this->addObj( $item );
23 }
24 }
25
26 /**
27 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
28 * class. Only used in debugging output.
29 * @since 1.17
30 *
31 * @param $caller
32 */
33 public function setCaller( $caller ) {
34 $this->caller = $caller;
35 }
36
37 /**
38 * @param $title Title
39 */
40 public function addObj( $title ) {
41 if ( is_object( $title ) ) {
42 $this->add( $title->getNamespace(), $title->getDBkey() );
43 } else {
44 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
45 }
46 }
47
48 /**
49 * @param $ns int
50 * @param $dbkey string
51 * @return mixed
52 */
53 public function add( $ns, $dbkey ) {
54 if ( $ns < 0 ) {
55 return;
56 }
57 if ( !array_key_exists( $ns, $this->data ) ) {
58 $this->data[$ns] = array();
59 }
60
61 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
62 }
63
64 /**
65 * Set the link list to a given 2-d array
66 * First key is the namespace, second is the DB key, value arbitrary
67 *
68 * @param $array array
69 */
70 public function setArray( $array ) {
71 $this->data = $array;
72 }
73
74 /**
75 * Returns true if no pages have been added, false otherwise.
76 *
77 * @return bool
78 */
79 public function isEmpty() {
80 return ($this->getSize() == 0);
81 }
82
83 /**
84 * Returns the size of the batch.
85 *
86 * @return int
87 */
88 public function getSize() {
89 return count( $this->data );
90 }
91
92 /**
93 * Do the query and add the results to the LinkCache object
94 *
95 * @return Array mapping PDBK to ID
96 */
97 public function execute() {
98 $linkCache = LinkCache::singleton();
99 return $this->executeInto( $linkCache );
100 }
101
102 /**
103 * Do the query and add the results to a given LinkCache object
104 * Return an array mapping PDBK to ID
105 *
106 * @param $cache LinkCache
107 * @return Array remaining IDs
108 */
109 protected function executeInto( &$cache ) {
110 wfProfileIn( __METHOD__ );
111 $res = $this->doQuery();
112 $this->doGenderQuery();
113 $ids = $this->addResultToCache( $cache, $res );
114 wfProfileOut( __METHOD__ );
115 return $ids;
116 }
117
118 /**
119 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
120 * As normal, titles will go into the static Title cache field.
121 * This function *also* stores extra fields of the title used for link
122 * parsing to avoid extra DB queries.
123 *
124 * @param $cache LinkCache
125 * @param $res
126 * @return Array of remaining titles
127 */
128 public function addResultToCache( $cache, $res ) {
129 if ( !$res ) {
130 return array();
131 }
132
133 // For each returned entry, add it to the list of good links, and remove it from $remaining
134
135 $ids = array();
136 $remaining = $this->data;
137 foreach ( $res as $row ) {
138 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
139 $cache->addGoodLinkObjFromRow( $title, $row );
140 $ids[$title->getPrefixedDBkey()] = $row->page_id;
141 unset( $remaining[$row->page_namespace][$row->page_title] );
142 }
143
144 // The remaining links in $data are bad links, register them as such
145 foreach ( $remaining as $ns => $dbkeys ) {
146 foreach ( $dbkeys as $dbkey => $unused ) {
147 $title = Title::makeTitle( $ns, $dbkey );
148 $cache->addBadLinkObj( $title );
149 $ids[$title->getPrefixedDBkey()] = 0;
150 }
151 }
152 return $ids;
153 }
154
155 /**
156 * Perform the existence test query, return a ResultWrapper with page_id fields
157 * @return Bool|ResultWrapper
158 */
159 public function doQuery() {
160 if ( $this->isEmpty() ) {
161 return false;
162 }
163 wfProfileIn( __METHOD__ );
164
165 // This is similar to LinkHolderArray::replaceInternal
166 $dbr = wfGetDB( DB_SLAVE );
167 $table = 'page';
168 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
169 'page_is_redirect', 'page_latest' );
170 $conds = $this->constructSet( 'page', $dbr );
171
172 // Do query
173 $caller = __METHOD__;
174 if ( strval( $this->caller ) !== '' ) {
175 $caller .= " (for {$this->caller})";
176 }
177 $res = $dbr->select( $table, $fields, $conds, $caller );
178 wfProfileOut( __METHOD__ );
179 return $res;
180 }
181
182 /**
183 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
184 *
185 * @return bool whether the query was successful
186 */
187 public function doGenderQuery() {
188 if ( $this->isEmpty() ) {
189 return false;
190 }
191
192 global $wgContLang;
193 if ( !$wgContLang->needsGenderDistinction() ) {
194 return false;
195 }
196
197 $genderCache = GenderCache::singleton();
198 $genderCache->doLinkBatch( $this->data, $this->caller );
199 return true;
200 }
201
202 /**
203 * Construct a WHERE clause which will match all the given titles.
204 *
205 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
206 * @param $db DatabaseBase object to use
207 * @return mixed string with SQL where clause fragment, or false if no items.
208 */
209 public function constructSet( $prefix, $db ) {
210 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
211 }
212 }