Documentation and type hinting.
[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 public function add( $ns, $dbkey ) {
49 if ( $ns < 0 ) {
50 return;
51 }
52 if ( !array_key_exists( $ns, $this->data ) ) {
53 $this->data[$ns] = array();
54 }
55
56 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
57 }
58
59 /**
60 * Set the link list to a given 2-d array
61 * First key is the namespace, second is the DB key, value arbitrary
62 *
63 * @param $array array
64 */
65 public function setArray( $array ) {
66 $this->data = $array;
67 }
68
69 /**
70 * Returns true if no pages have been added, false otherwise.
71 *
72 * @return bool
73 */
74 public function isEmpty() {
75 return ($this->getSize() == 0);
76 }
77
78 /**
79 * Returns the size of the batch.
80 *
81 * @return int
82 */
83 public function getSize() {
84 return count( $this->data );
85 }
86
87 /**
88 * Do the query and add the results to the LinkCache object
89 *
90 * @return Array mapping PDBK to ID
91 */
92 public function execute() {
93 $linkCache = LinkCache::singleton();
94 return $this->executeInto( $linkCache );
95 }
96
97 /**
98 * Do the query and add the results to a given LinkCache object
99 * Return an array mapping PDBK to ID
100 *
101 * @param $cache LinkCache
102 * @return Array remaining IDs
103 */
104 protected function executeInto( &$cache ) {
105 wfProfileIn( __METHOD__ );
106 $res = $this->doQuery();
107 $ids = $this->addResultToCache( $cache, $res );
108 $this->doGenderQuery();
109 wfProfileOut( __METHOD__ );
110 return $ids;
111 }
112
113 /**
114 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
115 * As normal, titles will go into the static Title cache field.
116 * This function *also* stores extra fields of the title used for link
117 * parsing to avoid extra DB queries.
118 *
119 * @param $cache LinkCache
120 * @param $res
121 * @return Array of remaining titles
122 */
123 public function addResultToCache( $cache, $res ) {
124 if ( !$res ) {
125 return array();
126 }
127
128 // For each returned entry, add it to the list of good links, and remove it from $remaining
129
130 $ids = array();
131 $remaining = $this->data;
132 foreach ( $res as $row ) {
133 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
134 $cache->addGoodLinkObjFromRow( $title, $row );
135 $ids[$title->getPrefixedDBkey()] = $row->page_id;
136 unset( $remaining[$row->page_namespace][$row->page_title] );
137 }
138
139 // The remaining links in $data are bad links, register them as such
140 foreach ( $remaining as $ns => $dbkeys ) {
141 foreach ( $dbkeys as $dbkey => $unused ) {
142 $title = Title::makeTitle( $ns, $dbkey );
143 $cache->addBadLinkObj( $title );
144 $ids[$title->getPrefixedDBkey()] = 0;
145 }
146 }
147 return $ids;
148 }
149
150 /**
151 * Perform the existence test query, return a ResultWrapper with page_id fields
152 * @return Bool|ResultWrapper
153 */
154 public function doQuery() {
155 if ( $this->isEmpty() ) {
156 return false;
157 }
158 wfProfileIn( __METHOD__ );
159
160 // This is similar to LinkHolderArray::replaceInternal
161 $dbr = wfGetDB( DB_SLAVE );
162 $table = 'page';
163 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
164 'page_is_redirect', 'page_latest' );
165 $conds = $this->constructSet( 'page', $dbr );
166
167 // Do query
168 $caller = __METHOD__;
169 if ( strval( $this->caller ) !== '' ) {
170 $caller .= " (for {$this->caller})";
171 }
172 $res = $dbr->select( $table, $fields, $conds, $caller );
173 wfProfileOut( __METHOD__ );
174 return $res;
175 }
176
177 /**
178 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
179 *
180 * @return bool whether the query was successful
181 */
182 public function doGenderQuery() {
183 if ( $this->isEmpty() ) {
184 return false;
185 }
186
187 global $wgContLang;
188 if ( !$wgContLang->needsGenderDistinction() ) {
189 return false;
190 }
191
192 $genderCache = GenderCache::singleton();
193 $genderCache->dolinkBatch( $this->data, $this->caller );
194 return true;
195 }
196
197 /**
198 * Construct a WHERE clause which will match all the given titles.
199 *
200 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
201 * @param $db DatabaseBase object to use
202 * @return mixed string with SQL where clause fragment, or false if no items.
203 */
204 public function constructSet( $prefix, $db ) {
205 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
206 }
207 }