<commit_before><?php
/**
 * This import-action will let you import a wordpress blog
 *
 * @author <NAME> <<EMAIL>>
 */
class BackendBlogImportWordpress extends BackendBaseActionEdit
{
    /**
     * @var array
     */
    private $authors = array();

    /**
     * @var array
     */
    private $attachments = array();


    /**
     * Execute the action
     */
    public function execute()
    {
        parent::execute();
        set_time_limit(0);
        $this->loadForm();
        $this->validateForm();
        $this->parse();
        $this->display();
    }


    /**
     * Load the form
     */
    private function loadForm()
    {
        $this->frm = new BackendForm('import');
        $this->frm->addFile('wordpress');
        $this->frm->addText('filter', SITE_URL);
    }


    /**
     * Validate the form
     */
    private function validateForm()
    {
        // Is the form submitted?
        if($this->frm->isSubmitted()) {
            // Cleanup the submitted fields, ignore fields that were added by hackers
            $this->frm->cleanupFields();

            // XML provided?
            if($this->frm->getField('wordpress')->isFilled()) {
                $this->frm->getField('wordpress')->isAllowedExtension(array('xml'), BL::err('XMLFilesOnly'));
            }

            // No file
            else {
                $this->frm->getField('wordpress')->addError(BL::err('FieldIsRequired'));
            }

            // No errors?
            if($this->frm->isCorrect()) {
                // Move the file
                $this->frm->getField('wordpress')->moveFile(FRONTEND_FILES_PATH . '/wordpress.xml');

                // Process the XML
                $this->processXML();

                // Remove the file
                SpoonFile::delete(FRONTEND_FILES_PATH . '/wordpress.xml');

                // Everything is saved, so redirect to the overview
                $this->redirect(BackendModel::createURLForAction('index') . '&report=imported');
            }
        }
    }


    /**
     * Process the xml
     */
    private function processXML()
    {
        // Init object
        $reader = new XMLReader();

        // Open the file
        $reader->open(FRONTEND_FILES_PATH . '/wordpress.xml');

        // Loop through the document
        while($reader->read())
        {
            // Start tag for item?
            if($reader->name != 'item' && $reader->name != 'wp:author') continue;

            // End tag?
            if($reader->nodeType == XMLReader::END_ELEMENT) continue;

            // Get the raw XML
            $xmlString = $reader->readOuterXml();

            // Read the XML as an SimpleXML-object
            /* @var SimpleXMLElement $xml */
            $xml = @simplexml_load_string($reader->readOuterXml());

            // Skip element if it isn't a valid SimpleXML-object
            if($xml === false) continue;

            // Is it really an item?
            if(substr($xmlString, 0, 5) == '<item') {
                // What type of content are we dealing with?
                switch ($xml->children('wp', true)->post_type) {
                    case 'post':
                        // Process as post
                        $this->processPost($xml);
                        break;

                    case 'page':
                        // Process as page
                        // $this->processPage($xml);
                        break;

                    case 'attachment':
                        // Process as attachment
                        $this->processAttachment($xml);
                        break;

                    default:
                        // Don't do anything
                        break;
                }
            }
            // Process the authors
            elseif (substr($xmlString, 0, 10) == '<wp:author') {
                $this->authors[(string) $xml->children('wp', true)->author_login] = array(
                    'id'           => (string) $xml->children('wp', true)->author_id,
                    'login'        => (string) $xml->children('wp', true)->author_login,
                    'email'        => (string) $xml->children('wp', true)->author_email,
                    'display_name' => (string) $xml->children('wp', true)->author_display_name,
                    'first_name'   => (string) $xml->children('wp', true)->author_first_name,
                    'last_name'    => (string) $xml->children('wp', true)->author_last_name,
                );
            }

            // End
            if(!$reader->read()) break;
        }

        // close
        $reader->close();
    }


    /**
     * Import a blog post
     *
     * @param SimpleXMLElement $xml
     * @return bool
     */
    private function processPost($xml)
    {
        // Are we really working with a post?
        if ($xml->children('wp', true)->post_type != 'post') {
            return false;
        }

        // This is a deleted post, don't import
        if ($xml->children('wp', true)->status == 'trash') {
            return false;
        }

        // Mapping for wordpress status => fork status
        $statusses = array(
            'draft'   => 'draft',
            'pending'  => 'draft',
            'private'  => 'private',
            'publish' => 'active',
            'future'  => 'publish',
            'trash'   => '',
        );
        $commentStatusses = array(
            'open'    => 'Y',
            'closed'  => 'N',
        );

        // Prepare item
        $item = array();
        $item['user_id'] = $this->handleUser((string) $xml->children('dc', true)->creator);
        $item['title'] = (string) $xml->title;
        $item['text'] = $this->handleUrls(
            (string) $xml->children('content', true)->encoded,
            $this->frm->getField('filter')->getValue()
        );
        $item['created_on'] = (string) $xml->children('wp', true)->post_date;
        $item['publish_on'] = (string) $xml->children('wp', true)->post_date;
        $item['edited_on'] = (string) $xml->children('wp', true)->post_date;
        $item['status'] = $statusses[(string) $xml->children('wp', true)->status];
        $item['allow_comments'] = $commentStatusses[(string) $xml->children('wp', true)->comment_status];

        // Some status corrections
        if ($item['status'] == 'draft') {<commit_msg>Remove unused functionality<commit_after><?php
/**
 * This import-action will let you import a wordpress blog
 *
 * @author <NAME> <<EMAIL>>
 */
class BackendBlogImportWordpress extends BackendBaseActionEdit
{
    /**
     * @var array
     */
    private $authors = array();

    /**
     * @var array
     */
    private $attachments = array();


    /**
     * Execute the action
     */
    public function execute()
    {
        parent::execute();
        set_time_limit(0);
        $this->loadForm();
        $this->validateForm();
        $this->parse();
        $this->display();
    }


    /**
     * Load the form
     */
    private function loadForm()
    {
        $this->frm = new BackendForm('import');
        $this->frm->addFile('wordpress');
        $this->frm->addText('filter', SITE_URL);
    }


    /**
     * Validate the form
     */
    private function validateForm()
    {
        // Is the form submitted?
        if($this->frm->isSubmitted()) {
            // Cleanup the submitted fields, ignore fields that were added by hackers
            $this->frm->cleanupFields();

            // XML provided?
            if($this->frm->getField('wordpress')->isFilled()) {
                $this->frm->getField('wordpress')->isAllowedExtension(array('xml'), BL::err('XMLFilesOnly'));
            }

            // No file
            else {
                $this->frm->getField('wordpress')->addError(BL::err('FieldIsRequired'));
            }

            // No errors?
            if($this->frm->isCorrect()) {
                // Move the file
                $this->frm->getField('wordpress')->moveFile(FRONTEND_FILES_PATH . '/wordpress.xml');

                // Process the XML
                $this->processXML();

                // Remove the file
                SpoonFile::delete(FRONTEND_FILES_PATH . '/wordpress.xml');

                // Everything is saved, so redirect to the overview
                $this->redirect(BackendModel::createURLForAction('index') . '&report=imported');
            }
        }
    }


    /**
     * Process the xml
     */
    private function processXML()
    {
        // Init object
        $reader = new XMLReader();

        // Open the file
        $reader->open(FRONTEND_FILES_PATH . '/wordpress.xml');

        // Loop through the document
        while($reader->read())
        {
            // Start tag for item?
            if($reader->name != 'item' && $reader->name != 'wp:author') continue;

            // End tag?
            if($reader->nodeType == XMLReader::END_ELEMENT) continue;

            // Get the raw XML
            $xmlString = $reader->readOuterXml();

            // Read the XML as an SimpleXML-object
            /* @var SimpleXMLElement $xml */
            $xml = @simplexml_load_string($reader->readOuterXml());

            // Skip element if it isn't a valid SimpleXML-object
            if($xml === false) continue;

            // Is it really an item?
            if(substr($xmlString, 0, 5) == '<item') {
                // What type of content are we dealing with?
                switch ($xml->children('wp', true)->post_type) {
                    case 'post':
                        // Process as post
                        $this->processPost($xml);
                        break;

                    case 'attachment':
                        // Process as attachment
                        $this->processAttachment($xml);
                        break;

                    default:
                        // Don't do anything
                        break;
                }
            }
            // Process the authors
            elseif (substr($xmlString, 0, 10) == '<wp:author') {
                $this->authors[(string) $xml->children('wp', true)->author_login] = array(
                    'id'           => (string) $xml->children('wp', true)->author_id,
                    'login'        => (string) $xml->children('wp', true)->author_login,
                    'email'        => (string) $xml->children('wp', true)->author_email,
                    'display_name' => (string) $xml->children('wp', true)->author_display_name,
                    'first_name'   => (string) $xml->children('wp', true)->author_first_name,
                    'last_name'    => (string) $xml->children('wp', true)->author_last_name,
                );
            }

            // End
            if(!$reader->read()) break;
        }

        // close
        $reader->close();
    }


    /**
     * Import a blog post
     *
     * @param SimpleXMLElement $xml
     * @return bool
     */
    private function processPost($xml)
    {
        // Are we really working with a post?
        if ($xml->children('wp', true)->post_type != 'post') {
            return false;
        }

        // This is a deleted post, don't import
        if ($xml->children('wp', true)->status == 'trash') {
            return false;
        }

        // Mapping for wordpress status => fork status
        $statusses = array(
            'draft'   => 'draft',
            'pending'  => 'draft',
            'private'  => 'private',
            'publish' => 'active',
            'future'  => 'publish',
        );
        $commentStatusses = array(
            'open'    => 'Y',
            'closed'  => 'N',
        );

        // Prepare item
        $item = array();
        $item['user_id'] = $this->handleUser((string) $xml->children('dc', true)->creator);
        $item['title'] = (string) $xml->title;
        $item['text'] = $this->handleUrls(
            (string) $xml->children('content', true)->encoded,
            $this->frm->getField('filter')->getValue()
        );
        $item['created_on'] = (string) $xml->children('wp', true)->post_date;
        $item['publish_on'] = (string) $xml->children('wp', true)->post_date;
        $item['edited_on'] = (string) $xml->children('wp', true)->post_date;
        $item['status'] = $statusses[(string) $xml->children('wp', true)->status];
        $item['allow_comments'] = $commentStatusses[(string) $xml->children('wp', true)->comment_status];

        // Some status corrections
        if ($item['status'] == 'draft') {
{'fraction_non_alphanumeric': 0.15625, 'fraction_numerical': 0.0009566326530612245, 'mean_word_length': 1.5523906408952186, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 201, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 69}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519869', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3308, 'n_words': 726}