From 38475c650bc65fdbd01c86c0d7991b0911e5a25b Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Tue, 12 Apr 2011 12:09:11 +0000 Subject: [PATCH] Clean up some direct $db->query($sql) calls. Remove limit/offset parameters from Article::getContributors() as they weren't being used anywhere and probably didn't work properly anyway. --- includes/Article.php | 62 +++++++++++++++-------------- includes/Export.php | 23 ++++++----- includes/parser/LinkHolderArray.php | 34 +++++++++------- 3 files changed, 65 insertions(+), 54 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index a52b613356..7bacc8a778 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -799,46 +799,47 @@ class Article { } /** - * FIXME: this does what? - * @param $limit Integer: default 0. - * @param $offset Integer: default 0. - * @return UserArrayFromResult object with User objects of article contributors for requested range + * Get a list of users who have edited this article, not including the user who made + * the most recent revision, which you can get from $article->getUser() if you want it + * @return UserArray */ - public function getContributors( $limit = 0, $offset = 0 ) { + public function getContributors() { # FIXME: this is expensive; cache this info somewhere. $dbr = wfGetDB( DB_SLAVE ); - $revTable = $dbr->tableName( 'revision' ); $userTable = $dbr->tableName( 'user' ); - $pageId = $this->getId(); + $tables = array( 'revision', 'user' ); - $user = $this->getUser(); + $fields = array( + "$userTable.*", + 'rev_user_text AS user_name', + 'MAX(rev_timestamp) AS timestamp', + ); + $conds = array( 'rev_page' => $this->getId() ); + + // The user who made the top revision gets credited as "this page was last edited by + // John, based on contributions by Tom, Dick and Harry", so don't include them twice. + $user = $this->getUser(); if ( $user ) { - $excludeCond = "AND rev_user != $user"; + $conds[] = "rev_user != $user"; } else { - $userText = $dbr->addQuotes( $this->getUserText() ); - $excludeCond = "AND rev_user_text != $userText"; + $conds[] = "rev_user_text != {$dbr->addQuotes( $this->getUserText() )}"; } - $deletedBit = $dbr->bitAnd( 'rev_deleted', Revision::DELETED_USER ); // username hidden? - - $sql = "SELECT {$userTable}.*, rev_user_text as user_name, MAX(rev_timestamp) as timestamp - FROM $revTable LEFT JOIN $userTable ON rev_user = user_id - WHERE rev_page = $pageId - $excludeCond - AND $deletedBit = 0 - GROUP BY rev_user, rev_user_text - ORDER BY timestamp DESC"; + $conds[] = "{$dbr->bitAnd( 'rev_deleted', Revision::DELETED_USER )} = 0"; // username hidden? - if ( $limit > 0 ) { - $sql = $dbr->limitResult( $sql, $limit, $offset ); - } - - $sql .= ' ' . $this->getSelectOptions(); - $res = $dbr->query( $sql, __METHOD__ ); + $jconds = array( + 'user' => array( 'LEFT JOIN', 'rev_user = user_id' ), + ); + $options = array( + 'GROUP BY' => array( 'rev_user', 'rev_user_text' ), + 'ORDER BY' => 'timestamp DESC', + ); + + $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $jconds ); return new UserArrayFromResult( $res ); } @@ -3618,10 +3619,11 @@ class Article { $dbw = wfGetDB( DB_MASTER ); $cutoff = $dbw->timestamp( time() - $wgRCMaxAge ); - $recentchanges = $dbw->tableName( 'recentchanges' ); - $sql = "DELETE FROM $recentchanges WHERE rc_timestamp < '{$cutoff}'"; - - $dbw->query( $sql ); + $dbw->delete( + 'recentchanges', + array( "rc_timestamp < '$cutoff'" ), + __METHOD__ + ); } } diff --git a/includes/Export.php b/includes/Export.php index 91eb7af375..65743b2f06 100644 --- a/includes/Export.php +++ b/includes/Export.php @@ -157,17 +157,23 @@ class WikiExporter { # Generates the distinct list of authors of an article # Not called by default (depends on $this->list_authors) # Can be set by Special:Export when not exporting whole history - protected function do_list_authors( $page , $revision , $cond ) { + protected function do_list_authors( $cond ) { wfProfileIn( __METHOD__ ); $this->author_list = ""; // rev_deleted - $nothidden = '(' . $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ') = 0'; - $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision} - WHERE page_id=rev_page AND $nothidden AND " . $cond ; - $result = $this->db->query( $sql, __METHOD__ ); - $resultset = $this->db->resultObject( $result ); - foreach ( $resultset as $row ) { + $res = $this->db->select( + array( 'page', 'revision' ), + array( 'DISTINCT rev_user_text', 'rev_user' ), + array( + $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0', + $cond, + 'page_id = rev_id', + ), + __METHOD__ + ); + + foreach ( $res as $row ) { $this->author_list .= "" . "" . htmlentities( $row->rev_user_text ) . @@ -240,8 +246,7 @@ class WikiExporter { } elseif ( $this->history & WikiExporter::CURRENT ) { # Latest revision dumps... if ( $this->list_authors && $cond != '' ) { // List authors, if so desired - list( $page, $revision ) = $this->db->tableNamesN( 'page', 'revision' ); - $this->do_list_authors( $page, $revision, $cond ); + $this->do_list_authors( $cond ); } $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ); } elseif ( $this->history & WikiExporter::STABLE ) { diff --git a/includes/parser/LinkHolderArray.php b/includes/parser/LinkHolderArray.php index 4ec3599490..80377325c0 100644 --- a/includes/parser/LinkHolderArray.php +++ b/includes/parser/LinkHolderArray.php @@ -270,6 +270,7 @@ class LinkHolderArray { # Generate query $query = false; $current = null; + $queries = array(); foreach ( $this->internals as $ns => $entries ) { foreach ( $entries as $entry ) { $title = $entry['title']; @@ -294,25 +295,28 @@ class LinkHolderArray { $colours[$pdbk] = 'new'; } else { # Not in the link cache, add it to the query - if ( !isset( $current ) ) { - $current = $ns; - $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest"; - $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN("; - } elseif ( $current != $ns ) { - $current = $ns; - $query .= ")) OR (page_namespace=$ns AND page_title IN("; - } else { - $query .= ', '; - } - - $query .= $dbr->addQuotes( $title->getDBkey() ); + $queries[$ns][] = $title->getDBkey(); } } } - if ( $query ) { - $query .= '))'; + if ( $queries ) { + $where = array(); + foreach( $queries as $ns => $pages ){ + $where[] = $dbr->makeList( + array( + 'page_namespace' => $ns, + 'page_title' => $pages, + ), + LIST_AND + ); + } - $res = $dbr->query( $query, __METHOD__ ); + $res = $dbr->select( + 'page', + array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ), + $dbr->makeList( $where, LIST_OR ), + __METHOD__ + ); # Fetch data and form into an associative array # non-existent = broken -- 2.20.1