+
+ /**
+ * Auto-generates a deletion reason
+ * @param bool &$hasHistory Whether the page has a history
+ */
+ public function generateReason(&$hasHistory)
+ {
+ global $wgContLang;
+ $dbw = wfGetDB(DB_MASTER);
+ // Get the last revision
+ $rev = Revision::newFromTitle($this->mTitle);
+ if(is_null($rev))
+ return false;
+ // Get the article's contents
+ $contents = $rev->getText();
+ $blank = false;
+ // If the page is blank, use the text from the previous revision,
+ // which can only be blank if there's a move/import/protect dummy revision involved
+ if($contents == '')
+ {
+ $prev = $rev->getPrevious();
+ if($prev)
+ {
+ $contents = $prev->getText();
+ $blank = true;
+ }
+ }
+
+ // Find out if there was only one contributor
+ // Only scan the last 20 revisions
+ $limit = 20;
+ $res = $dbw->select('revision', 'rev_user_text', array('rev_page' => $this->getID()), __METHOD__,
+ array('LIMIT' => $limit));
+ if($res === false)
+ // This page has no revisions, which is very weird
+ return false;
+ if($res->numRows() > 1)
+ $hasHistory = true;
+ else
+ $hasHistory = false;
+ $row = $dbw->fetchObject($res);
+ $onlyAuthor = $row->rev_user_text;
+ // Try to find a second contributor
+ while(($row = $dbw->fetchObject($res)))
+ if($row->rev_user_text != $onlyAuthor)
+ {
+ $onlyAuthor = false;
+ break;
+ }
+ $dbw->freeResult($res);
+
+ // Generate the summary with a '$1' placeholder
+ if($blank)
+ // The current revision is blank and the one before is also
+ // blank. It's just not our lucky day
+ $reason = wfMsgForContent('exbeforeblank', '$1');
+ else
+ {
+ if($onlyAuthor)
+ $reason = wfMsgForContent('excontentauthor', '$1', $onlyAuthor);
+ else
+ $reason = wfMsgForContent('excontent', '$1');
+ }
+
+ // Replace newlines with spaces to prevent uglyness
+ $contents = preg_replace("/[\n\r]/", ' ', $contents);
+ // Calculate the maximum amount of chars to get
+ // Max content length = max comment length - length of the comment (excl. $1) - '...'
+ $maxLength = 255 - (strlen($reason) - 2) - 3;
+ $contents = $wgContLang->truncate($contents, $maxLength, '...');
+ // Remove possible unfinished links
+ $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents );
+ // Now replace the '$1' placeholder
+ $reason = str_replace( '$1', $contents, $reason );
+ return $reason;
+ }
+