Yurik has uploaded a new change for review. https://gerrit.wikimedia.org/r/154704
Change subject: Create wiki pages & images from files ...................................................................... Create wiki pages & images from files Vagrant will rely on this script to provide "vagrant add-pages" command. See I79cfcc515a32e25c25817d67ac302eec4d5acda6 This script will recursively iterate through all files in the given directory, and add content of each file to the wiki as articles. * Subdirectories are treated as namespaces * Any filename that begins with "_" will be ignored * Anything in the File namespace will be uploaded as a file, not wiki page. * To modify wiki page of a files, create an extra file with the same name, plus an extra "_" at the end. Change-Id: Iddd3c2301e302edfb529bac78ac91226d77876b8 --- A maintenance/importPagesFromFiles.php 1 file changed, 120 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/04/154704/1 diff --git a/maintenance/importPagesFromFiles.php b/maintenance/importPagesFromFiles.php new file mode 100644 index 0000000..7241ee0 --- /dev/null +++ b/maintenance/importPagesFromFiles.php @@ -0,0 +1,120 @@ +<?php +/** + * Ensure all files in a given directory match content of the wiki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup Maintenance + */ + +require_once __DIR__ . '/Maintenance.php'; + +/** + * + */ +class ImportPagesFromFiles extends Maintenance { + public function __construct() { + parent::__construct(); + $this->mDescription = "Ensure all files in a given directory match content of the wiki"; + $this->addArg( 'dir', 'Directory with files', true ); + } + + public function execute() { + global $wgUser; + + $summary = 'vagrant add-pages'; + $dir = realpath( $this->getArg() ); + $wgUser = User::newFromName( 'Admin' ); + $exit = 0; + + if ( !file_exists( $dir ) ) { + $this->error( "Directory '$dir' does not exist\n", true ); + } + $this->output( "Adding all pages from $dir\n" ); + foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ) as $x ) { + if ( !$x->isFile() || substr( $x->getFilename(), 0, 1 ) === '_' ) { + continue; + } + $fullPath = $x->getPathname(); + if ( strpos( $fullPath, $dir ) !== 0 ) { + $this->error( "Dir '$dir' not found in '$fullPath'", true ); + } + // Remove container directory from the path and optional first '/' + $textTitle = ltrim( substr( $fullPath, strlen( $dir ) ), DIRECTORY_SEPARATOR ); + $textTitle = urldecode( str_replace( DIRECTORY_SEPARATOR, ':', $textTitle ) ); + $title = Title::newFromText( $textTitle ); + if ( !$title ) { + $this->error( "Invalid title", true ); + } + + // For files, if the name ends with '_', treat it as wikipage, not content + if ( $title->inNamespace( NS_FILE ) && substr( $textTitle, -1 ) !== '_' ) { + $this->output( "{$title}..." ); + $file = wfLocalFile( $title ); + $repo = $file->getRepo(); + $dupes = $repo->findBySha1( FSFile::getSha1Base36FromPath( $fullPath ) ); + if ( $dupes ) { + $this->output( "already exists as " . $dupes[0]->getName() . ", skipping\n" ); + continue; + } + $props = FSFile::getPropsFromPath( $fullPath ); + $publishOptions = array(); + $handler = MediaHandler::getHandler( $props['mime'] ); + if ( $handler ) { + $publishOptions['headers'] = $handler->getStreamHeaders( $props['metadata'] ); + } else { + $publishOptions['headers'] = array(); + } + $archive = $file->publish( $fullPath, 0, $publishOptions ); + if ( !$archive->isGood() ) { + $this->output( "failed. (" . $archive->getWikiText() . ")\n" ); + $exit = 1; + } + $textFile = $fullPath . '_'; + $text = is_file( $textFile ) ? file_get_contents( $textFile ) : ''; + if ( $file->recordUpload2( $archive->value, $summary, $text, $props ) ) { + $this->output( "done.\n" ); + } else { + $this->output( "recordUpload2() failed.\n" ); + } + } else { + $this->output( "Article {$title}..." ); + $page = WikiPage::factory( $title ); + $text = file_get_contents( $fullPath ); + $content = ContentHandler::makeContent( $text, $title ); + + $status = $page->doEditContent( $content, $summary ); + if ( $status->isOK() ) { + $this->output( "done" ); + $exit = 0; + } else { + $this->output( "failed" ); + $exit = 1; + } + if ( !$status->isGood() ) { + $this->output( ', ' . $status->getWikiText() ); + } + $this->output( "\n" ); + } + } + + exit( $exit ); + } +} + +$maintClass = "ImportPagesFromFiles"; +require_once RUN_MAINTENANCE_IF_MAIN; -- To view, visit https://gerrit.wikimedia.org/r/154704 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iddd3c2301e302edfb529bac78ac91226d77876b8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Yurik <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
