diff --git a/classes/File.php b/classes/File.php index 1e296242b7..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) @@ -610,12 +619,45 @@ 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 "...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, // transfer data and THEN run checkSchema $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 ( 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/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/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); } } 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 = ''; } 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";