From: Tim Starling Date: Fri, 23 Jun 2006 06:17:38 +0000 (+0000) Subject: SpecialContributions: fixed style, fixed sloppy handling of empty query results,... X-Git-Tag: 1.31.0-rc.0~56686 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=92545882d6fb1d979830ca3da12cc25fa76edb54;p=lhc%2Fweb%2Fwiklou.git SpecialContributions: fixed style, fixed sloppy handling of empty query results, fixed inefficient code. --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 5c0122a2dd..9910847887 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -150,7 +150,7 @@ function __autoload($class_name) { 'BrokenRedirectsPage' => 'SpecialBrokenRedirects.php', 'CategoriesPage' => 'SpecialCategories.php', 'EmailConfirmation' => 'SpecialConfirmemail.php', - 'contribs_finder' => 'SpecialContributions.php', + 'ContribsFinder' => 'SpecialContributions.php', 'DeadendPagesPage' => 'SpecialDeadendpages.php', 'DisambiguationsPage' => 'SpecialDisambiguations.php', 'DoubleRedirectsPage' => 'SpecialDoubleRedirects.php', diff --git a/includes/SpecialContributions.php b/includes/SpecialContributions.php index 2a1fc8d943..c35909e6a5 100644 --- a/includes/SpecialContributions.php +++ b/includes/SpecialContributions.php @@ -5,124 +5,139 @@ */ /** @package MediaWiki */ -class contribs_finder { +class ContribsFinder { var $username, $offset, $limit, $namespace; var $dbr; - function contribs_finder($username) { + function ContribsFinder( $username ) { $this->username = $username; $this->namespace = false; - $this->dbr =& wfGetDB(DB_SLAVE); + $this->dbr =& wfGetDB( DB_SLAVE ); } - function set_namespace($ns) { + function setNamespace( $ns ) { $this->namespace = $ns; } - function set_limit($limit) { + function setLimit( $limit ) { $this->limit = $limit; } - function set_offset($offset) { + function setOffset( $offset ) { $this->offset = $offset; } - function get_edit_limit($dir) { - list($index, $usercond) = $this->get_user_cond(); - $nscond = $this->get_namespace_cond(); - $use_index = $this->dbr->useIndexClause($index); - extract($this->dbr->tableNames('revision', 'page')); + function getEditLimit( $dir ) { + list( $index, $usercond ) = $this->getUserCond(); + $nscond = $this->getNamespaceCond(); + $use_index = $this->dbr->useIndexClause( $index ); + extract( $this->dbr->tableNames( 'revision', 'page' ) ); $sql = "SELECT rev_timestamp " . " FROM $page,$revision $use_index " . " WHERE rev_page=page_id AND $usercond $nscond" . " ORDER BY rev_timestamp $dir LIMIT 1"; - $res = $this->dbr->query($sql, "contribs_finder::get_edit_limit"); - while ($o = $this->dbr->fetchObject($res)) - $row = $o; - return $row->rev_timestamp; + $res = $this->dbr->query( $sql, __METHOD__ ); + $row = $this->dbr->fetchObject( $res ); + if ( $row ) { + return $row->rev_timestamp; + } else { + return false; + } } - function get_edit_limits() { + function getEditLimits() { return array( - $this->get_edit_limit("ASC"), - $this->get_edit_limit("DESC") + $this->getEditLimit( "ASC" ), + $this->getEditLimit( "DESC" ) ); } - function get_user_cond() { + function getUserCond() { $condition = ''; - if ($this->username == 'newbies') { - $max = $this->dbr->selectField('user', 'max(user_id)', false, 'make_sql'); + if ( $this->username == 'newbies' ) { + $max = $this->dbr->selectField( 'user', 'max(user_id)', false, 'make_sql' ); $condition = '>' . (int)($max - $max / 100); } - if ($condition == '') { - $condition = ' rev_user_text=' . $this->dbr->addQuotes($this->username); + if ( $condition == '' ) { + $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username ); $index = 'usertext_timestamp'; } else { $condition = ' rev_user '.$condition ; $index = 'user_timestamp'; } - return array($index, $condition); + return array( $index, $condition ); } - function get_namespace_cond() { - if ($this->namespace !== false) + function getNamespaceCond() { + if ( $this->namespace !== false ) return ' AND page_namespace = ' . (int)$this->namespace; return ''; } - function get_previous_offset_for_paging() { - list($index, $usercond) = $this->get_user_cond(); - $nscond = $this->get_namespace_cond(); + function getPreviousOffsetForPaging() { + list( $index, $usercond ) = $this->getUserCond(); + $nscond = $this->getNamespace_cond(); - $use_index = $this->dbr->useIndexClause($index); - extract($this->dbr->tableNames('page', 'revision')); + $use_index = $this->dbr->useIndexClause( $index ); + extract( $this->dbr->tableNames( 'page', 'revision' ) ); $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " . "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " . $usercond . $nscond; $sql .= " ORDER BY rev_timestamp ASC"; - $sql = $this->dbr->limitResult($sql, $this->limit, 0); - $res = $this->dbr->query($sql); - $rows = array(); - while ($obj = $this->dbr->fetchObject($res)) - $rows[] = $obj; - $this->dbr->freeResult($res); - return $rows[count($rows) - 1]->rev_timestamp; - } - - function get_first_offset_for_paging() { - list($index, $usercond) = $this->get_user_cond(); - $use_index = $this->dbr->useIndexClause($index); - extract($this->dbr->tableNames('page', 'revision')); - $nscond = $this->get_namespace_cond(); + $sql = $this->dbr->limitResult( $sql, $this->limit, 0 ); + $res = $this->dbr->query( $sql ); + + $numRows = $this->dbr->numRows( $res ); + if ( $numRows ) { + $this->dbr->dataSeek( $res, $numRows - 1 ); + $row = $this->dbr->fetchObject( $res ); + $offset = $row->rev_timestamp; + } else { + $offset = false; + } + $this->dbr->freeResult( $res ); + return $offset; + } + + function getFirstOffsetForPaging() { + list( $index, $usercond ) = $this->getUserCond(); + $use_index = $this->dbr->useIndexClause( $index ); + extract( $this->dbr->tableNames( 'page', 'revision' ) ); + $nscond = $this->getNamespaceCond(); $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " . "WHERE page_id = rev_page AND " . $usercond . $nscond; $sql .= " ORDER BY rev_timestamp ASC"; - $sql = $this->dbr->limitResult($sql, $this->limit, 0); - $res = $this->dbr->query($sql); - $rows = array(); - while ($obj = $this->dbr->fetchObject($res)) - $rows[] = $obj; - $this->dbr->freeResult($res); - return $rows[count($rows) - 1]->rev_timestamp; + $sql = $this->dbr->limitResult( $sql, $this->limit, 0 ); + $res = $this->dbr->query( $sql ); + + $numRows = $this->dbr->numRows( $res ); + if ( $numRows ) { + $this->dbr->dataSeek( $res, $numRows - 1 ); + $row = $this->dbr->fetchObject( $res ); + $offset = $row->rev_timestamp; + } else { + $offset = false; + } + $this->dbr->freeResult( $res ); + return $offset; } - /* private */ function make_sql() { + /* private */ function makeSql() { $userCond = $condition = $index = $offsetQuery = ''; - extract($this->dbr->tableNames('page', 'revision')); - list($index, $userCond) = $this->get_user_cond(); + extract( $this->dbr->tableNames( 'page', 'revision' ) ); + list( $index, $userCond ) = $this->getUserCond(); - if ($this->offset) + if ( $this->offset ) $offsetQuery = "AND rev_timestamp <= '{$this->offset}'"; - $nscond = $this->get_namespace_cond(); - $use_index = $this->dbr->useIndexClause($index); + $nscond = $this->getNamespaceCond(); + $use_index = $this->dbr->useIndexClause( $index ); $sql = "SELECT page_namespace,page_title,page_is_new,page_latest, rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text, @@ -130,16 +145,16 @@ class contribs_finder { FROM $page,$revision $use_index WHERE page_id=rev_page AND $userCond $nscond $offsetQuery ORDER BY rev_timestamp DESC"; - $sql = $this->dbr->limitResult($sql, $this->limit, 0); + $sql = $this->dbr->limitResult( $sql, $this->limit, 0 ); return $sql; } function find() { $contribs = array(); - $res = $this->dbr->query($this->make_sql(), 'contribs_finder::find'); - while ($c = $this->dbr->fetchObject($res)) + $res = $this->dbr->query( $this->makeSql(), __METHOD__ ); + while ( $c = $this->dbr->fetchObject( $res ) ) $contribs[] = $c; - $this->dbr->freeResult($res); + $this->dbr->freeResult( $res ); return $contribs; } }; @@ -155,14 +170,14 @@ function wfSpecialContributions( $par = null ) { global $wgUser, $wgOut, $wgLang, $wgRequest; $fname = 'wfSpecialContributions'; - $target = isset($par) ? $par : $wgRequest->getVal( 'target' ); - if (!strlen($target)) { - $wgOut->showErrorPage('notargettitle', 'notargettext'); + $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' ); + if ( !strlen( $target ) ) { + $wgOut->showErrorPage( 'notargettitle', 'notargettext' ); return; } $nt = Title::newFromURL( $target ); - if (!$nt) { + if ( !$nt ) { $wgOut->showErrorPage( 'notargettitle', 'notargettext' ); return; } @@ -170,112 +185,118 @@ function wfSpecialContributions( $par = null ) { $options = array(); list( $options['limit'], $options['offset']) = wfCheckLimits(); - $options['offset'] = $wgRequest->getVal('offset'); + $options['offset'] = $wgRequest->getVal( 'offset' ); /* Offset must be an integral. */ - if (!strlen($options['offset']) || !preg_match('/^[0-9]+$/', $options['offset'])) + if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) ) $options['offset'] = ''; - $title = Title::makeTitle(NS_SPECIAL, 'Contributions'); + $title = Title::makeTitle( NS_SPECIAL, 'Contributions' ); $options['target'] = $target; - $nt =& Title::makeTitle(NS_USER, $nt->getDBkey()); - $finder = new contribs_finder(($target == 'newbies') ? 'newbies' : $nt->getText()); - $finder->set_limit($options['limit']); - $finder->set_offset($options['offset']); + $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() ); + $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() ); + $finder->setLimit( $options['limit'] ); + $finder->setOffset( $options['offset'] ); - if (($ns = $wgRequest->getVal('namespace', null)) !== null && $ns !== '') { + if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) { #$options['namespace'] = intval( $ns ); - $finder->set_namespace($options['namespace']); + $finder->setNamespace( $options['namespace'] ); } else { $options['namespace'] = ''; } - if ($wgUser->isAllowed('rollback') && $wgRequest->getBool( 'bot' )) { + if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) { $options['bot'] = '1'; } - if ($wgRequest->getText('go') == 'prev') { - $options['offset'] = $finder->get_previous_offset_for_paging(); - $prevurl = $title->getLocalURL(wfArrayToCGI( $options )); - $wgOut->redirect($prevurl); - return; + if ( $wgRequest->getText( 'go' ) == 'prev' ) { + $offset = $finder->getPreviousOffsetForPaging(); + if ( $offset !== false ) { + $options['offset'] = $offset; + $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) ); + $wgOut->redirect( $prevurl ); + return; + } } - if ($wgRequest->getText('go') == 'first' && $target != 'newbies') { - $options['offset'] = $finder->get_first_offset_for_paging(); - $prevurl = $title->getLocalURL(wfArrayToCGI( $options )); - $wgOut->redirect($prevurl); - return; + if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') { + $offset = $finder->getFirstOffsetForPaging(); + if ( $offset !== false ) { + $options['offset'] = $offset; + $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) ); + $wgOut->redirect( $prevurl ); + return; + } } - if ($target == 'newbies') { + if ( $target == 'newbies' ) { $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') ); } else { - $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub($nt) ) ); + $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) ); } - $id = User::idFromName($nt->getText()); - wfRunHooks('SpecialContributionsBeforeMainOutput', $id ); + $id = User::idFromName( $nt->getText() ); + wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id ); - $wgOut->addHTML(contributionsForm($options)); + $wgOut->addHTML( contributionsForm( $options) ); $contribs = $finder->find(); - if (count($contribs) == 0) { + if ( count( $contribs ) == 0) { $wgOut->addWikiText( wfMsg( 'nocontribs' ) ); return; } - list($early, $late) = $finder->get_edit_limits(); - $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0; - $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp); - $atend = (!count($contribs) || $early == $lastts); + list( $early, $late ) = $finder->getEditLimits(); + $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0; + $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp ); + $atend = ( !count( $contribs ) || $early == $lastts ); // These four are defaults - $newestlink = wfMsgHtml('sp-contributions-newest'); - $oldestlink = wfMsgHtml('sp-contributions-oldest'); - $newerlink = wfMsgHtml('sp-contributions-newer', $options['limit']); - $olderlink = wfMsgHtml('sp-contributions-older', $options['limit']); + $newestlink = wfMsgHtml( 'sp-contributions-newest' ); + $oldestlink = wfMsgHtml( 'sp-contributions-oldest' ); + $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] ); + $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] ); - if (!$atstart) { - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('offset' => ''), $options)); + if ( !$atstart ) { + $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) ); $newestlink = "$newestlink"; - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('go' => 'prev'), $options)); + $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) ); $newerlink = "$newerlink"; } - if (!$atend) { - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('go' => 'first'), $options)); + if ( !$atend ) { + $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) ); $oldestlink = "$oldestlink"; - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('offset' => $lastts), $options)); + $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) ); $olderlink = "$olderlink"; } - if ($target == 'newbies') { + if ( $target == 'newbies' ) { $firstlast ="($newestlink)"; } else { $firstlast = "($newestlink | $oldestlink)"; } $urls = array(); - foreach (array(20, 50, 100, 250, 500) as $num) { - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('limit' => $num), $options)); - $urls[] = "".$wgLang->formatNum($num).""; + foreach ( array( 20, 50, 100, 250, 500 ) as $num ) { + $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) ); + $urls[] = "".$wgLang->formatNum( $num ).""; } - $bits = implode($urls, ' | '); + $bits = implode( $urls, ' | ' ); - $prevnextbits = $firstlast .' '. wfMsgHtml('viewprevnext', $newerlink, $olderlink, $bits); + $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits ); - $wgOut->addHTML( "

{$prevnextbits}

\n"); + $wgOut->addHTML( "

{$prevnextbits}

\n" ); $wgOut->addHTML( "\n" ); - $wgOut->addHTML( "

{$prevnextbits}

\n"); + $wgOut->addHTML( "

{$prevnextbits}

\n" ); } /** @@ -286,7 +307,7 @@ function contributionsSub( $nt ) { global $wgSysopUserBans, $wgLang, $wgUser; $sk = $wgUser->getSkin(); - $id = User::idFromName($nt->getText()); + $id = User::idFromName( $nt->getText() ); if ( 0 == $id ) { $ul = $nt->getText(); @@ -323,17 +344,17 @@ function contributionsForm( $options ) { $f = "
\n"; foreach ( $options as $name => $value ) { if( $name === 'namespace') continue; - $f .= "\t" . wfElement('input', array( + $f .= "\t" . wfElement( 'input', array( 'name' => $name, 'type' => 'hidden', - 'value' => $value)) . "\n"; + 'value' => $value ) ) . "\n"; } - $f .= '

' . wfMsgHtml('namespace') . ' ' . + $f .= '

' . wfMsgHtml( 'namespace' ) . ' ' . HTMLnamespaceselector( $options['namespace'], '' ) . - wfElement('input', array( + wfElement( 'input', array( 'type' => 'submit', - 'value' => wfMsg('allpagessubmit')) + 'value' => wfMsg( 'allpagessubmit' ) ) ) . "

\n"; @@ -381,7 +402,7 @@ function ucListEdit( $sk, $row ) { $difftext .= $messages['newarticle']; } - if( $wgUser->isAllowed('rollback') ) { + if( $wgUser->isAllowed( 'rollback' ) ) { $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : ''; $extraRollback .= '&token=' . urlencode( $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) ); @@ -399,7 +420,7 @@ function ucListEdit( $sk, $row ) { $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')'; $comment = $sk->revComment( $rev ); - $d = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true ); + $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true ); if( $rev->isDeleted( Revision::MW_REV_DELETED_TEXT ) ) { $d = '' . $d . '';