]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/LinkBatch.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / 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         public function setCaller( $caller ) {
32                 $this->caller = $caller;
33         }
34
35         public function addObj( $title ) {
36                 if ( is_object( $title ) ) {
37                         $this->add( $title->getNamespace(), $title->getDBkey() );
38                 } else {
39                         wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
40                 }
41         }
42
43         public function add( $ns, $dbkey ) {
44                 if ( $ns < 0 ) {
45                         return;
46                 }
47                 if ( !array_key_exists( $ns, $this->data ) ) {
48                         $this->data[$ns] = array();
49                 }
50
51                 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
52         }
53
54         /**
55          * Set the link list to a given 2-d array
56          * First key is the namespace, second is the DB key, value arbitrary
57          */
58         public function setArray( $array ) {
59                 $this->data = $array;
60         }
61
62         /**
63          * Returns true if no pages have been added, false otherwise.
64          */
65         public function isEmpty() {
66                 return ($this->getSize() == 0);
67         }
68
69         /**
70          * Returns the size of the batch.
71          */
72         public function getSize() {
73                 return count( $this->data );
74         }
75
76         /**
77          * Do the query and add the results to the LinkCache object
78          * Return an array mapping PDBK to ID
79          */
80          public function execute() {
81                 $linkCache = LinkCache::singleton();
82                 return $this->executeInto( $linkCache );
83          }
84
85         /**
86          * Do the query and add the results to a given LinkCache object
87          * Return an array mapping PDBK to ID
88          */
89         protected function executeInto( &$cache ) {
90                 wfProfileIn( __METHOD__ );
91                 $res = $this->doQuery();
92                 $ids = $this->addResultToCache( $cache, $res );
93                 wfProfileOut( __METHOD__ );
94                 return $ids;
95         }
96
97         /**
98          * Add a ResultWrapper containing IDs and titles to a LinkCache object.
99          * As normal, titles will go into the static Title cache field.
100          * This function *also* stores extra fields of the title used for link
101          * parsing to avoid extra DB queries.
102          */
103         public function addResultToCache( $cache, $res ) {
104                 if ( !$res ) {
105                         return array();
106                 }
107
108                 // For each returned entry, add it to the list of good links, and remove it from $remaining
109
110                 $ids = array();
111                 $remaining = $this->data;
112                 foreach ( $res as $row ) {
113                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
114                         $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
115                         $ids[$title->getPrefixedDBkey()] = $row->page_id;
116                         unset( $remaining[$row->page_namespace][$row->page_title] );
117                 }
118
119                 // The remaining links in $data are bad links, register them as such
120                 foreach ( $remaining as $ns => $dbkeys ) {
121                         foreach ( $dbkeys as $dbkey => $unused ) {
122                                 $title = Title::makeTitle( $ns, $dbkey );
123                                 $cache->addBadLinkObj( $title );
124                                 $ids[$title->getPrefixedDBkey()] = 0;
125                         }
126                 }
127                 return $ids;
128         }
129
130         /**
131          * Perform the existence test query, return a ResultWrapper with page_id fields
132          */
133         public function doQuery() {
134                 if ( $this->isEmpty() ) {
135                         return false;
136                 }
137                 wfProfileIn( __METHOD__ );
138
139                 // This is similar to LinkHolderArray::replaceInternal
140                 $dbr = wfGetDB( DB_SLAVE );
141                 $table = 'page';
142                 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
143                         'page_is_redirect', 'page_latest' );
144                 $conds = $this->constructSet( 'page', $dbr );
145
146                 // Do query
147                 $caller = __METHOD__;
148                 if ( strval( $this->caller ) !== '' ) {
149                         $caller .= " (for {$this->caller})";
150                 }
151                 $res = $dbr->select( $table, $fields, $conds, $caller );
152                 wfProfileOut( __METHOD__ );
153                 return $res;
154         }
155
156         /**
157          * Construct a WHERE clause which will match all the given titles.
158          *
159          * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
160          * @param $db DatabaseBase object to use
161          * @return mixed string with SQL where clause fragment, or false if no items.
162          */
163         public function constructSet( $prefix, $db ) {
164                 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
165         }
166 }