[DATABASE] Set all primary keys as "not null" explicitly

This commit is contained in:
Alexei Sorokin 2019-09-11 12:27:40 +03:00 committed by Diogo Peralta Cordeiro
parent 2b0251213f
commit 6b4344968d
4 changed files with 117 additions and 83 deletions

View File

@ -1,28 +1,29 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/* /*
* StatusNet - the distributed open-source microblogging tool * @copyright 2008-2009 StatusNet, Inc.
* Copyright (C) 2008, 2009, StatusNet, Inc. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Table Definition for file_thumbnail * Table Definition for file_thumbnail
*/ */
class File_thumbnail extends Managed_DataObject class File_thumbnail extends Managed_DataObject
{ {
public $__table = 'file_thumbnail'; // table name public $__table = 'file_thumbnail'; // table name
@ -44,8 +45,8 @@ class File_thumbnail extends Managed_DataObject
'urlhash' => array('type' => 'varchar', 'length' => 64, 'description' => 'sha256 of url field if non-empty'), 'urlhash' => array('type' => 'varchar', 'length' => 64, 'description' => 'sha256 of url field if non-empty'),
'url' => array('type' => 'text', 'description' => 'URL of thumbnail'), 'url' => array('type' => 'text', 'description' => 'URL of thumbnail'),
'filename' => array('type' => 'text', 'description' => 'if stored locally, filename is put here'), 'filename' => array('type' => 'text', 'description' => 'if stored locally, filename is put here'),
'width' => array('type' => 'int', 'description' => 'width of thumbnail'), 'width' => array('type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'),
'height' => array('type' => 'int', 'description' => 'height of thumbnail'), 'height' => array('type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'),
'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'), 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
), ),
'primary key' => array('file_id', 'width', 'height'), 'primary key' => array('file_id', 'width', 'height'),
@ -65,21 +66,26 @@ class File_thumbnail extends Managed_DataObject
* @param object $data * @param object $data
* @param int $file_id * @param int $file_id
*/ */
public static function saveNew($data, $file_id) { public static function saveNew($data, $file_id)
{
if (!empty($data->thumbnail_url)) { if (!empty($data->thumbnail_url)) {
// Non-photo types such as video will usually // Non-photo types such as video will usually
// show us a thumbnail, though it's not required. // show us a thumbnail, though it's not required.
self::saveThumbnail($file_id, self::saveThumbnail(
$data->thumbnail_url, $file_id,
$data->thumbnail_width, $data->thumbnail_url,
$data->thumbnail_height); $data->thumbnail_width,
} else if ($data->type == 'photo') { $data->thumbnail_height
);
} elseif ($data->type == 'photo') {
// The inline photo URL given should also fit within // The inline photo URL given should also fit within
// our requested thumbnail size, per oEmbed spec. // our requested thumbnail size, per oEmbed spec.
self::saveThumbnail($file_id, self::saveThumbnail(
$data->url, $file_id,
$data->width, $data->url,
$data->height); $data->width,
$data->height
);
} }
} }
@ -92,7 +98,8 @@ class File_thumbnail extends Managed_DataObject
* @return File_thumbnail * @return File_thumbnail
* @throws NoResultException if no File_thumbnail matched the criteria * @throws NoResultException if no File_thumbnail matched the criteria
*/ */
static function byFile(File $file, $notNullUrl=true) { public static function byFile(File $file, $notNullUrl = true)
{
$thumb = new File_thumbnail(); $thumb = new File_thumbnail();
$thumb->file_id = $file->getID(); $thumb->file_id = $file->getID();
if ($notNullUrl) { if ($notNullUrl) {
@ -116,7 +123,7 @@ class File_thumbnail extends Managed_DataObject
* @param int $width * @param int $width
* @param int $height * @param int $height
*/ */
static function saveThumbnail($file_id, $url, $width, $height, $filename=null) public static function saveThumbnail($file_id, $url, $width, $height, $filename = null)
{ {
$tn = new File_thumbnail; $tn = new File_thumbnail;
$tn->file_id = $file_id; $tn->file_id = $file_id;
@ -128,7 +135,7 @@ class File_thumbnail extends Managed_DataObject
return $tn; return $tn;
} }
static function path($filename) public static function path($filename)
{ {
File::tryFilename($filename); File::tryFilename($filename);
@ -142,7 +149,7 @@ class File_thumbnail extends Managed_DataObject
return $dir . $filename; return $dir . $filename;
} }
static function url($filename) public static function url($filename)
{ {
File::tryFilename($filename); File::tryFilename($filename);
@ -276,7 +283,7 @@ class File_thumbnail extends Managed_DataObject
return $this->file_id; return $this->file_id;
} }
static public function hashurl($url) public static function hashurl($url)
{ {
if (!mb_strlen($url)) { if (!mb_strlen($url)) {
throw new Exception('No URL provided to hash algorithm.'); throw new Exception('No URL provided to hash algorithm.');

View File

@ -1,4 +1,21 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
defined('GNUSOCIAL') || die();
/** /**
* Table Definition for request_queue * Table Definition for request_queue
*/ */
@ -23,7 +40,7 @@ class Group_join_queue extends Managed_DataObject
'description' => 'Holder for group join requests awaiting moderation.', 'description' => 'Holder for group join requests awaiting moderation.',
'fields' => array( 'fields' => array(
'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'), 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
'group_id' => array('type' => 'int', 'description' => 'remote or local group to join, if any'), 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local group to join, if any'),
'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'), 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
), ),
'primary key' => array('profile_id', 'group_id'), 'primary key' => array('profile_id', 'group_id'),
@ -48,27 +65,27 @@ class Group_join_queue extends Managed_DataObject
return $rq; return $rq;
} }
function getMember() public function getMember()
{ {
$member = Profile::getKV('id', $this->profile_id); $member = Profile::getKV('id', $this->profile_id);
if (empty($member)) { if (empty($member)) {
// TRANS: Exception thrown providing an invalid profile ID. // TRANS: Exception thrown providing an invalid profile ID.
// TRANS: %s is the invalid profile ID. // TRANS: %s is the invalid profile ID.
throw new Exception(sprintf(_('Profile ID %s is invalid.'),$this->profile_id)); throw new Exception(sprintf(_('Profile ID %s is invalid.'), $this->profile_id));
} }
return $member; return $member;
} }
function getGroup() public function getGroup()
{ {
$group = User_group::getKV('id', $this->group_id); $group = User_group::getKV('id', $this->group_id);
if (empty($group)) { if (empty($group)) {
// TRANS: Exception thrown providing an invalid group ID. // TRANS: Exception thrown providing an invalid group ID.
// TRANS: %s is the invalid group ID. // TRANS: %s is the invalid group ID.
throw new Exception(sprintf(_('Group ID %s is invalid.'),$this->group_id)); throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id));
} }
return $group; return $group;
@ -77,7 +94,7 @@ class Group_join_queue extends Managed_DataObject
/** /**
* Abort the pending group join... * Abort the pending group join...
*/ */
function abort() public function abort()
{ {
$profile = $this->getMember(); $profile = $this->getMember();
$group = $this->getGroup(); $group = $this->getGroup();
@ -93,7 +110,7 @@ class Group_join_queue extends Managed_DataObject
* *
* @return Group_member object on success * @return Group_member object on success
*/ */
function complete() public function complete()
{ {
$join = null; $join = null;
$profile = $this->getMember(); $profile = $this->getMember();

View File

@ -1,9 +1,26 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
defined('GNUSOCIAL') || die();
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
/** /**
* Table Definition for user_username * Table Definition for user_username
*/ */
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
class User_username extends Managed_DataObject class User_username extends Managed_DataObject
{ {
###START_AUTOCODE ###START_AUTOCODE
@ -23,8 +40,8 @@ class User_username extends Managed_DataObject
{ {
return array( return array(
'fields' => array( 'fields' => array(
'provider_name' => array('type' => 'varchar', 'length' => 191, 'description' => 'provider name'), 'provider_name' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'provider name'),
'username' => array('type' => 'varchar', 'length' => 191, 'description' => 'username'), 'username' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'username'),
'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice id this title relates to'), 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice id this title relates to'),
'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'), 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'), 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
@ -46,7 +63,7 @@ class User_username extends Managed_DataObject
* @param provider_name string name of the provider * @param provider_name string name of the provider
* @return mixed User_username instance if the registration succeeded, false if it did not * @return mixed User_username instance if the registration succeeded, false if it did not
*/ */
static function register($user, $username, $provider_name) public static function register($user, $username, $provider_name)
{ {
$user_username = new User_username(); $user_username = new User_username();
$user_username->user_id = $user->id; $user_username->user_id = $user->id;
@ -54,9 +71,9 @@ class User_username extends Managed_DataObject
$user_username->username = $username; $user_username->username = $username;
$user_username->created = common_sql_now(); $user_username->created = common_sql_now();
if($user_username->insert()){ if ($user_username->insert()) {
return $user_username; return $user_username;
}else{ } else {
return false; return false;
} }
} }

View File

@ -1,35 +1,30 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Store last-touched ID for various timelines * Store last-touched ID for various timelines
* *
* PHP version 5 * @category Data
* * @package GNUsocial
* @category Data * @author Evan Prodromou <evan@status.net>
* @package StatusNet * @copyright 2010 StatusNet, Inc.
* @author Evan Prodromou <evan@status.net> * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
exit(1);
}
require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
@ -40,13 +35,11 @@ require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
* So, we store the last ID we see from a timeline, and store it. Next time * So, we store the last ID we see from a timeline, and store it. Next time
* around, we use that ID in the since_id parameter. * around, we use that ID in the since_id parameter.
* *
* @category Action * @category Action
* @package StatusNet * @copyright 2010 StatusNet, Inc.
* @author Evan Prodromou <evan@status.net> * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
* *
* @see DB_DataObject * @see DB_DataObject
*/ */
class Twitter_synch_status extends Managed_DataObject class Twitter_synch_status extends Managed_DataObject
{ {
@ -62,7 +55,7 @@ class Twitter_synch_status extends Managed_DataObject
return array( return array(
'fields' => array( 'fields' => array(
'foreign_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'Foreign message ID'), 'foreign_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'Foreign message ID'),
'timeline' => array('type' => 'varchar', 'length' => 191, 'description' => 'timeline name'), 'timeline' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'timeline name'),
'last_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'last id fetched'), 'last_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'last id fetched'),
'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
@ -71,7 +64,7 @@ class Twitter_synch_status extends Managed_DataObject
); );
} }
static function getLastId($foreign_id, $timeline) public static function getLastId($foreign_id, $timeline)
{ {
$tss = self::pkeyGet(array('foreign_id' => $foreign_id, $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
'timeline' => $timeline)); 'timeline' => $timeline));
@ -83,7 +76,7 @@ class Twitter_synch_status extends Managed_DataObject
} }
} }
static function setLastId($foreign_id, $timeline, $last_id) public static function setLastId($foreign_id, $timeline, $last_id)
{ {
$tss = self::pkeyGet(array('foreign_id' => $foreign_id, $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
'timeline' => $timeline)); 'timeline' => $timeline));