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.
31 public function setCaller( $caller ) {
32 $this->caller
= $caller;
39 public function addObj( $title ) {
40 if ( is_object( $title ) ) {
41 $this->add( $title->getNamespace(), $title->getDBkey() );
43 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
47 public function add( $ns, $dbkey ) {
51 if ( !array_key_exists( $ns, $this->data
) ) {
52 $this->data
[$ns] = array();
55 $this->data
[$ns][str_replace( ' ', '_', $dbkey )] = 1;
59 * Set the link list to a given 2-d array
60 * First key is the namespace, second is the DB key, value arbitrary
62 public function setArray( $array ) {
67 * Returns true if no pages have been added, false otherwise.
69 public function isEmpty() {
70 return ($this->getSize() == 0);
74 * Returns the size of the batch.
76 public function getSize() {
77 return count( $this->data
);
81 * Do the query and add the results to the LinkCache object
82 * Return an array mapping PDBK to ID
84 public function execute() {
85 $linkCache = LinkCache
::singleton();
86 return $this->executeInto( $linkCache );
90 * Do the query and add the results to a given LinkCache object
91 * Return an array mapping PDBK to ID
93 protected function executeInto( &$cache ) {
94 wfProfileIn( __METHOD__
);
95 $res = $this->doQuery();
96 $ids = $this->addResultToCache( $cache, $res );
97 wfProfileOut( __METHOD__
);
102 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
103 * As normal, titles will go into the static Title cache field.
104 * This function *also* stores extra fields of the title used for link
105 * parsing to avoid extra DB queries.
107 public function addResultToCache( $cache, $res ) {
112 // For each returned entry, add it to the list of good links, and remove it from $remaining
115 $remaining = $this->data
;
116 foreach ( $res as $row ) {
117 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
118 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
119 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
120 unset( $remaining[$row->page_namespace
][$row->page_title
] );
123 // The remaining links in $data are bad links, register them as such
124 foreach ( $remaining as $ns => $dbkeys ) {
125 foreach ( $dbkeys as $dbkey => $unused ) {
126 $title = Title
::makeTitle( $ns, $dbkey );
127 $cache->addBadLinkObj( $title );
128 $ids[$title->getPrefixedDBkey()] = 0;
135 * Perform the existence test query, return a ResultWrapper with page_id fields
137 public function doQuery() {
138 if ( $this->isEmpty() ) {
141 wfProfileIn( __METHOD__
);
143 // This is similar to LinkHolderArray::replaceInternal
144 $dbr = wfGetDB( DB_SLAVE
);
146 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
147 'page_is_redirect', 'page_latest' );
148 $conds = $this->constructSet( 'page', $dbr );
151 $caller = __METHOD__
;
152 if ( strval( $this->caller
) !== '' ) {
153 $caller .= " (for {$this->caller})";
155 $res = $dbr->select( $table, $fields, $conds, $caller );
156 wfProfileOut( __METHOD__
);
161 * Construct a WHERE clause which will match all the given titles.
163 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
164 * @param $db DatabaseBase object to use
165 * @return mixed string with SQL where clause fragment, or false if no items.
167 public function constructSet( $prefix, $db ) {
168 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );