From 0e6b80ded340e2011d436671e65e4a6ba0975d66 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 27 May 2015 20:21:05 +0200 Subject: [PATCH 1/6] more debugging info on failed schema.php runSqlSet --- lib/schema.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/schema.php b/lib/schema.php index 0421bcb810..f536f01645 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -535,6 +535,7 @@ class Schema $res = $this->conn->query($sql); if ($_PEAR->isError($res)) { + common_debug('PEAR exception on query: '.$sql); PEAR_ErrorToPEAR_Exception($res); } } From cd0b70dbc1b4276e4c70d2c1711ac358aa34314a Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 27 May 2015 21:31:29 +0200 Subject: [PATCH 2/6] upgrade fix for file URLs longer than 191 chars --- classes/File.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/classes/File.php b/classes/File.php index 1e296242b7..0018a03e32 100644 --- a/classes/File.php +++ b/classes/File.php @@ -610,6 +610,37 @@ class File extends Managed_DataObject return; } echo "\nFound old $table table, upgrading it to contain 'urlhash' field..."; + + $file = new File(); + $file->query(sprintf('SELECT id, LEFT(url, 191) AS shortenedurl, COUNT(*) AS c FROM %1$s WHERE LENGTH(url)>191 GROUP BY shortenedurl HAVING c > 1', $schema->quoteIdentifier($table))); + print "\nFound {$file->N} URLs with too long entries in file table\n"; + while ($file->fetch()) { + // We've got a URL that is too long for our future file table + // so we'll cut it. We could save the original URL, but there is + // no guarantee it is complete anyway since the previous max was 255 chars. + $dupfile = new File(); + // First we find file entries that would be duplicates of this when shortened + // ... and we'll just throw the dupes out the window for now! It's already so borken. + $dupfile->query(sprintf('SELECT * FROM file WHERE LEFT(url, 191) = "%1$s"', $file->shortenedurl)); + // Leave one of the URLs in the database by using ->find(true) (fetches first entry) + if ($dupfile->find(true)) { + print "\nShortening url entry for $table id: {$file->id} ["; + $orig = clone($dupfile); + $dupfile->url = $file->shortenedurl; // make sure it's only 191 chars from now on + $dupfile->update($orig); + print "\nDeleting duplicate entries of too long URL on $table id: {$file->id} ["; + // only start deleting with this fetch. + while($dupfile->fetch()) { + print "."; + $dupfile->delete(); + } + print "]\n"; + } else { + print "\nWarning! URL suddenly disappeared from database: {$file->url}\n"; + } + } + + echo "\n...now running hacky pre-schemaupdate change for $table:"; // We have to create a urlhash that is _not_ the primary key, // transfer data and THEN run checkSchema $schemadef['fields']['urlhash'] = array ( From c31d6608a8d66ec5bf0ad27ef59730209ad40166 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 27 May 2015 21:54:51 +0200 Subject: [PATCH 3/6] remove _all_ file URLs not just the duplicates --- classes/File.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/File.php b/classes/File.php index 0018a03e32..9a8f9eaf53 100644 --- a/classes/File.php +++ b/classes/File.php @@ -639,6 +639,8 @@ class File extends Managed_DataObject print "\nWarning! URL suddenly disappeared from database: {$file->url}\n"; } } + echo "...and now all the non-duplicates which are longer than 191 characters...\n"; + $file->query('UPDATE file SET url=LEFT(url, 191) WHERE LENGTH(url)>191'); echo "\n...now running hacky pre-schemaupdate change for $table:"; // We have to create a urlhash that is _not_ the primary key, From f926e27a652fa2a82241b2bbe2216b505d900465 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 27 May 2015 22:37:20 +0200 Subject: [PATCH 4/6] urlhash will _be_ NULL on update, so NOT NULL won't work --- classes/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/File.php b/classes/File.php index 9a8f9eaf53..594506449a 100644 --- a/classes/File.php +++ b/classes/File.php @@ -648,7 +648,7 @@ class File extends Managed_DataObject $schemadef['fields']['urlhash'] = array ( 'type' => 'varchar', 'length' => 64, - 'not null' => true, + 'not null' => false, // this is because when adding column, all entries will _be_ NULL! 'description' => 'sha256 of destination URL (url field)', ); $schemadef['fields']['url'] = array ( From 3294d704a44203eb891d4b6485452fd16976ec2e Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 30 May 2015 15:41:04 +0200 Subject: [PATCH 5/6] scripts/nukefile.php for blasting crap from the server Deletes notices and the locally stored file based on File id, as you may want to just get rid of shit sometimes. --- classes/File.php | 9 +++++ classes/File_to_post.php | 18 ++++++++++ scripts/nukefile.php | 78 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100755 scripts/nukefile.php diff --git a/classes/File.php b/classes/File.php index 594506449a..1cb7eab6b8 100644 --- a/classes/File.php +++ b/classes/File.php @@ -249,6 +249,15 @@ class File extends Managed_DataObject return true; } + public function getFilename() + { + if (!self::validFilename($this->filename)) { + // TRANS: Client exception thrown if a file upload does not have a valid name. + throw new ClientException(_("Invalid filename.")); + } + return $this->filename; + } + // where should the file go? static function filename(Profile $profile, $origname, $mimetype) diff --git a/classes/File_to_post.php b/classes/File_to_post.php index 4c751ae4f3..b3c44d4a22 100644 --- a/classes/File_to_post.php +++ b/classes/File_to_post.php @@ -85,6 +85,24 @@ class File_to_post extends Managed_DataObject } } + static function getNoticeIDsByFile(File $file) + { + $f2p = new File_to_post(); + + $f2p->selectAdd(); + $f2p->selectAdd('post_id'); + + $f2p->file_id = $file->id; + + $ids = array(); + + if (!$f2p->find()) { + throw new NoResultException($f2p); + } + + return $f2p->fetchAll('post_id'); + } + function delete($useWhere=false) { $f = File::getKV('id', $this->file_id); diff --git a/scripts/nukefile.php b/scripts/nukefile.php new file mode 100755 index 0000000000..1381676483 --- /dev/null +++ b/scripts/nukefile.php @@ -0,0 +1,78 @@ +#!/usr/bin/env php +. + */ + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); + +$shortoptions = 'i::yv'; +$longoptions = array('id=', 'yes', 'verbose'); + +$helptext = <<getFilename(); + } catch (Exception $e) { + $filename = '(remote file or no filename)'; + } + print "About to PERMANENTLY delete file ($filename) ({$file->id}). Are you sure? [y/N] "; + $response = fgets(STDIN); + if (strtolower(trim($response)) != 'y') { + print "Aborting.\n"; + exit(0); + } +} + +print "Finding notices...\n"; +try { + $ids = File_to_post::getNoticeIDsByFile($file); + $notice = Notice::multiGet('id', $ids); + while ($notice->fetch()) { + print "Deleting notice {$notice->id}".($verbose ? ": $notice->content\n" : "\n"); + $notice->delete(); + } +} catch (NoResultException $e) { + print "No notices found with this File attached.\n"; +} +print "Deleting File object together with possibly locally stored copy.\n"; +$file->delete(); +print "DONE.\n"; From b4b8cb57b326b6a668a090da060d72c30a20e967 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 30 May 2015 16:40:00 +0200 Subject: [PATCH 6/6] slugify console.php prompt name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit since PHP thought it was fun to crash on Quitter EspaƱa and I couldn't be bothered messing with readline --- classes/Profile.php | 5 +++++ scripts/console.php | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/classes/Profile.php b/classes/Profile.php index 6eb09782b1..47e144032d 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -880,6 +880,11 @@ class Profile extends Managed_DataObject $inst->delete(); } + $localuser = User::getKV('id', $this->id); + if ($localuser instanceof User) { + $localuser->delete(); + } + return parent::delete($useWhere); } diff --git a/scripts/console.php b/scripts/console.php index c260ffaa00..692cedf8d1 100755 --- a/scripts/console.php +++ b/scripts/console.php @@ -113,7 +113,7 @@ function readline_emulation($prompt) function console_help() { - print "Welcome to StatusNet's interactive PHP console!\n"; + print "Welcome to GNU social's interactive PHP console!\n"; print "Type some PHP code and it'll execute...\n"; print "\n"; print "Hint: return a value of any type to output it via var_export():\n"; @@ -128,8 +128,8 @@ function console_help() } if (CONSOLE_INTERACTIVE) { - print "StatusNet interactive PHP console... type ctrl+D or enter 'exit' to exit.\n"; - $prompt = common_config('site', 'name') . '> '; + print "GNU social interactive PHP console... type ctrl+D or enter 'exit' to exit.\n"; + $prompt = common_slugify(common_config('site', 'name')) . '> '; } else { $prompt = ''; }