예제 #1
0
function wfImpliedRedirects_articleFromTitle(Title &$title, &$article)
{
    // We only want to mess with titles for pages that don't already exist.
    if (!$title->exists() && $title->getNamespace() == NS_MAIN) {
        $origTitle = $title->getDBkey();
        // this format has the characters as we need them already
        // If there is more than one colon, the vast majority of the time it seems to be in the name of the song rather than the artist so we
        // use strpos instead of strrpos (this query was used to create this assumption: "SELECT page_title FROM wiki_page WHERE page_title LIKE '%:%:%'").
        $index = mb_strpos($origTitle, ":");
        if ($index !== false) {
            $artist = mb_substr($origTitle, 0, $index);
            $song = mb_substr($origTitle, $index + 1);
            // Borrow functions from the SOAP
            define('LYRICWIKI_SOAP_FUNCS_ONLY', true);
            // so we can borrow functions from server.php w/o initializing a SOAP request.
            $debug = false;
            global $IP;
            include_once $IP . '/extensions/3rdparty/LyricWiki/server.php';
            // If the artist has a redirect on their own page, that generally means that all songs belong to that finalized name...
            // so try to grab the song using that version of the artist's name.
            $artistTitle = lw_getTitle($artist);
            // leaves the original version in tact
            $finalName = $artistTitle;
            $page = lw_getPage($artistTitle, $finalName, $debug);
            print !$debug ? "" : "found:\n{$page}";
            if ($finalName != $artistTitle) {
                print !$debug ? "" : "Artist redirect found to \"{$finalName}\". Applying to song \"{$song}\".\n";
            }
            $titleStr = utf8_decode(lw_getTitle($finalName, $song));
            // decode is used to prevent double-encode calls that would otherwise happen.  I'm skeptical as to whether this would always work (assuming the special char was in the original title instead of the redirected artist as tested).
            print !$debug ? "" : "Title \"{$titleStr}\"\n";
            // If the song was still not found... chop off any trailing parentheses and try again. - SWC 20070101
            if (!lw_pageExists($titleStr)) {
                print !$debug ? "" : "{$titleStr} not found.\n";
                $finalSong = preg_replace("/\\s*\\(.*\$/", "", $song);
                if ($song != $finalSong) {
                    $titleStr = lw_getTitle($finalName, $finalSong);
                    print !$debug ? "" : "Looking without parentheses for \"{$titleStr}\"\n";
                }
            } else {
                print !$debug ? "" : "{$titleStr} found.\n";
            }
            // We successfully found a page using implied redirects... change this request around.
            if ($titleStr != $origTitle) {
                if (!class_exists('LW_ImpliedRedirect')) {
                    // TODO: move to a separate file
                    class LW_ImpliedRedirect extends Article
                    {
                        var $mTarget;
                        function __construct($source, $target)
                        {
                            Article::__construct($source);
                            $this->mTarget = $target;
                            $this->mIsRedirect = true;
                        }
                        function followRedirect()
                        {
                            return $this->mTarget;
                        }
                        function loadPageData($data = 'fromdb')
                        {
                            Article::loadPageData($data);
                            $this->mIsRedirect = true;
                        }
                        // since we're certain the target exists, we might as well say so
                        // this fools Our404Handler into following the redirect
                        function exists()
                        {
                            return true;
                        }
                    }
                }
                $target = Title::newFromDBkey($titleStr);
                if ($target && $target->exists()) {
                    $article = new LW_ImpliedRedirect($title, $target);
                    //trigger redirect to implied page.
                }
            }
        }
    }
    return true;
}
예제 #2
0
function postArtist($overwriteIfExists, $artist, $albums)
{
    // TODO: IMPLEMENT
    ob_start();
    print_r($albums);
    $albumStr = ob_get_clean();
    $id = requestStarted(__METHOD__, $overwriteIfExists ? "overwrite" : "no-overwrite" . "{$artist}|{$albumStr}");
    $retVal = array('artist' => $artist, 'dataUsed' => false, 'message' => 'Not implemented yet.  This would give info on whether some of the data, none, or all of it was used');
    global $SHUT_DOWN_API;
    if ($SHUT_DOWN_API) {
        global $SHUT_DOWN_API_REASON;
        $retVal['message'] = $SHUT_DOWN_API_REASON;
    } else {
        global $wgUser;
        if (!$wgUser->isLoggedIn()) {
            // If this is a SOAP request, the credentials may be in the SOAP headers.
            global $funcsOnly;
            if ($funcsOnly) {
                // Not a SOAP request, so we have to already be logged in.
                $retVal['message'] = "Must be logged in to use postArtist().";
            } else {
                lw_tryLogin();
            }
        }
        $artistName = lw_getTitle($artist);
        $pageTitle = Title::newFromDBkey(utf8_decode(htmlspecialchars_decode($artistName)));
        $pageExists = $pageTitle && $pageTitle->exists();
        // call here and store the result to check after page is created to determine if it was an overwrite
        if ($pageExists) {
            // TODO: REMOVE
            if (false) {
                $currData = getArtist($artist);
                $currAlbums = $currData['albums'];
                $content = lw_getPage($artistName);
                // Find the appropriate place chronologically for the album and insert it.
                for ($cnt = 0; $cnt < count($albums); $cnt++) {
                    $activeAlbum = $albums[$cnt];
                    // the album we are going to insert
                    $activeAlbum = $activeAlbum['albums'];
                    // weird side-effect... every album is wrapped in an 'albums' array.
                    if (strtolower($activeAlbum['album']) == "other songs") {
                        $activeAlbum['year'] = 0;
                    }
                    /*if($artist == "Sahara Hotnights"){
                    			print "Active album:\n";
                    			print_r($activeAlbum);
                    			print "\nAll albums:\n";
                    			print_r($albums);
                    		}
                    		print (($artist!="Sahara Hotnights")?"":"Active album: *".$activeAlbum['album']."*\n");
                    		*/
                    // Find correct order to insert in.
                    $insertBefore = -1;
                    $doneAdding = false;
                    for ($index = 0; $index < count($currAlbums); $index++) {
                        $comparisonAlbum = $currAlbums[$index];
                        if ($activeAlbum['year'] < $comparisonAlbum['year'] && strtolower($activeAlbum['album']) != "other songs") {
                            $insertBefore = $index;
                            $index = count($currAlbums);
                            // stop looping
                        } else {
                            if ($activeAlbum['year'] == $comparisonAlbum['year']) {
                                if (strtolower($activeAlbum['album']) == strtolower($comparisonAlbum['album'])) {
                                    // Album already exists, just merge the track listings.
                                    $doneAdding = true;
                                    // album already exists, don't add whole album to the code
                                    $additionalTracks = array();
                                    $tracks = $activeAlbum['songs'];
                                    $tracksFound = $comparisonAlbum['songs'];
                                    foreach ($tracks as $currTrack) {
                                        $found = false;
                                        for ($trackNum = 0; $trackNum < count($tracksFound) && !$found; $trackNum++) {
                                            if ($currTrack == $tracksFound[$trackNum]) {
                                                $found = true;
                                            }
                                        }
                                        if (!$found) {
                                            $additionalTracks[] = $currTrack;
                                        }
                                    }
                                    // If there were extra tracks, add them to the end of the track listing
                                    if (count($additionalTracks) > 0) {
                                        $wikiCode = lw_tracksToWiki($artistName, $additionalTracks);
                                        $wikiCode = trim($wikiCode);
                                        $albumName = $comparisonAlbum['album'];
                                        $albumYear = $comparisonAlbum['year'];
                                        $albumName = str_replace(" ", "_", $albumName);
                                        $albumName = str_replace("_", "[_ ]", $albumName);
                                        $artistReg = str_replace(" ", "_", $artistName);
                                        $artistReg = str_replace("_", "[_ ]", $artistReg);
                                        if (strtolower($activeAlbum['album']) == "other songs") {
                                            //print (($artist!="Sahara Hotnights")?"":"Other songs merged\n");
                                            $content = preg_replace("/(==\\s*\\[\\[{$artistReg}:{$albumName}(\\|.*?\\]\\]|\\]\\])\\s*==\\s*.*?)(\n[^#*{])/si", "\$1\n{$wikiCode}\$3", $content);
                                        } else {
                                            //print (($artist!="Sahara Hotnights")?"":"Normal album merged\n");
                                            $content = preg_replace("/(==\\s*\\[\\[{$artistReg}:{$albumName}" . "[_ ]\\({$albumYear}\\)(\\|.*?\\]\\]|\\]\\])\\s*==\\s*.*?)(\n[^#*{])/si", "\$1\n{$wikiCode}\$3", $content);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Search for a match in the code of where this album goes.
                    if (!$doneAdding) {
                        //print (($artist!="Sahara Hotnights")?"":"Still looking for album\n");
                        if ($insertBefore == -1) {
                            //print (($artist!="Sahara Hotnights")?"":"Album goes at end\n");
                            // Insert after the last album (if there is an Other Songs album, do it before that but after the last real album).
                            $wikiCode = lw_albumDataToWiki($artist, $activeAlbum);
                            if (count($currAlbums) > 0) {
                                $lastAlbum = $currAlbums[count($currAlbums) - 1];
                                if (strtolower($lastAlbum['album']) == "other songs" && count($currAlbums) > 1) {
                                    $lastAlbum = $currAlbums[count($currAlbums) - 2];
                                    // put new album after last REAL album.
                                }
                                $lastAlbumName = $lastAlbum['album'];
                                $lastAlbumYear = $lastAlbum['year'];
                                $wikiCode = trim($wikiCode) . "\n";
                                // only needs one new line-break, not the traditional two
                                $lastAlbumName = str_replace(" ", "_", $lastAlbumName);
                                $lastAlbumName = str_replace("_", "[_ ]", $lastAlbumName);
                                $artistReg = str_replace(" ", "_", $artistName);
                                $artistReg = str_replace("_", "[_ ]", $artistReg);
                                $content = preg_replace("/(==\\s*\\[\\[{$artistReg}:{$lastAlbumName}" . "[_ ]\\({$lastAlbumYear}\\)(\\|.*?\\]\\]|\\]\\])\\s*==\\s*.*?\n[^#*{])/si", "\$1{$wikiCode}", $content);
                            } else {
                                //print (($artist!="Sahara Hotnights")?"":"No albums existed\n");
                                $content = $wikiCode . $content;
                                // if there were no albums yet, just throw the new album on the beginning.
                            }
                        } else {
                            //print (($artist!="Sahara Hotnights")?"":"Album goes before $insertBefore\n");
                            $beforeAlbum = $currAlbums[$insertBefore]['album'];
                            $beforeYear = $currAlbums[$insertBefore]['year'];
                            $albumWiki = lw_albumDataToWiki($artist, $activeAlbum);
                            $origContent = $content;
                            $beforeAlbum = str_replace(" ", "_", $beforeAlbum);
                            $beforeAlbum = str_replace("_", "[_ ]", $beforeAlbum);
                            $artistReg = str_replace(" ", "_", $artistName);
                            $artistReg = str_replace("_", "[_ ]", $artistReg);
                            $content = preg_replace("/(==\\s*\\[\\[{$artistReg}:{$beforeAlbum}" . "[_ ]\\({$beforeYear}\\))/si", "{$albumWiki}\$1", $content);
                            if ($origContent == $content) {
                                //print (($artist!="Sahara Hotnights")?"":"That didn't work... just throwing on top.\n");
                                // Couldn't find place... stuff it at top of the file to minimize damage; a human can sort it out later.
                                $content = $albumWiki . $content;
                            }
                        }
                    }
                }
                // TODO: OMG REMOVE!
                /*if($artist == "Sahara Hotnights"){
                			print "Exiting because this is a test.\n";
                			exit;
                		}*/
                // Send the updated code here.
                $content = "[[Category:Review_Me]]\n" . $content;
                // TODO: REMOVE AFTER UBERBOT SUBMISSIONS
                $summary = "Page " . ($pageExists ? "edited" : "created") . " using [[LyricWiki:SOAP|LyricWiki's SOAP Webservice]]";
                $editWorked = lw_createPage($pageTitle, $content, $summary);
                $retVal['dataUsed'] = true;
                if ($editWorked !== true) {
                    $retVal['message'] = "Error from EditPage: {$editWorked}";
                } else {
                    if (isset($pageTitle) && $pageExists) {
                        $retVal['message'] = "Page overwritten. ";
                    } else {
                        $retVal['message'] = "Page created. ";
                    }
                }
            }
        } else {
            // The artist page doesn't exist yet... create it.
            $content = "";
            $content .= "[[Category:Review_Me]]\n";
            // TODO: REMOVE AFTER UBERBOT SUBMISSIONS
            $content .= "{{Wikipedia}}\n\n";
            // Build the page-content.
            foreach ($albums as $albumWrapper) {
                $currAlbum = $albumWrapper['albums'];
                // weird side-effect... every album is wrapped in an 'albums' array.
                $content .= lw_albumDataToWiki($currAlbum);
            }
            $content .= "{{Artist}}\n\n";
            $fLetter = lw_fLetter($artistName);
            $content .= "[[Category:Artists {$fLetter}]]\n";
            $summary = "Page " . ($pageExists ? "edited" : "created") . " using [[LyricWiki:SOAP|LyricWiki's SOAP Webservice]]";
            $returnStr = lw_createPage($pageTitle, $content, $summary);
            $retVal['dataUsed'] = true;
            if (isset($pageTitle) && $pageExists) {
                $retVal['message'] = "Page overwritten. " . ($returnStr == "" ? "" : "({$returnStr})");
            } else {
                $retVal['message'] = "Page created. " . ($returnStr == "" ? "" : "({$returnStr})");
            }
            // Also create pages for all of the albums that appeared on this page.
            $numUsed = $numSkipped = 0;
            foreach ($albums as $albumWrapper) {
                $currAlbum = $albumWrapper['albums'];
                $albumName = $currAlbum['album'];
                $year = $currAlbum['year'];
                $asin = $currAlbum['amazonLink'];
                $songs = $currAlbum['songs'];
                $albumResponse = postAlbum($overwriteIfExists, $artistName, $albumName, $year, $asin, $songs);
                if ($albumResponse['dataUsed']) {
                    $numUsed++;
                } else {
                    $numSkipped++;
                }
            }
            if ($numUsed > 0 || $numSkipped > 0) {
                $retVal['message'] .= " - album pages: {$numUsed} made, {$numSkipped} skipped";
            }
        }
    }
    requestFinished($id);
    return $retVal;
}