r86321 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86320‎ | r86321 | r86322 >
Date:16:43, 18 April 2011
Author:ialex
Status:ok
Tags:
Comment:
* Use DatabaseBase::selectField() to build the query instead of throwing a raw query in DatabaseBase::query()
* Use MWNamespace::getContentNamespaces() instead of $wgContentNamespaces
Modified paths:
  • /trunk/phase3/maintenance/updateArticleCount.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/updateArticleCount.php
@@ -27,9 +27,6 @@
2828
2929 class UpdateArticleCount extends Maintenance {
3030
31 - // Content namespaces
32 - private $namespaces;
33 -
3431 public function __construct() {
3532 parent::__construct();
3633 $this->mDescription = "Count of the number of articles and update the site statistics table";
@@ -37,8 +34,6 @@
3835 }
3936
4037 public function execute() {
41 - global $wgContentNamespaces;
42 - $this->namespaces = $wgContentNamespaces;
4338 $this->output( "Counting articles..." );
4439 $result = $this->count();
4540
@@ -58,42 +53,22 @@
5954 }
6055
6156 /**
62 - * Produce a comma-delimited set of namespaces
63 - * Includes paranoia
64 - *
65 - * @return string
66 - */
67 - private function makeNsSet() {
68 - foreach ( $this->namespaces as $namespace )
69 - $namespaces[] = intval( $namespace );
70 - return implode( ', ', $namespaces );
71 - }
72 -
73 - /**
74 - * Produce SQL for the query
75 - *
76 - * @param $dbr Database handle
77 - * @return string
78 - */
79 - private function makeSql( $dbr ) {
80 - list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
81 - $nsset = $this->makeNsSet();
82 - return "SELECT COUNT(DISTINCT page_id) AS pagecount " .
83 - "FROM $page, $pagelinks " .
84 - "WHERE pl_from=page_id and page_namespace IN ( $nsset ) " .
85 - "AND page_is_redirect = 0 AND page_len > 0";
86 - }
87 -
88 - /**
8957 * Count the number of valid content pages in the wiki
9058 *
9159 * @return mixed Integer, or false if there's a problem
9260 */
9361 private function count() {
94 - $dbr = wfGetDB( DB_SLAVE );
95 - $res = $dbr->query( $this->makeSql( $dbr ), __METHOD__ );
96 - $row = $dbr->fetchObject( $res );
97 - return $row ? $row->pagecount : false;
 62+ return wfGetDB( DB_SLAVE )->selectField(
 63+ array( 'page', 'pagelinks' ),
 64+ 'COUNT(DISTINCT page_id)',
 65+ array(
 66+ 'pl_from=page_id',
 67+ 'page_namespace' => MWNamespace::getContentNamespaces(),
 68+ 'page_is_redirect' => 0,
 69+ 'page_len > 0',
 70+ ),
 71+ __METHOD__
 72+ );
9873 }
9974 }
10075

Status & tagging log