Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.79% |
80 / 351 |
|
14.63% |
6 / 41 |
CRAP | |
0.00% |
0 / 1 |
| QueryPage | |
22.86% |
80 / 350 |
|
14.63% |
6 / 41 |
5870.68 | |
0.00% |
0 / 1 |
| getPages | |
7.69% |
3 / 39 |
|
0.00% |
0 / 1 |
5.15 | |||
| setLinkBatchFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLinkBatchFactory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getHttpRequestFactory | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getDisabledQueryPages | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| setListoutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getQueryInfo | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getOrderFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| usesTimestamps | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sortDescending | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isExpensive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isCacheable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isCached | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isSyndicated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| usesExternalSource | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| formatResult | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getPageHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| showEmptyText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| linkParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| recache | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
72 | |||
| getRecacheDB | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| deleteAllCachedData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| reallyDoQuery | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| reallyDoQueryInternal | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
11 | |||
| reallyDoQueryExternal | |
92.31% |
36 / 39 |
|
0.00% |
0 / 1 |
11.06 | |||
| doQuery | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| fetchFromCache | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| getCacheOrderFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCachedTimestamp | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getLimitOffset | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getDBLimit | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getMaxResults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 68 |
|
0.00% |
0 / 1 |
240 | |||
| outputResults | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
90 | |||
| openList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| closeList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| preprocessResults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| executeLBFromResultWrapper | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| setDBLoadBalancer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDBLoadBalancer | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setDatabaseProvider | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDatabaseProvider | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Base code for "query" special pages. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup SpecialPage |
| 8 | */ |
| 9 | |
| 10 | namespace MediaWiki\SpecialPage; |
| 11 | |
| 12 | use Exception; |
| 13 | use MediaWiki\Cache\LinkBatchFactory; |
| 14 | use MediaWiki\Config\Config; |
| 15 | use MediaWiki\HookContainer\HookRunner; |
| 16 | use MediaWiki\Html\Html; |
| 17 | use MediaWiki\Http\HttpRequestFactory; |
| 18 | use MediaWiki\Linker\LinkTarget; |
| 19 | use MediaWiki\MainConfigNames; |
| 20 | use MediaWiki\MediaWikiServices; |
| 21 | use MediaWiki\Output\OutputPage; |
| 22 | use MediaWiki\Skin\Skin; |
| 23 | use MediaWiki\Specials\SpecialAncientPages; |
| 24 | use MediaWiki\Specials\SpecialBrokenRedirects; |
| 25 | use MediaWiki\Specials\SpecialDeadendPages; |
| 26 | use MediaWiki\Specials\SpecialDoubleRedirects; |
| 27 | use MediaWiki\Specials\SpecialFewestRevisions; |
| 28 | use MediaWiki\Specials\SpecialLinkSearch; |
| 29 | use MediaWiki\Specials\SpecialListDuplicatedFiles; |
| 30 | use MediaWiki\Specials\SpecialListRedirects; |
| 31 | use MediaWiki\Specials\SpecialLonelyPages; |
| 32 | use MediaWiki\Specials\SpecialLongPages; |
| 33 | use MediaWiki\Specials\SpecialMediaStatistics; |
| 34 | use MediaWiki\Specials\SpecialMIMESearch; |
| 35 | use MediaWiki\Specials\SpecialMostCategories; |
| 36 | use MediaWiki\Specials\SpecialMostImages; |
| 37 | use MediaWiki\Specials\SpecialMostInterwikis; |
| 38 | use MediaWiki\Specials\SpecialMostLinked; |
| 39 | use MediaWiki\Specials\SpecialMostLinkedCategories; |
| 40 | use MediaWiki\Specials\SpecialMostLinkedTemplates; |
| 41 | use MediaWiki\Specials\SpecialMostRevisions; |
| 42 | use MediaWiki\Specials\SpecialShortPages; |
| 43 | use MediaWiki\Specials\SpecialUncategorizedCategories; |
| 44 | use MediaWiki\Specials\SpecialUncategorizedImages; |
| 45 | use MediaWiki\Specials\SpecialUncategorizedPages; |
| 46 | use MediaWiki\Specials\SpecialUncategorizedTemplates; |
| 47 | use MediaWiki\Specials\SpecialUnusedCategories; |
| 48 | use MediaWiki\Specials\SpecialUnusedImages; |
| 49 | use MediaWiki\Specials\SpecialUnusedTemplates; |
| 50 | use MediaWiki\Specials\SpecialUnwatchedPages; |
| 51 | use MediaWiki\Specials\SpecialWantedCategories; |
| 52 | use MediaWiki\Specials\SpecialWantedFiles; |
| 53 | use MediaWiki\Specials\SpecialWantedPages; |
| 54 | use MediaWiki\Specials\SpecialWantedTemplates; |
| 55 | use MediaWiki\Specials\SpecialWithoutInterwiki; |
| 56 | use RuntimeException; |
| 57 | use stdClass; |
| 58 | use Wikimedia\Rdbms\DBError; |
| 59 | use Wikimedia\Rdbms\FakeResultWrapper; |
| 60 | use Wikimedia\Rdbms\IConnectionProvider; |
| 61 | use Wikimedia\Rdbms\IDatabase; |
| 62 | use Wikimedia\Rdbms\ILoadBalancer; |
| 63 | use Wikimedia\Rdbms\IReadableDatabase; |
| 64 | use Wikimedia\Rdbms\IResultWrapper; |
| 65 | use Wikimedia\Rdbms\SelectQueryBuilder; |
| 66 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 67 | |
| 68 | /** |
| 69 | * This is a class for doing query pages; since they're almost all the same, |
| 70 | * we factor out some of the functionality into a superclass, and let |
| 71 | * subclasses derive from it. |
| 72 | * |
| 73 | * @stable to extend |
| 74 | * |
| 75 | * @ingroup SpecialPage |
| 76 | */ |
| 77 | abstract class QueryPage extends SpecialPage { |
| 78 | /** @var bool Whether or not we want plain listoutput rather than an ordered list */ |
| 79 | protected $listoutput = false; |
| 80 | |
| 81 | /** @var int The offset and limit in use, as passed to the query() function */ |
| 82 | protected $offset = 0; |
| 83 | |
| 84 | /** @var int */ |
| 85 | protected $limit = 0; |
| 86 | |
| 87 | /** |
| 88 | * The number of rows returned by the query. Reading this variable |
| 89 | * only makes sense in functions that are run after the query has been |
| 90 | * done, such as preprocessResults() and formatRow(). |
| 91 | * |
| 92 | * @var int |
| 93 | */ |
| 94 | protected $numRows; |
| 95 | |
| 96 | /** |
| 97 | * @var string|null|false |
| 98 | */ |
| 99 | protected $cachedTimestamp = null; |
| 100 | |
| 101 | /** |
| 102 | * @var bool Whether to show prev/next links |
| 103 | */ |
| 104 | protected $shownavigation = true; |
| 105 | |
| 106 | /** @var ILoadBalancer|null */ |
| 107 | private $loadBalancer = null; |
| 108 | |
| 109 | /** @var IConnectionProvider|null */ |
| 110 | private $databaseProvider = null; |
| 111 | |
| 112 | /** @var LinkBatchFactory|null */ |
| 113 | private $linkBatchFactory = null; |
| 114 | |
| 115 | /** @var HttpRequestFactory|null */ |
| 116 | private $httpRequestFactory = null; |
| 117 | |
| 118 | /** |
| 119 | * Get a list of query page classes and their associated special pages, |
| 120 | * for periodic updates. |
| 121 | * |
| 122 | * When changing this list, you should ensure that maintenance/updateSpecialPages.php still works |
| 123 | * including when test data exists. |
| 124 | * |
| 125 | * @return array[] List of [ class-string $class, string $specialPageName, ?int $limit (optional) ]. |
| 126 | * Limit defaults to $wgQueryCacheLimit if not given. |
| 127 | */ |
| 128 | public static function getPages() { |
| 129 | static $qp = null; |
| 130 | |
| 131 | if ( $qp === null ) { |
| 132 | $qp = [ |
| 133 | [ SpecialAncientPages::class, 'Ancientpages' ], |
| 134 | [ SpecialBrokenRedirects::class, 'BrokenRedirects' ], |
| 135 | [ SpecialDeadendPages::class, 'Deadendpages' ], |
| 136 | [ SpecialDoubleRedirects::class, 'DoubleRedirects' ], |
| 137 | [ SpecialListDuplicatedFiles::class, 'ListDuplicatedFiles' ], |
| 138 | [ SpecialLinkSearch::class, 'LinkSearch' ], |
| 139 | [ SpecialListRedirects::class, 'Listredirects' ], |
| 140 | [ SpecialLonelyPages::class, 'Lonelypages' ], |
| 141 | [ SpecialLongPages::class, 'Longpages' ], |
| 142 | [ SpecialMediaStatistics::class, 'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ], |
| 143 | [ SpecialMIMESearch::class, 'MIMEsearch' ], |
| 144 | [ SpecialMostCategories::class, 'Mostcategories' ], |
| 145 | [ SpecialMostImages::class, 'Mostimages' ], |
| 146 | [ SpecialMostInterwikis::class, 'Mostinterwikis' ], |
| 147 | [ SpecialMostLinkedCategories::class, 'Mostlinkedcategories' ], |
| 148 | [ SpecialMostLinkedTemplates::class, 'Mostlinkedtemplates' ], |
| 149 | [ SpecialMostLinked::class, 'Mostlinked' ], |
| 150 | [ SpecialMostRevisions::class, 'Mostrevisions' ], |
| 151 | [ SpecialFewestRevisions::class, 'Fewestrevisions' ], |
| 152 | [ SpecialShortPages::class, 'Shortpages' ], |
| 153 | [ SpecialUncategorizedCategories::class, 'Uncategorizedcategories' ], |
| 154 | [ SpecialUncategorizedPages::class, 'Uncategorizedpages' ], |
| 155 | [ SpecialUncategorizedImages::class, 'Uncategorizedimages' ], |
| 156 | [ SpecialUncategorizedTemplates::class, 'Uncategorizedtemplates' ], |
| 157 | [ SpecialUnusedCategories::class, 'Unusedcategories' ], |
| 158 | [ SpecialUnusedImages::class, 'Unusedimages' ], |
| 159 | [ SpecialWantedCategories::class, 'Wantedcategories' ], |
| 160 | [ SpecialWantedFiles::class, 'Wantedfiles' ], |
| 161 | [ SpecialWantedPages::class, 'Wantedpages' ], |
| 162 | [ SpecialWantedTemplates::class, 'Wantedtemplates' ], |
| 163 | [ SpecialUnwatchedPages::class, 'Unwatchedpages' ], |
| 164 | [ SpecialUnusedTemplates::class, 'Unusedtemplates' ], |
| 165 | [ SpecialWithoutInterwiki::class, 'Withoutinterwiki' ], |
| 166 | ]; |
| 167 | ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onWgQueryPages( $qp ); |
| 168 | } |
| 169 | |
| 170 | return $qp; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @since 1.36 |
| 175 | * @param LinkBatchFactory $linkBatchFactory |
| 176 | */ |
| 177 | final protected function setLinkBatchFactory( LinkBatchFactory $linkBatchFactory ) { |
| 178 | $this->linkBatchFactory = $linkBatchFactory; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @since 1.36 |
| 183 | * @return LinkBatchFactory |
| 184 | */ |
| 185 | final protected function getLinkBatchFactory(): LinkBatchFactory { |
| 186 | if ( $this->linkBatchFactory === null ) { |
| 187 | // Fallback if not provided |
| 188 | // TODO Change to wfWarn in a future release |
| 189 | $this->linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); |
| 190 | } |
| 191 | return $this->linkBatchFactory; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @return HttpRequestFactory |
| 196 | */ |
| 197 | private function getHttpRequestFactory(): HttpRequestFactory { |
| 198 | if ( $this->httpRequestFactory === null ) { |
| 199 | // Fallback if not provided |
| 200 | // TODO Do not rely on global state |
| 201 | $this->httpRequestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory(); |
| 202 | } |
| 203 | return $this->httpRequestFactory; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get a list of disabled query pages and their run mode |
| 208 | * @param Config $config |
| 209 | * @return string[] |
| 210 | */ |
| 211 | public static function getDisabledQueryPages( Config $config ) { |
| 212 | $disableQueryPageUpdate = $config->get( MainConfigNames::DisableQueryPageUpdate ); |
| 213 | |
| 214 | if ( !is_array( $disableQueryPageUpdate ) ) { |
| 215 | return []; |
| 216 | } |
| 217 | |
| 218 | $pages = []; |
| 219 | foreach ( $disableQueryPageUpdate as $name => $runMode ) { |
| 220 | if ( is_int( $name ) ) { |
| 221 | // The run mode may be omitted |
| 222 | $pages[$runMode] = 'disabled'; |
| 223 | } else { |
| 224 | $pages[$name] = $runMode; |
| 225 | } |
| 226 | } |
| 227 | return $pages; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * A mutator for $this->listoutput; |
| 232 | * |
| 233 | * @param bool $bool |
| 234 | */ |
| 235 | protected function setListoutput( $bool ) { |
| 236 | $this->listoutput = $bool; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Subclasses return an SQL query here, formatted as an array with the |
| 241 | * following keys: |
| 242 | * tables => Table(s) for passing to Database::select() |
| 243 | * fields => Field(s) for passing to Database::select(), may be * |
| 244 | * conds => WHERE conditions |
| 245 | * options => options |
| 246 | * join_conds => JOIN conditions |
| 247 | * |
| 248 | * Note that the query itself should return the following three columns: |
| 249 | * 'namespace', 'title', and 'value'. 'value' is used for sorting. |
| 250 | * |
| 251 | * These may be stored in the querycache table for expensive queries, |
| 252 | * and that cached data will be returned sometimes, so the presence of |
| 253 | * extra fields can't be relied upon. The cached 'value' column will be |
| 254 | * an integer; non-numeric values are useful only for sorting the |
| 255 | * initial query (except if they're timestamps, see usesTimestamps()). |
| 256 | * |
| 257 | * Don't include an ORDER or LIMIT clause, they will be added. |
| 258 | * |
| 259 | * @return array |
| 260 | * @since 1.18, abstract since 1.43 |
| 261 | */ |
| 262 | abstract public function getQueryInfo(); |
| 263 | |
| 264 | /** |
| 265 | * Subclasses return an array of fields to order by here. Don't append |
| 266 | * DESC to the field names, that'll be done automatically if |
| 267 | * sortDescending() returns true. |
| 268 | * @stable to override |
| 269 | * @return string[] |
| 270 | * @since 1.18 |
| 271 | */ |
| 272 | protected function getOrderFields() { |
| 273 | return [ 'value' ]; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Does this query return timestamps rather than integers in its |
| 278 | * 'value' field? If true, this class will convert 'value' to a |
| 279 | * UNIX timestamp for caching. |
| 280 | * NOTE: formatRow() may get timestamps in TS::MW (mysql), TS::DB (pgsql) |
| 281 | * or TS::UNIX (querycache) format, so be sure to always run them |
| 282 | * through wfTimestamp() |
| 283 | * @stable to override |
| 284 | * @return bool |
| 285 | * @since 1.18 |
| 286 | */ |
| 287 | public function usesTimestamps() { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Override to sort by increasing values |
| 293 | * |
| 294 | * @stable to override |
| 295 | * @return bool |
| 296 | */ |
| 297 | protected function sortDescending() { |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Should this query page only be updated offline on large wikis? |
| 303 | * |
| 304 | * If the query for this page is considered too expensive to run on-demand |
| 305 | * for large wikis (e.g. every time the special page is viewed), then |
| 306 | * implement this as returning true. |
| 307 | * |
| 308 | * "Large wikis" are those that enable $wgMiserMode. The value of |
| 309 | * ::isExpensive() has no effect by default when $wgMiserMode is off. |
| 310 | * |
| 311 | * It is expected that such large wikis, periodically run the |
| 312 | * UpdateSpecialPages maintenance script to update these query pages. |
| 313 | * |
| 314 | * By enabling $wgDisableQueryPages, all query pages will be considered |
| 315 | * as expensive and updated offline only, even query pages that do not |
| 316 | * mark themselves as expensive. |
| 317 | * |
| 318 | * @stable to override |
| 319 | * @return bool |
| 320 | */ |
| 321 | public function isExpensive() { |
| 322 | return $this->getConfig()->get( MainConfigNames::DisableQueryPages ); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Is the output of this query cacheable? Non-cacheable expensive pages |
| 327 | * will be disabled in miser mode and will not have their results written |
| 328 | * to the querycache table. |
| 329 | * @stable to override |
| 330 | * @return bool |
| 331 | * @since 1.18 |
| 332 | */ |
| 333 | public function isCacheable() { |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Whether or not the output of the page in question is retrieved from |
| 339 | * the database cache. |
| 340 | * |
| 341 | * @stable to override |
| 342 | * @return bool |
| 343 | */ |
| 344 | public function isCached() { |
| 345 | return $this->isExpensive() && $this->getConfig()->get( MainConfigNames::MiserMode ); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Sometimes we don't want to build rss / atom feeds. |
| 350 | * |
| 351 | * @stable to override |
| 352 | * @return bool |
| 353 | */ |
| 354 | public function isSyndicated() { |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Check if this query page is configured to fetch data from an external source via HTTP. |
| 360 | * |
| 361 | * @since 1.45 |
| 362 | * @stable to override |
| 363 | * @return bool True if configured to use an external data source, false otherwise |
| 364 | */ |
| 365 | public function usesExternalSource(): bool { |
| 366 | $config = $this->getConfig(); |
| 367 | $externalSources = $config->get( MainConfigNames::ExternalQuerySources ); |
| 368 | $pageName = $this->getName(); |
| 369 | |
| 370 | return !empty( $externalSources[$pageName]['enabled'] ) && |
| 371 | !empty( $externalSources[$pageName]['url'] ); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Formats the results of the query for display. The skin is the current |
| 376 | * skin; you can use it for making links. The result is a single row of |
| 377 | * result data. You should be able to grab SQL results from it. |
| 378 | * If the function returns false, the line output will be skipped. |
| 379 | * @param Skin $skin |
| 380 | * @param stdClass $result Result row |
| 381 | * @return string|bool String or false to skip |
| 382 | */ |
| 383 | abstract protected function formatResult( $skin, $result ); |
| 384 | |
| 385 | /** |
| 386 | * The content returned by this function will be output before any result |
| 387 | * |
| 388 | * @stable to override |
| 389 | * @return string |
| 390 | */ |
| 391 | protected function getPageHeader() { |
| 392 | return ''; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Outputs some kind of an informative message (via OutputPage) to let the |
| 397 | * user know that the query returned nothing and thus there's nothing to |
| 398 | * show. |
| 399 | * |
| 400 | * @since 1.26 |
| 401 | */ |
| 402 | protected function showEmptyText() { |
| 403 | $this->getOutput()->addWikiMsg( 'specialpage-empty' ); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * If using extra form wheely-dealies, return a set of parameters here |
| 408 | * as an associative array. They will be encoded and added to the paging |
| 409 | * links (prev/next/lengths). |
| 410 | * |
| 411 | * @stable to override |
| 412 | * @return array |
| 413 | */ |
| 414 | protected function linkParameters() { |
| 415 | return []; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Clear the cache and save new results |
| 420 | * |
| 421 | * @stable to override |
| 422 | * |
| 423 | * @param int|false $limit Limit for SQL statement or false for no limit |
| 424 | * @param bool $unused Unused since 1.43, kept for backwards-compatibility |
| 425 | * @throws DBError|Exception |
| 426 | * @return bool|int |
| 427 | */ |
| 428 | public function recache( $limit, $unused = true ) { |
| 429 | if ( !$this->isCacheable() ) { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | $fname = static::class . '::recache'; |
| 434 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 435 | |
| 436 | // Do query |
| 437 | $res = $this->reallyDoQuery( $limit, false ); |
| 438 | $num = false; |
| 439 | if ( $res ) { |
| 440 | $num = $res->numRows(); |
| 441 | // Fetch results |
| 442 | $vals = []; |
| 443 | foreach ( $res as $i => $row ) { |
| 444 | if ( isset( $row->value ) ) { |
| 445 | if ( $this->usesTimestamps() ) { |
| 446 | $value = (int)wfTimestamp( TS::UNIX, $row->value ); |
| 447 | } else { |
| 448 | $value = intval( $row->value ); // T16414 |
| 449 | } |
| 450 | } else { |
| 451 | $value = $i; |
| 452 | } |
| 453 | |
| 454 | $vals[] = [ |
| 455 | 'qc_type' => $this->getName(), |
| 456 | 'qc_namespace' => $row->namespace, |
| 457 | 'qc_title' => $row->title, |
| 458 | 'qc_value' => $value |
| 459 | ]; |
| 460 | } |
| 461 | |
| 462 | $dbw->doAtomicSection( |
| 463 | $fname, |
| 464 | function ( IDatabase $dbw, $fname ) { |
| 465 | // Clear out any old cached data |
| 466 | $dbw->newDeleteQueryBuilder() |
| 467 | ->deleteFrom( 'querycache' ) |
| 468 | ->where( [ 'qc_type' => $this->getName() ] ) |
| 469 | ->caller( $fname )->execute(); |
| 470 | // Update the querycache_info record for the page |
| 471 | $dbw->newInsertQueryBuilder() |
| 472 | ->insertInto( 'querycache_info' ) |
| 473 | ->row( [ 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ] ) |
| 474 | ->onDuplicateKeyUpdate() |
| 475 | ->uniqueIndexFields( [ 'qci_type' ] ) |
| 476 | ->set( [ 'qci_timestamp' => $dbw->timestamp() ] ) |
| 477 | ->caller( $fname )->execute(); |
| 478 | } |
| 479 | ); |
| 480 | // Save results into the querycache table on the primary DB |
| 481 | if ( count( $vals ) ) { |
| 482 | foreach ( array_chunk( $vals, 500 ) as $chunk ) { |
| 483 | $dbw->newInsertQueryBuilder() |
| 484 | ->insertInto( 'querycache' ) |
| 485 | ->rows( $chunk ) |
| 486 | ->caller( $fname )->execute(); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return $num; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Get a DB connection to be used for slow recache queries |
| 496 | * @stable to override |
| 497 | * @return IReadableDatabase |
| 498 | */ |
| 499 | protected function getRecacheDB() { |
| 500 | return $this->getDatabaseProvider()->getReplicaDatabase( false, 'vslow' ); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Remove a cached result. |
| 505 | * Useful for interactive backlogs where the user can fix problems in-place. |
| 506 | * @param LinkTarget $title The page to remove. |
| 507 | * @since 1.34 |
| 508 | */ |
| 509 | public function delete( LinkTarget $title ) { |
| 510 | if ( $this->isCached() ) { |
| 511 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 512 | $dbw->newDeleteQueryBuilder() |
| 513 | ->deleteFrom( 'querycache' ) |
| 514 | ->where( [ |
| 515 | 'qc_type' => $this->getName(), |
| 516 | 'qc_namespace' => $title->getNamespace(), |
| 517 | 'qc_title' => $title->getDBkey(), |
| 518 | ] ) |
| 519 | ->caller( __METHOD__ )->execute(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Remove all cached value |
| 525 | * This is needed when the page is no longer using the cache |
| 526 | * @since 1.36 |
| 527 | */ |
| 528 | public function deleteAllCachedData() { |
| 529 | $fname = static::class . '::' . __FUNCTION__; |
| 530 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 531 | $dbw->newDeleteQueryBuilder() |
| 532 | ->deleteFrom( 'querycache' ) |
| 533 | ->where( [ 'qc_type' => $this->getName() ] ) |
| 534 | ->caller( $fname )->execute(); |
| 535 | $dbw->newDeleteQueryBuilder() |
| 536 | ->deleteFrom( 'querycachetwo' ) |
| 537 | ->where( [ 'qcc_type' => $this->getName() ] ) |
| 538 | ->caller( $fname )->execute(); |
| 539 | $dbw->newDeleteQueryBuilder() |
| 540 | ->deleteFrom( 'querycache_info' ) |
| 541 | ->where( [ 'qci_type' => $this->getName() ] ) |
| 542 | ->caller( $fname )->execute(); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Run the query and return the result |
| 547 | * @stable to override |
| 548 | * @param int|false $limit Numerical limit or false for no limit |
| 549 | * @param int|false $offset Numerical offset or false for no offset |
| 550 | * @return IResultWrapper |
| 551 | * @since 1.18 |
| 552 | */ |
| 553 | public function reallyDoQuery( $limit, $offset = false ) { |
| 554 | if ( $this->usesExternalSource() ) { |
| 555 | return $this->reallyDoQueryExternal( $limit, $offset ); |
| 556 | } |
| 557 | |
| 558 | return $this->reallyDoQueryInternal( $limit, $offset ); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Run the query and return the result |
| 563 | * |
| 564 | * @param int|false $limit Numerical limit or false for no limit |
| 565 | * @param int|false $offset Numerical offset or false for no offset |
| 566 | * @return IResultWrapper |
| 567 | */ |
| 568 | private function reallyDoQueryInternal( $limit, $offset = false ) { |
| 569 | $fname = static::class . '::reallyDoQueryInternal'; |
| 570 | $dbr = $this->getRecacheDB(); |
| 571 | $query = $this->getQueryInfo(); |
| 572 | $order = $this->getOrderFields(); |
| 573 | |
| 574 | if ( $this->sortDescending() ) { |
| 575 | foreach ( $order as &$field ) { |
| 576 | $field .= ' DESC'; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | $tables = isset( $query['tables'] ) ? (array)$query['tables'] : []; |
| 581 | $fields = isset( $query['fields'] ) ? (array)$query['fields'] : []; |
| 582 | $conds = isset( $query['conds'] ) ? (array)$query['conds'] : []; |
| 583 | $options = isset( $query['options'] ) ? (array)$query['options'] : []; |
| 584 | $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : []; |
| 585 | |
| 586 | $queryBuilder = $dbr->newSelectQueryBuilder() |
| 587 | ->tables( $tables ) |
| 588 | ->fields( $fields ) |
| 589 | ->conds( $conds ) |
| 590 | ->caller( $fname ) |
| 591 | ->options( $options ) |
| 592 | ->joinConds( $join_conds ); |
| 593 | if ( $order ) { |
| 594 | $queryBuilder->orderBy( $order ); |
| 595 | } |
| 596 | |
| 597 | if ( $limit !== false ) { |
| 598 | $queryBuilder->limit( intval( $limit ) ); |
| 599 | } |
| 600 | |
| 601 | if ( $offset !== false ) { |
| 602 | $queryBuilder->offset( intval( $offset ) ); |
| 603 | } |
| 604 | |
| 605 | return $queryBuilder->fetchResultSet(); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Run the query and return the result |
| 610 | * |
| 611 | * @param int|false $limit Numerical limit or false for no limit |
| 612 | * @param int|false $offset Numerical offset or false for no offset |
| 613 | * @return IResultWrapper |
| 614 | */ |
| 615 | private function reallyDoQueryExternal( $limit, $offset = false ) { |
| 616 | $fname = static::class . '::reallyDoQueryExternal'; |
| 617 | $httpRequestFactory = $this->getHttpRequestFactory(); |
| 618 | $externalSources = $this->getConfig()->get( MainConfigNames::ExternalQuerySources ); |
| 619 | $pageName = $this->getName(); |
| 620 | $config = $externalSources[$pageName]; |
| 621 | |
| 622 | $options = []; |
| 623 | if ( isset( $config['timeout'] ) ) { |
| 624 | $options['timeout'] = (int)$config['timeout']; |
| 625 | } |
| 626 | |
| 627 | $request = $httpRequestFactory->create( $config['url'], $options, $fname ); |
| 628 | |
| 629 | $status = $request->execute(); |
| 630 | if ( !$status->isOK() ) { |
| 631 | throw new RuntimeException( "Failed to fetch data from external source '{$pageName}': " . |
| 632 | $status->getMessage()->text() ); |
| 633 | } |
| 634 | |
| 635 | $content = $request->getContent(); |
| 636 | if ( $content === null || $content === '' ) { |
| 637 | throw new RuntimeException( "Empty response received from external source '{$pageName}'" ); |
| 638 | } |
| 639 | |
| 640 | $decoded = json_decode( $content, true ); |
| 641 | if ( $decoded === null ) { |
| 642 | throw new RuntimeException( "Invalid JSON response from external source '{$pageName}': " . |
| 643 | json_last_error_msg() ); |
| 644 | } |
| 645 | |
| 646 | if ( !is_array( $decoded ) ) { |
| 647 | throw new RuntimeException( "Expected array data from external source '{$pageName}', got " . |
| 648 | gettype( $decoded ) ); |
| 649 | } |
| 650 | |
| 651 | $result = []; |
| 652 | foreach ( $decoded as $i => $row ) { |
| 653 | if ( !is_array( $row ) ) { |
| 654 | throw new RuntimeException( "Invalid row data at index {$i} from external source '{$pageName}': " . |
| 655 | 'expected array, got ' . gettype( $row ) ); |
| 656 | } |
| 657 | |
| 658 | $requiredFields = [ 'qc_namespace', 'qc_title', 'qc_value' ]; |
| 659 | foreach ( $requiredFields as $field ) { |
| 660 | if ( !array_key_exists( $field, $row ) ) { |
| 661 | throw new RuntimeException( |
| 662 | "Missing required field '{$field}' in row {$i} from external source '{$pageName}'" ); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | $result[] = (object)[ |
| 667 | 'namespace' => $row['qc_namespace'], |
| 668 | 'title' => $row['qc_title'], |
| 669 | 'value' => $row['qc_value'] |
| 670 | ]; |
| 671 | } |
| 672 | |
| 673 | return new FakeResultWrapper( $result ); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Somewhat deprecated, you probably want to be using execute() |
| 678 | * @param int|false $offset |
| 679 | * @param int|false $limit |
| 680 | * @return IResultWrapper |
| 681 | */ |
| 682 | public function doQuery( $offset = false, $limit = false ) { |
| 683 | if ( $this->isCached() && $this->isCacheable() ) { |
| 684 | return $this->fetchFromCache( $limit, $offset ); |
| 685 | } else { |
| 686 | return $this->reallyDoQuery( $limit, $offset ); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Fetch the query results from the query cache |
| 692 | * @stable to override |
| 693 | * |
| 694 | * @param int|false $limit Numerical limit or false for no limit |
| 695 | * @param int|false $offset Numerical offset or false for no offset |
| 696 | * @return IResultWrapper |
| 697 | * @since 1.18 |
| 698 | */ |
| 699 | public function fetchFromCache( $limit, $offset = false ) { |
| 700 | $dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
| 701 | $queryBuilder = $dbr->newSelectQueryBuilder() |
| 702 | ->select( [ 'qc_type', 'namespace' => 'qc_namespace', 'title' => 'qc_title', 'value' => 'qc_value' ] ) |
| 703 | ->from( 'querycache' ) |
| 704 | ->where( [ 'qc_type' => $this->getName() ] ); |
| 705 | |
| 706 | if ( $limit !== false ) { |
| 707 | $queryBuilder->limit( intval( $limit ) ); |
| 708 | } |
| 709 | |
| 710 | if ( $offset !== false ) { |
| 711 | $queryBuilder->offset( intval( $offset ) ); |
| 712 | } |
| 713 | |
| 714 | $order = $this->getCacheOrderFields(); |
| 715 | if ( $this->sortDescending() ) { |
| 716 | $queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC ); |
| 717 | } else { |
| 718 | $queryBuilder->orderBy( $order ); |
| 719 | } |
| 720 | |
| 721 | return $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Return the order fields for fetchFromCache. Default is to always use |
| 726 | * "ORDER BY value" which was the default prior to this function. |
| 727 | * @stable to override |
| 728 | * @return array |
| 729 | * @since 1.29 |
| 730 | */ |
| 731 | protected function getCacheOrderFields() { |
| 732 | return [ 'value' ]; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * @return string|false |
| 737 | */ |
| 738 | public function getCachedTimestamp() { |
| 739 | if ( $this->cachedTimestamp === null ) { |
| 740 | $dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
| 741 | $fname = static::class . '::getCachedTimestamp'; |
| 742 | $this->cachedTimestamp = $dbr->newSelectQueryBuilder() |
| 743 | ->select( 'qci_timestamp' ) |
| 744 | ->from( 'querycache_info' ) |
| 745 | ->where( [ 'qci_type' => $this->getName() ] ) |
| 746 | ->caller( $fname )->fetchField(); |
| 747 | } |
| 748 | return $this->cachedTimestamp; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Returns limit and offset, as returned by $this->getRequest()->getLimitOffsetForUser(). |
| 753 | * Subclasses may override this to further restrict or modify limit and offset. |
| 754 | * |
| 755 | * @note Restricts the offset parameter, as most query pages have inefficient paging |
| 756 | * |
| 757 | * Its generally expected that the returned limit will not be 0, and the returned |
| 758 | * offset will be less than the max results. |
| 759 | * |
| 760 | * @since 1.26 |
| 761 | * @return int[] list( $limit, $offset ) |
| 762 | */ |
| 763 | protected function getLimitOffset() { |
| 764 | [ $limit, $offset ] = $this->getRequest() |
| 765 | ->getLimitOffsetForUser( $this->getUser() ); |
| 766 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 767 | $maxResults = $this->getMaxResults(); |
| 768 | // Can't display more than max results on a page |
| 769 | $limit = min( $limit, $maxResults ); |
| 770 | // Can't skip over more than the end of $maxResults |
| 771 | $offset = min( $offset, $maxResults + 1 ); |
| 772 | } |
| 773 | return [ $limit, $offset ]; |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * What is limit to fetch from DB |
| 778 | * |
| 779 | * Used to make it appear the DB stores less results then it actually does |
| 780 | * @param int $uiLimit Limit from UI |
| 781 | * @param int $uiOffset Offset from UI |
| 782 | * @return int Limit to use for DB (not including extra row to see if at end) |
| 783 | */ |
| 784 | protected function getDBLimit( $uiLimit, $uiOffset ) { |
| 785 | $maxResults = $this->getMaxResults(); |
| 786 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 787 | $limit = min( $uiLimit + 1, $maxResults - $uiOffset ); |
| 788 | return max( $limit, 0 ); |
| 789 | } else { |
| 790 | return $uiLimit + 1; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Get max number of results we can return in miser mode. |
| 796 | * |
| 797 | * Most QueryPage subclasses use inefficient paging, so limit the max amount we return |
| 798 | * This matters for uncached query pages that might otherwise accept an offset of 3 million |
| 799 | * |
| 800 | * @stable to override |
| 801 | * @since 1.27 |
| 802 | * @return int |
| 803 | */ |
| 804 | protected function getMaxResults() { |
| 805 | // Max of 10000, unless we store more than 10000 in query cache. |
| 806 | return max( $this->getConfig()->get( MainConfigNames::QueryCacheLimit ), 10000 ); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * This is the actual workhorse. It does everything needed to make a |
| 811 | * real, honest-to-gosh query page. |
| 812 | * @stable to override |
| 813 | * @param string|null $par |
| 814 | */ |
| 815 | public function execute( $par ) { |
| 816 | $this->checkPermissions(); |
| 817 | |
| 818 | $this->setHeaders(); |
| 819 | $this->outputHeader(); |
| 820 | |
| 821 | $out = $this->getOutput(); |
| 822 | |
| 823 | if ( $this->isCached() && !$this->isCacheable() ) { |
| 824 | $out->addWikiMsg( 'querypage-disabled' ); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | $out->setSyndicated( $this->isSyndicated() ); |
| 829 | |
| 830 | if ( $this->limit == 0 && $this->offset == 0 ) { |
| 831 | [ $this->limit, $this->offset ] = $this->getLimitOffset(); |
| 832 | } |
| 833 | $dbLimit = $this->getDBLimit( $this->limit, $this->offset ); |
| 834 | // @todo Use doQuery() |
| 835 | if ( !$this->isCached() ) { |
| 836 | // select one extra row for navigation |
| 837 | $res = $this->reallyDoQuery( $dbLimit, $this->offset ); |
| 838 | } else { |
| 839 | // Get the cached result, select one extra row for navigation |
| 840 | $res = $this->fetchFromCache( $dbLimit, $this->offset ); |
| 841 | if ( !$this->listoutput ) { |
| 842 | // Fetch the timestamp of this update |
| 843 | $ts = $this->getCachedTimestamp(); |
| 844 | $lang = $this->getLanguage(); |
| 845 | $maxResults = $lang->formatNum( $this->getConfig()->get( |
| 846 | MainConfigNames::QueryCacheLimit ) ); |
| 847 | |
| 848 | if ( $ts ) { |
| 849 | $user = $this->getUser(); |
| 850 | $updated = $lang->userTimeAndDate( $ts, $user ); |
| 851 | $updateddate = $lang->userDate( $ts, $user ); |
| 852 | $updatedtime = $lang->userTime( $ts, $user ); |
| 853 | $out->addMeta( 'Data-Cache-Time', $ts ); |
| 854 | $out->addJsConfigVars( 'dataCacheTime', $ts ); |
| 855 | $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults ); |
| 856 | } else { |
| 857 | $out->addWikiMsg( 'perfcached', $maxResults ); |
| 858 | } |
| 859 | |
| 860 | // If updates on this page have been disabled, let the user know |
| 861 | // that the data set won't be refreshed for now |
| 862 | $disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() ); |
| 863 | if ( isset( $disabledQueryPages[$this->getName()] ) ) { |
| 864 | $runMode = $disabledQueryPages[$this->getName()]; |
| 865 | if ( $runMode === 'disabled' ) { |
| 866 | $out->wrapWikiMsg( |
| 867 | "<div class=\"mw-querypage-no-updates\">\n$1\n</div>", |
| 868 | 'querypage-no-updates' |
| 869 | ); |
| 870 | } else { |
| 871 | // Messages used here: querypage-updates-periodical |
| 872 | $out->wrapWikiMsg( |
| 873 | "<div class=\"mw-querypage-updates-" . $runMode . "\">\n$1\n</div>", |
| 874 | 'querypage-updates-' . $runMode |
| 875 | ); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | $this->numRows = $res->numRows(); |
| 882 | |
| 883 | $dbr = $this->getRecacheDB(); |
| 884 | $this->preprocessResults( $dbr, $res ); |
| 885 | |
| 886 | $out->addHTML( Html::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) ); |
| 887 | |
| 888 | // Top header and navigation |
| 889 | if ( $this->shownavigation ) { |
| 890 | $out->addHTML( $this->getPageHeader() ); |
| 891 | if ( $this->numRows > 0 ) { |
| 892 | $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams( |
| 893 | min( $this->numRows, $this->limit ), // do not show the one extra row, if exist |
| 894 | $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() ); |
| 895 | // Disable the "next" link when we reach the end |
| 896 | $miserMaxResults = $this->getConfig()->get( MainConfigNames::MiserMode ) |
| 897 | && ( $this->offset + $this->limit >= $this->getMaxResults() ); |
| 898 | $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults; |
| 899 | $paging = $this->buildPrevNextNavigation( $this->offset, |
| 900 | $this->limit, $this->linkParameters(), $atEnd, $par ); |
| 901 | $out->addHTML( '<p>' . $paging . '</p>' ); |
| 902 | } else { |
| 903 | // No results to show, so don't bother with "showing X of Y" etc. |
| 904 | // -- just let the user know and give up now |
| 905 | $this->showEmptyText(); |
| 906 | $out->addHTML( Html::closeElement( 'div' ) ); |
| 907 | return; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // The actual results; specialist subclasses will want to handle this |
| 912 | // with more than a straight list, so we hand them the info, plus |
| 913 | // an OutputPage, and let them get on with it |
| 914 | $this->outputResults( $out, |
| 915 | $this->getSkin(), |
| 916 | $dbr, // Should use IResultWrapper for this |
| 917 | $res, |
| 918 | min( $this->numRows, $this->limit ), // do not format the one extra row, if exist |
| 919 | $this->offset ); |
| 920 | |
| 921 | // Repeat the paging links at the bottom |
| 922 | if ( $this->shownavigation ) { |
| 923 | // @phan-suppress-next-line PhanPossiblyUndeclaredVariable paging is set when used here |
| 924 | $out->addHTML( '<p>' . $paging . '</p>' ); |
| 925 | } |
| 926 | |
| 927 | $out->addHTML( Html::closeElement( 'div' ) ); |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Format and output report results using the given information plus |
| 932 | * OutputPage |
| 933 | * |
| 934 | * @stable to override |
| 935 | * |
| 936 | * @param OutputPage $out OutputPage to print to |
| 937 | * @param Skin $skin User skin to use |
| 938 | * @param IReadableDatabase $dbr Database (read) connection to use |
| 939 | * @param IResultWrapper $res Result pointer |
| 940 | * @param int $num Number of available result rows |
| 941 | * @param int $offset Paging offset |
| 942 | */ |
| 943 | protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { |
| 944 | if ( $num > 0 ) { |
| 945 | $html = []; |
| 946 | if ( !$this->listoutput ) { |
| 947 | $html[] = $this->openList( $offset ); |
| 948 | } |
| 949 | |
| 950 | // $res might contain the whole 1,000 rows, so we read up to |
| 951 | // $num [should update this to use a Pager] |
| 952 | // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found |
| 953 | for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) { |
| 954 | $line = $this->formatResult( $skin, $row ); |
| 955 | if ( $line ) { |
| 956 | $html[] = $this->listoutput |
| 957 | ? $line |
| 958 | : "<li>{$line}</li>\n"; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | if ( !$this->listoutput ) { |
| 963 | $html[] = $this->closeList(); |
| 964 | } |
| 965 | |
| 966 | $html = $this->listoutput |
| 967 | ? $this->getContentLanguage()->listToText( $html ) |
| 968 | : implode( '', $html ); |
| 969 | |
| 970 | $out->addHTML( $html ); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * @param int $offset |
| 976 | * @return string |
| 977 | */ |
| 978 | protected function openList( $offset ) { |
| 979 | return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n"; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * @return string |
| 984 | */ |
| 985 | protected function closeList() { |
| 986 | return "</ol>\n"; |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Do any necessary preprocessing of the result object. |
| 991 | * @stable to override |
| 992 | * @param IReadableDatabase $db |
| 993 | * @param IResultWrapper $res |
| 994 | */ |
| 995 | protected function preprocessResults( $db, $res ) { |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include |
| 1000 | * title and optional the namespace field) and executes the batch. This operation will pre-cache |
| 1001 | * LinkCache information like page existence and information for stub color and redirect hints. |
| 1002 | * |
| 1003 | * @note Call self::setLinkBatchFactory from special page constructor when use |
| 1004 | * |
| 1005 | * @param IResultWrapper $res The result wrapper to process. Needs to include the title |
| 1006 | * field and namespace field, if the $ns parameter isn't set. |
| 1007 | * @param int|null $ns Use this namespace for the given titles in the result wrapper, |
| 1008 | * instead of the namespace value of $res. |
| 1009 | */ |
| 1010 | protected function executeLBFromResultWrapper( IResultWrapper $res, $ns = null ) { |
| 1011 | if ( !$res->numRows() ) { |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | $batch = $this->getLinkBatchFactory()->newLinkBatch()->setCaller( __METHOD__ ); |
| 1016 | foreach ( $res as $row ) { |
| 1017 | $batch->add( $ns ?? (int)$row->namespace, $row->title ); |
| 1018 | } |
| 1019 | $batch->execute(); |
| 1020 | |
| 1021 | $res->seek( 0 ); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * @since 1.36 |
| 1026 | * @deprecated since 1.43, use self::setDatabaseProvider |
| 1027 | * @param ILoadBalancer $loadBalancer |
| 1028 | */ |
| 1029 | final protected function setDBLoadBalancer( ILoadBalancer $loadBalancer ) { |
| 1030 | $this->loadBalancer = $loadBalancer; |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * @since 1.36 |
| 1035 | * @deprecated since 1.43, use self::getDatabaseProvider |
| 1036 | * @return ILoadBalancer |
| 1037 | */ |
| 1038 | final protected function getDBLoadBalancer(): ILoadBalancer { |
| 1039 | if ( $this->loadBalancer === null ) { |
| 1040 | // Fallback if not provided |
| 1041 | // TODO Change to wfWarn in a future release |
| 1042 | $this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 1043 | } |
| 1044 | return $this->loadBalancer; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * @since 1.41 |
| 1049 | * @param IConnectionProvider $databaseProvider |
| 1050 | */ |
| 1051 | final protected function setDatabaseProvider( IConnectionProvider $databaseProvider ) { |
| 1052 | $this->databaseProvider = $databaseProvider; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * @since 1.41 |
| 1057 | * @return IConnectionProvider |
| 1058 | */ |
| 1059 | final protected function getDatabaseProvider(): IConnectionProvider { |
| 1060 | if ( $this->databaseProvider === null ) { |
| 1061 | $this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 1062 | } |
| 1063 | return $this->databaseProvider; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /** @deprecated class alias since 1.41 */ |
| 1068 | class_alias( QueryPage::class, 'QueryPage' ); |