Removed all instances of empty() where error suppression was not intended. Replaced with conversion to boolean, count() or empty string check as appropriate. Fixes a number of bugs due to incorrect conversion to boolean: suppressed edit summaries containing '0', ignored titles called '0', searches for '0' ignored, etc.

This commit is contained in:
Tim Starling 2008-10-25 14:04:43 +00:00
parent c5bd0fae09
commit f48c6070df
25 changed files with 50 additions and 48 deletions

View file

@ -572,7 +572,7 @@ abstract class ApiBase {
if (is_array($allowedValues)) {
# Check for unknown values
$unknown = array_diff($valuesList, $allowedValues);
if(!empty($unknown))
if(count($unknown))
{
if($allowMultiple)
{

View file

@ -88,7 +88,7 @@ class ApiBlock extends ApiBase {
$userID = $expiry = null;
$retval = $form->doBlock($userID, $expiry);
if(!empty($retval))
if(count($retval))
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg($retval);

View file

@ -73,15 +73,15 @@ class ApiDelete extends ApiBase {
$reason = (isset($params['reason']) ? $params['reason'] : NULL);
if ($titleObj->getNamespace() == NS_IMAGE) {
$retval = self::deletefile($params['token'], $titleObj, $params['oldimage'], $reason, false);
if(!empty($retval))
$retval = self::deleteFile($params['token'], $titleObj, $params['oldimage'], $reason, false);
if(count($retval))
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg(current($retval));
} else {
$articleObj = new Article($titleObj);
$retval = self::delete($articleObj, $params['token'], $reason);
if(!empty($retval))
if(count($retval))
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg(current($retval));

View file

@ -68,7 +68,7 @@ class ApiEditPage extends ApiBase {
$errors = $titleObj->getUserPermissionsErrors('edit', $wgUser);
if(!$titleObj->exists())
$errors = array_merge($errors, $titleObj->getUserPermissionsErrors('create', $wgUser));
if(!empty($errors))
if(count($errors))
$this->dieUsageMsg($errors[0]);
$articleObj = new Article($titleObj);
@ -145,7 +145,7 @@ class ApiEditPage extends ApiBase {
$r = array();
if(!wfRunHooks('APIEditBeforeSave', array(&$ep, $ep->textbox1, &$r)))
{
if(!empty($r))
if(count($r))
{
$r['result'] = "Failure";
$this->getResult()->addValue(null, $this->getModuleName(), $r);

View file

@ -114,15 +114,15 @@ class ApiFormatXml extends ApiFormatBase {
}
}
if (is_null($subElemIndName) && !empty ($indElements))
if (is_null($subElemIndName) && count($indElements))
ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
if (count($subElements) && count($indElements) && !is_null($subElemContent))
ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
if (!is_null($subElemContent)) {
$this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
} elseif (empty ($indElements) && empty ($subElements)) {
} elseif (!count($indElements) && !count($subElements)) {
$this->printText($indstr . wfElement($elemName, $elemValue));
} else {
$this->printText($indstr . wfElement($elemName, $elemValue, null));

View file

@ -590,7 +590,7 @@ class ApiMain extends ApiBase {
public static function makeHelpMsgHeader($module, $paramName) {
$modulePrefix = $module->getModulePrefix();
if (!empty($modulePrefix))
if (strval($modulePrefix) !== '')
$modulePrefix = "($modulePrefix) ";
return "* $paramName={$module->getModuleName()} $modulePrefix*";

View file

@ -368,7 +368,7 @@ class ApiPageSet extends ApiQueryBase {
}
private function initFromPageIds($pageids) {
if(empty($pageids))
if(!count($pageids))
return;
$pageids = array_map('intval', $pageids); // paranoia
@ -440,7 +440,7 @@ class ApiPageSet extends ApiQueryBase {
else
{
// The remaining pageids do not exist
if(empty($this->mMissingPageIDs))
if(!$this->mMissingPageIDs)
$this->mMissingPageIDs = array_keys($remaining);
else
$this->mMissingPageIDs = array_merge($this->mMissingPageIDs, array_keys($remaining));
@ -450,7 +450,7 @@ class ApiPageSet extends ApiQueryBase {
private function initFromRevIDs($revids) {
if(empty($revids))
if(!count($revids))
return;
$db = $this->getDB();
@ -488,7 +488,7 @@ class ApiPageSet extends ApiQueryBase {
// Repeat until all redirects have been resolved
// The infinite loop is prevented by keeping all known pages in $this->mAllPages
while (!empty ($this->mPendingRedirectIDs)) {
while ($this->mPendingRedirectIDs) {
// Resolve redirects by querying the pagelinks table, and repeat the process
// Create a new linkBatch object for the next pass
@ -537,7 +537,7 @@ class ApiPageSet extends ApiQueryBase {
$this->mRedirectTitles[$from] = $to;
}
$db->freeResult($res);
if(!empty($this->mPendingRedirectIDs))
if($this->mPendingRedirectIDs)
{
# We found pages that aren't in the redirect table
# Add them
@ -580,7 +580,7 @@ class ApiPageSet extends ApiQueryBase {
continue; // There's nothing else we can do
}
$iw = $titleObj->getInterwiki();
if (!empty($iw)) {
if (strval($iw) !== '') {
// This title is an interwiki link.
$this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
} else {

View file

@ -57,7 +57,7 @@ class ApiPatrol extends ApiBase {
$this->dieUsageMsg(array('nosuchrcid', $params['rcid']));
$retval = RecentChange::markPatrolled($params['rcid']);
if(!empty($retval))
if($retval)
$this->dieUsageMsg(current($retval));
$result = array('rcid' => $rc->getAttribute('rc_id'));

View file

@ -46,7 +46,7 @@ class ApiProtect extends ApiBase {
$this->dieUsageMsg(array('missingparam', 'title'));
if(!isset($params['token']))
$this->dieUsageMsg(array('missingparam', 'token'));
if(!isset($params['protections']) || empty($params['protections']))
if(empty($params['protections']))
$this->dieUsageMsg(array('missingparam', 'protections'));
if(!$wgUser->matchEditToken($params['token']))
@ -57,7 +57,7 @@ class ApiProtect extends ApiBase {
$this->dieUsageMsg(array('invalidtitle', $params['title']));
$errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
if(!empty($errors))
if($errors)
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg(current($errors));

View file

@ -256,7 +256,7 @@ class ApiQuery extends ApiBase {
);
}
if (!empty ($normValues)) {
if (count($normValues)) {
$result->setIndexedTagName($normValues, 'n');
$result->addValue('query', 'normalized', $normValues);
}
@ -270,7 +270,7 @@ class ApiQuery extends ApiBase {
);
}
if (!empty ($intrwValues)) {
if (count($intrwValues)) {
$result->setIndexedTagName($intrwValues, 'i');
$result->addValue('query', 'interwiki', $intrwValues);
}
@ -284,7 +284,7 @@ class ApiQuery extends ApiBase {
);
}
if (!empty ($redirValues)) {
if (count($redirValues)) {
$result->setIndexedTagName($redirValues, 'r');
$result->addValue('query', 'redirects', $redirValues);
}
@ -293,7 +293,7 @@ class ApiQuery extends ApiBase {
// Missing revision elements
//
$missingRevIDs = $pageSet->getMissingRevisionIDs();
if (!empty ($missingRevIDs)) {
if (count($missingRevIDs)) {
$revids = array ();
foreach ($missingRevIDs as $revid) {
$revids[$revid] = array (
@ -335,7 +335,7 @@ class ApiQuery extends ApiBase {
$pages[$pageid] = $vals;
}
if (!empty ($pages)) {
if (count($pages)) {
if ($this->params['indexpageids']) {
$pageIDs = array_keys($pages);

View file

@ -211,7 +211,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
}
$db->freeResult($res);
if($this->redirect && !empty($this->redirTitles))
if($this->redirect && count($this->redirTitles))
{
$this->resetQueryParams();
$this->prepareSecondQuery($resultPageSet);

View file

@ -41,9 +41,10 @@ class ApiQueryCategoryInfo extends ApiQueryBase {
public function execute() {
$alltitles = $this->getPageSet()->getAllTitlesByNamespace();
$categories = $alltitles[NS_CATEGORY];
if(empty($categories))
if ( empty( $alltitles[NS_CATEGORY] ) ) {
return;
}
$categories = $alltitles[NS_CATEGORY];
$titles = $this->getPageSet()->getGoodTitles() +
$this->getPageSet()->getMissingTitles();

View file

@ -50,9 +50,10 @@ class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
private function run($resultPageSet = null) {
$params = $this->extractRequestParams();
$namespaces = $this->getPageSet()->getAllTitlesByNamespace();
$images = $namespaces[NS_IMAGE];
if(empty($images))
if ( empty( $namespaces[NS_IMAGE] ) ) {
return;
}
$images = $namespaces[NS_IMAGE];
$this->addTables('image', 'i1');
$this->addTables('image', 'i2');

View file

@ -54,7 +54,7 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
// Find the right prefix
global $wgUrlProtocols;
if(!is_null($protocol) && !empty($protocol) && !in_array($protocol, $wgUrlProtocols))
if($protocol && !in_array($protocol, $wgUrlProtocols))
{
foreach ($wgUrlProtocols as $p) {
if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {

View file

@ -197,7 +197,7 @@ class ApiQueryInfo extends ApiQueryBase {
$pageLength = $pageSet->getCustomField('page_len');
$db = $this->getDB();
if ($fld_protection && !empty($titles)) {
if ($fld_protection && count($titles)) {
$this->addTables('page_restrictions');
$this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade'));
$this->addWhereFld('pr_page', array_keys($titles));
@ -273,7 +273,7 @@ class ApiQueryInfo extends ApiQueryBase {
}
// We don't need to check for pt stuff if there are no nonexistent titles
if($fld_protection && !empty($missing))
if($fld_protection && count($missing))
{
$this->resetQueryParams();
// Construct a custom WHERE clause that matches all titles in $missing
@ -367,7 +367,7 @@ class ApiQueryInfo extends ApiQueryBase {
else if($fld_talkid)
$talktitles[] = $t->getTalkPage();
}
if(!empty($talktitles) || !empty($subjecttitles))
if(count($talktitles) || count($subjecttitles))
{
// Construct a custom WHERE clause that matches
// all titles in $talktitles and $subjecttitles

View file

@ -213,7 +213,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
if ($this->fld_timestamp) {
$vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
}
if ($this->fld_comment && !empty ($row->log_comment)) {
if ($this->fld_comment && isset($row->log_comment)) {
$vals['comment'] = $row->log_comment;
}

View file

@ -100,7 +100,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
$this->addWhereRange('rc_timestamp', $dir, $start, $end);
$this->addWhereFld('rc_namespace', $namespace);
$this->addWhereFld('rc_deleted', 0);
if(!empty($titles))
if($titles)
{
$lb = new LinkBatch;
foreach($titles as $t)
@ -256,7 +256,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
private function extractRowInfo($row) {
/* If page was moved somewhere, get the title of the move target. */
$movedToTitle = false;
if (!empty($row->rc_moved_to_title))
if (isset($row->rc_moved_to_title) && $row->rc_moved_to_title !== '')
$movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
/* Determine the title of the page that has been changed. */
@ -320,7 +320,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
$vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
/* Add edit summary / log summary. */
if ($this->fld_comment && !empty ($row->rc_comment)) {
if ($this->fld_comment && isset($row->rc_comment)) {
$vals['comment'] = $row->rc_comment;
}

View file

@ -288,7 +288,7 @@ class ApiQueryRevisions extends ApiQueryBase {
if ($this->fld_comment) {
$comment = $revision->getComment();
if (!empty($comment))
if (strval($comment) !== '')
$vals['comment'] = $comment;
}

View file

@ -54,7 +54,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
$limit = $params['limit'];
$query = $params['search'];
$what = $params['what'];
if (is_null($query) || empty($query))
if (strval($query) === '')
$this->dieUsage("empty search string is not allowed", 'param-search');
$search = SearchEngine::create();

View file

@ -227,7 +227,7 @@ class ApiQueryContributions extends ApiQueryBase {
$vals['top'] = '';
}
if ($this->fld_comment && !empty ($row->rev_comment))
if ($this->fld_comment && isset( $row->rev_comment ) )
$vals['comment'] = $row->rev_comment;
return $vals;

View file

@ -68,7 +68,7 @@ if (!defined('MEDIAWIKI')) {
else
$goodNames[] = $n;
}
if(empty($goodNames))
if(!count($goodNames))
return $retval;
$db = $this->getDb();

View file

@ -237,7 +237,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
$vals['newlen'] = intval($row->rc_new_len);
}
if ($this->fld_comment && !empty ($row->rc_comment))
if ($this->fld_comment && isset( $row->rc_comment ))
$vals['comment'] = $row->rc_comment;
return $vals;

View file

@ -100,7 +100,7 @@ class ApiResult extends ApiBase {
}
elseif (is_array($arr[$name]) && is_array($value)) {
$merged = array_intersect_key($arr[$name], $value);
if (empty ($merged))
if (!count($merged))
$arr[$name] += $value;
else
ApiBase :: dieDebug(__METHOD__, "Attempting to merge element $name");
@ -180,7 +180,7 @@ class ApiResult extends ApiBase {
}
}
if (empty($name))
if (!$name)
$data[] = $value; // Add list element
else
ApiResult :: setElement($data, $name, $value); // Add named element
@ -201,7 +201,7 @@ if (!function_exists('array_intersect_key')) {
$argc = func_num_args();
if ($argc > 2) {
for ($i = 1; !empty($isec) && $i < $argc; $i++) {
for ($i = 1; $isec && $i < $argc; $i++) {
$arr = func_get_arg($i);
foreach (array_keys($isec) as $key) {

View file

@ -66,7 +66,7 @@ class ApiRollback extends ApiBase {
$details = null;
$retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], $details);
if(!empty($retval))
if($retval)
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg(current($retval));

View file

@ -69,7 +69,7 @@ class ApiUnblock extends ApiBase {
$user = $params['user'];
$reason = (is_null($params['reason']) ? '' : $params['reason']);
$retval = IPUnblockForm::doUnblock($id, $user, $reason, $range);
if(!empty($retval))
if($retval)
$this->dieUsageMsg($retval);
$res['id'] = $id;