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