4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
16 * For debugging which method is using this class.
20 function __construct( $arr = array() ) {
21 foreach( $arr as $item ) {
22 $this->addObj( $item );
27 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
28 * class. Only used in debugging output.
33 public function setCaller( $caller ) {
34 $this->caller
= $caller;
40 public function addObj( $title ) {
41 if ( is_object( $title ) ) {
42 $this->add( $title->getNamespace(), $title->getDBkey() );
44 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
48 public function add( $ns, $dbkey ) {
52 if ( !array_key_exists( $ns, $this->data
) ) {
53 $this->data
[$ns] = array();
56 $this->data
[$ns][str_replace( ' ', '_', $dbkey )] = 1;
60 * Set the link list to a given 2-d array
61 * First key is the namespace, second is the DB key, value arbitrary
65 public function setArray( $array ) {
70 * Returns true if no pages have been added, false otherwise.
74 public function isEmpty() {
75 return ($this->getSize() == 0);
79 * Returns the size of the batch.
83 public function getSize() {
84 return count( $this->data
);
88 * Do the query and add the results to the LinkCache object
89 * Return an array mapping PDBK to ID
91 public function execute() {
92 $linkCache = LinkCache
::singleton();
93 return $this->executeInto( $linkCache );
97 * Do the query and add the results to a given LinkCache object
98 * Return an array mapping PDBK to ID
100 protected function executeInto( &$cache ) {
101 wfProfileIn( __METHOD__
);
102 $res = $this->doQuery();
103 $ids = $this->addResultToCache( $cache, $res );
104 $this->doGenderQuery();
105 wfProfileOut( __METHOD__
);
110 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
111 * As normal, titles will go into the static Title cache field.
112 * This function *also* stores extra fields of the title used for link
113 * parsing to avoid extra DB queries.
118 public function addResultToCache( $cache, $res ) {
123 // For each returned entry, add it to the list of good links, and remove it from $remaining
126 $remaining = $this->data
;
127 foreach ( $res as $row ) {
128 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
129 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
130 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
131 unset( $remaining[$row->page_namespace
][$row->page_title
] );
134 // The remaining links in $data are bad links, register them as such
135 foreach ( $remaining as $ns => $dbkeys ) {
136 foreach ( $dbkeys as $dbkey => $unused ) {
137 $title = Title
::makeTitle( $ns, $dbkey );
138 $cache->addBadLinkObj( $title );
139 $ids[$title->getPrefixedDBkey()] = 0;
146 * Perform the existence test query, return a ResultWrapper with page_id fields
148 public function doQuery() {
149 if ( $this->isEmpty() ) {
152 wfProfileIn( __METHOD__
);
154 // This is similar to LinkHolderArray::replaceInternal
155 $dbr = wfGetDB( DB_SLAVE
);
157 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
158 'page_is_redirect', 'page_latest' );
159 $conds = $this->constructSet( 'page', $dbr );
162 $caller = __METHOD__
;
163 if ( strval( $this->caller
) !== '' ) {
164 $caller .= " (for {$this->caller})";
166 $res = $dbr->select( $table, $fields, $conds, $caller );
167 wfProfileOut( __METHOD__
);
171 public function doGenderQuery() {
172 if ( $this->isEmpty() ) {
177 if ( !$wgContLang->needsGenderDistinction() ) {
181 $genderCache = GenderCache
::singleton();
182 $genderCache->dolinkBatch( $this->data
, $this->caller
);
186 * Construct a WHERE clause which will match all the given titles.
188 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
189 * @param $db DatabaseBase object to use
190 * @return mixed string with SQL where clause fragment, or false if no items.
192 public function constructSet( $prefix, $db ) {
193 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );